
Research
/Security News
Miasma Mini Shai-Hulud Hits ImmobiliareLabs npm Packages
Miasma Mini Shai-Hulud hits @immobiliarelabs Backstage plugins, targeting GitLab and LDAP auth packages on npm.
api-ts-utils
Advanced tools
Create documented REST API using controllers and interfaces in typescript
APIs in NodeJS should have a single source of truth for api specs between code and docs, as well as compile time type safety.
Schemas are often duplicated between json schemas for input validation, typescript types, jsdoc comment annotations or swagger-specific wrappers in your app. Routes and response status codes also suffer from similar issues where code can get out of sync with documentation.
TS-API solves this by leveraging the typescript parser to generate:
TypeScript types are extracted from the source code, such as this example:
export interface User {
name: string;
isActive?: boolean;
}
is converted to a json schema:
"User": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"isActive": {
"type": "boolean"
}
},
"required": [
"name"
]
}
@controller('/user')
export class User extends ControllerBase {
@get('/')
async listUsers(): Promise<User[]> {
...
return [{ ...account1 }, ...]
}
The controller and Rest verbs (get, post, put, etc.) optionally take a route override, otherwise it uses the class or method name by default.
A router tree is build by TS-API from all controllers in the typescript path:
AccountRouter.get('/', async(req,res,next) => { ... }
You can mount this anywhere in your app, use middleware, and treat it like any other ExpressJS router. The controller gives full access to req/res/next in the constructor. There's further customizations support, such as hooks to customize input validation, but by default it returns a 400 status.
OpenAPI 3 output for a sample controller:

ReDoc is also supported as viewer.
npm install --save-dev ts-api
First make this package a dependency. This will provide the necessary decorators @controller, @router @get @post, etc. The analyzer will search for those names and generate code that uses them, but these decorators also do things themselves like invoke the runtime type checker.
The key steps are:
See an example controller for a working reference.
@router('/api')
export default class Router extends RouterBase {
constructor(app: any) {
super(app);
require('./__routes')(this);
}
}
The easiest way to do this is an npm script (or npm install -g):
tsc && cg
The cg CLI tool can take options and specific files:
cg <options> <list of files>
You can verify output by using the hosted docs. The route will depend on where you mount the app, such as:
http://localhost:3002/api/docs
FAQs
Create documented REST API using controllers and interfaces in typescript
The npm package api-ts-utils receives a total of 458 weekly downloads. As such, api-ts-utils popularity was classified as not popular.
We found that api-ts-utils demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Research
/Security News
Miasma Mini Shai-Hulud hits @immobiliarelabs Backstage plugins, targeting GitLab and LDAP auth packages on npm.

Security News
Rolldown paused Rust React Compiler integration after a 5MB binary size increase raised concerns about shipping React-specific code to all Vite users.

Security News
/Research
Mini Shai-Hulud expands into the Go ecosystem after hitting LeoPlatform npm packages and targeting GitHub Actions workflows.