Giter Site home page Giter Site logo

node-ts-openapi-dsl's Introduction

TypeScript OpenAPI DSL

Build OpenAPI3 contracts declaratively.

Install

npm i ts-openapi-dsl

Usage

Example

spec.paths = paths({
  getUser: operation({
    method: 'get',
    path: '/users/:id',
    responses: {
      200: json({
        schema: array(string()),
      }),
    },
  }),
});

// same as
spec.paths = {
  '/users/:id': {
    'get': {
      operationId: 'getUser',
      responses: {
        '200': {
          content: {
            'application/json': {
              schema: {
                type: 'array',
                items: {
                  type: 'string',
                },
              },
            },
          },
        },
      },
    },
  },
};

Schema helpers

Schema helpers can be used to remove some boilerplate around JSON schema definitions. Every helper returns a JSON schema object.

import { types as t } from 'ts-openapi-dsl';

const User = t.object('An object that represents a user', {
  id: t.integer('The ID of the user'),
  name: t.string('The name of the user'),
  createdAt: t.dateTime(),
  foo: {
    type: 'string',
    description: 'You can use raw JSON schema as well...',
  },
});

Every helper accepts an extra parameter that can be used to decorate the schema with additional properties.

boolean(description?: string, extra?: Schema): Schema

Defines a boolean schema, essentially:

const type = {
  type: 'boolean',
  description,
  ...extra,
};

integer(description?: string, extra?: Schema): Schema

Defines an integer schema, essentially:

const type = {
  type: 'integer',
  description,
  ...extra,
};

number(description?: string, extra?: Schema): Schema

Defines an number schema, essentially:

const type = {
  type: 'number',
  description,
  ...extra,
};

string(description?: string, extra?: Schema): Schema

Defines a string schema, essentially:

const type = {
  type: 'string',
  description,
  ...extra,
};

pattern(pattern: RegExp, description?: string, extra?: Schema): Schema

Defines a string schema with a regex pattern.

date(description?: string, extra?: Schema): Schema

Defines a string schema with date format.

dateTime(description?: string, extra?: Schema): Schema

Defines a string schema with date-time format.

binary(description?: string, extra?: Schema): Schema

Defines a string schema with binary format.

email(description?: string, extra?: Schema): Schema

Defines a string schema with email format.

constant(value: string, description?: string, extra?: Schema): Schema

Defines a string schema with an enum that only accept a single value, essentially:

const type = {
  type: 'string',
  description,
  enum: [value],
  ...extra,
};

choice(values: Record<string, string>, extra?: Schema): Schema

Defines a string schema with an enum that only accepts predefined values. Keys of values are the allowed values, and the corresponding values in the object are the descriptions.

const type = choice({
  FOO: 'Description of FOO',
  BAR: 'Description of BAR',
});

// same as
cons type = {
  type: 'string',
  enum: ['FOO', 'BAR'],
  description: '* `FOO` - Description of FOO\n* `BAR` - Description of BAR',
};

array(items: Schema, description?: string, extra?: Schema): Schema

Defines an array schema with the specified items, essentially:

const type = {
  type: 'array',
  description,
  items,
  ...extra,
};

object(properties: Record<string, Schema>, description?: string, extra?: Schema): Schema

Defines an object schema with the specified properties.

Every helpers has an optional variant, eg. string.optional(...), which signals to its parent object to make it an optional property.

Objects are strict by default (ie. additionalProperties is set to false).

const type = object({
  foo: string(),
  bar: string.optional(),
});

// same as
const type = {
  type: 'object',
  additionalProperties: false,
  required: ['foo'],
  properties: {
    foo: {
      type: 'string',
    },
    bar: {
      type: 'string',
    },
  },
};

required(schema: Schema): Schema

Turns the given schema into a required schema (ie. in a wrapper object, it'll be a required property).

const wrapped = string.optional();

const wrapper = object({
  foo: wrapped,
  bar: required(wrapped),
});

// same as
const wrapper = {
  type: 'object',
  additionalProperties: false,
  required: ['bar'],
  properties: {
    foo: { type: 'string' },
    bar: { type: 'string' },
  },
};

optional(schema: Schema): Schema

Turns the given schema into an optional schema (ie. in a wrapper object, it won't be a required property).

const wrapped = string.required();

const wrapper = object({
  foo: wrapped,
  bar: optional(wrapped),
});

// same as
const wrapper = {
  type: 'object',
  additionalProperties: false,
  required: ['foo'],
  properties: {
    foo: { type: 'string' },
    bar: { type: 'string' },
  },
};

OpenAPI helpers

response(props: Response): ResponseObject

Defines a response object with a single media type:

const res = response({
  description: 'Response description',
  headers,
  links,
  type: 'application/json',
  schema,
  examples,
  encoding,
});

// same as
const res = {
  description: 'Response description',
  headers,
  links,
  content: {
    'application/json': {
      schema,
      examples,
      encoding,
    },
  },
};

json(props: Response): ResponseObject

Shorthand for creating application/json responses.

const res = json({
  description: 'Response description',
  headers,
  links,
  schema,
  examples,
  encoding,
});

// same as
const res = {
  description: 'Response description',
  headers,
  links,
  content: {
    'application/json': {
      schema,
      examples,
      encoding,
    },
  },
};

operation(op: Operation): OperationObject

Defines an operation with path and method.

paths(ops: Record<string, Operation>): PathsObject

Creates paths based on the provided ops.

License

MIT

node-ts-openapi-dsl's People

Contributors

madbence avatar necccc avatar

Stargazers

Roman avatar Roman Vasilev avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.