Skip to content

Commit

Permalink
Merge 589c79e into dd08c77
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-sa authored Oct 21, 2023
2 parents dd08c77 + 589c79e commit feb9889
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
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;

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

0 comments on commit feb9889

Please sign in to comment.