📚 Swagger integration for tinyhttp
Go to file
shayan javani cacc0fbd32 chore: updated deps 2023-10-19 13:10:18 +03:30
.github/workflows fix: another syntax error 2021-06-29 13:24:52 +03:00
.husky feat: support recursive schemas 2022-02-11 10:29:53 +03:30
src Fix serveDocs() 2023-10-10 17:16:16 -03:00
tests fix(test): fixed the tests 2023-10-19 13:10:00 +03:30
.eslintignore chore: updated config 2021-06-28 15:45:48 +07:00
.eslintrc chore: bump deps, fix tests replace esbuild loader with tsx 2023-03-20 00:32:34 +02:00
.gitignore only output .js 2021-06-27 08:58:31 +04:30
.prettierignore chore: updated config 2021-06-28 15:45:48 +07:00
.prettierrc style: added prettier + eslint 2021-06-27 12:27:02 +07:00
LICENSE Initial commit 2021-06-23 12:20:27 +03:00
README.md Update README.md 2023-05-27 14:45:05 -04:00
commitlint.config.cjs chore: updated config 2021-06-28 15:45:48 +07:00
logo.svg Add files via upload 2021-06-27 00:38:31 +03:00
package.json chore: updated deps 2023-10-19 13:10:18 +03:30
pnpm-lock.yaml chore: updated deps 2023-10-19 13:10:18 +03:30
rollup.config.js chore: bump deps, fix tests replace esbuild loader with tsx 2023-03-20 00:32:34 +02:00
tsconfig.json fix: add `@tinyhttp/app` as a direct dependency 2021-08-22 17:53:47 +03:00

README.md


@tinyhttp/swagger

NPM NPM GitHub Workflow Status Coverage


Swagger integration for tinyhttp. This library allows you to easily generate documentation for your API.

Install

pnpm i @tinyhttp/swagger

Example

import { App } from '@tinyhttp/app'
import { addToDocs, serveDocs } from '@tinyhttp/swagger'

// In case the value for a given field is an object, @tinyhttp/swagger only uses the type, optional or items (in case type is array)
// Other fields are ignored and are shown here only to imply that the same schema object can be used for validation by the fastest-validator package
const schema = {
  id: { type: 'number', positive: true, integer: true },
  name: { type: 'string', min: 3, max: 255 },
  status: 'boolean'
}

const app = new App()

app
  .get('/docs/:docId', addToDocs({ params: { docId: 'number' } }), (req, res) => {
    res.status(200).send('done')
  })
  .post(
    '/docs/:docId',
    addToDocs(
      {
        headers: { authorization: 'string' },
        params: { docId: 'number' },
        body: schema
      },
      ['docs']
    ),
    (req, res) => void res.status(200).send('done')
  )
  .get('/users', addToDocs({ query: { userId: { type: 'number', optional: true } } }, ['users']), (req, res) => {
    res.status(200).send('done')
  })
  .get('/:userId/:docId', addToDocs({ params: { userId: 'number', docId: 'number' } }), (req, res) => {
    res.status(200).send('done')
  })

// Only title is required. if servers and description are not provided, nothing is shown. version and prefix have default values of 0.1 and docs.
serveDocs(app, {
  title: 'example',
  version: '1.0',
  prefix: 'api-docs',
  description: 'this is an example for @tinyhttp/swagger',
  servers: ['www.host1.com/api/v1', 'api.host2.org/v1']
})
app.listen(3000)