There are several tools available that can help generate an OpenAPI specification from a TypeScript type declaration. Here are a few options:
/**
* @swagger
*
* definitions:
* User:
* type: object
* properties:
* firstName:
* type: string
* lastName:
* type: string
*/
interface User {
firstName: string;
lastName: string;
}
import { OpenAPIV3 } from 'ts-openapi';
interface User {
firstName: string;
lastName: string;
}
const schema: OpenAPIV3.Document = {
openapi: '3.0.0',
info: {
title: 'My API',
version: '1.0.0',
},
paths: {
'/users': {
get: {
summary: 'Get a list of users',
responses: {
'200': {
description: 'OK',
content: {
'application/json': {
schema: {
type: 'array',
items: {
$ref: '#/components/schemas/User',
},
},
},
},
},
},
},
},
},
components: {
schemas: {
User: {
type: 'object',
properties: {
firstName: {
type: 'string',
},
lastName: {
type: 'string',
},
},
},
},
},
};
import { createConnection } from 'typeorm';
import { routingControllersToSpec } from 'routing-controllers-openapi';
import { UserController } from './controllers';
async function getApiSpec() {
const connection = await createConnection();
const routingControllersOptions = {
controllers: [UserController],
};
const spec = routingControllersToSpec(routingControllersOptions, {}, {
components: {
schemas: {
...connection.options.entities.reduce((acc, entity) => {
acc[entity.name] = {
type: 'object',
properties: entity.columns.reduce((innerAcc, column) => {
innerAcc[column.propertyName] = {
type: column.type,
};
return innerAcc;
}, {}),
};
return acc;
}, {}),
},
},
});
await connection.close();
return spec;
}
const apiSpec = await getApiSpec();
These are just a few examples of the tools available for generating OpenAPI schemas from TypeScript types. Choose the one that best fits your needs and preferences.
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2021-06-04 11:00:00 +0000
Seen: 14 times
Last updated: Dec 23 '22
How can one ensure that sub-classes have uniform method parameters in TypeScript?
Can a TypeScript definition for icon names be provided in MaterialCommunityIcons for React Native?
How can I deal with Expression.Error related to a column in Power Query?
How can you implement pagination in Oracle for the LISTAGG() function?
What is the process for implementing a FutureBuilder on an OnTap function in Flutter?
How can we require users to be logged in before they can access the root folders in WordPress?
In SCSS, what is the method for grouping and reusing a set of classes and styles?