Skip to content

Commit 69674a7

Browse files
authored
feat(server): support chaining after .callable and .actionable (#223)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **Refactor** - Enhanced procedure operations to return enriched objects with consistent properties and improved dynamic behavior. - Updated return types for `callable` and `actionable` methods to ensure they return specific procedure types. - **Tests** - Updated test validations to ensure that the procedure operations reliably expose all expected attributes. - Added assertions to confirm the presence of properties on the `applied` object in test cases. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent 352022d commit 69674a7

File tree

5 files changed

+42
-15
lines changed

5 files changed

+42
-15
lines changed

packages/server/src/implementer-procedure.test-d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ describe('ImplementedProcedure', () => {
146146
})
147147

148148
expectTypeOf(applied).toEqualTypeOf<
149-
Procedure<
149+
ImplementedProcedure<
150150
InitialContext,
151151
CurrentContext,
152152
typeof inputSchema,
@@ -164,7 +164,7 @@ describe('ImplementedProcedure', () => {
164164
})
165165

166166
expectTypeOf(applied).toEqualTypeOf<
167-
Procedure<
167+
ImplementedProcedure<
168168
InitialContext,
169169
CurrentContext,
170170
typeof inputSchema,

packages/server/src/implementer-procedure.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export interface ImplementedProcedure<
7373
TClientContext
7474
>
7575
>
76-
): Procedure<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>
76+
): ImplementedProcedure<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>
7777
& ProcedureClient<TClientContext, TInputSchema, TOutputSchema, TErrorMap >
7878

7979
/**
@@ -90,7 +90,7 @@ export interface ImplementedProcedure<
9090
TClientContext
9191
>
9292
>
93-
): Procedure<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>
93+
): ImplementedProcedure<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>
9494
& ((...rest: ClientRest<TClientContext, InferSchemaInput<TInputSchema>>) => Promise<InferSchemaOutput<TOutputSchema>>)
9595
}
9696

packages/server/src/procedure-decorated.test-d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ describe('DecoratedProcedure', () => {
174174
})
175175

176176
expectTypeOf(applied).toEqualTypeOf<
177-
Procedure<
177+
DecoratedProcedure<
178178
InitialContext,
179179
CurrentContext,
180180
typeof inputSchema,
@@ -192,7 +192,7 @@ describe('DecoratedProcedure', () => {
192192
})
193193

194194
expectTypeOf(applied).toEqualTypeOf<
195-
Procedure<
195+
DecoratedProcedure<
196196
InitialContext,
197197
CurrentContext,
198198
typeof inputSchema,

packages/server/src/procedure-decorated.test.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,19 +107,42 @@ describe('decoratedProcedure', () => {
107107

108108
expect(createProcedureClient).toBeCalledTimes(1)
109109
expect(createProcedureClient).toBeCalledWith(decorated, options)
110-
expect(applied).not.haveOwnPropertyDescriptor('use')
110+
111+
// can access to function properties
112+
expect('name' in applied).toBe(true)
113+
expect(typeof applied.name).toBe('string')
114+
expect('length' in applied).toBe(true)
115+
expect(typeof applied.length).toBe('number')
116+
117+
expect('use' in applied).toBe(true)
118+
expect('route' in applied).toBe(true)
119+
expect('meta' in applied).toBe(true)
120+
121+
expect(applied.route({})).toBeInstanceOf(DecoratedProcedure)
122+
expect(applied.route({})).toEqual(decorated)
111123
})
112124

113125
it('.actionable', () => {
114126
const options = { context: { db: 'postgres' } }
115127

116128
const applied = decorated.actionable(options)
117-
118129
expect(applied).toBeInstanceOf(Function)
119130
expect(applied).toSatisfy(isProcedure)
120131

121132
expect(createProcedureClient).toBeCalledTimes(1)
122133
expect(createProcedureClient).toBeCalledWith(decorated, options)
123-
expect(applied).not.haveOwnPropertyDescriptor('use')
134+
135+
// can access to function properties
136+
expect('name' in applied).toBe(true)
137+
expect(typeof applied.name).toBe('string')
138+
expect('length' in applied).toBe(true)
139+
expect(typeof applied.length).toBe('number')
140+
141+
expect('use' in applied).toBe(true)
142+
expect('route' in applied).toBe(true)
143+
expect('meta' in applied).toBe(true)
144+
145+
expect(applied.route({})).toBeInstanceOf(DecoratedProcedure)
146+
expect(applied.route({})).toEqual(decorated)
124147
})
125148
})

packages/server/src/procedure-decorated.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,16 @@ export class DecoratedProcedure<
117117
TClientContext
118118
>
119119
>
120-
): Procedure<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>
120+
): DecoratedProcedure<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>
121121
& ProcedureClient<TClientContext, TInputSchema, TOutputSchema, TErrorMap> {
122-
return Object.assign(createProcedureClient(this, ...rest as any), {
123-
'~type': 'Procedure' as const,
124-
'~orpc': this['~orpc'],
125-
})
122+
return new Proxy(createProcedureClient(this, ...rest as any), {
123+
get: (target, key) => {
124+
return Reflect.has(this, key) ? Reflect.get(this, key) : Reflect.get(target, key)
125+
},
126+
has: (target, key) => {
127+
return Reflect.has(this, key) || Reflect.has(target, key)
128+
},
129+
}) as any
126130
}
127131

128132
/**
@@ -140,7 +144,7 @@ export class DecoratedProcedure<
140144
>
141145
>
142146
):
143-
& Procedure<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>
147+
& DecoratedProcedure<TInitialContext, TCurrentContext, TInputSchema, TOutputSchema, TErrorMap, TMeta>
144148
& ((...rest: ClientRest<TClientContext, InferSchemaInput<TInputSchema>>) => Promise<InferSchemaOutput<TOutputSchema>>) {
145149
return this.callable(...rest)
146150
}

0 commit comments

Comments
 (0)