acacode / swagger-typescript-api

Generate the API Client for Fetch or Axios from an OpenAPI Specification
MIT License
3.4k stars 361 forks source link

Function name generation may be misleading/incorrect #821

Open lionel87 opened 4 months ago

lionel87 commented 4 months ago

I had issues with the function name generation, a path like /user/logout generated as api.user.logoutList() but it is not actually a list.
A different path like /clients/{zone} is generated as api.clientsDetail() which is actually a list.

It would be a good feature to give control over the naming, or use the naming scheme that the swagger.io generator uses. it suffixes the name with the http method, like api.user.logoutGET() and api.clientsGET().

The current behavior that checks if the path contains placeholders are working only on simple or example apis I quess.

https://github.com/acacode/swagger-typescript-api/blob/b0fab0d214d24f853594daca1c83064a9b373a2a/templates/base/route-name.ejs#L18

swagger-typescript-api version: 13.0.12

snuup commented 3 months ago

This is very serious. WIthout any reason my api method "getBunny" is renamed into "getBunnyList". Makes no sense at all.

petr-jilek commented 1 month ago

You can create your template for your project.

For example, you can create the templates folder in your project and add the mentioned route-name.ejs file to this folder.

In this file templates/route-name.ejs you can create custom behavior. For example update the methodAliases part like:

const methodAliases = {
  get: (pathName, hasPathInserts) => _.camelCase(`${pathName}_get`),
  post: (pathName, hasPathInserts) => _.camelCase(`${pathName}_post`),
  put: (pathName, hasPathInserts) => _.camelCase(`${pathName}_put`),
  patch: (pathName, hasPathInserts) => _.camelCase(`${pathName}_patch`),
  delete: (pathName, hasPathInserts) => _.camelCase(`${pathName}_delete`),
};

Then in your command specify the template folder path. For example:

swagger-typescript-api -p http://localhost:5000/swagger/v1/swagger.json -o ./api -n api.ts -t ./templates

I tried it only with this file in the templates folder and it worked.