express-yaschema-api-handler
Advanced tools
Comparing version 1.0.8 to 1.1.0
@@ -11,2 +11,3 @@ "use strict"; | ||
}; | ||
var _a; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -144,3 +145,7 @@ exports.registerHttpApiHandler = void 0; | ||
const handlers = [...middlewares, asyncHandlerWrapper(expressHandler)]; | ||
app[methodName](relativizedUrl, ...handlers); | ||
// Delaying the actual registration with Express slightly so we can re-order the registrations to be handled in the correct order (e.g. | ||
// longer exact matches first) | ||
registerHandlerSoon(methodName, relativizedUrl, () => { | ||
app[methodName](convertYaschemaParamSyntaxForExpress(relativizedUrl), ...handlers); | ||
}); | ||
}; | ||
@@ -157,3 +162,38 @@ exports.registerHttpApiHandler = registerHttpApiHandler; | ||
}; | ||
/** Converts from api-lib / use syntax like `{name}` to method routing syntax like `:name` */ | ||
const convertYaschemaParamSyntaxForExpress = (relativeUrl) => relativeUrl.replace(/\{([^}]+)\}/g, ':$1'); | ||
const isUnsupportedHttpMethod = (method) => unsupportedHttpMethods.has(method); | ||
const registerHandlerSoon = (methodName, url, adder) => expressYaschemaDelayedApiRegistration('http', methodName, url, adder); | ||
// Note: this same code is also included in other packages, like express-yaschema-ws-api-handler, so those registrations can be consistently | ||
// ordered as well | ||
/* eslint-disable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access */ | ||
let globalPendingApiRegistrationTimeout; | ||
const globalPendingApiRegistrations = {}; | ||
const finalizeRegistration = () => { | ||
const keys = Object.keys(globalPendingApiRegistrations).sort((a, b) => b.localeCompare(a)); | ||
for (const key of keys) { | ||
console.info(`Registering handler for ${globalPendingApiRegistrations[key].humanReadableKey}`); | ||
globalPendingApiRegistrations[key].adder(); | ||
delete globalPendingApiRegistrations[key]; | ||
} | ||
}; | ||
global.expressYaschemaDelayedApiRegistration = | ||
(_a = global.expressYaschemaDelayedApiRegistration) !== null && _a !== void 0 ? _a : (((protocol, methodName, relativeUrl, adder) => { | ||
if (globalPendingApiRegistrationTimeout !== undefined) { | ||
clearTimeout(globalPendingApiRegistrationTimeout); | ||
globalPendingApiRegistrationTimeout = undefined; | ||
} | ||
// We want: | ||
// - HTTP to be the lowest-priority protocol | ||
// - Longer matches to be processed before shorter ones | ||
// - Literal matches to be processed before patterns | ||
globalPendingApiRegistrations[`${protocol.replace(/^http$/g, '!!!!')}~${methodName}~${relativeUrl.replace(/[{}]/g, '!')}`] = { | ||
humanReadableKey: `${methodName} ${protocol}://${relativeUrl}`, | ||
adder | ||
}; | ||
globalPendingApiRegistrationTimeout = setTimeout(finalizeRegistration, 0); | ||
})); | ||
const expressYaschemaDelayedApiRegistration = global | ||
.expressYaschemaDelayedApiRegistration; | ||
/* eslint-enable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access */ | ||
//# sourceMappingURL=register-http-api-handler.js.map |
{ | ||
"name": "express-yaschema-api-handler", | ||
"version": "1.0.8", | ||
"version": "1.1.0", | ||
"description": "Express handler support for yaschema-api", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -228,3 +228,8 @@ import type { Express, NextFunction, Request, RequestHandler, Response } from 'express'; | ||
const handlers: RequestHandler[] = [...middlewares, asyncHandlerWrapper(expressHandler)]; | ||
app[methodName](relativizedUrl, ...handlers); | ||
// Delaying the actual registration with Express slightly so we can re-order the registrations to be handled in the correct order (e.g. | ||
// longer exact matches first) | ||
registerHandlerSoon(methodName, relativizedUrl, () => { | ||
app[methodName](convertYaschemaParamSyntaxForExpress(relativizedUrl), ...handlers); | ||
}); | ||
}; | ||
@@ -248,2 +253,48 @@ | ||
/** Converts from api-lib / use syntax like `{name}` to method routing syntax like `:name` */ | ||
const convertYaschemaParamSyntaxForExpress = (relativeUrl: string) => relativeUrl.replace(/\{([^}]+)\}/g, ':$1'); | ||
const isUnsupportedHttpMethod = (method: HttpMethod): method is UnsupportedHttpMethod => unsupportedHttpMethods.has(method); | ||
type ExpressYaschemaDelayedApiRegistrationFunc = (protocol: string, methodName: string, relativeUrl: string, adder: () => void) => void; | ||
const registerHandlerSoon = (methodName: ExpressHandlerMethodName, url: string, adder: () => void) => | ||
expressYaschemaDelayedApiRegistration('http', methodName, url, adder); | ||
// Note: this same code is also included in other packages, like express-yaschema-ws-api-handler, so those registrations can be consistently | ||
// ordered as well | ||
/* eslint-disable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access */ | ||
let globalPendingApiRegistrationTimeout: ReturnType<typeof setTimeout> | undefined; | ||
const globalPendingApiRegistrations: Record<string, { humanReadableKey: string; adder: () => void }> = {}; | ||
const finalizeRegistration = () => { | ||
const keys = Object.keys(globalPendingApiRegistrations).sort((a, b) => b.localeCompare(a)); | ||
for (const key of keys) { | ||
console.info(`Registering handler for ${globalPendingApiRegistrations[key].humanReadableKey}`); | ||
globalPendingApiRegistrations[key].adder(); | ||
delete globalPendingApiRegistrations[key]; | ||
} | ||
}; | ||
(global as any).expressYaschemaDelayedApiRegistration = | ||
(global as any).expressYaschemaDelayedApiRegistration ?? | ||
(((protocol: string, methodName: string, relativeUrl: string, adder: () => void) => { | ||
if (globalPendingApiRegistrationTimeout !== undefined) { | ||
clearTimeout(globalPendingApiRegistrationTimeout); | ||
globalPendingApiRegistrationTimeout = undefined; | ||
} | ||
// We want: | ||
// - HTTP to be the lowest-priority protocol | ||
// - Longer matches to be processed before shorter ones | ||
// - Literal matches to be processed before patterns | ||
globalPendingApiRegistrations[`${protocol.replace(/^http$/g, '!!!!')}~${methodName}~${relativeUrl.replace(/[{}]/g, '!')}`] = { | ||
humanReadableKey: `${methodName} ${protocol}://${relativeUrl}`, | ||
adder | ||
}; | ||
globalPendingApiRegistrationTimeout = setTimeout(finalizeRegistration, 0); | ||
}) satisfies ExpressYaschemaDelayedApiRegistrationFunc); | ||
const expressYaschemaDelayedApiRegistration = (global as any) | ||
.expressYaschemaDelayedApiRegistration as ExpressYaschemaDelayedApiRegistrationFunc; | ||
/* eslint-enable @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-member-access */ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
68567
889