Search code examples
typescriptapitypesswaggergenerate

swagger-typescript-api - how can i remove type of request after request name?


I would like to generate types from a Swagger file without adding a request type after the request name.

For example: When I have endpoint /assortment and request type: POST. I get Api.assortmentCreate() method in api client.

I want to delete the name "create" because it might be something else.

Does anyone know how to help me please?


Solution

  • swagger-typescript-api is template driven. It uses Eta templates. You can supply your own template to override the default templates. You can do this by creating a local templates directory and dropping your replacement templates in there. For example I wanted to change request names from the routeCreate format to postRoute, So I dropped the following route-name.eta template into templates.

    <%
    const { routeInfo, utils } = it;
    const {
      operationId,
      method,
      route,
      moduleName,
      responsesTypes,
      description,
      tags,
      summary,
      pathArgs,
    } = routeInfo;
    const { _, fmtToJSDocLine, require } = utils;
    
    const methodAliases = {
      get: (suffix) => _.camelCase(`get${suffix}`),
      post: (suffix) => _.camelCase(`post${suffix}`),
      put: (suffix) => _.camelCase(`put${suffix}`),
      patch: (suffix) => _.camelCase(`patch${suffix}`),
      delete: (suffix) => _.camelCase(`delete${suffix}`),
    };
    
    const createCustomOperationId = (method, route, moduleName) => {
        console.log(`=== ${method} ${route}`);
      const hasPathInserts = /\{(\w){1,}\}/g.test(route);
      const splitedRouteBySlash = _.compact(_.replace(route, /\{(\w){1,}\}/g, "").split("/"));
      const routeParts = (splitedRouteBySlash.length > 1
        ? splitedRouteBySlash.splice(1)
        : splitedRouteBySlash
      ).join("_");
      const suffix = route.indexOf('{scope}') < 0 ? '' : '_scope';
      return methodAliases[method]
        ? methodAliases[method](suffix)
        : _.camelCase(_.lowerCase(method) + "_" + [moduleName].join("_")) || "index";
    };
    
    if (operationId)
      return _.camelCase(operationId);
    if (route === "/")
      return _.camelCase(`${_.lowerCase(method)}Root`);
    
    return createCustomOperationId(method, route, moduleName);
    %>