https://github.com/steelydylan/express-ts-generator
Create express typed Request and Response based on OpenAPI specification
https://github.com/steelydylan/express-ts-generator
Last synced: 8 months ago
JSON representation
Create express typed Request and Response based on OpenAPI specification
- Host: GitHub
- URL: https://github.com/steelydylan/express-ts-generator
- Owner: steelydylan
- Created: 2020-01-26T07:05:10.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2020-01-26T08:53:14.000Z (over 6 years ago)
- Last Synced: 2025-09-08T00:26:42.459Z (9 months ago)
- Language: JavaScript
- Size: 171 KB
- Stars: 4
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# express-ts-generator
It strongly support type definition for express request and response model in accordance with Open API
## Install
```sh
$ npm install dtsgenerator express-ts-generator --save
```
## Generate definition file
```sh
$ npx dtsgen openapi/openapi.yaml -o ./src/@types/openapi.d.ts && npx apigen -s ./src/@types/openapi.d.ts -d ./src/@types/api.ts
```
generated file will look like this
```ts
/* eslint-disable @typescript-eslint/no-namespace */
/* eslint-disable @typescript-eslint/no-explicit-any */
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { Controller } from 'express-ts-generator';
export namespace Music$MusicIdController {
export type Get = Controller<{
response: Paths.Music$MusicId.Get.Responses.$200;
}>;
}
export namespace MusicsController {
export type Get = Controller<{
response: Paths.Musics.Get.Responses.$200;
}>;
export type Post = Controller<{
body: Paths.Musics.Post.RequestBody;
}>;
}
```
## Usage
in express controller...
```ts
import { SomeController } from './types/api';
export const Post: SomeController.Post = async (
request,
response
): Promise => {
// request and response will be typed automatically
};
export const Get: SomeController.Get = async (
request,
response
): Promise => {
// request and response will be typed automatically
};
```
## You can see how useful it is!!

## Demo Repository
[Github](https://github.com/steelydylan/express-ts-generator-test)