openapi-backend
Advanced tools
Comparing version
@@ -80,2 +80,3 @@ import type { Options as AjvOpts } from 'ajv'; | ||
validate: boolean | BoolPredicate; | ||
ignoreTrailingSlashes: boolean; | ||
ajvOpts: AjvOpts; | ||
@@ -101,2 +102,3 @@ customizeAjv: AjvCustomizer | undefined; | ||
* @param {boolean} opts.validate - whether to validate requests with Ajv (default: true) | ||
* @param {boolean} opts.ignoreTrailingSlashes - whether to ignore trailing slashes when routing (default: true) | ||
* @param {boolean} opts.ajvOpts - default ajv opts to pass to the validator | ||
@@ -103,0 +105,0 @@ * @param {{ [operationId: string]: Handler | ErrorHandler }} opts.handlers - Operation handlers to be registered |
@@ -39,2 +39,3 @@ "use strict"; | ||
* @param {boolean} opts.validate - whether to validate requests with Ajv (default: true) | ||
* @param {boolean} opts.ignoreTrailingSlashes - whether to ignore trailing slashes when routing (default: true) | ||
* @param {boolean} opts.ajvOpts - default ajv opts to pass to the validator | ||
@@ -57,3 +58,3 @@ * @param {{ [operationId: string]: Handler | ErrorHandler }} opts.handlers - Operation handlers to be registered | ||
]; | ||
const optsWithDefaults = Object.assign({ apiRoot: '/', validate: true, strict: false, quick: false, ajvOpts: {}, handlers: {}, securityHandlers: {} }, opts); | ||
const optsWithDefaults = Object.assign({ apiRoot: '/', validate: true, strict: false, quick: false, ignoreTrailingSlashes: true, ajvOpts: {}, handlers: {}, securityHandlers: {} }, opts); | ||
this.apiRoot = optsWithDefaults.apiRoot; | ||
@@ -64,2 +65,3 @@ this.inputDocument = optsWithDefaults.definition; | ||
this.validate = optsWithDefaults.validate; | ||
this.ignoreTrailingSlashes = optsWithDefaults.ignoreTrailingSlashes; | ||
this.handlers = Object.assign({}, optsWithDefaults.handlers); // Copy to avoid mutating passed object | ||
@@ -117,3 +119,7 @@ this.securityHandlers = Object.assign({}, optsWithDefaults.securityHandlers); // Copy to avoid mutating passed object | ||
// initalize router with dereferenced definition | ||
this.router = new router_1.OpenAPIRouter({ definition: this.definition, apiRoot: this.apiRoot }); | ||
this.router = new router_1.OpenAPIRouter({ | ||
definition: this.definition, | ||
apiRoot: this.apiRoot, | ||
ignoreTrailingSlashes: this.ignoreTrailingSlashes, | ||
}); | ||
// initalize validator with dereferenced definition | ||
@@ -120,0 +126,0 @@ if (this.validate !== false) { |
{ | ||
"name": "openapi-backend", | ||
"description": "Build, Validate, Route, Authenticate and Mock using OpenAPI definitions. Framework-agnostic", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"author": "Viljami Kuosmanen <viljami@avoinsorsa.fi>", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
@@ -119,3 +119,3 @@ <h1 align="center"><img alt="openapi-backend" src="./header.png" style="max-width:50rem"></h1> | ||
```javascript | ||
import Hapi from 'hapi'; | ||
import Hapi from '@hapi/hapi'; | ||
@@ -122,0 +122,0 @@ const server = new Hapi.Server({ host: '0.0.0.0', port: 9000 }); |
@@ -46,2 +46,3 @@ import type { OpenAPIV3 } from 'openapi-types'; | ||
apiRoot: string; | ||
private ignoreTrailingSlashes; | ||
/** | ||
@@ -58,2 +59,3 @@ * Creates an instance of OpenAPIRouter | ||
apiRoot?: string; | ||
ignoreTrailingSlashes?: boolean; | ||
}); | ||
@@ -88,5 +90,4 @@ /** | ||
* - http method to lowercase | ||
* - path leading slash 👍 | ||
* - path trailing slash 👎 | ||
* - path query string 👎 | ||
* - remove path leading slash | ||
* - remove path query string | ||
* | ||
@@ -99,4 +100,6 @@ * @export | ||
/** | ||
* Normalises path for matching: strips apiRoot prefix from the path. | ||
* Normalises path for matching: strips apiRoot prefix from the path | ||
* | ||
* Also depending on configuration, will remove trailing slashes | ||
* | ||
* @export | ||
@@ -106,3 +109,3 @@ * @param {string} path | ||
*/ | ||
normalizePath(path: string): string; | ||
normalizePath(pathInput: string): string; | ||
/** | ||
@@ -109,0 +112,0 @@ * Parses and normalizes a request |
@@ -24,4 +24,6 @@ "use strict"; | ||
constructor(opts) { | ||
var _a; | ||
this.definition = opts.definition; | ||
this.apiRoot = opts.apiRoot || '/'; | ||
this.ignoreTrailingSlashes = (_a = opts.ignoreTrailingSlashes) !== null && _a !== void 0 ? _a : true; | ||
} | ||
@@ -121,5 +123,4 @@ matchOperation(req, strict) { | ||
* - http method to lowercase | ||
* - path leading slash 👍 | ||
* - path trailing slash 👎 | ||
* - path query string 👎 | ||
* - remove path leading slash | ||
* - remove path query string | ||
* | ||
@@ -131,11 +132,19 @@ * @export | ||
normalizeRequest(req) { | ||
return Object.assign(Object.assign({}, req), { path: (req.path || '') | ||
.trim() | ||
.split('?')[0] // remove query string | ||
.replace(/\/+$/, '') // remove trailing slash | ||
.replace(/^\/*/, '/'), method: req.method.trim().toLowerCase() }); | ||
var _a; | ||
let path = ((_a = req.path) === null || _a === void 0 ? void 0 : _a.trim()) || ''; | ||
// add leading prefix to path | ||
if (!path.startsWith('/')) { | ||
path = `/${path}`; | ||
} | ||
// remove query string from path | ||
path = path.split('?')[0]; | ||
// normalize method to lowercase | ||
const method = req.method.trim().toLowerCase(); | ||
return Object.assign(Object.assign({}, req), { path, method }); | ||
} | ||
/** | ||
* Normalises path for matching: strips apiRoot prefix from the path. | ||
* Normalises path for matching: strips apiRoot prefix from the path | ||
* | ||
* Also depending on configuration, will remove trailing slashes | ||
* | ||
* @export | ||
@@ -145,4 +154,13 @@ * @param {string} path | ||
*/ | ||
normalizePath(path) { | ||
return path.replace(new RegExp(`^${this.apiRoot}/?`), '/'); | ||
normalizePath(pathInput) { | ||
let path = pathInput.trim(); | ||
// strip apiRoot from path | ||
if (path.startsWith(this.apiRoot)) { | ||
path = path.replace(new RegExp(`^${this.apiRoot}/?`), '/'); | ||
} | ||
// remove trailing slashes from path if ignoreTrailingSlashes = true | ||
while (this.ignoreTrailingSlashes && path.length > 1 && path.endsWith('/')) { | ||
path = path.substr(0, path.length - 1); | ||
} | ||
return path; | ||
} | ||
@@ -149,0 +167,0 @@ /** |
104393
1.28%2233
1.32%