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

feat(rpc): make controller decorator name optional #491

Merged
merged 9 commits into from
Nov 9, 2023
11 changes: 8 additions & 3 deletions packages/rpc/src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ import { ClassDecoratorResult, createClassDecoratorContext, createPropertyDecora
import { ControllerDefinition } from './model.js';

class RpcController {
name?: string;
// Defaults to the name of the class
name!: string;
marcus-sa marked this conversation as resolved.
Show resolved Hide resolved

definition?: ControllerDefinition<any>;

actions = new Map<string, RpcAction>();

getPath(): string {
return this.definition ? this.definition.path : this.name || '';
return this.definition ? this.definition.path : this.name;
}
}

Expand All @@ -38,7 +39,7 @@ export class RpcAction {
class RpcClass {
t = new RpcController;

controller(nameOrDefinition: string | ControllerDefinition<any>) {
controller(nameOrDefinition?: string | ControllerDefinition<any>) {
if ('string' === typeof nameOrDefinition) {
this.t.name = nameOrDefinition;
} else {
Expand All @@ -49,6 +50,10 @@ class RpcClass {
addAction(name: string, action: RpcAction) {
this.t.actions.set(name, action);
}

onDecorator(classType: ClassType) {
this.t.name ??= classType.name;
}
}

export const rpcClass: ClassDecoratorResult<typeof RpcClass> = createClassDecoratorContext(RpcClass);
Expand Down
11 changes: 10 additions & 1 deletion packages/rpc/tests/controller.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import { assertType, entity, Positive, ReflectionClass, ReflectionKind } from '@deepkit/type';
import { expect, test } from '@jest/globals';
import { DirectClient } from '../src/client/client-direct.js';
import { getActions, rpc } from '../src/decorators.js';
import { getActions, rpc, rpcClass } from '../src/decorators.js';
import { RpcKernel, RpcKernelConnection } from '../src/server/kernel.js';
import { Session, SessionState } from '../src/server/security.js';
import { BehaviorSubject } from 'rxjs';
import { getClassName, sleep } from '@deepkit/core';
import { ProgressTracker } from '@deepkit/core-rxjs';

test('default name', () => {
@rpc.controller()
class Controller {}

expect(rpcClass._fetch(Controller)).toMatchObject({
name: 'Controller',
});
});

test('decorator', async () => {
@rpc.controller('name')
class Controller {
Expand Down