MCPcopy Create free account
hub / github.com/middleapi/orpc / Implement

Function Implement

packages/nest/src/implement.ts:46–99  ·  view source on GitHub ↗
(
  contract: T,
)

Source from the content-addressed store, hash-verified

44 * @see {@link https://orpc.dev/docs/openapi/integrations/implement-contract-in-nest#implement-your-contract NestJS Implement Contract Docs}
45 */
46export function Implement<T extends ContractRouter<any>>(
47 contract: T,
48): <U extends Promisable<Router<T, ORPCGlobalContext>>>(
49 target: Record<PropertyKey, any>,
50 propertyKey: string,
51 descriptor: TypedPropertyDescriptor<(...args: any[]) => U>,
52) => void {
53 if (isContractProcedure(contract)) {
54 const method = fallbackContractConfig('defaultMethod', contract['~orpc'].route.method)
55 const path = contract['~orpc'].route.path
56
57 if (path === undefined) {
58 throw new Error(`
59 @Implement decorator requires contract to have a 'path'.
60 Please define one using 'path' property on the '.route' method.
61 Or use "populateContractRouterPaths" from "@orpc/contract" utility to automatically fill in any missing paths.
62 `)
63 }
64
65 return (target, propertyKey, descriptor) => {
66 applyDecorators(
67 MethodDecoratorMap[method](toNestPattern(path)),
68 HttpCode(fallbackContractConfig('defaultSuccessStatus', contract['~orpc'].route.successStatus)),
69 UseInterceptors(ImplementInterceptor),
70 )(target, propertyKey, descriptor)
71 }
72 }
73
74 return (target, propertyKey, descriptor) => {
75 for (const key in contract) {
76 let methodName = `${propertyKey}_${key}`
77
78 let i = 0
79 while (methodName in target) {
80 methodName = `${propertyKey}_${key}_${i++}`
81 }
82
83 target[methodName] = async function (...args: any[]) {
84 const router = await descriptor.value!.apply(this, args)
85 return getRouter(router, [key])
86 }
87
88 for (const p of Reflect.getOwnMetadataKeys(target, propertyKey)) {
89 Reflect.defineMetadata(p, Reflect.getOwnMetadata(p, target, propertyKey), target, methodName)
90 }
91
92 for (const p of Reflect.getOwnMetadataKeys(target.constructor, propertyKey)) {
93 Reflect.defineMetadata(p, Reflect.getOwnMetadata(p, target.constructor, propertyKey), target.constructor, methodName)
94 }
95
96 Implement(get(contract, [key]) as any)(target, methodName, Object.getOwnPropertyDescriptor(target, methodName)!)
97 }
98 }
99}
100
101type NestParams = Record<string, string | string[]>
102

Callers 9

FastifyControllerClass · 0.90
HonoControllerClass · 0.90
AuthControllerClass · 0.90
OtherControllerClass · 0.90
PlanetControllerClass · 0.90

Calls 5

isContractProcedureFunction · 0.90
fallbackContractConfigFunction · 0.90
toNestPatternFunction · 0.90
getRouterFunction · 0.90
getFunction · 0.90

Tested by

no test coverage detected