1.0.9 • Published 3 years ago

hapi-openapi3 v1.0.9

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

Hapi OpenAPI 3

Generates OpenAPI 3 for Hapi. Validator should be Joi. Inspired by hapi-swagger.

Installation

npm install hapi-openapi3

Basic setup

const hapiOpenApi3 = require("hapi-openapi3");
const Hapi = require("hapi");
const Joi = require("joi");

const start = async () => {
  const server = Hapi.Server({
    host: "localhost",
    port: 3000,
  });

  await server.register({
    plugin: hapiOpenApi3,
    options: {
      info: {
        title: "Test API Documentation",
        version: "1.0.0",
      },
    },
  });

  server.route({
    path: "/example",
    method: "get",
    options: {
      validate: {
        headers: {
          authorization: Joi.string().required(),
        },
      },
    },
  });

  await server.start();
};

start();

Then open /openapi.json

Options

Server Plugin Options

  • includeFn - function that determines if route will be included in open api definition (type: (route: RequestRoute) => boolean, required: true)
  • pathFn - function that provides path and must return a new path (type: (path: string) => string))
  • jsonPath - path to open api 3 json definition. (type: string, default: /openapi.json)
  • pathPrefixSize - selects what segment of the URL path is used to group endpoints. (type: number, default: 1)
  • singleSchemaInParams - ignores parameters (header, params, query) that cannot be parsed to a single schema. (type: boolean, default: false)
  • info - info about the project (type: info)
  • serversFn - function that provides url and must return array of server URL (type: (url: string) => server[])
  • servers - array of server URL, this will automatically be derived from hapi's server object if left blank. Ignored if serversFn is provided (type: server[])

Route Options

See examples

  • options.validate - used by this plugin to define headers, params, query and payload schema (type: Hapi.RouteOptionsValidate)
  • options.response.schema - Schema of response, status code defaults to 200 (type: Joi.Schema)
  • options.response.status - schema of response per status code (type: object<string, Joi.schema>)
  • options.plugins["hapi-openapi3"].request - You can add examples here. (type: requestOptions)
  • options.plugins["hapi-openapi3"].response.schema - You can add examples here. You can also add your schema here (type: responseOptions)
  • options.plugins["hapi-openapi3"].response.status - You can add examples here per status code. You can also add your schema here (type: object<string, responseOptions>)

References:

fielddescriptiontypedefaultrequired
titleProject TitlestringDocumentation Pagetrue
versionProject Versionstring1.0.0true
descriptionProject Descriptionstringfalse
termsOfServiceProject Terms of Servicestringfalse
termsOfServiceProject Terms of Servicestringfalse
contact.nameContact Namestringfalse
contact.urlContact URLstringfalse
contact.emailContact Emailstringfalse
license.emailLicense Namestringfalse
license.urlLicense URLstringfalse
fielddescriptiontypedefaultrequired
urlServer URLstring<server_url>true
descriptionProject Versionstringfalse
optiondescriptiontypedefaultrequired
exampleSingle Exampleanyfalse
examplesMultiple Examplesobject\<string, any>false
optiondescriptiontypedefaultrequired
headerJoi Schema of Headerstringfalse
payloadJoi Schema of Payloadstringfalse
exampleSingle Exampleanyfalse
examplesMultiple Examplesobject\<string, any>false
1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.3-alpha.7

3 years ago

1.0.3-alpha.5

3 years ago

1.0.3-alpha.6

3 years ago

1.0.3-beta.0

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.3-alpha.4

3 years ago

1.0.3-alpha.3

3 years ago

1.0.3-alpha.2

3 years ago

1.0.3-alpha.1

3 years ago

1.0.3-alpha.0

3 years ago

1.0.2-alpha.0

3 years ago