@temporary-name/server 1.9.3-alpha.592ae1e02e18c5274906b132313b083116248636 → 1.9.3-alpha.5bb5db108c105334106e6177e3043b9c45fbe69c

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/dist/adapters/aws-lambda/index.d.mts +5 -7
  2. package/dist/adapters/aws-lambda/index.d.ts +5 -7
  3. package/dist/adapters/aws-lambda/index.mjs +6 -9
  4. package/dist/adapters/fetch/index.d.mts +8 -86
  5. package/dist/adapters/fetch/index.d.ts +8 -86
  6. package/dist/adapters/fetch/index.mjs +18 -160
  7. package/dist/adapters/node/index.d.mts +9 -64
  8. package/dist/adapters/node/index.d.ts +9 -64
  9. package/dist/adapters/node/index.mjs +16 -125
  10. package/dist/handler/index.d.mts +28 -0
  11. package/dist/handler/index.d.ts +28 -0
  12. package/dist/handler/index.mjs +8 -0
  13. package/dist/helpers/index.mjs +3 -29
  14. package/dist/index.d.mts +372 -493
  15. package/dist/index.d.ts +372 -493
  16. package/dist/index.mjs +546 -469
  17. package/dist/openapi/index.d.mts +18 -53
  18. package/dist/openapi/index.d.ts +18 -53
  19. package/dist/openapi/index.mjs +384 -378
  20. package/dist/shared/server.B9A3AnYU.mjs +523 -0
  21. package/dist/shared/server.Bh0-Oy3H.mjs +156 -0
  22. package/dist/shared/server.Bmxjwleq.d.ts +39 -0
  23. package/dist/shared/server.BnAospUv.mjs +334 -0
  24. package/dist/shared/server.Bx9JTGMu.mjs +433 -0
  25. package/dist/shared/server.C1RJffw4.mjs +30 -0
  26. package/dist/shared/server.CjPiuQYH.d.mts +51 -0
  27. package/dist/shared/server.CjPiuQYH.d.ts +51 -0
  28. package/dist/shared/server.DLyn62VH.d.mts +39 -0
  29. package/dist/shared/server.DpIhEnBO.d.mts +515 -0
  30. package/dist/shared/server.DpIhEnBO.d.ts +515 -0
  31. package/package.json +13 -30
  32. package/dist/adapters/standard/index.d.mts +0 -42
  33. package/dist/adapters/standard/index.d.ts +0 -42
  34. package/dist/adapters/standard/index.mjs +0 -11
  35. package/dist/plugins/index.d.mts +0 -160
  36. package/dist/plugins/index.d.ts +0 -160
  37. package/dist/plugins/index.mjs +0 -288
  38. package/dist/shared/server.BEHw7Eyx.mjs +0 -247
  39. package/dist/shared/server.BKSOrA6h.d.mts +0 -192
  40. package/dist/shared/server.BKSOrA6h.d.ts +0 -192
  41. package/dist/shared/server.BKh8I1Ny.mjs +0 -239
  42. package/dist/shared/server.BeuTpcmO.d.mts +0 -23
  43. package/dist/shared/server.C1fnTLq0.d.mts +0 -57
  44. package/dist/shared/server.CQyYNJ1H.d.ts +0 -57
  45. package/dist/shared/server.DLsti1Pv.mjs +0 -293
  46. package/dist/shared/server.SLLuK6_v.d.ts +0 -23
@@ -0,0 +1,523 @@
1
+ import { isAsyncIteratorObject, toArray } from '@temporary-name/shared';
2
+ import { mapEventIterator } from '@temporary-name/standard-server';
3
+ import { custom, safeParseAsync } from '@temporary-name/zod';
4
+ import { V as ValidationError } from './server.Bx9JTGMu.mjs';
5
+ import { JSONSchemaFormat, JSONSchemaContentEncoding } from '@temporary-name/server/openapi';
6
+ import { registry, globalRegistry } from 'zod/v4/core';
7
+
8
+ const EVENT_ITERATOR_DETAILS_SYMBOL = Symbol("ORPC_EVENT_ITERATOR_DETAILS");
9
+ function eventIterator(yields, returns) {
10
+ const schema = custom(
11
+ (iterator) => isAsyncIteratorObject(iterator)
12
+ ).transform((iterator) => {
13
+ const mapped = mapEventIterator(iterator, {
14
+ async value(value, done) {
15
+ const schema2 = done ? returns : yields;
16
+ if (!schema2) {
17
+ return value;
18
+ }
19
+ const result = await safeParseAsync(schema2, value);
20
+ if (result.success) {
21
+ return result.data;
22
+ } else {
23
+ throw new Error("Event iterator validation failed", {
24
+ cause: new ValidationError({
25
+ issues: result.error.issues,
26
+ data: value
27
+ })
28
+ });
29
+ }
30
+ },
31
+ error: async (error) => error
32
+ });
33
+ return mapped;
34
+ });
35
+ schema[EVENT_ITERATOR_DETAILS_SYMBOL] = {
36
+ yields,
37
+ returns
38
+ };
39
+ return schema;
40
+ }
41
+ function getEventIteratorSchemaDetails(schema) {
42
+ if (schema === void 0) {
43
+ return void 0;
44
+ }
45
+ return schema[EVENT_ITERATOR_DETAILS_SYMBOL];
46
+ }
47
+
48
+ var JsonSchemaXNativeType = /* @__PURE__ */ ((JsonSchemaXNativeType2) => {
49
+ JsonSchemaXNativeType2["BigInt"] = "bigint";
50
+ JsonSchemaXNativeType2["RegExp"] = "regexp";
51
+ JsonSchemaXNativeType2["Date"] = "date";
52
+ JsonSchemaXNativeType2["Url"] = "url";
53
+ JsonSchemaXNativeType2["Set"] = "set";
54
+ JsonSchemaXNativeType2["Map"] = "map";
55
+ return JsonSchemaXNativeType2;
56
+ })(JsonSchemaXNativeType || {});
57
+
58
+ const JSON_SCHEMA_REGISTRY = registry();
59
+ const JSON_SCHEMA_INPUT_REGISTRY = registry();
60
+ const JSON_SCHEMA_OUTPUT_REGISTRY = registry();
61
+
62
+ class ZodToJsonSchemaConverter {
63
+ maxLazyDepth;
64
+ maxStructureDepth;
65
+ anyJsonSchema;
66
+ unsupportedJsonSchema;
67
+ undefinedJsonSchema;
68
+ constructor(options = {}) {
69
+ this.maxLazyDepth = options.maxLazyDepth ?? 2;
70
+ this.maxStructureDepth = options.maxStructureDepth ?? 10;
71
+ this.anyJsonSchema = options.anyJsonSchema ?? {};
72
+ this.unsupportedJsonSchema = options.unsupportedJsonSchema ?? { not: {} };
73
+ this.undefinedJsonSchema = options.undefinedJsonSchema ?? { not: {} };
74
+ }
75
+ convert = (schema, options) => {
76
+ return this.#convert(schema, options, 0, 0);
77
+ };
78
+ #convert(schema, options, lazyDepth, structureDepth, isHandledCustomJSONSchema = false) {
79
+ if (structureDepth > this.maxStructureDepth) {
80
+ return [false, this.anyJsonSchema];
81
+ }
82
+ if (!options.minStructureDepthForRef || options.minStructureDepthForRef <= structureDepth) {
83
+ const components = toArray(options.components);
84
+ for (const component of components) {
85
+ if (component.schema === schema && component.allowedStrategies.includes(options.strategy)) {
86
+ return [component.required, { $ref: component.ref }];
87
+ }
88
+ }
89
+ }
90
+ if (!isHandledCustomJSONSchema) {
91
+ const customJSONSchema = this.#getCustomJsonSchema(schema, options);
92
+ if (customJSONSchema) {
93
+ const [required, json] = this.#convert(schema, options, lazyDepth, structureDepth, true);
94
+ return [required, { ...json, ...customJSONSchema }];
95
+ }
96
+ }
97
+ switch (schema._zod.def.type) {
98
+ case "string": {
99
+ const string = schema;
100
+ const json = { type: "string" };
101
+ const { minimum, maximum, format, patterns, contentEncoding } = string._zod.bag;
102
+ if (typeof minimum === "number") {
103
+ json.minLength = minimum;
104
+ }
105
+ if (typeof maximum === "number") {
106
+ json.maxLength = maximum;
107
+ }
108
+ if (typeof contentEncoding === "string") {
109
+ json.contentEncoding = this.#handleContentEncoding(contentEncoding);
110
+ }
111
+ if (typeof format === "string" && format !== "regex" && json.contentEncoding === void 0) {
112
+ json.format = this.#handleStringFormat(format);
113
+ }
114
+ if (patterns instanceof Set && json.contentEncoding === void 0 && json.format === void 0) {
115
+ for (const pattern of patterns) {
116
+ if (json.pattern === void 0) {
117
+ json.pattern = pattern.source;
118
+ } else {
119
+ json.allOf ??= [];
120
+ json.allOf.push({ pattern: pattern.source });
121
+ }
122
+ }
123
+ }
124
+ if (format === "jwt" && json.contentEncoding === void 0 && json.format === void 0 && json.pattern === void 0) {
125
+ json.pattern = /^[\w-]+\.[\w-]+\.[\w-]+$/.source;
126
+ }
127
+ return [true, json];
128
+ }
129
+ case "number": {
130
+ const number = schema;
131
+ const json = { type: "number" };
132
+ const { minimum, maximum, format, multipleOf, exclusiveMaximum, exclusiveMinimum } = number._zod.bag;
133
+ if (typeof format === "string" && format?.includes("int")) {
134
+ json.type = "integer";
135
+ }
136
+ if (typeof minimum === "number") {
137
+ json.minimum = minimum;
138
+ }
139
+ if (typeof maximum === "number") {
140
+ json.maximum = maximum;
141
+ }
142
+ if (typeof exclusiveMinimum === "number") {
143
+ json.exclusiveMinimum = exclusiveMinimum;
144
+ }
145
+ if (typeof exclusiveMaximum === "number") {
146
+ json.exclusiveMaximum = exclusiveMaximum;
147
+ }
148
+ if (typeof multipleOf === "number") {
149
+ json.multipleOf = multipleOf;
150
+ }
151
+ return [true, json];
152
+ }
153
+ case "boolean": {
154
+ return [true, { type: "boolean" }];
155
+ }
156
+ case "bigint": {
157
+ return [
158
+ true,
159
+ {
160
+ type: "string",
161
+ pattern: "^-?[0-9]+$",
162
+ "x-native-type": JsonSchemaXNativeType.BigInt
163
+ }
164
+ ];
165
+ }
166
+ case "date": {
167
+ return [
168
+ true,
169
+ {
170
+ type: "string",
171
+ format: JSONSchemaFormat.DateTime,
172
+ "x-native-type": JsonSchemaXNativeType.Date
173
+ }
174
+ ];
175
+ }
176
+ case "null": {
177
+ return [true, { type: "null" }];
178
+ }
179
+ case "undefined":
180
+ case "void": {
181
+ return [false, this.undefinedJsonSchema];
182
+ }
183
+ case "any": {
184
+ return [false, this.anyJsonSchema];
185
+ }
186
+ case "unknown": {
187
+ return [false, this.anyJsonSchema];
188
+ }
189
+ case "never": {
190
+ return [true, this.unsupportedJsonSchema];
191
+ }
192
+ case "array": {
193
+ const array = schema;
194
+ const json = { type: "array" };
195
+ const { minimum, maximum } = array._zod.bag;
196
+ if (typeof minimum === "number") {
197
+ json.minItems = minimum;
198
+ }
199
+ if (typeof maximum === "number") {
200
+ json.maxItems = maximum;
201
+ }
202
+ json.items = this.#handleArrayItemJsonSchema(
203
+ this.#convert(array._zod.def.element, options, lazyDepth, structureDepth + 1),
204
+ options
205
+ );
206
+ return [true, json];
207
+ }
208
+ case "object": {
209
+ const object = schema;
210
+ const json = { type: "object" };
211
+ for (const [key, value] of Object.entries(object._zod.def.shape)) {
212
+ const [itemRequired, itemJson] = this.#convert(value, options, lazyDepth, structureDepth + 1);
213
+ json.properties ??= {};
214
+ json.properties[key] = itemJson;
215
+ if (itemRequired) {
216
+ json.required ??= [];
217
+ json.required.push(key);
218
+ }
219
+ }
220
+ if (object._zod.def.catchall) {
221
+ if (object._zod.def.catchall._zod.def.type === "never") {
222
+ json.additionalProperties = false;
223
+ } else {
224
+ const [_, addJson] = this.#convert(
225
+ object._zod.def.catchall,
226
+ options,
227
+ lazyDepth,
228
+ structureDepth + 1
229
+ );
230
+ json.additionalProperties = addJson;
231
+ }
232
+ }
233
+ return [true, json];
234
+ }
235
+ case "union": {
236
+ const union = schema;
237
+ const anyOf = [];
238
+ let required = true;
239
+ for (const item of union._zod.def.options) {
240
+ const [itemRequired, itemJson] = this.#convert(item, options, lazyDepth, structureDepth);
241
+ if (!itemRequired) {
242
+ required = false;
243
+ }
244
+ if (options.strategy === "input") {
245
+ if (itemJson !== this.undefinedJsonSchema && itemJson !== this.unsupportedJsonSchema) {
246
+ anyOf.push(itemJson);
247
+ }
248
+ } else {
249
+ if (itemJson !== this.undefinedJsonSchema) {
250
+ anyOf.push(itemJson);
251
+ }
252
+ }
253
+ }
254
+ return [required, anyOf.length === 1 ? anyOf[0] : { anyOf }];
255
+ }
256
+ case "intersection": {
257
+ const intersection = schema;
258
+ const json = { allOf: [] };
259
+ let required = false;
260
+ for (const item of [intersection._zod.def.left, intersection._zod.def.right]) {
261
+ const [itemRequired, itemJson] = this.#convert(item, options, lazyDepth, structureDepth);
262
+ json.allOf.push(itemJson);
263
+ if (itemRequired) {
264
+ required = true;
265
+ }
266
+ }
267
+ return [required, json];
268
+ }
269
+ case "tuple": {
270
+ const tuple = schema;
271
+ const json = { type: "array", prefixItems: [] };
272
+ for (const item of tuple._zod.def.items) {
273
+ json.prefixItems.push(
274
+ this.#handleArrayItemJsonSchema(
275
+ this.#convert(item, options, lazyDepth, structureDepth + 1),
276
+ options
277
+ )
278
+ );
279
+ }
280
+ if (tuple._zod.def.rest) {
281
+ json.items = this.#handleArrayItemJsonSchema(
282
+ this.#convert(tuple._zod.def.rest, options, lazyDepth, structureDepth + 1),
283
+ options
284
+ );
285
+ }
286
+ const { minimum, maximum } = tuple._zod.bag;
287
+ if (typeof minimum === "number") {
288
+ json.minItems = minimum;
289
+ }
290
+ if (typeof maximum === "number") {
291
+ json.maxItems = maximum;
292
+ }
293
+ return [true, json];
294
+ }
295
+ case "record": {
296
+ const record = schema;
297
+ const json = { type: "object" };
298
+ json.propertyNames = this.#convert(
299
+ record._zod.def.keyType,
300
+ options,
301
+ lazyDepth,
302
+ structureDepth + 1
303
+ )[1];
304
+ json.additionalProperties = this.#convert(
305
+ record._zod.def.valueType,
306
+ options,
307
+ lazyDepth,
308
+ structureDepth + 1
309
+ )[1];
310
+ return [true, json];
311
+ }
312
+ case "map": {
313
+ const map = schema;
314
+ return [
315
+ true,
316
+ {
317
+ type: "array",
318
+ items: {
319
+ type: "array",
320
+ prefixItems: [
321
+ this.#handleArrayItemJsonSchema(
322
+ this.#convert(map._zod.def.keyType, options, lazyDepth, structureDepth + 1),
323
+ options
324
+ ),
325
+ this.#handleArrayItemJsonSchema(
326
+ this.#convert(map._zod.def.valueType, options, lazyDepth, structureDepth + 1),
327
+ options
328
+ )
329
+ ],
330
+ maxItems: 2,
331
+ minItems: 2
332
+ },
333
+ "x-native-type": JsonSchemaXNativeType.Map
334
+ }
335
+ ];
336
+ }
337
+ case "set": {
338
+ const set = schema;
339
+ return [
340
+ true,
341
+ {
342
+ type: "array",
343
+ uniqueItems: true,
344
+ items: this.#handleArrayItemJsonSchema(
345
+ this.#convert(set._zod.def.valueType, options, lazyDepth, structureDepth + 1),
346
+ options
347
+ ),
348
+ "x-native-type": JsonSchemaXNativeType.Set
349
+ }
350
+ ];
351
+ }
352
+ case "enum": {
353
+ const enum_ = schema;
354
+ return [true, { enum: Object.values(enum_._zod.def.entries) }];
355
+ }
356
+ case "literal": {
357
+ const literal = schema;
358
+ let required = true;
359
+ const values = /* @__PURE__ */ new Set();
360
+ for (const value of literal._zod.def.values) {
361
+ if (value === void 0) {
362
+ required = false;
363
+ } else {
364
+ values.add(typeof value === "bigint" ? value.toString() : value);
365
+ }
366
+ }
367
+ const json = values.size === 0 ? this.undefinedJsonSchema : values.size === 1 ? { const: values.values().next().value } : { enum: Array.from(values) };
368
+ return [required, json];
369
+ }
370
+ case "file": {
371
+ const file = schema;
372
+ const oneOf = [];
373
+ const { mime } = file._zod.bag;
374
+ if (mime === void 0 || Array.isArray(mime) && mime.every((m) => typeof m === "string")) {
375
+ for (const type of mime ?? ["*/*"]) {
376
+ oneOf.push({
377
+ type: "string",
378
+ contentMediaType: type
379
+ });
380
+ }
381
+ }
382
+ return [true, oneOf.length === 1 ? oneOf[0] : { anyOf: oneOf }];
383
+ }
384
+ case "transform": {
385
+ return [false, this.anyJsonSchema];
386
+ }
387
+ case "nullable": {
388
+ const nullable = schema;
389
+ const [required, json] = this.#convert(
390
+ nullable._zod.def.innerType,
391
+ options,
392
+ lazyDepth,
393
+ structureDepth
394
+ );
395
+ return [required, { anyOf: [json, { type: "null" }] }];
396
+ }
397
+ case "nonoptional": {
398
+ const nonoptional = schema;
399
+ const [, json] = this.#convert(nonoptional._zod.def.innerType, options, lazyDepth, structureDepth);
400
+ return [true, json];
401
+ }
402
+ case "success": {
403
+ return [true, { type: "boolean" }];
404
+ }
405
+ case "default":
406
+ case "prefault": {
407
+ const default_ = schema;
408
+ const [, json] = this.#convert(default_._zod.def.innerType, options, lazyDepth, structureDepth);
409
+ return [
410
+ false,
411
+ {
412
+ ...json,
413
+ default: default_._zod.def.defaultValue
414
+ }
415
+ ];
416
+ }
417
+ case "catch": {
418
+ const catch_ = schema;
419
+ return this.#convert(catch_._zod.def.innerType, options, lazyDepth, structureDepth);
420
+ }
421
+ case "nan": {
422
+ return [true, options.strategy === "input" ? this.unsupportedJsonSchema : { type: "null" }];
423
+ }
424
+ case "pipe": {
425
+ const pipe = schema;
426
+ return this.#convert(
427
+ options.strategy === "input" ? pipe._zod.def.in : pipe._zod.def.out,
428
+ options,
429
+ lazyDepth,
430
+ structureDepth
431
+ );
432
+ }
433
+ case "readonly": {
434
+ const readonly_ = schema;
435
+ const [required, json] = this.#convert(
436
+ readonly_._zod.def.innerType,
437
+ options,
438
+ lazyDepth,
439
+ structureDepth
440
+ );
441
+ return [required, { ...json, readOnly: true }];
442
+ }
443
+ case "template_literal": {
444
+ const templateLiteral = schema;
445
+ return [
446
+ true,
447
+ {
448
+ type: "string",
449
+ pattern: templateLiteral._zod.pattern.source
450
+ }
451
+ ];
452
+ }
453
+ case "optional": {
454
+ const optional = schema;
455
+ const [, json] = this.#convert(optional._zod.def.innerType, options, lazyDepth, structureDepth);
456
+ return [false, json];
457
+ }
458
+ case "lazy": {
459
+ const lazy = schema;
460
+ const currentLazyDepth = lazyDepth + 1;
461
+ if (currentLazyDepth > this.maxLazyDepth) {
462
+ return [false, this.anyJsonSchema];
463
+ }
464
+ return this.#convert(lazy._zod.def.getter(), options, currentLazyDepth, structureDepth);
465
+ }
466
+ default: {
467
+ schema._zod.def.type;
468
+ return [true, this.unsupportedJsonSchema];
469
+ }
470
+ }
471
+ }
472
+ #getCustomJsonSchema(schema, options) {
473
+ if (options.strategy === "input" && JSON_SCHEMA_INPUT_REGISTRY.has(schema)) {
474
+ return JSON_SCHEMA_INPUT_REGISTRY.get(schema);
475
+ }
476
+ if (options.strategy === "output" && JSON_SCHEMA_OUTPUT_REGISTRY.has(schema)) {
477
+ return JSON_SCHEMA_OUTPUT_REGISTRY.get(schema);
478
+ }
479
+ if (JSON_SCHEMA_REGISTRY.has(schema)) {
480
+ return JSON_SCHEMA_REGISTRY.get(schema);
481
+ }
482
+ const global = globalRegistry.get(schema);
483
+ if (global) {
484
+ return {
485
+ title: global.title,
486
+ description: global.description,
487
+ examples: Array.isArray(global.examples) ? global.examples : void 0
488
+ };
489
+ }
490
+ }
491
+ #handleArrayItemJsonSchema([required, schema], options) {
492
+ if (required || options.strategy === "input" || schema.default !== void 0) {
493
+ return schema;
494
+ }
495
+ if (schema === this.undefinedJsonSchema) {
496
+ return { type: "null" };
497
+ }
498
+ return {
499
+ anyOf: [
500
+ // schema can contain { type: 'null' } so we should use anyOf instead of oneOf
501
+ schema,
502
+ { type: "null" }
503
+ ]
504
+ };
505
+ }
506
+ #handleStringFormat(format) {
507
+ if (format === "guid") {
508
+ return JSONSchemaFormat.UUID;
509
+ }
510
+ if (format === "url") {
511
+ return JSONSchemaFormat.URI;
512
+ }
513
+ if (format === "datetime") {
514
+ return JSONSchemaFormat.DateTime;
515
+ }
516
+ return Object.values(JSONSchemaFormat).includes(format) ? format : void 0;
517
+ }
518
+ #handleContentEncoding(contentEncoding) {
519
+ return Object.values(JSONSchemaContentEncoding).includes(contentEncoding) ? contentEncoding : void 0;
520
+ }
521
+ }
522
+
523
+ export { JsonSchemaXNativeType as J, ZodToJsonSchemaConverter as Z, JSON_SCHEMA_REGISTRY as a, JSON_SCHEMA_INPUT_REGISTRY as b, JSON_SCHEMA_OUTPUT_REGISTRY as c, eventIterator as e, getEventIteratorSchemaDetails as g };
@@ -0,0 +1,156 @@
1
+ import { resolveMaybeOptionalOptions, toArray, value, runWithSpan, isAsyncIteratorObject, overlayProxy, asyncIteratorWithSpan } from '@temporary-name/shared';
2
+ import { HibernationEventIterator, mapEventIterator } from '@temporary-name/standard-server';
3
+ import { safeDecodeAsync, safeEncodeAsync } from '@temporary-name/zod';
4
+ import { u as unlazy, B as BadRequestError, V as ValidationError, I as InternalServerError } from './server.Bx9JTGMu.mjs';
5
+
6
+ function mergeCurrentContext(context, other) {
7
+ return { ...context, ...other };
8
+ }
9
+
10
+ function middlewareOutputFn(output) {
11
+ return { output, context: {} };
12
+ }
13
+
14
+ function createProcedureClient(lazyableProcedure, ...rest) {
15
+ const options = resolveMaybeOptionalOptions(rest);
16
+ return async (...[input, callerOptions]) => {
17
+ const path = toArray(options.path);
18
+ const { default: procedure } = await unlazy(lazyableProcedure);
19
+ const clientContext = callerOptions?.context ?? {};
20
+ const context = await value(options.context ?? {}, clientContext);
21
+ const output = await runWithSpan({ name: "call_procedure", signal: callerOptions?.signal }, (span) => {
22
+ span?.setAttribute("procedure.path", [...path]);
23
+ return executeProcedureInternal(procedure, input, {
24
+ context,
25
+ path,
26
+ procedure,
27
+ request: callerOptions?.request,
28
+ signal: callerOptions?.signal,
29
+ lastEventId: callerOptions?.lastEventId
30
+ });
31
+ });
32
+ if (isAsyncIteratorObject(output)) {
33
+ if (output instanceof HibernationEventIterator) {
34
+ return output;
35
+ }
36
+ return overlayProxy(
37
+ output,
38
+ mapEventIterator(
39
+ asyncIteratorWithSpan(
40
+ { name: "consume_event_iterator_output", signal: callerOptions?.signal },
41
+ output
42
+ ),
43
+ {
44
+ value: (v) => v,
45
+ error: async (e) => e
46
+ }
47
+ )
48
+ );
49
+ }
50
+ return output;
51
+ };
52
+ }
53
+ async function validateInput(procedure, input) {
54
+ const schemas = procedure["~orpc"].schemas;
55
+ return runWithSpan({ name: "validate_input" }, async () => {
56
+ const resultBody = await safeDecodeAsync(schemas.bodySchema, input.body, { parseType: "body" });
57
+ const resultPath = await safeDecodeAsync(schemas.pathSchema, input.path, { parseType: "path" });
58
+ const resultQuery = await safeDecodeAsync(schemas.querySchema, input.query, { parseType: "query" });
59
+ const issues = [];
60
+ if (!resultBody.success) {
61
+ issues.push(...resultBody.error.issues.map((i) => ({ ...i, path: ["body", ...i.path] })));
62
+ }
63
+ if (!resultPath.success) {
64
+ issues.push(...resultPath.error.issues.map((i) => ({ ...i, path: ["path", ...i.path] })));
65
+ }
66
+ if (!resultQuery.success) {
67
+ issues.push(...resultQuery.error.issues.map((i) => ({ ...i, path: ["query", ...i.path] })));
68
+ }
69
+ if (issues.length > 0) {
70
+ throw new BadRequestError({
71
+ code: "input_validation_failed",
72
+ message: "Input validation failed",
73
+ cause: new ValidationError({
74
+ issues,
75
+ data: input
76
+ })
77
+ });
78
+ }
79
+ const results = {
80
+ body: resultBody.data,
81
+ path: resultPath.data,
82
+ query: resultQuery.data
83
+ };
84
+ return results;
85
+ });
86
+ }
87
+ async function validateOutput(procedure, output) {
88
+ const schema = procedure["~orpc"].schemas.outputSchema;
89
+ if (!schema) {
90
+ return output;
91
+ }
92
+ return runWithSpan({ name: "validate_output" }, async () => {
93
+ const result = await safeEncodeAsync(schema, output, { parseType: "output" });
94
+ if (!result.success) {
95
+ throw new InternalServerError({
96
+ message: "Output validation failed",
97
+ cause: new ValidationError({
98
+ issues: result.error.issues,
99
+ data: output
100
+ })
101
+ });
102
+ }
103
+ return result.data;
104
+ });
105
+ }
106
+ async function executeProcedureInternal(procedure, input, options) {
107
+ const middlewares = procedure["~orpc"].middlewares;
108
+ const inputValidationIndex = Math.min(
109
+ Math.max(0, procedure["~orpc"].inputValidationIndex),
110
+ middlewares.length
111
+ );
112
+ const outputValidationIndex = Math.min(
113
+ Math.max(0, procedure["~orpc"].outputValidationIndex),
114
+ middlewares.length
115
+ );
116
+ const next = async (index, context, input2) => {
117
+ let currentInput = input2;
118
+ if (index === inputValidationIndex) {
119
+ currentInput = await validateInput(procedure, currentInput);
120
+ }
121
+ const mid = middlewares[index];
122
+ const output = mid ? await runWithSpan({ name: `middleware.${mid.name}`, signal: options.signal }, async (span) => {
123
+ span?.setAttribute("middleware.index", index);
124
+ span?.setAttribute("middleware.name", mid.name);
125
+ const result = await mid(
126
+ {
127
+ ...options,
128
+ context,
129
+ next: async (nextOptions) => {
130
+ const nextContext = nextOptions?.context ?? {};
131
+ return {
132
+ output: await next(index + 1, mergeCurrentContext(context, nextContext), currentInput),
133
+ // NB: Pretty sure this isn't used (or meant to be used) at runtime, it's just there
134
+ // to get type inference in the builder (via the caller returning the output of next() in
135
+ // the middleware function)
136
+ context: nextContext
137
+ };
138
+ }
139
+ },
140
+ currentInput,
141
+ middlewareOutputFn
142
+ );
143
+ return result.output;
144
+ }) : await runWithSpan(
145
+ { name: "handler", signal: options.signal },
146
+ () => procedure["~orpc"].handler(currentInput, { ...options, context })
147
+ );
148
+ if (index === outputValidationIndex) {
149
+ return await validateOutput(procedure, output);
150
+ }
151
+ return output;
152
+ };
153
+ return next(0, options.context, input);
154
+ }
155
+
156
+ export { middlewareOutputFn as a, createProcedureClient as c, mergeCurrentContext as m };