|
| 1 | +import { oc } from '@orpc/contract' |
| 2 | +import { implement, os } from '@orpc/server' |
| 3 | +import { describe, it } from 'vitest' |
| 4 | +import { z } from 'zod' |
| 5 | +import { Implement } from './implement' |
| 6 | + |
| 7 | +export const inputSchema = z.object({ input: z.number().transform(n => `${n}`) }) |
| 8 | + |
| 9 | +export const outputSchema = z.object({ output: z.number().transform(n => `${n}`) }) |
| 10 | + |
| 11 | +describe('@Implement', () => { |
| 12 | + it('require return an implemented procedure that satisfy default initial context', () => { |
| 13 | + const contract = oc.input(inputSchema).output(outputSchema) |
| 14 | + |
| 15 | + class _ImplProcedureController { |
| 16 | + @Implement(contract) |
| 17 | + ping() { |
| 18 | + return implement(contract).handler(() => ({}) as any) |
| 19 | + } |
| 20 | + |
| 21 | + @Implement(contract) |
| 22 | + ping_with_middleware_context() { |
| 23 | + return implement(contract).use(({ next }) => next({ context: { extra: 'value' } })).handler(() => ({}) as any) |
| 24 | + } |
| 25 | + |
| 26 | + // @ts-expect-error --- return invalid |
| 27 | + @Implement(contract) |
| 28 | + ping_invalid() { |
| 29 | + return 'invalid' |
| 30 | + } |
| 31 | + |
| 32 | + // @ts-expect-error --- initial context is not allowed |
| 33 | + @Implement(contract) |
| 34 | + ping_invalid_initial_context() { |
| 35 | + return implement(contract).$context<{ a: string }>().handler(() => ({}) as any) |
| 36 | + } |
| 37 | + |
| 38 | + // @ts-expect-error --- implement wrong contract |
| 39 | + @Implement(contract) |
| 40 | + ping_wrong_implement() { |
| 41 | + return implement(oc.input(inputSchema)).handler(() => ({}) as any) |
| 42 | + } |
| 43 | + } |
| 44 | + }) |
| 45 | + |
| 46 | + it('require return an implemented router that satisfy default initial context', () => { |
| 47 | + const contract = { |
| 48 | + ping: oc.input(inputSchema).output(outputSchema), |
| 49 | + } |
| 50 | + |
| 51 | + class _ImplProcedureController { |
| 52 | + @Implement(contract) |
| 53 | + ping() { |
| 54 | + return { |
| 55 | + ping: implement(contract.ping).handler(() => ({}) as any), |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Implement(contract) |
| 60 | + ping_with_middleware_context() { |
| 61 | + return { |
| 62 | + ping: implement(contract.ping).use(({ next }) => next({ context: { extra: 'value' } })).handler(() => ({}) as any), |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Implement(contract) |
| 67 | + ping_with_lazy() { |
| 68 | + return { |
| 69 | + ping: os.lazy(() => Promise.resolve({ default: implement(contract.ping).handler(() => ({}) as any) })), |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + // @ts-expect-error --- return invalid |
| 74 | + @Implement(contract) |
| 75 | + ping_invalid() { |
| 76 | + return 'invalid' |
| 77 | + } |
| 78 | + |
| 79 | + // @ts-expect-error --- initial context is not allowed |
| 80 | + @Implement(contract) |
| 81 | + ping_invalid_initial_context() { |
| 82 | + return { |
| 83 | + ping: implement(contract.ping).$context<{ a: string }>().handler(() => ({}) as any), |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + // @ts-expect-error --- initial context is not allowed |
| 88 | + @Implement(contract) |
| 89 | + ping_invalid_initial_context_lazy() { |
| 90 | + return { |
| 91 | + ping: os.lazy(() => Promise.resolve({ default: implement(contract.ping).$context<{ a: string }>().handler(() => ({}) as any) })), |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // @ts-expect-error --- implement wrong contract |
| 96 | + @Implement(contract) |
| 97 | + ping_wrong_implement() { |
| 98 | + return { |
| 99 | + ping: implement(oc.input(inputSchema)).handler(() => ({}) as any), |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + // @ts-expect-error --- implement wrong contract |
| 104 | + @Implement(contract) |
| 105 | + ping_wrong_implement_lazy() { |
| 106 | + return { |
| 107 | + ping: os.lazy(() => Promise.resolve({ default: implement(oc.input(inputSchema)).handler(() => ({}) as any) })), |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + }) |
| 112 | +}) |
0 commit comments