Skip to main content
Home

Built and signed on GitHub Actions

OpenAPI 3.1 schema builder

This package works with Cloudflare Workers, Node.js, Deno, BunIt is unknown whether this package works with Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
It is unknown whether this package works with Browsers
JSR Score
88%
Published
2 months ago (0.1.3)

OpenAPI 3 Builder

TODO:

  • Write doc
  • Add support: webhooks, callbacks, links

Examples

Create Doc

import { createDoc } from '@maks11060/openapi'
import { zodPlugin } from '@maks11060/openapi/zod'
import { z } from 'zod'

const doc = createDoc({
  plugins: {schema: [zodPlugin()]},
  info: {title: 'Test', version: '1.0.0'},
})

console.log(doc.toDoc())      // get schema object
console.log(doc.toJSON(true)) // output in json format
console.log(doc.toYAML())     // output in yaml format

Usage

import { createDoc } from '@maks11060/openapi'
import { zodPlugin } from '@maks11060/openapi/zod'
import { z } from 'zod'

const doc = createDoc({
  plugins: {schema: [zodPlugin()]},
  info: {title: 'Test', version: '1.0.0'},
  tags: [{name: 'users'}],
})

doc.server({url: 'https://example.com'})

const oauth2 = doc.addSecuritySchema.oauth2('OAuth2', {
  authorizationCode: {
    authorizationUrl: 'https://example.com/authorize',
    tokenUrl: 'https://example.com/api/token',
    scopes: {
      read: '',
      edit: '',
    },
  },
})

const unauthorizedResponse = doc.addResponse('Unauthorized', (t) => {
  t.content('application/json', {}) //
    .example('Example', (t) => t.value({error: 'Unauthorized'}))
})

const user = z.object({
  id: z.string(),
  username: z.string(),
})

doc
  .addPath('/user') //
  .get((t) => {
    t.tag('users')
    t.summary('Get the authenticated user')
    t.security(oauth2, ['read'])

    t.response(200, (t) => {
      t.content('application/json', user) //
        .example('User', (t) => t.value({id: '1', username: 'user1'}))
    })
    t.response(401, unauthorizedResponse)
  })
  .patch((t) => {
    t.tag('users')
    t.describe('Update user')
    t.security(oauth2, ['edit'])

    t.response(200, (t) => {
      t.content('application/json', user) //
        .example('User', (t) => t.value({id: '1', username: 'user1'}))
    })
    t.response(401, unauthorizedResponse)
  })
Built and signed on
GitHub Actions

New Ticket: Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@maks11060/openapi

Import symbol

import * as openapi from "@maks11060/openapi";
or

Import directly with a jsr specifier

import * as openapi from "jsr:@maks11060/openapi";

Add Package

pnpm i jsr:@maks11060/openapi
or (using pnpm 10.8 or older)
pnpm dlx jsr add @maks11060/openapi

Import symbol

import * as openapi from "@maks11060/openapi";

Add Package

yarn add jsr:@maks11060/openapi
or (using Yarn 4.8 or older)
yarn dlx jsr add @maks11060/openapi

Import symbol

import * as openapi from "@maks11060/openapi";

Add Package

vlt install jsr:@maks11060/openapi

Import symbol

import * as openapi from "@maks11060/openapi";

Add Package

npx jsr add @maks11060/openapi

Import symbol

import * as openapi from "@maks11060/openapi";

Add Package

bunx jsr add @maks11060/openapi

Import symbol

import * as openapi from "@maks11060/openapi";