express-yaschema-api-handler
Advanced tools
Comparing version 1.1.2 to 1.2.0
export * from './http-api-handler-wrapper'; | ||
export * from './logging'; | ||
export * from './on-request-validation-error'; | ||
@@ -3,0 +4,0 @@ export * from './on-response-validation-error'; |
@@ -18,2 +18,3 @@ "use strict"; | ||
__exportStar(require("./http-api-handler-wrapper"), exports); | ||
__exportStar(require("./logging"), exports); | ||
__exportStar(require("./on-request-validation-error"), exports); | ||
@@ -20,0 +21,0 @@ __exportStar(require("./on-response-validation-error"), exports); |
export * from './config/exports'; | ||
export * from './register-api-handler/exports'; | ||
export * from './register-http-api-handler/exports'; | ||
@@ -3,0 +4,0 @@ export * from './types/exports'; |
@@ -18,4 +18,5 @@ "use strict"; | ||
__exportStar(require("./config/exports"), exports); | ||
__exportStar(require("./register-api-handler/exports"), exports); | ||
__exportStar(require("./register-http-api-handler/exports"), exports); | ||
__exportStar(require("./types/exports"), exports); | ||
//# sourceMappingURL=index.js.map |
@@ -5,3 +5,4 @@ import type { Express } from 'express'; | ||
import type { HttpApiHandlerOptions } from '../types/HttpApiHandlerOptions'; | ||
/** Be sure to call `finalizeApiHandlerRegistrations` once all API registrations have been added. */ | ||
export declare const registerHttpApiHandler: <ReqHeadersT extends AnyHeaders, ReqParamsT extends AnyParams, ReqQueryT extends AnyQuery, ReqBodyT extends unknown, ResStatusT extends number, ResHeadersT extends AnyHeaders, ResBodyT extends unknown, ErrResStatusT extends number, ErrResHeadersT extends AnyHeaders, ErrResBodyT extends unknown>(app: Express, api: HttpApi<ReqHeadersT, ReqParamsT, ReqQueryT, ReqBodyT, ResStatusT, ResHeadersT, ResBodyT, ErrResStatusT, ErrResHeadersT, ErrResBodyT>, { requestValidationMode, responseValidationMode, middlewares }: HttpApiHandlerOptions, handler: HttpApiHandler<ReqHeadersT, ReqParamsT, ReqQueryT, ReqBodyT, ResStatusT, ResHeadersT, ResBodyT, ErrResStatusT, ErrResHeadersT, ErrResBodyT, Record<string, never>>) => void; | ||
//# sourceMappingURL=register-http-api-handler.d.ts.map |
@@ -11,3 +11,2 @@ "use strict"; | ||
}; | ||
var _a; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
@@ -23,2 +22,3 @@ exports.registerHttpApiHandler = void 0; | ||
const get_url_pathname_1 = require("../internal-utils/get-url-pathname"); | ||
const register_api_handler_1 = require("../register-api-handler/register-api-handler"); | ||
const anyStringSerializableTypeSchema = yaschema_1.schema.oneOf3(yaschema_1.schema.number().setAllowedSerializationForms(['number', 'string']), yaschema_1.schema.boolean().setAllowedSerializationForms(['boolean', 'string']), yaschema_1.schema.string()); | ||
@@ -34,2 +34,3 @@ const anyReqHeadersSchema = yaschema_1.schema.record(yaschema_1.schema.string(), anyStringSerializableTypeSchema).optional(); | ||
const anyResBodySchema = yaschema_1.schema.any().allowNull().optional(); | ||
/** Be sure to call `finalizeApiHandlerRegistrations` once all API registrations have been added. */ | ||
const registerHttpApiHandler = (app, api, { requestValidationMode = (0, validation_mode_1.getDefaultRequestValidationMode)(), responseValidationMode = (0, validation_mode_1.getDefaultResponseValidationMode)(), middlewares = [] }, handler) => { | ||
@@ -149,3 +150,3 @@ const expressHandler = (req, res, next) => __awaiter(void 0, void 0, void 0, function* () { | ||
// longer exact matches first) | ||
registerHandlerSoon(methodName, relativizedUrl, () => { | ||
(0, register_api_handler_1.registerApiHandler)('http', methodName, relativizedUrl, () => { | ||
app[methodName](convertYaschemaParamSyntaxForExpress(relativizedUrl), ...handlers); | ||
@@ -167,35 +168,2 @@ }); | ||
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.1.2", | ||
"version": "1.2.0", | ||
"description": "Express handler support for yaschema-api", | ||
@@ -20,4 +20,4 @@ "keywords": [ | ||
"build:dev": "trash dev-build && yarn build && mkdir dev-build && cp -r lib dev-build && cp package.json dev-build && (cd dev-build && echo 'package' `pwd`)", | ||
"clean": "trash coverage dev-build docs lib", | ||
"generate:docs": "npx typedoc src/index.ts", | ||
"clean": "trash coverage dev-build ../docs lib", | ||
"generate:docs": "npx typedoc src/index.ts --out ../docs", | ||
"lint": "eslint 'src/**/*.ts?(x)' --max-warnings 0", | ||
@@ -37,7 +37,10 @@ "prepublishOnly": "yarn clean && yarn test && yarn lint && yarn test:audit && yarn clean && cp ./README.md ../ && yarn build && yarn generate:docs", | ||
"devDependencies": { | ||
"@types/body-parser": "^1.19.2", | ||
"@types/express": "^4.17.17", | ||
"@types/jest": "^29.4.0", | ||
"@types/lodash": "4.14.191", | ||
"@types/node-fetch": "^2.6.2", | ||
"@typescript-eslint/eslint-plugin": "^5.50.0", | ||
"@typescript-eslint/parser": "^5.50.0", | ||
"body-parser": "^1.20.2", | ||
"eslint": "8.33.0", | ||
@@ -52,5 +55,7 @@ "eslint-config-prettier": "^8.6.0", | ||
"express": "^4.18.2", | ||
"http-status-codes": "^2.2.0", | ||
"jest": "29.4.1", | ||
"jest-environment-jsdom": "^29.4.1", | ||
"madge": "6.0.0", | ||
"node-fetch": "^2.6.9", | ||
"prettier": "2.8.3", | ||
@@ -60,4 +65,5 @@ "trash-cli": "5.0.0", | ||
"typedoc": "^0.23.24", | ||
"typescript": "4.9.5" | ||
"typescript": "4.9.5", | ||
"yaschema-api-fetcher": "^1.1.0" | ||
} | ||
} |
@@ -36,3 +36,3 @@ # express-yaschema-api-handler | ||
registerHttpApiHandler(app, postPing, {}, async ({ express: _express, input, output }) => { | ||
output.success(200, { headers: {}, body: (input.body.echo?.length ?? 0) > 0 ? `PONG ${input.body.echo ?? ''}` : 'PONG' }); | ||
output.success(200, { body: (input.body.echo?.length ?? 0) > 0 ? `PONG ${input.body.echo ?? ''}` : 'PONG' }); | ||
}); | ||
@@ -39,0 +39,0 @@ ``` |
export * from './http-api-handler-wrapper'; | ||
export * from './logging'; | ||
export * from './on-request-validation-error'; | ||
export * from './on-response-validation-error'; | ||
export * from './validation-mode'; |
export * from './config/exports'; | ||
export * from './register-api-handler/exports'; | ||
export * from './register-http-api-handler/exports'; | ||
export * from './types/exports'; | ||
export type { Express, NextFunction, Request, RequestHandler, Response } from 'express'; |
@@ -23,2 +23,3 @@ import type { Express, NextFunction, Request, RequestHandler, Response } from 'express'; | ||
import { getUrlPathnameUsingRouteType } from '../internal-utils/get-url-pathname'; | ||
import { registerApiHandler } from '../register-api-handler/register-api-handler'; | ||
import type { HttpApiHandler } from '../types/HttpApiHandler'; | ||
@@ -44,2 +45,3 @@ import type { HttpApiHandlerOptions } from '../types/HttpApiHandlerOptions'; | ||
/** Be sure to call `finalizeApiHandlerRegistrations` once all API registrations have been added. */ | ||
export const registerHttpApiHandler = < | ||
@@ -217,3 +219,3 @@ ReqHeadersT extends AnyHeaders, | ||
// longer exact matches first) | ||
registerHandlerSoon(methodName, relativizedUrl, () => { | ||
registerApiHandler('http', methodName, relativizedUrl, () => { | ||
app[methodName](convertYaschemaParamSyntaxForExpress(relativizedUrl), ...handlers); | ||
@@ -243,44 +245,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
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
91548
90
1366
28