refactor(microservices): rename prerequest hook method

This commit is contained in:
suuuuuuminnnnnn
2026-02-25 19:16:34 +09:00
parent ead0d87b07
commit e240e2fadf
5 changed files with 17 additions and 17 deletions

View File

@@ -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();
* });

View File

@@ -137,7 +137,7 @@ export class ApplicationConfig {
return this.globalRequestGuards;
}
public useGlobalPreRequestHooks(...hooks: PreRequestHook[]) {
public registerPreRequestHook(...hooks: PreRequestHook[]) {
this.globalPreRequestHooks = this.globalPreRequestHooks.concat(hooks);
}

View File

@@ -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]);
});

View File

@@ -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;
}

View File

@@ -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);
});