@flanc/router-json
Advanced tools
Comparing version 0.0.3 to 0.0.4
50
index.js
"use strict"; | ||
/* istanbul ignore file */ | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.register = exports.errorHandler = exports.path = void 0; | ||
const router_commons_1 = require("flanc/router-commons"); | ||
const documentation_1 = require("flanc/router-commons/documentation"); | ||
let router; | ||
exports.path = '/jsonapi'; | ||
function printDocumentation(req, res) { | ||
res.set('Access-Control-Allow-Origin', '*'); | ||
res.status(200).json(documentation_1.render(router._spec)); | ||
function formatter(data) { | ||
return (typeof data === 'string') ? data : JSON.stringify(data); | ||
} | ||
function errorHandler(err, req, res, next) { | ||
if (!req.path.match(new RegExp(`^${exports.path}`))) { | ||
next(err); | ||
return; | ||
function Router(app, RouterConstructor) { | ||
const router = new RouterConstructor(); | ||
router._spec = {}; | ||
function printDocumentation(req, res) { | ||
res.set('Access-Control-Allow-Origin', '*'); | ||
res.status(200).json(documentation_1.render(router._spec)); | ||
} | ||
res.status(err.status || 500).send(err); | ||
next(); | ||
} | ||
exports.errorHandler = errorHandler; | ||
function setupRouter(RouterConstructor) { | ||
router = new RouterConstructor(); | ||
router._spec = {}; | ||
if (process.env.NODE_ENV !== 'production') | ||
router.get('/docs', printDocumentation); | ||
router.register = function register(route) { | ||
router_commons_1.register(route, router, { | ||
supportedMethods: ['get', 'post', 'patch', 'delete'], | ||
responseHeaders: { 'content-type': 'application/json' }, | ||
formatError: (error) => formatter(error), | ||
formatResponse: (response) => formatter(response), | ||
}); | ||
}; | ||
router.start = function start() { }; | ||
router.stop = function stop() { }; | ||
return router; | ||
} | ||
exports.default = setupRouter; | ||
function formatter(data) { | ||
return (typeof data === 'string') ? data : JSON.stringify(data); | ||
} | ||
function register(route) { | ||
router_commons_1.register(route, router, { | ||
supportedMethods: ['get', 'post', 'patch', 'delete'], | ||
responseHeaders: { 'content-type': 'application/json' }, | ||
formatError: (error) => formatter(error), | ||
formatResponse: (response) => formatter(response), | ||
}); | ||
} | ||
exports.register = register; | ||
exports.default = Router; | ||
Router.defaultMountPath = '/jsonapi'; |
{ | ||
"name": "@flanc/router-json", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "The openapi router for flanc apis", | ||
@@ -5,0 +5,0 @@ "license": "Apache-2.0", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5766
103