OpenAPI & Service Generator
An optional Node script provided separately from the Core HTTP client. It is only used when generating TypeScript types and service files from OpenAPI/Swagger JSON.
Optional feature: Not required for Core usage
General users of @byeolnaerim/typed-rx-http, /next, or /rsocket do not need to run this script or install openapi-typescript in their project.
Isolation of openapi-typescript dependency
Generating OpenAPI types requires the openapi-typescript CLI, but the package is not included in the general dependencies. The devDependencies.typescript of typed-rx-http maintains TypeScript version 6.0.3, and only uses a separate npx temporary environment in the OpenAPI auto node script.
In the basic OpenAPI generation step, openapi-typescript and [email protected] are run together, so it does not change or use the TypeScript/openapi-typescript versions installed in the user's project.
If needed, the entire command can be fixed to openApiTypescriptCommand.
Alternatively, you can just change the package version that composes the default command.
First: Prepare the project file that commonServiceFile will point to.
Before commonServiceFile appears in the generator example, you need to understand this file first. commonServiceFile is not an option that creates a file, but the path to the project-owned file from which the generated service imports common HTTP functions. The file name can be freely chosen, such as rxjsHttpService.ts or commonService.ts.
Minimum complete version full code
The file below creates createHttpClient once in the project and exports callApi and callApiStream for reuse by the generated service. If cache or session auth is not needed, you can start with this structure.
rxjsHttpService.ts
tsFull code including Session/CSR/SSR cache
To use callApiClientCache or callApiServerCache in the generated service or to handle request-specific headers and session authentication in Next.js, expand to the following full form.
rxjsHttpService.ts
tsExecution method
Generated Files
The default configuration generates the files below.
apiUnionArrays.tsgenerates not only OpenAPI schema enums but also readonly constant arrays for query, path, header, and cookie parameter enums, and handles items.enum for array query parameters.
Optional: Connect backend Swagger with project commonService
From here, an example of connecting the generator to the actual project. WebFlux/webflux-fe-dev-assistant is just one way to provide Swagger and is not a mandatory dependency.
Prepare Swagger documentation
Swagger can be from Springdoc, other OpenAPI tools, or manually written files. If you use WebFlux functional endpoints, webflux-fe-dev-assistantYou can also use this method to provide Swagger documentation.
Example of generating with specified options in the project
You can specify the backend endpoint, the swagger.json to save, the type/service output path, and commonServiceFile according to the project structure.
generateSwagger.cjs
jsIn environments where documents cannot be received via HTTP, you can use the same output options along with this. generateSwaggerFromFileYou can use this.
commonServiceFile is the import target path
commonServiceFileis the path to the project-owned file from which the generated service imports callApi, callApiStream, and cache wrapper. The generator does not have the option to create or overwrite this file.
ApiBusinessService.ts
tsExample of where the generation results are placed in the project.
The common file specified as commonServiceFile like rxjsHttpService.ts is managed directly by the project. The results under auto and @types/auto will be regenerated if the documentation changes.
File name and function name rules
The service file name is determined by the first two segments of the URL, and the function name is determined by the segments after the third. Path variables are included in the function name in the By + PascalCase format.
GET /api/business/workplaces/search
ApiBusinessService.ts → workplacesSearch({ params })
GET /api/business/workplaces/{id}
ApiBusinessService.ts → workplacesById({ path })
GET /api/orders/history/search
ApiOrdersService.ts → historySearch({ params })
POST /oauth2/login
Oauth2LoginService.ts → post({ body })
Arguments for the creation function
GeneratedServiceUsage.tsx
tsxThe query parameter key of the generated service is params, the path variable is path, and the request body is body. Each is converted to ServiceArguments for queryString, pathVariable, and body within the creation function.