Skip to content

Commit

Permalink
feat(di): add DIContext.PLATFORM property to distinguish the current …
Browse files Browse the repository at this point in the history
…platform runtime
  • Loading branch information
Romakita committed Jun 19, 2024
1 parent 2f73902 commit 4e22e87
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 6 deletions.
5 changes: 4 additions & 1 deletion packages/di/src/node/domain/DIContext.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ describe("DIContext", () => {
expect(context.dateStart).toBeInstanceOf(Date);
expect(context.container).toBeInstanceOf(Map);
expect(context.env).toEqual("test");
expect(context.PLATFORM).toEqual("DI");

context.logger.info("test");

Expand All @@ -47,7 +48,8 @@ describe("DIContext", () => {
},
logger,
injector: DITest.injector,
maxStackSize: 0
maxStackSize: 0,
platform: "OTHER"
});

context.next = jest.fn();
Expand All @@ -56,6 +58,7 @@ describe("DIContext", () => {
expect(context.dateStart).toBeInstanceOf(Date);
expect(context.container).toBeInstanceOf(Map);
expect(context.env).toEqual("test");
expect(context.PLATFORM).toEqual("OTHER");

context.next();
context.logger.info("test");
Expand Down
7 changes: 5 additions & 2 deletions packages/di/src/node/domain/DIContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ export interface DIContextOptions extends Omit<ContextLoggerOptions, "dateStart"
id: string;
injector: InjectorService;
logger: any;
platform?: string;
}

export class DIContext {
[x: string]: any;

readonly PLATFORM: string = "DI";
#container?: LocalsContainer;
#cache?: Map<any, any>;
#logger?: ContextLogger;

constructor(public opts: DIContextOptions) {}
constructor(public opts: DIContextOptions) {
this.PLATFORM = opts.platform || this.PLATFORM;
}

/**
* Logger attached to the context request.
Expand Down
1 change: 1 addition & 0 deletions packages/platform/common/src/domain/PlatformContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class PlatformContext<
PReq extends PlatformRequest = PlatformRequest,
PRes extends PlatformResponse = PlatformResponse
> extends DIContext {
readonly PLATFORM: string = "WWW";
public event: IncomingEvent;
/**
* The data return by the previous endpoint if you use multiple handler on the same route. By default data is empty.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {ParamValidationError} from "../errors/ParamValidationError.js";
import {ParseExpressionPipe} from "../pipes/ParseExpressionPipe.js";

export type PlatformParamsScope<Context extends DIContext = DIContext> = {$ctx: Context} & Record<string, any>;
export type PlatformParamsCallback<Context extends DIContext = DIContext> = (scope: PlatformParamsScope) => Promise<any>;
export type PlatformParamsCallback<Context extends DIContext = DIContext> = (scope: PlatformParamsScope<Context>) => Promise<any>;

/**
* Platform Params abstraction layer.
Expand Down Expand Up @@ -43,14 +43,14 @@ export class PlatformParams {
handler?: any;
}): PlatformParamsCallback<Context> {
if (!token || !propertyKey) {
return (scope: PlatformParamsScope) => handler(scope.$ctx);
return (scope: PlatformParamsScope<Context>) => handler(scope.$ctx);
}

const store = JsonMethodStore.fromMethod(token, propertyKey);
const getArguments = this.compile<Context>(store);
const provider = this.injector.getProvider(token)!;

return async (scope: PlatformParamsScope) => {
return async (scope: PlatformParamsScope<Context>) => {
const container = provider.scope === ProviderScope.REQUEST ? scope.$ctx.container : undefined;
const [instance, args] = await Promise.all([this.injector.invoke<any>(token, container), getArguments(scope)]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class ServerlessContext extends DIContext {
readonly context: Context;
readonly event: APIGatewayProxyEventBase<APIGatewayEventDefaultAuthorizerContext>;
readonly endpoint: JsonEntityStore;
readonly PLATFORM = "SERVERLESS";

constructor({event, context, endpoint, ...options}: ServerlessContextOptions) {
super({
Expand Down

0 comments on commit 4e22e87

Please sign in to comment.