@mojaloop/central-services-shared
Version:
Shared code for mojaloop central services
82 lines (70 loc) • 2.94 kB
JavaScript
/*****
License
--------------
Copyright © 2020-2025 Mojaloop Foundation
The Mojaloop files are made available by the Mojaloop Foundation under the Apache License, Version 2.0 (the "License") and you may not use these files except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, the Mojaloop files are distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Contributors
--------------
This is the official list of the Mojaloop project contributors for this file.
Names of the original copyright holders (individuals or organizations)
should be listed with a '*' in the first column. People who have
contributed from an organization can be listed under the organization
that actually holds the copyright for their contributions (see the
Mojaloop Foundation for an example). Those individuals should have
their names indented and be marked with a '-'. Email address can be added
optionally within square brackets <email>.
* Mojaloop Foundation
- Name Surname <name.surname@mojaloop.io>
* Steven Oderayi <steven.oderayi@modusbox.com>
--------------
******/
'use strict'
const APIDocBuilder = require('../../documentation').APIDocBuilder
/**
* Hapi plugin to add '/swagger.json' and '/documentation' endpoints.
* It generates API documentation from supplied OpenAPI spec (json or yaml).
*
* options.documentPath - Full path to the OpenAPI (fka Swagger) document (JSON or YAML). Mutually exclusive to `document` option.
* options.document - OpenAPI document as string. Mutually exclusive to `documentPath` option.
*/
const plugin = {
name: 'apiDocumentation',
register: function (server, options) {
if (!options.documentPath && !options.document) throw new Error('API documentation plugin requires `options.documentPath` or `options.document` to be set.')
server.route([
{
method: 'GET',
path: '/swagger.json',
options: {
tags: ['api', 'documentation'],
handler: swaggerJSONHandler,
plugins: {
apiDocumentation: false
}
}
},
{
method: 'GET',
path: '/documentation',
options: {
tags: ['api', 'documentation'],
handler: documentationHandler,
plugins: {
apiDocumentation: false
}
}
}
])
}
}
const documentationHandler = async (_, h) => {
return h.response(await APIDocBuilder.generateDocumentation(h.realm.pluginOptions))
}
const swaggerJSONHandler = (_, h) => {
return h
.response(APIDocBuilder.swaggerJSON(h.realm.pluginOptions))
.header('content-type', 'application/json')
}
module.exports = { plugin }