@davinci/http-server
Advanced tools
Comparing version 3.2.2 to 3.3.0
@@ -1,1 +0,1 @@ | ||
{"processes":{"829d783c-476f-417f-998f-6fafe2531b82":{"parent":null,"children":[]},"94df5ecb-bc71-4102-abb9-f88f5fd658c1":{"parent":"b7813162-e8d8-4a3b-8303-d99518871e08","children":[]},"b7813162-e8d8-4a3b-8303-d99518871e08":{"parent":null,"children":["94df5ecb-bc71-4102-abb9-f88f5fd658c1"]}},"files":{"/home/circleci/project/packages/http-server/src/AjvValidator.ts":["829d783c-476f-417f-998f-6fafe2531b82","94df5ecb-bc71-4102-abb9-f88f5fd658c1"],"/home/circleci/project/packages/http-server/src/decorators/index.ts":["829d783c-476f-417f-998f-6fafe2531b82","94df5ecb-bc71-4102-abb9-f88f5fd658c1"],"/home/circleci/project/packages/http-server/src/decorators/route.ts":["829d783c-476f-417f-998f-6fafe2531b82","94df5ecb-bc71-4102-abb9-f88f5fd658c1"],"/home/circleci/project/packages/http-server/src/decorators/types.ts":["829d783c-476f-417f-998f-6fafe2531b82","94df5ecb-bc71-4102-abb9-f88f5fd658c1"],"/home/circleci/project/packages/http-server/src/httpErrors.ts":["829d783c-476f-417f-998f-6fafe2531b82","94df5ecb-bc71-4102-abb9-f88f5fd658c1"],"/home/circleci/project/packages/http-server/src/HttpServerModule.ts":["829d783c-476f-417f-998f-6fafe2531b82","94df5ecb-bc71-4102-abb9-f88f5fd658c1"],"/home/circleci/project/packages/http-server/src/index.ts":["829d783c-476f-417f-998f-6fafe2531b82","94df5ecb-bc71-4102-abb9-f88f5fd658c1"],"/home/circleci/project/packages/http-server/src/types.ts":["829d783c-476f-417f-998f-6fafe2531b82","94df5ecb-bc71-4102-abb9-f88f5fd658c1"]},"externalIds":{}} | ||
{"processes":{"342178b9-3be2-4854-ac2a-cbb18447c30f":{"parent":null,"children":[]},"345aa86d-c9ad-40f6-861d-f896aa5cb117":{"parent":null,"children":["9844deac-dbc7-44b2-914e-10c37503e8f2"]},"9844deac-dbc7-44b2-914e-10c37503e8f2":{"parent":"345aa86d-c9ad-40f6-861d-f896aa5cb117","children":[]}},"files":{"/home/circleci/project/packages/http-server/src/AjvValidator.ts":["342178b9-3be2-4854-ac2a-cbb18447c30f","9844deac-dbc7-44b2-914e-10c37503e8f2"],"/home/circleci/project/packages/http-server/src/decorators/index.ts":["342178b9-3be2-4854-ac2a-cbb18447c30f","9844deac-dbc7-44b2-914e-10c37503e8f2"],"/home/circleci/project/packages/http-server/src/decorators/route.ts":["342178b9-3be2-4854-ac2a-cbb18447c30f","9844deac-dbc7-44b2-914e-10c37503e8f2"],"/home/circleci/project/packages/http-server/src/decorators/types.ts":["342178b9-3be2-4854-ac2a-cbb18447c30f","9844deac-dbc7-44b2-914e-10c37503e8f2"],"/home/circleci/project/packages/http-server/src/httpErrors.ts":["342178b9-3be2-4854-ac2a-cbb18447c30f","9844deac-dbc7-44b2-914e-10c37503e8f2"],"/home/circleci/project/packages/http-server/src/HttpServerModule.ts":["342178b9-3be2-4854-ac2a-cbb18447c30f","9844deac-dbc7-44b2-914e-10c37503e8f2"],"/home/circleci/project/packages/http-server/src/index.ts":["342178b9-3be2-4854-ac2a-cbb18447c30f","9844deac-dbc7-44b2-914e-10c37503e8f2"],"/home/circleci/project/packages/http-server/src/types.ts":["342178b9-3be2-4854-ac2a-cbb18447c30f","9844deac-dbc7-44b2-914e-10c37503e8f2"]},"externalIds":{}} |
@@ -1,3 +0,3 @@ | ||
import { App, EntityRegistry, Interceptor, Module } from '@davinci/core'; | ||
import { ClassReflection, ClassType, MethodReflection } from '@davinci/reflector'; | ||
import { App, EntityRegistry, Interceptor, InterceptorDecoratorMeta, Module } from '@davinci/core'; | ||
import { ClassReflection, ClassType, DecoratorId, MethodReflection } from '@davinci/reflector'; | ||
import { ContextFactory, HttpServerInterceptor, HttpServerModuleOptions, ParameterConfiguration, ParameterSource, RequestHandler, Route, StaticServeOptions, ValidationFactory } from './types'; | ||
@@ -15,3 +15,3 @@ interface HttpServerModuleGenerics<ModuleOptions> { | ||
contextFactory?: ContextFactory<unknown>; | ||
globalInterceptors: Array<HttpServerInterceptor>; | ||
globalInterceptors: Array<Omit<InterceptorDecoratorMeta<HttpServerInterceptor>, typeof DecoratorId>>; | ||
entityRegistry: EntityRegistry; | ||
@@ -28,3 +28,3 @@ routes: Route<SMG['Request']>[]; | ||
setHttpServer(httpServer: SMG['Server']): void; | ||
setGlobalInterceptors(interceptors: Array<HttpServerInterceptor>): void; | ||
setGlobalInterceptors(interceptors: HttpServerModuleOptions['globalInterceptors']): void; | ||
setContextFactory<Context>(contextFactory: ContextFactory<Context, SMG['Request']>): this; | ||
@@ -31,0 +31,0 @@ createRoutes(): Promise<Route<SMG['Request']>[]>; |
@@ -25,3 +25,3 @@ "use strict"; | ||
this.validationFactory = (_a = moduleOptions === null || moduleOptions === void 0 ? void 0 : moduleOptions.validationFactory) !== null && _a !== void 0 ? _a : (0, AjvValidator_1.createAjvValidator)(); | ||
this.globalInterceptors = (_b = moduleOptions === null || moduleOptions === void 0 ? void 0 : moduleOptions.globalInterceptors) !== null && _b !== void 0 ? _b : []; | ||
this.setGlobalInterceptors((_b = moduleOptions === null || moduleOptions === void 0 ? void 0 : moduleOptions.globalInterceptors) !== null && _b !== void 0 ? _b : []); | ||
} | ||
@@ -41,3 +41,7 @@ getModuleId() { | ||
setGlobalInterceptors(interceptors) { | ||
this.globalInterceptors = interceptors; | ||
this.globalInterceptors = interceptors.map(i => { | ||
return typeof i === 'function' | ||
? { handler: i, meta: { stage: 'postValidation' } } | ||
: { handler: i.handler, meta: { stage: i.stage } }; | ||
}); | ||
} | ||
@@ -106,5 +110,10 @@ setContextFactory(contextFactory) { | ||
...this.globalInterceptors, | ||
...(0, core_1.getInterceptorsHandlers)(controllerReflection), | ||
...(0, core_1.getInterceptorsHandlers)(methodReflection) | ||
]; | ||
...(0, core_1.getInterceptorsDecorators)(controllerReflection), | ||
...(0, core_1.getInterceptorsDecorators)(methodReflection) | ||
].reduce((acc, interceptor) => { | ||
var _a, _b; | ||
const stage = (_b = (_a = interceptor.meta) === null || _a === void 0 ? void 0 : _a.stage) !== null && _b !== void 0 ? _b : 'postValidation'; | ||
acc[stage].push(interceptor.handler); | ||
return acc; | ||
}, { preValidation: [], postValidation: [] }); | ||
const validatorFunction = await ((_a = this.validationFactory) === null || _a === void 0 ? void 0 : _a.call(this, route)); | ||
@@ -182,4 +191,5 @@ // using a named function here for better instrumentation and reporting | ||
const result = await (0, core_1.executeInterceptorsStack)([ | ||
...interceptors.preValidation, | ||
validationInterceptor, | ||
...interceptors, | ||
...interceptors.postValidation, | ||
(_next, context) => controller[methodName](...context.handlerArgs) | ||
@@ -186,0 +196,0 @@ ], interceptorsBag); |
import { ClassReflection, MethodReflection, TypeValue } from '@davinci/reflector'; | ||
import { Interceptor, InterceptorBagDetails, JSONSchema } from '@davinci/core'; | ||
import { Interceptor, InterceptorBagDetails, InterceptorDecoratorMeta, JSONSchema } from '@davinci/core'; | ||
import { Level } from 'pino'; | ||
@@ -55,2 +55,3 @@ import { ControllerDecoratorMetadata, MethodDecoratorMetadata, ParameterDecoratorOptions, Verb } from './decorators'; | ||
}; | ||
export type HttpServerInterceptorStage = 'preValidation' | 'postValidation'; | ||
export type HttpServerInterceptor<Bag extends HttpInterceptorBag = HttpInterceptorBag> = Interceptor<Bag, { | ||
@@ -60,2 +61,5 @@ request?: Bag['Request']; | ||
route?: Route<Bag['Request']>; | ||
meta?: { | ||
stage?: HttpServerInterceptorStage; | ||
}; | ||
}>; | ||
@@ -70,3 +74,6 @@ export interface HttpServerModuleOptions { | ||
querystringJsonParsing?: boolean; | ||
globalInterceptors?: Array<HttpServerInterceptor>; | ||
globalInterceptors?: Array<HttpServerInterceptor | { | ||
handler: InterceptorDecoratorMeta['handler']; | ||
stage: HttpServerInterceptorStage; | ||
}>; | ||
logger?: { | ||
@@ -73,0 +80,0 @@ name?: string; |
@@ -1,3 +0,3 @@ | ||
import { App, EntityRegistry, Interceptor, Module } from '@davinci/core'; | ||
import { ClassReflection, ClassType, MethodReflection } from '@davinci/reflector'; | ||
import { App, EntityRegistry, Interceptor, InterceptorDecoratorMeta, Module } from '@davinci/core'; | ||
import { ClassReflection, ClassType, DecoratorId, MethodReflection } from '@davinci/reflector'; | ||
import { ContextFactory, HttpServerInterceptor, HttpServerModuleOptions, ParameterConfiguration, ParameterSource, RequestHandler, Route, StaticServeOptions, ValidationFactory } from './types'; | ||
@@ -15,3 +15,3 @@ interface HttpServerModuleGenerics<ModuleOptions> { | ||
contextFactory?: ContextFactory<unknown>; | ||
globalInterceptors: Array<HttpServerInterceptor>; | ||
globalInterceptors: Array<Omit<InterceptorDecoratorMeta<HttpServerInterceptor>, typeof DecoratorId>>; | ||
entityRegistry: EntityRegistry; | ||
@@ -28,3 +28,3 @@ routes: Route<SMG['Request']>[]; | ||
setHttpServer(httpServer: SMG['Server']): void; | ||
setGlobalInterceptors(interceptors: Array<HttpServerInterceptor>): void; | ||
setGlobalInterceptors(interceptors: HttpServerModuleOptions['globalInterceptors']): void; | ||
setContextFactory<Context>(contextFactory: ContextFactory<Context, SMG['Request']>): this; | ||
@@ -31,0 +31,0 @@ createRoutes(): Promise<Route<SMG['Request']>[]>; |
@@ -5,3 +5,3 @@ /* | ||
*/ | ||
import { di, EntityRegistry, executeInterceptorsStack, getInterceptorsHandlers, mapParallel, mapSeries, Module } from '@davinci/core'; | ||
import { di, EntityRegistry, executeInterceptorsStack, getInterceptorsDecorators, mapParallel, mapSeries, Module } from '@davinci/core'; | ||
import pathUtils from 'path'; | ||
@@ -22,3 +22,3 @@ import pino from 'pino'; | ||
this.validationFactory = (_a = moduleOptions === null || moduleOptions === void 0 ? void 0 : moduleOptions.validationFactory) !== null && _a !== void 0 ? _a : createAjvValidator(); | ||
this.globalInterceptors = (_b = moduleOptions === null || moduleOptions === void 0 ? void 0 : moduleOptions.globalInterceptors) !== null && _b !== void 0 ? _b : []; | ||
this.setGlobalInterceptors((_b = moduleOptions === null || moduleOptions === void 0 ? void 0 : moduleOptions.globalInterceptors) !== null && _b !== void 0 ? _b : []); | ||
} | ||
@@ -38,3 +38,7 @@ getModuleId() { | ||
setGlobalInterceptors(interceptors) { | ||
this.globalInterceptors = interceptors; | ||
this.globalInterceptors = interceptors.map(i => { | ||
return typeof i === 'function' | ||
? { handler: i, meta: { stage: 'postValidation' } } | ||
: { handler: i.handler, meta: { stage: i.stage } }; | ||
}); | ||
} | ||
@@ -103,5 +107,10 @@ setContextFactory(contextFactory) { | ||
...this.globalInterceptors, | ||
...getInterceptorsHandlers(controllerReflection), | ||
...getInterceptorsHandlers(methodReflection) | ||
]; | ||
...getInterceptorsDecorators(controllerReflection), | ||
...getInterceptorsDecorators(methodReflection) | ||
].reduce((acc, interceptor) => { | ||
var _a, _b; | ||
const stage = (_b = (_a = interceptor.meta) === null || _a === void 0 ? void 0 : _a.stage) !== null && _b !== void 0 ? _b : 'postValidation'; | ||
acc[stage].push(interceptor.handler); | ||
return acc; | ||
}, { preValidation: [], postValidation: [] }); | ||
const validatorFunction = await ((_a = this.validationFactory) === null || _a === void 0 ? void 0 : _a.call(this, route)); | ||
@@ -179,4 +188,5 @@ // using a named function here for better instrumentation and reporting | ||
const result = await executeInterceptorsStack([ | ||
...interceptors.preValidation, | ||
validationInterceptor, | ||
...interceptors, | ||
...interceptors.postValidation, | ||
(_next, context) => controller[methodName](...context.handlerArgs) | ||
@@ -183,0 +193,0 @@ ], interceptorsBag); |
import { ClassReflection, MethodReflection, TypeValue } from '@davinci/reflector'; | ||
import { Interceptor, InterceptorBagDetails, JSONSchema } from '@davinci/core'; | ||
import { Interceptor, InterceptorBagDetails, InterceptorDecoratorMeta, JSONSchema } from '@davinci/core'; | ||
import { Level } from 'pino'; | ||
@@ -55,2 +55,3 @@ import { ControllerDecoratorMetadata, MethodDecoratorMetadata, ParameterDecoratorOptions, Verb } from './decorators'; | ||
}; | ||
export type HttpServerInterceptorStage = 'preValidation' | 'postValidation'; | ||
export type HttpServerInterceptor<Bag extends HttpInterceptorBag = HttpInterceptorBag> = Interceptor<Bag, { | ||
@@ -60,2 +61,5 @@ request?: Bag['Request']; | ||
route?: Route<Bag['Request']>; | ||
meta?: { | ||
stage?: HttpServerInterceptorStage; | ||
}; | ||
}>; | ||
@@ -70,3 +74,6 @@ export interface HttpServerModuleOptions { | ||
querystringJsonParsing?: boolean; | ||
globalInterceptors?: Array<HttpServerInterceptor>; | ||
globalInterceptors?: Array<HttpServerInterceptor | { | ||
handler: InterceptorDecoratorMeta['handler']; | ||
stage: HttpServerInterceptorStage; | ||
}>; | ||
logger?: { | ||
@@ -73,0 +80,0 @@ name?: string; |
@@ -6,2 +6,13 @@ # Change Log | ||
# [3.3.0](https://github.com/HPInc/davinci/compare/@davinci/http-server@3.2.2...@davinci/http-server@3.3.0) (2022-12-21) | ||
### Features | ||
* **httpInterceptor:** added ability to specify stage: 'preValidation' | 'postValidation' ([#195](https://github.com/HPInc/davinci/issues/195)) ([d61b571](https://github.com/HPInc/davinci/commit/d61b571f7b4dd46397f1b5ecf7e838378cb9a517)) | ||
## [3.2.2](https://github.com/HPInc/davinci/compare/@davinci/http-server@3.2.1...@davinci/http-server@3.2.2) (2022-12-21) | ||
@@ -8,0 +19,0 @@ |
{ | ||
"name": "@davinci/http-server", | ||
"version": "3.2.2", | ||
"version": "3.3.0", | ||
"description": "Framework for rapid and consistent API development", | ||
@@ -23,3 +23,3 @@ "main": "build-cjs/index.js", | ||
"dependencies": { | ||
"@davinci/core": "^2.3.0", | ||
"@davinci/core": "^2.4.0", | ||
"@davinci/reflector": "^2.1.1" | ||
@@ -38,3 +38,3 @@ }, | ||
}, | ||
"gitHead": "b2370db6c9e5be315292a9662070bb1c0acf0933" | ||
"gitHead": "ce58c72379e078fb047bfd7eba6f214fc3220de6" | ||
} |
@@ -10,4 +10,5 @@ /* | ||
executeInterceptorsStack, | ||
getInterceptorsHandlers, | ||
getInterceptorsDecorators, | ||
Interceptor, | ||
InterceptorDecoratorMeta, | ||
InterceptorNext, | ||
@@ -50,3 +51,3 @@ mapParallel, | ||
contextFactory?: ContextFactory<unknown>; | ||
globalInterceptors: Array<HttpServerInterceptor> = []; | ||
globalInterceptors: Array<Omit<InterceptorDecoratorMeta<HttpServerInterceptor>, typeof DecoratorId>> = []; | ||
entityRegistry = di.container.resolve(EntityRegistry); | ||
@@ -61,3 +62,3 @@ routes: Route<SMG['Request']>[] = []; | ||
this.validationFactory = moduleOptions?.validationFactory ?? createAjvValidator(); | ||
this.globalInterceptors = moduleOptions?.globalInterceptors ?? []; | ||
this.setGlobalInterceptors(moduleOptions?.globalInterceptors ?? []); | ||
} | ||
@@ -81,4 +82,8 @@ | ||
public setGlobalInterceptors(interceptors: Array<HttpServerInterceptor>) { | ||
this.globalInterceptors = interceptors; | ||
public setGlobalInterceptors(interceptors: HttpServerModuleOptions['globalInterceptors']) { | ||
this.globalInterceptors = interceptors.map(i => { | ||
return typeof i === 'function' | ||
? { handler: i, meta: { stage: 'postValidation' } } | ||
: { handler: i.handler, meta: { stage: i.stage } }; | ||
}); | ||
} | ||
@@ -171,8 +176,17 @@ | ||
const httpServerModule = this; | ||
const interceptors = [ | ||
...this.globalInterceptors, | ||
...getInterceptorsHandlers(controllerReflection), | ||
...getInterceptorsHandlers(methodReflection) | ||
]; | ||
...getInterceptorsDecorators<HttpServerInterceptor>(controllerReflection), | ||
...getInterceptorsDecorators<HttpServerInterceptor>(methodReflection) | ||
].reduce( | ||
(acc, interceptor) => { | ||
const stage = interceptor.meta?.stage ?? 'postValidation'; | ||
acc[stage].push(interceptor.handler); | ||
return acc; | ||
}, | ||
{ preValidation: [], postValidation: [] } | ||
); | ||
const validatorFunction: ValidationFunction | null = await this.validationFactory?.(route); | ||
@@ -265,4 +279,5 @@ | ||
[ | ||
...interceptors.preValidation, | ||
validationInterceptor, | ||
...interceptors, | ||
...interceptors.postValidation, | ||
(_next, context) => controller[methodName](...context.handlerArgs) | ||
@@ -269,0 +284,0 @@ ], |
@@ -7,3 +7,3 @@ /* | ||
import { ClassReflection, MethodReflection, TypeValue } from '@davinci/reflector'; | ||
import { Interceptor, InterceptorBagDetails, JSONSchema } from '@davinci/core'; | ||
import { Interceptor, InterceptorBagDetails, InterceptorDecoratorMeta, JSONSchema } from '@davinci/core'; | ||
import { Level } from 'pino'; | ||
@@ -74,5 +74,14 @@ import { ControllerDecoratorMetadata, MethodDecoratorMetadata, ParameterDecoratorOptions, Verb } from './decorators'; | ||
export type HttpServerInterceptorStage = 'preValidation' | 'postValidation'; | ||
export type HttpServerInterceptor<Bag extends HttpInterceptorBag = HttpInterceptorBag> = Interceptor< | ||
Bag, | ||
{ request?: Bag['Request']; response?: Bag['Response']; route?: Route<Bag['Request']> } | ||
{ | ||
request?: Bag['Request']; | ||
response?: Bag['Response']; | ||
route?: Route<Bag['Request']>; | ||
meta?: { | ||
stage?: HttpServerInterceptorStage; | ||
}; | ||
} | ||
>; | ||
@@ -88,3 +97,5 @@ | ||
querystringJsonParsing?: boolean; | ||
globalInterceptors?: Array<HttpServerInterceptor>; | ||
globalInterceptors?: Array< | ||
HttpServerInterceptor | { handler: InterceptorDecoratorMeta['handler']; stage: HttpServerInterceptorStage } | ||
>; | ||
logger?: { | ||
@@ -91,0 +102,0 @@ name?: string; |
@@ -19,3 +19,9 @@ /* | ||
import { reflect } from '@davinci/reflector'; | ||
import { HttpServerModule, HttpServerModuleOptions, ParameterConfiguration, route } from '../../src'; | ||
import { | ||
HttpServerInterceptor, | ||
HttpServerModule, | ||
HttpServerModuleOptions, | ||
ParameterConfiguration, | ||
route | ||
} from '../../src'; | ||
import { expect } from '../support/chai'; | ||
@@ -349,2 +355,119 @@ | ||
it('should create a request handler that process preValidation interceptors', async () => { | ||
const ctrlInterceptorPre = sinon.stub().callsFake(next => next()); | ||
const ctrlInterceptorPost = sinon.stub().callsFake(next => next()); | ||
const methodInterceptorPre = sinon.stub().callsFake(next => next()); | ||
const methodInterceptorPost = sinon.stub().callsFake(next => next()); | ||
@interceptor<HttpServerInterceptor>(ctrlInterceptorPre, { stage: 'preValidation' }) | ||
@interceptor<HttpServerInterceptor>(ctrlInterceptorPost, { stage: 'postValidation' }) | ||
@route.controller({ basePath: '/api/customers' }) | ||
class CustomerController { | ||
@interceptor<HttpServerInterceptor>(methodInterceptorPre, { stage: 'preValidation' }) | ||
@interceptor<HttpServerInterceptor>(methodInterceptorPost, { stage: 'postValidation' }) | ||
@route.post({ path: '/all' }) | ||
find(@route.body({ required: true }) body) { | ||
return { body }; | ||
} | ||
} | ||
const dummyHttpServer = new DummyHttpServer(); | ||
const controllerReflection = reflect(CustomerController); | ||
const methodReflection = controllerReflection.methods[0]; | ||
const requestHandler = await dummyHttpServer.createRequestHandler(new CustomerController(), 'find', { | ||
path: '/all', | ||
verb: 'get', | ||
controllerReflection, | ||
methodReflection, | ||
parametersConfig: dummyHttpServer.createParametersConfigurations({ | ||
controllerReflection, | ||
methodReflection | ||
}) | ||
}); | ||
const reqMock = {}; | ||
const resMock = {}; | ||
const result = await requestHandler(reqMock, resMock); | ||
expect(result[1]).to.containSubset({ | ||
statusCode: 400, | ||
name: 'BadRequest', | ||
errors: [ | ||
{ | ||
instancePath: '/body', | ||
schemaPath: '#/type', | ||
keyword: 'type', | ||
params: { | ||
type: 'object' | ||
}, | ||
message: 'must be object' | ||
} | ||
] | ||
}); | ||
expect(ctrlInterceptorPre.called).to.be.true; | ||
expect(ctrlInterceptorPost.called).to.be.false; | ||
expect(methodInterceptorPre.called).to.be.true; | ||
expect(methodInterceptorPost.called).to.be.false; | ||
}); | ||
it('should create a request handler tha process interceptors in the correct order', async () => { | ||
const callsOrder = []; | ||
const createHandler = name => next => { | ||
callsOrder.push(name); | ||
return next(); | ||
}; | ||
const globalInterceptorPre = sinon.stub().callsFake(createHandler('globalInterceptorPre')); | ||
const globalInterceptorPost = sinon.stub().callsFake(createHandler('globalInterceptorPost')); | ||
const ctrlInterceptorPre = sinon.stub().callsFake(createHandler('ctrlInterceptorPre')); | ||
const ctrlInterceptorPost = sinon.stub().callsFake(createHandler('ctrlInterceptorPost')); | ||
const methodInterceptorPre = sinon.stub().callsFake(createHandler('methodInterceptorPre')); | ||
const methodInterceptorPost = sinon.stub().callsFake(createHandler('methodInterceptorPost')); | ||
sinon | ||
.stub(DummyHttpServer.prototype, 'createValidationInterceptor') | ||
.callsFake(() => createHandler('validationInterceptor')); | ||
@interceptor<HttpServerInterceptor>(ctrlInterceptorPre, { stage: 'preValidation' }) | ||
@interceptor<HttpServerInterceptor>(ctrlInterceptorPost, { stage: 'postValidation' }) | ||
@route.controller({ basePath: '/api/customers' }) | ||
class CustomerController { | ||
@interceptor<HttpServerInterceptor>(methodInterceptorPre, { stage: 'preValidation' }) | ||
@interceptor<HttpServerInterceptor>(methodInterceptorPost, { stage: 'postValidation' }) | ||
@route.post({ path: '/' }) | ||
find(@route.body() body) { | ||
return { body }; | ||
} | ||
} | ||
const dummyHttpServer = new DummyHttpServer({ | ||
globalInterceptors: [ | ||
{ handler: globalInterceptorPre, stage: 'preValidation' }, | ||
{ handler: globalInterceptorPost, stage: 'postValidation' } | ||
] | ||
}); | ||
const controllerReflection = reflect(CustomerController); | ||
const methodReflection = controllerReflection.methods[0]; | ||
const requestHandler = await dummyHttpServer.createRequestHandler(new CustomerController(), 'find', { | ||
path: '/', | ||
verb: 'post', | ||
controllerReflection, | ||
methodReflection, | ||
parametersConfig: dummyHttpServer.createParametersConfigurations({ | ||
controllerReflection, | ||
methodReflection | ||
}) | ||
}); | ||
const reqMock = { body: {} }; | ||
const resMock = {}; | ||
await requestHandler(reqMock, resMock); | ||
expect(callsOrder).to.be.deep.equal([ | ||
'globalInterceptorPre', | ||
'ctrlInterceptorPre', | ||
'methodInterceptorPre', | ||
'validationInterceptor', | ||
'globalInterceptorPost', | ||
'ctrlInterceptorPost', | ||
'methodInterceptorPost' | ||
]); | ||
}); | ||
it('should be able to pass global interceptors', async () => { | ||
@@ -351,0 +474,0 @@ const interceptorsOrder = []; |
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
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
768575
6137
Updated@davinci/core@^2.4.0