Skip to content

Commit

Permalink
Merge pull request #13923 from DylanVeldra/fix-tenant-context-payload
Browse files Browse the repository at this point in the history
fix(core): merge req context with tenant payload in the request instance
  • Loading branch information
kamilmysliwiec authored Nov 25, 2024
2 parents 84b8744 + 81597f7 commit 9286733
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 11 deletions.
19 changes: 19 additions & 0 deletions integration/scopes/e2e/durable-providers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,25 @@ describe('Durable providers', () => {
expect(result.body).deep.equal({ tenantId: '3' });
});

it(`should return the same tenantId both from durable request scoped service and non-durable request scoped service`, async () => {
let result: request.Response;
result = await new Promise<request.Response>(resolve =>
performHttpCall(1, resolve, '/durable/request-context'),
);
expect(result.body).deep.equal({
durableService: '1',
nonDurableService: '1',
});

result = await new Promise<request.Response>(resolve =>
performHttpCall(2, resolve, '/durable/request-context'),
);
expect(result.body).deep.equal({
durableService: '2',
nonDurableService: '2',
});
});

it(`should not cache durable providers that throw errors`, async () => {
let result: request.Response;

Expand Down
10 changes: 6 additions & 4 deletions integration/scopes/src/durable/durable-context-id.strategy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { ContextId, ContextIdStrategy, HostComponentInfo } from '@nestjs/core';
import { Request } from 'express';

export type TenantContext = {
tenantId: string;
forceError?: boolean;
};

const tenants = new Map<string, ContextId>();

export class DurableContextIdStrategy implements ContextIdStrategy {
Expand All @@ -17,10 +22,7 @@ export class DurableContextIdStrategy implements ContextIdStrategy {
tenants.set(tenantId, tenantSubTreeId);
}

const payload: {
tenantId: string;
forceError?: boolean;
} = { tenantId };
const payload: TenantContext = { tenantId };
if (forceError) {
payload.forceError = true;
}
Expand Down
18 changes: 16 additions & 2 deletions integration/scopes/src/durable/durable.controller.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { Controller, Get } from '@nestjs/common';
import { DurableService } from './durable.service';
import { NonDurableService } from './non-durable.service';

@Controller('durable')
export class DurableController {
constructor(private readonly durableService: DurableService) {}
constructor(
private readonly durableService: DurableService,
private readonly nonDurableService: NonDurableService,
) {}

@Get()
greeting(): string {
Expand All @@ -12,6 +16,16 @@ export class DurableController {

@Get('echo')
echo() {
return this.durableService.requestPayload;
return {
tenantId: this.durableService.getTenantId(),
};
}

@Get('request-context')
getRequestContext() {
return {
durableService: this.durableService.getTenantId(),
nonDurableService: this.nonDurableService.getTenantId(),
};
}
}
2 changes: 2 additions & 0 deletions integration/scopes/src/durable/durable.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ import { APP_GUARD } from '@nestjs/core';
import { DurableController } from './durable.controller';
import { DurableGuard } from './durable.guard';
import { DurableService } from './durable.service';
import { NonDurableService } from './non-durable.service';

@Module({
controllers: [DurableController],
providers: [
DurableService,
NonDurableService,
{
provide: APP_GUARD,
useClass: DurableGuard,
Expand Down
8 changes: 6 additions & 2 deletions integration/scopes/src/durable/durable.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import {
Scope,
} from '@nestjs/common';
import { REQUEST } from '@nestjs/core';
import { TenantContext } from './durable-context-id.strategy';

@Injectable({ scope: Scope.REQUEST, durable: true })
export class DurableService {
public instanceCounter = 0;

constructor(
@Inject(REQUEST)
public readonly requestPayload: { tenantId: string; forceError: boolean },
@Inject(REQUEST) private readonly requestPayload: TenantContext,
) {
if (requestPayload.forceError) {
throw new PreconditionFailedException('Forced error');
Expand All @@ -23,4 +23,8 @@ export class DurableService {
++this.instanceCounter;
return `Hello world! Counter: ${this.instanceCounter}`;
}

getTenantId() {
return this.requestPayload.tenantId;
}
}
14 changes: 14 additions & 0 deletions integration/scopes/src/durable/non-durable.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Inject, Injectable, Scope } from '@nestjs/common';
import { REQUEST } from '@nestjs/core';
import { TenantContext } from './durable-context-id.strategy';

@Injectable()
export class NonDurableService {
constructor(
@Inject(REQUEST) private readonly requestPayload: TenantContext,
) {}

getTenantId() {
return this.requestPayload.tenantId;
}
}
4 changes: 3 additions & 1 deletion packages/core/middleware/middleware-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,9 @@ export class MiddlewareModule<
configurable: false,
});

const requestProviderValue = isTreeDurable ? contextId.payload : request;
const requestProviderValue = isTreeDurable
? contextId.payload
: Object.assign(request, contextId.payload);
this.container.registerRequestProvider(requestProviderValue, contextId);
}
return contextId;
Expand Down
4 changes: 3 additions & 1 deletion packages/core/router/router-explorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,9 @@ export class RouterExplorer {
configurable: false,
});

const requestProviderValue = isTreeDurable ? contextId.payload : request;
const requestProviderValue = isTreeDurable
? contextId.payload
: Object.assign(request, contextId.payload);
this.container.registerRequestProvider(requestProviderValue, contextId);
}
return contextId;
Expand Down
4 changes: 3 additions & 1 deletion packages/microservices/listeners-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,9 @@ export class ListenersController {
configurable: false,
});

const requestProviderValue = isTreeDurable ? contextId.payload : request;
const requestProviderValue = isTreeDurable
? contextId.payload
: Object.assign(request, contextId.payload);
this.container.registerRequestProvider(requestProviderValue, contextId);
}
return contextId;
Expand Down

0 comments on commit 9286733

Please sign in to comment.