Ask Your Question
1

How can an OpenAPI specification be created from a TypeScript type?

asked 2021-06-04 11:00:00 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2022-12-23 05:00:00 +0000

plato gravatar image

There are several tools available that can help generate an OpenAPI specification from a TypeScript type declaration. Here are a few options:

  1. swagger-jsdoc: This tool allows you to document your TypeScript code using JSDoc comments and generates an OpenAPI specification based on those comments. Here's an example:
/**
 * @swagger
 *
 * definitions:
 *   User:
 *     type: object
 *     properties:
 *       firstName:
 *         type: string
 *       lastName:
 *         type: string
 */

interface User {
  firstName: string;
  lastName: string;
}
  1. ts-openapi: This is a TypeScript library that allows you to define your API using TypeScript interfaces and generates an OpenAPI schema. Here's an example:
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',
          },
        },
      },
    },
  },
};
  1. TypeORM: If you're using TypeORM as your ORM, you can use it to generate an OpenAPI schema based on your entity definitions. Here's an example:
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.

edit flag offensive delete link more

Your Answer

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

Add Answer


Question Tools

Stats

Asked: 2021-06-04 11:00:00 +0000

Seen: 14 times

Last updated: Dec 23 '22