MCPcopy Create free account
hub / github.com/zenstackhq/zenstack / handleProc

Method handleProc

packages/orm/src/client/client-impl.ts:316–369  ·  view source on GitHub ↗
(name: string, input: unknown)

Source from the content-addressed store, hash-verified

314 }
315
316 private async handleProc(name: string, input: unknown) {
317 if (!('procedures' in this.$options) || !this.$options || typeof this.$options.procedures !== 'object') {
318 throw createConfigError('Procedures are not configured for the client.');
319 }
320
321 const procDef = (this.$schema.procedures ?? {})[name];
322 if (!procDef) {
323 throw createConfigError(`Procedure "${name}" is not defined in schema.`);
324 }
325
326 const procOptions = this.$options.procedures as ProceduresOptions<
327 SchemaDef & {
328 procedures: Record<string, ProcedureDef>;
329 }
330 >;
331 if (!procOptions[name] || typeof procOptions[name] !== 'function') {
332 throw createConfigError(`Procedure "${name}" does not have a handler configured.`);
333 }
334
335 // Validate inputs using the same validator infrastructure as CRUD operations.
336 const validatedInput = this.inputValidator.validateProcedureInput(name, input);
337
338 const handler = procOptions[name] as Function;
339
340 const invokeWithClient = async (client: any, _input: unknown) => {
341 let proceed = async (nextInput: unknown) => {
342 const sanitizedNextInput =
343 nextInput && typeof nextInput === 'object' && !Array.isArray(nextInput) ? nextInput : {};
344
345 return handler({ client, ...sanitizedNextInput });
346 };
347
348 // apply plugins
349 const plugins = [...(client.$options?.plugins ?? [])];
350 for (const plugin of plugins) {
351 const onProcedure = plugin.onProcedure;
352 if (onProcedure) {
353 const _proceed = proceed;
354 proceed = (nextInput: unknown) =>
355 onProcedure({
356 client,
357 name,
358 mutation: !!procDef.mutation,
359 input: nextInput,
360 proceed: (finalInput: unknown) => _proceed(finalInput),
361 }) as Promise<unknown>;
362 }
363 }
364
365 return proceed(_input);
366 };
367
368 return invokeWithClient(this as any, validatedInput);
369 }
370
371 async $connect() {
372 await this.kysely.connection().execute(async (conn) => {

Callers 1

$procsMethod · 0.95

Calls 2

createConfigErrorFunction · 0.90

Tested by

no test coverage detected