Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api calls names #380

Closed
VitaliKhileuski opened this issue Aug 15, 2022 · 6 comments
Closed

api calls names #380

VitaliKhileuski opened this issue Aug 15, 2022 · 6 comments

Comments

@VitaliKhileuski
Copy link

How can i customize name generating of api calls? For this moment i see that name generate from route parts and word Create that is not comfortable for me
image

@TimAlaerts
Copy link

@VitaliKhileuski in case you are still wondering you can specify the name in swagger via the "operationId" property and the generator will use that name instead

@js2me
Copy link
Member

js2me commented Aug 19, 2022

Hello @VitaliKhileuski !
You can use hooks api \ templates (route-name) for customizing name generating of api calls

In my case I use hooks api
image

Where generateOperationId is


const ACTION_METHODS = ["post", "put", "patch"]

const generateOperationId = (routeInfo) => {
  const {
    method,
    route,
    moduleName,
    pathArgs,
  } = routeInfo;

  const usageRoutePart = _.split(route, moduleName)[1] || _.split(route, _.kebabCase(moduleName))[1]
  const parts = _.split(route, "/");
  const lastPart = _.last(parts);

  const firstPathArg = pathArgs[0]

  if (!usageRoutePart) {
    return _.camelCase(`${method}_${moduleName}`)
  }

  return _.camelCase(_.compact([
    ACTION_METHODS.includes(method) ? "" : method,
    lastPart,
    ...(firstPathArg && firstPathArg.name !== lastPart ? [
      'by',
      firstPathArg.name
    ] : []),
  ]).join('_'))
};

@js2me js2me closed this as completed Aug 19, 2022
@js2me
Copy link
Member

js2me commented Aug 19, 2022

Otherwise you are able to change operation id with using templates
https://github.com/acacode/swagger-typescript-api/blob/master/templates/base/route-name.eta

Also FYI - return value from route-name.eta template is a second argument for onFormatRouteName hook called templateRouteName

@VitaliKhileuski
Copy link
Author

@js2me Thanks, I used operation id as names

@Leningram
Copy link

Where should I put the config with hooks? In what file and what folder? I just have swagger.json in my root folder of react app and then just run npx swagger-typescript-api ...etc

@js2me
Copy link
Member

js2me commented Nov 10, 2022

@Leningram Hi!

You can put config with hooks where you want!

I put config at the root of project, near with src, types folder inside the swagger-api-codegen directory
image

And use this scripts from package.json
image

And thats my config

require('dotenv').config();
const { generateApi } = require("swagger-typescript-api");
const path = require('path');
const generateOperationId = require("./generateOperationId");

generateApi({
  url: process.env.SWAGGER_SCHEMA,
  output: path.resolve(__dirname, '../src/shared/api/__generated__'),
  templates: path.resolve(__dirname, './templates'),
  httpClientType: "axios",
  cleanOutput: true,
  modular: true,
  singleHttpClient: true,
  moduleNameIndex: 2,
  extractRequestBody: true,
  extractRequestParams: true,
  extractResponseBody: true,
  extractResponseError: true,
  generateResponses: true,
  sortTypes: true,
  hooks: {
    onFormatRouteName: (routeInfo, templateRouteName) => {
      if (!routeInfo.operationId) {
        return generateOperationId(routeInfo);
      }

      return templateRouteName;
    },
    onPrepareConfig: (configuration) => {
      // configuration.routes.combined = configuration.routes.combined.filter(route => route.moduleName !== "ws")
    }
  }
})

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants