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

Function implementerInternal

packages/server/src/implementer.ts:99–189  ·  view source on GitHub ↗
(
  contract: T,
  config: BuilderConfig,
  middlewares: AnyMiddleware[],
)

Source from the content-addressed store, hash-verified

97 }
98
99export function implementerInternal<
100 T extends AnyContractRouter,
101 TInitialContext extends Context,
102 TCurrentContext extends Context,
103>(
104 contract: T,
105 config: BuilderConfig,
106 middlewares: AnyMiddleware[],
107): ImplementerInternal<T, TInitialContext, TCurrentContext> {
108 if (isContractProcedure(contract)) {
109 const impl = new Builder({
110 ...contract['~orpc'],
111 config,
112 middlewares,
113 inputValidationIndex: fallbackConfig('initialInputValidationIndex', config?.initialInputValidationIndex) + middlewares.length,
114 outputValidationIndex: fallbackConfig('initialOutputValidationIndex', config?.initialOutputValidationIndex) + middlewares.length,
115 dedupeLeadingMiddlewares: fallbackConfig('dedupeLeadingMiddlewares', config.dedupeLeadingMiddlewares),
116 })
117
118 return impl as any
119 }
120
121 const impl = new Proxy(contract, {
122 get: (target, key) => {
123 if (typeof key !== 'string') {
124 return Reflect.get(target, key)
125 }
126
127 let method: AnyFunction | undefined
128
129 if (key === 'middleware') {
130 method = (mid: any) => decorateMiddleware(mid)
131 }
132 else if (key === 'use') {
133 method = (mid: any) => {
134 return implementerInternal(
135 contract,
136 config,
137 addMiddleware(middlewares, mid),
138 )
139 }
140 }
141 else if (key === 'router') {
142 method = (router: any) => {
143 const adapted = enhanceRouter(router, {
144 middlewares,
145 errorMap: {},
146 prefix: undefined,
147 tags: undefined,
148 dedupeLeadingMiddlewares: fallbackConfig('dedupeLeadingMiddlewares', config.dedupeLeadingMiddlewares),
149 })
150
151 return setHiddenRouterContract(adapted, contract)
152 }
153 }
154 else if (key === 'lazy') {
155 method = (loader: () => Promise<{ default: AnyRouter }>) => {
156 const adapted = enhanceRouter(lazy(loader) as any, {

Callers 1

implementFunction · 0.85

Calls 9

isContractProcedureFunction · 0.90
fallbackConfigFunction · 0.90
decorateMiddlewareFunction · 0.90
addMiddlewareFunction · 0.90
enhanceRouterFunction · 0.90
setHiddenRouterContractFunction · 0.90
lazyFunction · 0.90
getContractRouterFunction · 0.90
getMethod · 0.65

Tested by

no test coverage detected