@map-colonies/openapi-express-types
v0.2.0
Published
TypeScript utilities to create strictly typed express request handlers from an OpenAPI specification.
Downloads
178
Maintainers
Keywords
Readme
@map-colonies/openapi-express-types
TypeScript utilities to create strictly typed express request handlers from an OpenAPI specification.
API documentation
Check the autogenerated documentation here.
Usage
import type { TypedRequestHandlers } from '@map-colonies/openapi-express-types';
import type { paths, operations } from './generated-openapi-types';
// Enforces correct typed request handlers based on OpenAPI schema
export type Handlers = TypedRequestHandlers<
paths,
operations,
{
locals: { user: string }; // Optional custom express locals
}
>;
// Example
const getPetById: Handlers['getPetById'] = (req, res, next) => {
// req.params.petId is strongly typed
// res.status() / res.json() will be strongly typed
res.status(200).json({ id: 1, name: 'Fido' });
};