@temporary-name/server 1.9.3-alpha.62445d8d52a6787e7750865cd468fca8cccd3e28 → 1.9.3-alpha.6a70825f6d8c3dc99b87e4e5b778aea0fa007417

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 (32) hide show
  1. package/dist/adapters/aws-lambda/index.d.mts +3 -3
  2. package/dist/adapters/aws-lambda/index.d.ts +3 -3
  3. package/dist/adapters/aws-lambda/index.mjs +4 -3
  4. package/dist/adapters/fetch/index.d.mts +3 -3
  5. package/dist/adapters/fetch/index.d.ts +3 -3
  6. package/dist/adapters/fetch/index.mjs +4 -3
  7. package/dist/adapters/node/index.d.mts +3 -3
  8. package/dist/adapters/node/index.d.ts +3 -3
  9. package/dist/adapters/node/index.mjs +4 -3
  10. package/dist/adapters/standard/index.d.mts +10 -5
  11. package/dist/adapters/standard/index.d.ts +10 -5
  12. package/dist/adapters/standard/index.mjs +4 -3
  13. package/dist/index.d.mts +51 -83
  14. package/dist/index.d.ts +51 -83
  15. package/dist/index.mjs +50 -213
  16. package/dist/openapi/index.d.mts +1 -1
  17. package/dist/openapi/index.d.ts +1 -1
  18. package/dist/openapi/index.mjs +52 -34
  19. package/dist/plugins/index.d.mts +2 -2
  20. package/dist/plugins/index.d.ts +2 -2
  21. package/dist/shared/{server.CT1xhSmE.d.mts → server.BCcLYvdF.d.mts} +1 -1
  22. package/dist/shared/server.CHV9AQHl.mjs +412 -0
  23. package/dist/shared/{server.cjcgLdr1.d.ts → server.CdeqmULw.d.ts} +1 -1
  24. package/dist/shared/{server.B93y_8tj.d.mts → server.DHezmW6C.d.mts} +2 -2
  25. package/dist/shared/server.DN9mVGfv.mjs +11 -0
  26. package/dist/shared/{server.CqTex_jI.mjs → server.DWwaAM-a.mjs} +13 -23
  27. package/dist/shared/{server.D_fags8X.d.ts → server.Da-qLzdU.d.ts} +2 -2
  28. package/dist/shared/{server.C3RuMHWl.d.mts → server.DecvGKtb.d.mts} +118 -68
  29. package/dist/shared/{server.C3RuMHWl.d.ts → server.DecvGKtb.d.ts} +118 -68
  30. package/dist/shared/{server.Kxw442A9.mjs → server.JtIZ8YG7.mjs} +1 -11
  31. package/package.json +10 -10
  32. package/dist/shared/server.BYYf0Wn6.mjs +0 -202
@@ -1,202 +0,0 @@
1
- import { validateORPCError, ValidationError } from '@temporary-name/contract';
2
- import { resolveMaybeOptionalOptions, ORPCError, toArray, value, runWithSpan, intercept, isAsyncIteratorObject, overlayProxy, asyncIteratorWithSpan } from '@temporary-name/shared';
3
- import { HibernationEventIterator, mapEventIterator } from '@temporary-name/standard-server';
4
- import { safeParseAsync } from '@temporary-name/zod';
5
-
6
- function mergeCurrentContext(context, other) {
7
- return { ...context, ...other };
8
- }
9
-
10
- function createORPCErrorConstructorMap(errors) {
11
- const proxy = new Proxy(errors, {
12
- get(target, code) {
13
- if (typeof code !== "string") {
14
- return Reflect.get(target, code);
15
- }
16
- const item = (...rest) => {
17
- const options = resolveMaybeOptionalOptions(rest);
18
- const config = errors[code];
19
- return new ORPCError(code, {
20
- defined: Boolean(config),
21
- status: config?.status,
22
- message: options.message ?? config?.message,
23
- data: options.data,
24
- cause: options.cause
25
- });
26
- };
27
- return item;
28
- }
29
- });
30
- return proxy;
31
- }
32
-
33
- const LAZY_SYMBOL = Symbol("ORPC_LAZY_SYMBOL");
34
- function lazy(loader, meta = {}) {
35
- return {
36
- [LAZY_SYMBOL]: {
37
- loader,
38
- meta
39
- }
40
- };
41
- }
42
- function isLazy(item) {
43
- return (typeof item === "object" || typeof item === "function") && item !== null && LAZY_SYMBOL in item;
44
- }
45
- function getLazyMeta(lazied) {
46
- return lazied[LAZY_SYMBOL].meta;
47
- }
48
- function unlazy(lazied) {
49
- return isLazy(lazied) ? lazied[LAZY_SYMBOL].loader() : Promise.resolve({ default: lazied });
50
- }
51
-
52
- function middlewareOutputFn(output) {
53
- return { output, context: {} };
54
- }
55
-
56
- function createProcedureClient(lazyableProcedure, ...rest) {
57
- const options = resolveMaybeOptionalOptions(rest);
58
- return async (...[input, callerOptions]) => {
59
- const path = toArray(options.path);
60
- const { default: procedure } = await unlazy(lazyableProcedure);
61
- const clientContext = callerOptions?.context ?? {};
62
- const context = await value(options.context ?? {}, clientContext);
63
- const errors = createORPCErrorConstructorMap(procedure["~orpc"].errorMap);
64
- const validateError = async (e) => {
65
- if (e instanceof ORPCError) {
66
- return await validateORPCError(procedure["~orpc"].errorMap, e);
67
- }
68
- return e;
69
- };
70
- try {
71
- const output = await runWithSpan({ name: "call_procedure", signal: callerOptions?.signal }, (span) => {
72
- span?.setAttribute("procedure.path", [...path]);
73
- return intercept(
74
- toArray(options.interceptors),
75
- {
76
- context,
77
- input,
78
- // input only optional when it undefinable so we can safely cast it
79
- errors,
80
- path,
81
- procedure,
82
- signal: callerOptions?.signal,
83
- lastEventId: callerOptions?.lastEventId
84
- },
85
- (interceptorOptions) => executeProcedureInternal(interceptorOptions.procedure, interceptorOptions)
86
- );
87
- });
88
- if (isAsyncIteratorObject(output)) {
89
- if (output instanceof HibernationEventIterator) {
90
- return output;
91
- }
92
- return overlayProxy(
93
- output,
94
- mapEventIterator(
95
- asyncIteratorWithSpan(
96
- { name: "consume_event_iterator_output", signal: callerOptions?.signal },
97
- output
98
- ),
99
- {
100
- value: (v) => v,
101
- error: (e) => validateError(e)
102
- }
103
- )
104
- );
105
- }
106
- return output;
107
- } catch (e) {
108
- throw await validateError(e);
109
- }
110
- };
111
- }
112
- async function validateInput(procedure, input) {
113
- const schema = procedure["~orpc"].inputSchema;
114
- if (!schema) {
115
- return input;
116
- }
117
- return runWithSpan({ name: "validate_input" }, async () => {
118
- const result = await safeParseAsync(schema, input);
119
- if (!result.success) {
120
- throw new ORPCError("BAD_REQUEST", {
121
- message: "Input validation failed",
122
- data: {
123
- issues: result.error.issues
124
- },
125
- cause: new ValidationError({
126
- message: "Input validation failed",
127
- issues: result.error.issues,
128
- data: input
129
- })
130
- });
131
- }
132
- return result.data;
133
- });
134
- }
135
- async function validateOutput(procedure, output) {
136
- const schema = procedure["~orpc"].outputSchema;
137
- if (!schema) {
138
- return output;
139
- }
140
- return runWithSpan({ name: "validate_output" }, async () => {
141
- const result = await safeParseAsync(schema, output);
142
- if (!result.success) {
143
- throw new ORPCError("INTERNAL_SERVER_ERROR", {
144
- message: "Output validation failed",
145
- cause: new ValidationError({
146
- message: "Output validation failed",
147
- issues: result.error.issues,
148
- data: output
149
- })
150
- });
151
- }
152
- return result.data;
153
- });
154
- }
155
- async function executeProcedureInternal(procedure, options) {
156
- const middlewares = procedure["~orpc"].middlewares;
157
- const inputValidationIndex = Math.min(
158
- Math.max(0, procedure["~orpc"].inputValidationIndex),
159
- middlewares.length
160
- );
161
- const outputValidationIndex = Math.min(
162
- Math.max(0, procedure["~orpc"].outputValidationIndex),
163
- middlewares.length
164
- );
165
- const next = async (index, context, input) => {
166
- let currentInput = input;
167
- if (index === inputValidationIndex) {
168
- currentInput = await validateInput(procedure, currentInput);
169
- }
170
- const mid = middlewares[index];
171
- const output = mid ? await runWithSpan({ name: `middleware.${mid.name}`, signal: options.signal }, async (span) => {
172
- span?.setAttribute("middleware.index", index);
173
- span?.setAttribute("middleware.name", mid.name);
174
- const result = await mid(
175
- {
176
- ...options,
177
- context,
178
- next: async (...[nextOptions]) => {
179
- const nextContext = nextOptions?.context ?? {};
180
- return {
181
- output: await next(index + 1, mergeCurrentContext(context, nextContext), currentInput),
182
- context: nextContext
183
- };
184
- }
185
- },
186
- currentInput,
187
- middlewareOutputFn
188
- );
189
- return result.output;
190
- }) : await runWithSpan(
191
- { name: "handler", signal: options.signal },
192
- () => procedure["~orpc"].handler({ ...options, context, input: currentInput })
193
- );
194
- if (index === outputValidationIndex) {
195
- return await validateOutput(procedure, output);
196
- }
197
- return output;
198
- };
199
- return next(0, options.context, options.input);
200
- }
201
-
202
- export { LAZY_SYMBOL as L, createORPCErrorConstructorMap as a, middlewareOutputFn as b, createProcedureClient as c, getLazyMeta as g, isLazy as i, lazy as l, mergeCurrentContext as m, unlazy as u };