3.0.11 • Published 1 year ago

@nanoexpress/middleware-schemator v3.0.11

Weekly downloads
3
License
MIT
Repository
github
Last release
1 year ago

schemator

schemator middleware for nanoexpress

You can think this middleware combine of Swagger documentation and Ajv validation like original nanoexpress built-in feature + swagger-ui middleware

Installation

npm i @nanoexpress/middleware-schemator
# or
yarn add @nanoexpress/middleware-schemator

Schema type

  • YML (preferred)
  • JSON
  • RAW JS Object

YML

const schematorInstance = schemator({ swaggerPath: './swagger.yml' });

app.define(schematorInstance.define);

/// auth/index.js
app.get(
  '/auth',
  // Here any body-parser, form-data logic (all preprocess middlewares)
  schematorInstance.load({
    attach: '/auth',
    method: 'get',
    path: './auth/docs.yml'
  }),
  async (req) => {
    // your logic here
  }
);

JSON

const schematorInstance = schemator({ swaggerPath: './swagger.json' });

app.define(schematorInstance.define);

/// auth/index.js
app.get(
  '/auth',
  // Here any body-parser, form-data logic (all preprocess middlewares)
  schematorInstance.load({
    attach: '/auth',
    method: 'get',
    path: './auth/docs.json'
  }),
  async (req) => {
    // your logic here
  }
);

RAW JS Object

const schematorInstance = schemator({ swaggerRAW: {} });

app.define(schematorInstance.define);

/// auth/index.js
app.get(
  '/auth',
  // Here any body-parser, form-data logic (all preprocess middlewares)
  schematorInstance.load({
    attach: '/auth',
    method: 'get',
    raw: {
      path: {
        '/': {
          /* infos here */
        }
      }
    }
  }),
  async (req) => {
    // your logic here
  }
);

Example

See the examples directory

Usage

ESM Module

import schemator from '@nanoexpress/middleware-schemator';

const schematorInstance = schemator({ swaggerPath: './swagger.yml' });

app.define(schematorInstance.define);

app.get(
  '/auth',
  // Here any body-parser, form-data logic (all preprocess middlewares)
  schematorInstance.load({
    attach: '/auth',
    method: 'get',
    path: './auth/docs.yml'
  }),
  async (req) => {
    // your logic here
  }
);

CJS Module

const bodyParser = require('@nanoexpress/middleware-schemator/cjs');

const schematorInstance = schemator({ swaggerPath: './swagger.yml' });

app.define(schematorInstance.define);

app.get(
  '/auth',
  // Here any body-parser, form-data logic (all preprocess middlewares)
  schematorInstance.load({
    attach: '/auth',
    method: 'get',
    path: './auth/docs.yml'
  }),
  async (req) => {
    // your logic here
  }
);

Options

Initialize options

Options nameDefaultRequiredDescription
options.swaggerPath-Yes or options.swaggerRAWSwagger schema file path
options.swaggerRAW-Yes or options.swaggerPathSwagger schema RAW Object

schematorInstance.load(options, AjvConfig) options

Options nameDefaultRequiredDescription
options.method-YesYour router method (lowercase)
options.attach-YesYour router path (with Swagger path format)
options.path-Yes or options.rawRoute Swagger schema file path
options.raw-Yes or options.pathRoute Swagger schema RAW Object

See Ajv configurations for more customization

schematorInstance.render(options) options

Options nameDefaultRequiredDescription
options.titleSchematorYesSwagger UI Title
options.exposePath-YesschematorInstance.expose() path

License

MIT

3.0.11

1 year ago

3.0.10

3 years ago

3.0.9

3 years ago

3.0.8

3 years ago

3.0.7

3 years ago

3.0.6

3 years ago

3.0.5

3 years ago

3.0.4

3 years ago

3.0.3

3 years ago

3.0.2

3 years ago

3.0.1

3 years ago

3.1.0

3 years ago

3.0.0

3 years ago

2.0.0

3 years ago

1.1.0

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago