@spec-validator/open-api-endpoint
TypeScript icon, indicating that this package has built-in type declarations

0.13.0 • Public • Published

@spec-validator/open-api-endpoint

The endpoint decorates server configs with an additional set of routes that generate open-api spec and a ui for it.

import http from 'http'

import { createServer, _ } from '@spec-validator/rest-api-server'
import { segmentField as $, numberField, stringField } from '@spec-validator/validator/fields'
import { withOpenApi } from '@spec-validator/open-api-endpoint'
import { expectType } from '@spec-validator/test-utils/expectType'

const itemSpec = {
  title: stringField(),
  description: stringField(),
}

const server = createServer(withOpenApi({
  routes: [
    _.POST($._('/items')).spec({
      request: {
        body: itemSpec,
      },
      response: {
        body: numberField(),
        headers: {
          title: stringField(),
        },
      },
    }).handler(async () => ({
      body: 42,
      headers: {
        title: 'Foo',
      },
    })),
    _.GET($._('/items')).spec({
      response: {
        body: [itemSpec],
      },
    }).handler(async () => ({
      body: [
        {
          title: 'Item N',
          description: 'Description',
        },
      ],
    }))
  ]
}))

expectType<http.Server & { serve: () => http.Server }, typeof server>(true)

Once served via a regular call:

server.serve()

OpenApi ui will be available at http://localhost:8080/open-api-ui without any additional effort.

Readme

Keywords

none

Package Sidebar

Install

npm i @spec-validator/open-api-endpoint

Weekly Downloads

5

Version

0.13.0

License

Apache-2.0

Unpacked Size

35.1 kB

Total Files

18

Last publish

Collaborators

  • gurunars