From aadde2d46185a7dbba4c4b2c47c693ff252be219 Mon Sep 17 00:00:00 2001 From: "Marcus S. Abildskov" <8391194+marcus-sa@users.noreply.github.com> Date: Sat, 21 Oct 2023 16:45:24 +0200 Subject: [PATCH 1/8] feat(rpc): make controller decorator name or definition optional The rpc controller metadata name would then default to the name of the class Signed-off-by: Marcus S. Abildskov <8391194+marcus-sa@users.noreply.github.com> --- packages/rpc/src/decorators.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/rpc/src/decorators.ts b/packages/rpc/src/decorators.ts index 030968642..9dd63673c 100644 --- a/packages/rpc/src/decorators.ts +++ b/packages/rpc/src/decorators.ts @@ -38,7 +38,7 @@ export class RpcAction { class RpcClass { t = new RpcController; - controller(nameOrDefinition: string | ControllerDefinition) { + controller(nameOrDefinition?: string | ControllerDefinition) { if ('string' === typeof nameOrDefinition) { this.t.name = nameOrDefinition; } else { @@ -49,6 +49,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 = createClassDecoratorContext(RpcClass); From 3eda0d40679b5230463d5ad6d23db3fe3e308639 Mon Sep 17 00:00:00 2001 From: "Marcus S. Abildskov" <8391194+marcus-sa@users.noreply.github.com> Date: Sat, 21 Oct 2023 16:50:27 +0200 Subject: [PATCH 2/8] Update controller.spec.ts Signed-off-by: Marcus S. Abildskov <8391194+marcus-sa@users.noreply.github.com> --- packages/rpc/tests/controller.spec.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/rpc/tests/controller.spec.ts b/packages/rpc/tests/controller.spec.ts index c9dfab90f..a861a98b3 100644 --- a/packages/rpc/tests/controller.spec.ts +++ b/packages/rpc/tests/controller.spec.ts @@ -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 controller metadata name', () => { + @rpc.controller() + class Controller {} + + expect(rpcClass._fetch(Controller)).toMatchObject({ + name: 'Controller', + }); +}); + test('decorator', async () => { @rpc.controller('name') class Controller { From 901781eb8fb6b173f0c105c5a81fb74a5005f931 Mon Sep 17 00:00:00 2001 From: "Marcus S. Abildskov" <8391194+marcus-sa@users.noreply.github.com> Date: Sat, 21 Oct 2023 16:51:02 +0200 Subject: [PATCH 3/8] Update controller.spec.ts Signed-off-by: Marcus S. Abildskov <8391194+marcus-sa@users.noreply.github.com> --- packages/rpc/tests/controller.spec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/rpc/tests/controller.spec.ts b/packages/rpc/tests/controller.spec.ts index a861a98b3..fea4260bc 100644 --- a/packages/rpc/tests/controller.spec.ts +++ b/packages/rpc/tests/controller.spec.ts @@ -8,7 +8,7 @@ import { BehaviorSubject } from 'rxjs'; import { getClassName, sleep } from '@deepkit/core'; import { ProgressTracker } from '@deepkit/core-rxjs'; -test('default controller metadata name', () => { +test('default name', () => { @rpc.controller() class Controller {} From 819dedd22ec786a142d4838ae24135e1671aeaa0 Mon Sep 17 00:00:00 2001 From: "Marcus S. Abildskov" <8391194+marcus-sa@users.noreply.github.com> Date: Sat, 21 Oct 2023 16:52:58 +0200 Subject: [PATCH 4/8] Update decorators.ts Signed-off-by: Marcus S. Abildskov <8391194+marcus-sa@users.noreply.github.com> --- packages/rpc/src/decorators.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/rpc/src/decorators.ts b/packages/rpc/src/decorators.ts index 9dd63673c..05de1a7b5 100644 --- a/packages/rpc/src/decorators.ts +++ b/packages/rpc/src/decorators.ts @@ -13,7 +13,8 @@ import { ClassDecoratorResult, createClassDecoratorContext, createPropertyDecora import { ControllerDefinition } from './model.js'; class RpcController { - name?: string; + // Defaults to the name of the class + name: string; definition?: ControllerDefinition; From ac7a441cba686bf5caa5528f7d49582bdf067208 Mon Sep 17 00:00:00 2001 From: "Marcus S. Abildskov" <8391194+marcus-sa@users.noreply.github.com> Date: Sat, 21 Oct 2023 16:53:17 +0200 Subject: [PATCH 5/8] Update decorators.ts Signed-off-by: Marcus S. Abildskov <8391194+marcus-sa@users.noreply.github.com> --- packages/rpc/src/decorators.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/rpc/src/decorators.ts b/packages/rpc/src/decorators.ts index 05de1a7b5..f4e245974 100644 --- a/packages/rpc/src/decorators.ts +++ b/packages/rpc/src/decorators.ts @@ -21,7 +21,7 @@ class RpcController { actions = new Map(); getPath(): string { - return this.definition ? this.definition.path : this.name || ''; + return this.definition ? this.definition.path : this.name; } } From 1546a418c9f0fc4f91e0dc0a61dadb823da07237 Mon Sep 17 00:00:00 2001 From: "Marcus S. Abildskov" <8391194+marcus-sa@users.noreply.github.com> Date: Sat, 21 Oct 2023 16:53:40 +0200 Subject: [PATCH 6/8] Update decorators.ts Signed-off-by: Marcus S. Abildskov <8391194+marcus-sa@users.noreply.github.com> --- packages/rpc/src/decorators.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/rpc/src/decorators.ts b/packages/rpc/src/decorators.ts index f4e245974..f72da2e52 100644 --- a/packages/rpc/src/decorators.ts +++ b/packages/rpc/src/decorators.ts @@ -52,7 +52,7 @@ class RpcClass { } onDecorator(classType: ClassType) { - this.t.name ||= classType.name; + this.t.name ??= classType.name; } } From 589c79ecf0f645460c9dabdf4dff62bb8361e9b9 Mon Sep 17 00:00:00 2001 From: "Marcus S. Abildskov" <8391194+marcus-sa@users.noreply.github.com> Date: Sat, 21 Oct 2023 16:58:34 +0200 Subject: [PATCH 7/8] Update decorators.ts Signed-off-by: Marcus S. Abildskov <8391194+marcus-sa@users.noreply.github.com> --- packages/rpc/src/decorators.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/rpc/src/decorators.ts b/packages/rpc/src/decorators.ts index f72da2e52..0591bde06 100644 --- a/packages/rpc/src/decorators.ts +++ b/packages/rpc/src/decorators.ts @@ -14,7 +14,7 @@ import { ControllerDefinition } from './model.js'; class RpcController { // Defaults to the name of the class - name: string; + name!: string; definition?: ControllerDefinition; From 50f21f96fc4c403838abe39368cb4df6ff087003 Mon Sep 17 00:00:00 2001 From: marcus-sa Date: Mon, 30 Oct 2023 22:29:04 +0100 Subject: [PATCH 8/8] feat(rpc): add support for class typeName in rpc class decorator --- packages/rpc/src/decorators.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/rpc/src/decorators.ts b/packages/rpc/src/decorators.ts index 0591bde06..173ba91ad 100644 --- a/packages/rpc/src/decorators.ts +++ b/packages/rpc/src/decorators.ts @@ -9,12 +9,19 @@ */ 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 { // Defaults to the name of the class - name!: string; + name: string = ''; definition?: ControllerDefinition; @@ -52,7 +59,7 @@ class RpcClass { } onDecorator(classType: ClassType) { - this.t.name ??= classType.name; + this.t.name ??= reflect(classType).typeName || classType.name; } }