Utility for automatic generation of API client from Nest project source code. Designed for use inside a monorepository and does not require manual description of response options and parameters in controllers.
The package creates a small typed wrapper for each method in the controller and reuses their types - this allows the application to work with minimal configuration. For the same reason, there is a limitation - imports must be configured between applications, so revortex can primarily be used in monorepositories.
The application must be installed in the client-side repository:
# The code generator itself
pnpm add -D revortex
# A small wrapper for working in runtime
pnpm add revortex-wrapperFor running the application, you can use command line parameters that will override the values in the revortex.json configuration file.
pnpm exec revortex --out ./lib/ --alias ~server/ --mainPath ./src/main.ts --sourceDir src/ ../server/Create a revortex.json file in the root of the project or create revortex field in package.json file and set the required fields:
Link to the root directory of the project's Nest repository, e.g. ../server/ if the server and client repositories are on the same level.
A reference to the directory where the final client will be saved, e.g. ./lib/.
Path alias (if configured) to the src directory of the server side, will help set up more explicit and shorter imports. By default is built from repo and sourceDir, such as ../server/src/.
If when building Nest application you have organized the location of modules differently from the starting template, you need to specify the path to the directory that contains the modules, relative to the root directory of the server part of the application. The default is src/.
If the main file of the Nest application is located in a different place than the default src/main.ts, you need to specify the path to it relative to the root directory of the server part of the application.
If you want to specify output file name, you can use this parameter. By default, the file is named index.ts.
A normal revortex.json file looks like this:
{
"repo": "../server/",
"out": "./lib/",
"alias": "~server/"
}The application should be run from the same directory as the configuration file:
pnpm exec revortexAdditionally, you can register a command in Taskfile to run the generation from any directory and in a standardized way:
version: '3'
vars:
WEB_PATH: ./apps/web
tasks:
generate-api:
dir: “{{.WEB_PATH}}”
cmds:
- pnpm exec revortex
- prettier -w ./lib/index.tstask generate-apiNow we need to create an instance of api using the out-of-the-box utility and the ky package:
import ky from 'ky'
import { apiCall } from 'revortex/dist/wrapper'
import { createApi } from '../lib'
const api = createApi(ky, apiCall)
await api.AppController.getHello()At this point, an instance of ky is needed to create the wrapper, since the wrapper itself is built on interaction with this package.
- Add support for
axiosto create a wrapper - Add support for
fetchto create a wrapper. - Add support for parameters at application startup instead of configuration file
