tulios / mappersmith

is a lightweight rest client for node.js and the browser
https://www.npmjs.com/package/mappersmith
MIT License
332 stars 71 forks source link

[idea] support openapi/swagger #454

Open OleksandrKucherenko opened 6 months ago

OleksandrKucherenko commented 6 months ago

Two use cases:

klippx commented 6 months ago

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:

I spotted a number of things, like:

If someone wants to do this I would say two things

OleksandrKucherenko commented 6 months ago

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.

OleksandrKucherenko commented 6 months ago

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

OleksandrKucherenko commented 6 months ago

Examples of generators:

That may give extra power to the mappersmith

klippx commented 5 months ago

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?