Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module provider visibility issue failing test #595

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { eventDispatcher } from '@deepkit/event';
import { httpWorkflow } from '@deepkit/http';
import { AuthenticationModule } from './auth-module.js';
import { SessionForRequest } from './session-for-request.dto.js';

export class AuthenticationListener {
@eventDispatcher.listen(httpWorkflow.onAuth)
onAuth(event: typeof httpWorkflow.onAuth.event, module: AuthenticationModule) {
event.injectorContext.set(
SessionForRequest,
new SessionForRequest(
'sidValue',
'uidValue',
),
module
);
}
}
23 changes: 23 additions & 0 deletions packages/framework/tests/provider-visibility-issue/auth-module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { createModule } from '@deepkit/app';
import { SessionForRequest } from './session-for-request.dto.js';
import { AuthenticationListener } from './auth-listener.js';
import { http } from '@deepkit/http';


@http.controller('/auth')
class OAuth2Controller {
@http.GET('/whoami')
whoami(sess: SessionForRequest) {
// Trying to consume "SessionForRequest" within the same module it's provided – no luck
return sess;
}
}

export class AuthenticationModule extends createModule({
providers: [
{ provide: SessionForRequest, scope: 'http', useValue: undefined },
],
controllers: [OAuth2Controller],
listeners: [AuthenticationListener],
exports: [SessionForRequest],
}) { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import { expect, test } from '@jest/globals';
import { http, HttpKernel, HttpRequest } from '@deepkit/http';
import { App, createModule } from '@deepkit/app';
import { FrameworkModule } from '../../src/module.js';
import { SessionForRequest } from './session-for-request.dto.js';
import { AuthenticationModule } from './auth-module.js';

test('provider-visibility-issue', async () => {
class IAMModule extends createModule({ exports: [AuthenticationModule] }) {
imports = [new AuthenticationModule()];
}


@http.controller('/another')
class AnotherDomainModuleController {
@http.GET('/action')
action(sess: SessionForRequest) {
// Trying to consume "SessionForRequest" within another module – still no luck
return sess;
}
}
class AnotherDomainModule extends createModule({}) {
controllers = [AnotherDomainModuleController];
}

class DomainModule extends createModule({ exports: [IAMModule] }) {
imports = [new IAMModule(), new AnotherDomainModule()];
}

class InfrastructureModule extends createModule({
// The whole issue gets "fixed" if DomainModule gets exported here, but what if I don't need to export it?
exports: [],
}) {
imports = [new DomainModule()];
}

const app = new App({
imports: [
new FrameworkModule({ debug: true }),
new InfrastructureModule(),
],
providers: [],
});

/**
* These fail due to the following error:
*
* Controller for route /auth/whoami parameter resolving error:
* DependenciesUnmetError: Parameter sess is required but provider returned undefined.
*/
expect((await app.get(HttpKernel).request(HttpRequest.GET('/auth/whoami'))).json).toMatchObject({
sessionId: 'sidValue',
userId: 'uidValue'
});
expect((await app.get(HttpKernel).request(HttpRequest.GET('/another/action'))).json).toMatchObject({
sessionId: 'sidValue',
userId: 'uidValue'
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export class SessionForRequest {
constructor(
public readonly sessionId: string,
public readonly userId: string,
) {}
}
3 changes: 2 additions & 1 deletion packages/framework/tests/service-container.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import { rpc } from '@deepkit/rpc';
import { App, AppModule, createModule, ServiceContainer } from '@deepkit/app';
import { FrameworkModule } from '../src/module.js';
import { Database, DatabaseEvent, DatabaseRegistry, MemoryDatabaseAdapter, Query } from '@deepkit/orm';
import { EventDispatcher } from '@deepkit/event';
import { eventDispatcher, EventDispatcher } from '@deepkit/event';
import { PrimaryKey } from '@deepkit/type';
import { http, HttpKernel, HttpRequest, httpWorkflow } from '@deepkit/http';

test('controller', () => {
class MyService {
Expand Down
Loading