GitHub - outsid3rx/revortex · GitHub
Skip to content

outsid3rx/revortex

Repository files navigation

revortex

Same page on russian.

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.

Principle

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.

Installation

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-wrapper

Configuration and startup

Configuration via parameters

For 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/

Configuration via file or package.json

Create a revortex.json file in the root of the project or create revortex field in package.json file and set the required fields:

repo

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.

out

A reference to the directory where the final client will be saved, e.g. ./lib/.

alias (optional)

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/.

sourceDir (optional)

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/.

mainPath (optional)

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.

file (optional)

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/"
}

Run

The application should be run from the same directory as the configuration file:

pnpm exec revortex

Additionally, 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.ts
task generate-api

Usage

Now 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.

Roadmap

  • Add support for axios to create a wrapper
  • Add support for fetch to create a wrapper.
  • Add support for parameters at application startup instead of configuration file

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

Contributors