npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ts-openapi-dsl

v0.0.9

Published

TypeScript DSL for building OpenAPI specifications

Downloads

86

Readme

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