| 212 | & ImplementerInternal<TContract, TInitialContext, TCurrentContext> |
| 213 | |
| 214 | export function implement<T extends AnyContractRouter, TContext extends Context = Record<never, never>>( |
| 215 | contract: T, |
| 216 | config: BuilderConfig = {}, |
| 217 | ): Implementer<T, TContext, TContext> { |
| 218 | const implInternal = implementerInternal(contract, config, []) |
| 219 | |
| 220 | const impl = new Proxy(implInternal, { |
| 221 | get: (target, key) => { |
| 222 | let method: AnyFunction | undefined |
| 223 | |
| 224 | if (key === '$context') { |
| 225 | method = () => impl |
| 226 | } |
| 227 | else if (key === '$config') { |
| 228 | method = (config: BuilderConfig) => implement(contract, config) |
| 229 | } |
| 230 | |
| 231 | const next = Reflect.get(target, key) |
| 232 | |
| 233 | if (!method || !next || (typeof next !== 'function' && typeof next !== 'object')) { |
| 234 | return method || next |
| 235 | } |
| 236 | |
| 237 | return new Proxy(method, { |
| 238 | get(_, key) { |
| 239 | return Reflect.get(next, key) |
| 240 | }, |
| 241 | }) |
| 242 | }, |
| 243 | }) |
| 244 | |
| 245 | return impl as any |
| 246 | } |