How to customize generated method names — remove controller prefix from operation names? #1764
-
|
Hi, I'm using swagger-typescript-api@13.10.0 with the --modular and --axios flags to generate a TypeScript API client from an OpenAPI 3.0 schema. export class Contents<SecurityDataType = unknown> extends HttpClient<SecurityDataType> {
contentsControllerPostAsync = (...) => this.request<void, any>({ ... });
contentsControllerGetAsync = (...) => this.request<void, any>({ ... });
contentsControllerGetStreamAsync = (...) => this.request<void, any>({ ... });
}Desired behavior: export class Contents<SecurityDataType = unknown> extends HttpClient<SecurityDataType> {
postAsync = (...) => this.request<void, any>({ ... });
getAsync = (...) => this.request<void, any>({ ... });
getStreamAsync = (...) => this.request<void, any>({ ... });
}Questions:
Any guidance or examples would be greatly appreciated. Thank you for your work on this tool! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Yes, this is the kind of customization I would do with a hook, not by post-processing generated files. In current Example config idea: import { generateApi } from 'swagger-typescript-api'
const stripControllerPrefix = (name: string) =>
name.replace(/^[A-Za-z0-9]+Controller_?/i, '')
await generateApi({
input: './openapi.json',
output: './src/api',
modular: true,
httpClientType: 'axios',
hooks: {
onCreateRouteName(routeNameInfo, rawRouteInfo) {
return {
...routeNameInfo,
usage: stripControllerPrefix(routeNameInfo.usage),
original: stripControllerPrefix(routeNameInfo.original),
}
},
},
})A couple of caveats:
So: use a hook for generation-time customization; preprocess If this solves it, please mark the comment as the answer so others can find it faster. |
Beta Was this translation helpful? Give feedback.
Yes, this is the kind of customization I would do with a hook, not by post-processing generated files.
In current
swagger-typescript-api, route names can be changed withhooks.onCreateRouteNameorhooks.onFormatRouteName. For your case,onCreateRouteNameis the clearest because it receives the final route name info and the raw route info.Example config idea: