mirror of
https://github.com/nestjs/nest.git
synced 2026-02-26 06:35:32 +00:00
refactor(microservices): rename prerequest hook method
This commit is contained in:
@@ -10,7 +10,7 @@ import { ExecutionContext } from '../features/execution-context.interface.js';
|
||||
* @example
|
||||
* ```typescript
|
||||
* const als = new AsyncLocalStorage();
|
||||
* app.useGlobalPreRequestHooks((context, next) => {
|
||||
* app.registerPreRequestHook((context, next) => {
|
||||
* als.enterWith({ correlationId: uuid() });
|
||||
* return next();
|
||||
* });
|
||||
|
||||
@@ -137,7 +137,7 @@ export class ApplicationConfig {
|
||||
return this.globalRequestGuards;
|
||||
}
|
||||
|
||||
public useGlobalPreRequestHooks(...hooks: PreRequestHook[]) {
|
||||
public registerPreRequestHook(...hooks: PreRequestHook[]) {
|
||||
this.globalPreRequestHooks = this.globalPreRequestHooks.concat(hooks);
|
||||
}
|
||||
|
||||
|
||||
@@ -108,15 +108,15 @@ describe('ApplicationConfig', () => {
|
||||
describe('PreRequestHooks', () => {
|
||||
it('should set global preRequest hooks', () => {
|
||||
const hooks = [() => {}, () => {}];
|
||||
appConfig.useGlobalPreRequestHooks(...(hooks as any));
|
||||
appConfig.registerPreRequestHook(...(hooks as any));
|
||||
|
||||
expect(appConfig.getGlobalPreRequestHooks()).toEqual(hooks);
|
||||
});
|
||||
it('should accumulate multiple useGlobalPreRequestHooks calls', () => {
|
||||
it('should accumulate multiple registerPreRequestHook calls', () => {
|
||||
const hook1 = () => {};
|
||||
const hook2 = () => {};
|
||||
appConfig.useGlobalPreRequestHooks(hook1 as any);
|
||||
appConfig.useGlobalPreRequestHooks(hook2 as any);
|
||||
appConfig.registerPreRequestHook(hook1 as any);
|
||||
appConfig.registerPreRequestHook(hook2 as any);
|
||||
|
||||
expect(appConfig.getGlobalPreRequestHooks()).toEqual([hook1, hook2]);
|
||||
});
|
||||
|
||||
@@ -253,13 +253,13 @@ export class NestMicroservice
|
||||
*
|
||||
* @param {...PreRequestHook} hooks
|
||||
*/
|
||||
public useGlobalPreRequestHooks(...hooks: PreRequestHook[]): this {
|
||||
public registerPreRequestHook(...hooks: PreRequestHook[]): this {
|
||||
if (this.isInitialized) {
|
||||
this.logger.warn(
|
||||
'Cannot apply global preRequest hooks: registration must occur before initialization.',
|
||||
);
|
||||
}
|
||||
this.applicationConfig.useGlobalPreRequestHooks(...hooks);
|
||||
this.applicationConfig.registerPreRequestHook(...hooks);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@@ -153,11 +153,11 @@ describe('NestMicroservice', () => {
|
||||
expect(onStub).toHaveBeenCalledWith('test:event', cb);
|
||||
});
|
||||
|
||||
describe('useGlobalPreRequestHooks', () => {
|
||||
it('should delegate to applicationConfig.useGlobalPreRequestHooks', () => {
|
||||
describe('registerPreRequestHook', () => {
|
||||
it('should delegate to applicationConfig.registerPreRequestHook', () => {
|
||||
const mockConfig = {
|
||||
...createMockAppConfig(),
|
||||
useGlobalPreRequestHooks: vi.fn(),
|
||||
registerPreRequestHook: vi.fn(),
|
||||
} as unknown as ApplicationConfig;
|
||||
|
||||
const instance = new NestMicroservice(
|
||||
@@ -168,15 +168,15 @@ describe('NestMicroservice', () => {
|
||||
);
|
||||
|
||||
const hook = (_ctx: any, next: any) => next();
|
||||
instance.useGlobalPreRequestHooks(hook);
|
||||
instance.registerPreRequestHook(hook);
|
||||
|
||||
expect(mockConfig.useGlobalPreRequestHooks).toHaveBeenCalledWith(hook);
|
||||
expect(mockConfig.registerPreRequestHook).toHaveBeenCalledWith(hook);
|
||||
});
|
||||
|
||||
it('should warn when called after initialization', () => {
|
||||
const mockConfig = {
|
||||
...createMockAppConfig(),
|
||||
useGlobalPreRequestHooks: vi.fn(),
|
||||
registerPreRequestHook: vi.fn(),
|
||||
} as unknown as ApplicationConfig;
|
||||
|
||||
const instance = new NestMicroservice(
|
||||
@@ -190,7 +190,7 @@ describe('NestMicroservice', () => {
|
||||
(instance as any).isInitialized = true;
|
||||
|
||||
const hook = (_ctx: any, next: any) => next();
|
||||
instance.useGlobalPreRequestHooks(hook);
|
||||
instance.registerPreRequestHook(hook);
|
||||
|
||||
expect(warnSpy).toHaveBeenCalled();
|
||||
});
|
||||
@@ -198,7 +198,7 @@ describe('NestMicroservice', () => {
|
||||
it('should return this for fluent API chaining', () => {
|
||||
const mockConfig = {
|
||||
...createMockAppConfig(),
|
||||
useGlobalPreRequestHooks: vi.fn(),
|
||||
registerPreRequestHook: vi.fn(),
|
||||
} as unknown as ApplicationConfig;
|
||||
|
||||
const instance = new NestMicroservice(
|
||||
@@ -209,7 +209,7 @@ describe('NestMicroservice', () => {
|
||||
);
|
||||
|
||||
const hook = (_ctx: any, next: any) => next();
|
||||
const result = instance.useGlobalPreRequestHooks(hook);
|
||||
const result = instance.registerPreRequestHook(hook);
|
||||
|
||||
expect(result).toBe(instance);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user