Skip to content
New issue

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

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

Already on GitHub? # to your account

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

Merged
merged 9 commits into from
Nov 9, 2023
20 changes: 16 additions & 4 deletions packages/rpc/src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,26 @@
*/

import { ClassType } from '@deepkit/core';
import { ClassDecoratorResult, createClassDecoratorContext, createPropertyDecoratorContext, mergeDecorator, PropertyDecoratorResult } from '@deepkit/type';
import {
ClassDecoratorResult,
createClassDecoratorContext,
createPropertyDecoratorContext,
mergeDecorator,
PropertyDecoratorResult,
reflect
} from '@deepkit/type';
import { ControllerDefinition } from './model.js';

class RpcController {
name?: string;
// Defaults to the name of the class
name: string = '';

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 +46,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 +57,10 @@ class RpcClass {
addAction(name: string, action: RpcAction) {
this.t.actions.set(name, action);
}

onDecorator(classType: ClassType) {
this.t.name ??= reflect(classType).typeName || 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
Loading