Comments (5)
Interesting idea, seems useful. Lets take https://petstore.swagger.io/ as an example, trying to "auto generate" this by hand:
forge({
host: 'https://petstore.swagger.io/v2',
resources: {
Pet: {
uploadImage: {
method: 'post',
path: '/pet/{petId}/uploadImage',
},
// No good name can be auto generated, relying on PUT = update
update: {
method: 'put',
path: '/pet',
},
// No good name can be auto generated, relying on POST = create
create: {
method: 'post',
path: '/pet',
},
findByStatus: {
method: 'get',
// Hard to express the required query parameters,
// it would be better to hand roll it and require the user to provide the query parameters
// path: '/pet/findByStatus/{status}',
// even though that does not map to the actual API that we are auto-generating for
path: '/pet/findByStatus',
},
// Probably need to suffix with "ByPetId" to distinguish it from potential GET /pet (if it existed)
// Relying on GET = get, adding a (By + {petId}) camelized to separate it from vanilla "get" as mentioned above
getByPetId: {
method: 'get',
path: '/pet/{petId}',
},
// This is another POST request, but it's not a good name.
// Need to distinguish it from the other POST request "create".
// But it looks strange to use Pet.createById vs Pet.create, and to add salt to injury
// the swagger docs call this request "update pet" even though it is a post request, should have been a PUT
createByPetId: {
method: 'post',
path: '/pet/{petId}',
},
// Similarly
deleteByPetId: {
method: 'delete',
path: '/pet/{petId}',
},
},
Store: {
createOrder: {
method: 'post',
path: '/store/order',
},
getOrderByOrderId: {
method: 'get',
path: '/store/order/{orderId}',
},
deleteOrderByOrderId: {
method: 'delete',
path: '/store/order/{orderId}',
},
getStoreInventory: {
method: 'get',
path: '/store/inventory',
},
},
},
});
First of all:
- Mappersmith would probably want to group into two primary resources Pet and Store.
- Then we want to have useful names to describe what the resourceMethod is doing, but this also need to be generated, which is the big immediate challenge
I spotted a number of things, like:
- trying to rely on "POST = create" (even though in this example it can apparently be "POST = update")
- many resources are competing for similar names (POST /pet vs POST /pet/{petId}) that need to generate code that does not clash and correctly represent the operation
- dependent on swagger docs to name their vars and camelize from that
If someone wants to do this I would say two things
- don't bring in any dependencies
- if you have to bring in dependencies, consider making this an npx command (and a separate project)
from mappersmith.
Can it be a sub-package of the mappersmith? Maybe introduce some namespace for it?
https://github.com/OpenAPITools/openapi-generator solves the problem of naming. Maybe not in the most efficient way, and anyway its a subject of change after first iteration.
from mappersmith.
There are many generator available - https://openapi-generator.tech/docs/generators
So I assume we can simply provide own generator for openapi-generator tool that uses Mappersmith
from mappersmith.
Examples of generators:
- https://openapi-generator.tech/docs/generators/typescript-axios/
- https://openapi-generator.tech/docs/generators/typescript-fetch/
- https://openapi-generator.tech/docs/generators/protobuf-schema/
That may give extra power to the mappersmith
from mappersmith.
Very very interesting. Thanks for sharing! I have to say that I noticed that someone outside of Axios (with no contribution or affiliation with them) added the generator for typescript-axios
. Lucky them, I guess.
If I understand correctly then we can achieve this with a PR to the repo:
If anyone want to work on this, go for it.
Do you think we can close this for now, and open a new issue if there is an action to be done in this repo?
from mappersmith.
Related Issues (20)
- Why does "Network error" have 400 status code? HOT 3
- 2.42.0-beta.1 is published to NPM as latest HOT 1
- `uglifyjs-webpack-plugin` deprecated HOT 1
- configs.fetch is badly typed HOT 2
- Bug: memory leak with when using Http(s)Agent keepAlive=true HOT 5
- unusedMocks not working on Typescript HOT 1
- Import middlewares HOT 4
- Smoke test new release candidate
- Issue when importing Mappersmith from a TypeScript file and using esbuild HOT 8
- Missing some exports on version 2.43.3 HOT 1
- [idea] support protobuf/grpc HOT 2
- [idea] support AbortController
- [idea] support major datatype/schema validation tools like ZOD/JOI/IO-TS/etc HOT 3
- [idea] align/sync library functionality with modern alternatives like GOT HOT 3
- [idea] provide "api adapters" for easier migration between libraries
- [idea] Support express.js path syntax
- [idea] Bun and Deno support
- [idea] http/3 support sample
- [idea] provide experimental support for uWebSockets
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from mappersmith.