@temporary-name/server 1.9.3-alpha.03f5d40e5b399f85012c2fb4e98167e26d551d36 → 1.9.3-alpha.102eab0800942eb736f7669a86c850ebfdfcd4a3

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 +55 -103
  14. package/dist/index.d.ts +55 -103
  15. package/dist/index.mjs +60 -223
  16. package/dist/openapi/index.d.mts +1 -1
  17. package/dist/openapi/index.d.ts +1 -1
  18. package/dist/openapi/index.mjs +60 -76
  19. package/dist/plugins/index.d.mts +2 -2
  20. package/dist/plugins/index.d.ts +2 -2
  21. package/dist/shared/{server.CbLTWfgn.d.mts → server.BCcLYvdF.d.mts} +1 -1
  22. package/dist/shared/server.CHV9AQHl.mjs +412 -0
  23. package/dist/shared/{server.Bk5r0-2R.d.ts → server.CdeqmULw.d.ts} +1 -1
  24. package/dist/shared/{server.Bs6ka_UE.d.mts → server.DHezmW6C.d.mts} +2 -2
  25. package/dist/shared/server.DN9mVGfv.mjs +11 -0
  26. package/dist/shared/{server.BVxcyR6X.mjs → server.DWwaAM-a.mjs} +12 -44
  27. package/dist/shared/{server.D2UFMrxf.d.ts → server.Da-qLzdU.d.ts} +2 -2
  28. package/dist/shared/{server.CZNLCQBm.d.mts → server.DecvGKtb.d.mts} +125 -75
  29. package/dist/shared/{server.CZNLCQBm.d.ts → server.DecvGKtb.d.ts} +125 -75
  30. package/dist/shared/{server.BEHw7Eyx.mjs → server.JtIZ8YG7.mjs} +1 -11
  31. package/package.json +10 -10
  32. package/dist/shared/server.DcfsPloY.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
- const LAZY_SYMBOL = Symbol("ORPC_LAZY_SYMBOL");
7
- function lazy(loader, meta = {}) {
8
- return {
9
- [LAZY_SYMBOL]: {
10
- loader,
11
- meta
12
- }
13
- };
14
- }
15
- function isLazy(item) {
16
- return (typeof item === "object" || typeof item === "function") && item !== null && LAZY_SYMBOL in item;
17
- }
18
- function getLazyMeta(lazied) {
19
- return lazied[LAZY_SYMBOL].meta;
20
- }
21
- function unlazy(lazied) {
22
- return isLazy(lazied) ? lazied[LAZY_SYMBOL].loader() : Promise.resolve({ default: lazied });
23
- }
24
-
25
- function mergeCurrentContext(context, other) {
26
- return { ...context, ...other };
27
- }
28
-
29
- function createORPCErrorConstructorMap(errors) {
30
- const proxy = new Proxy(errors, {
31
- get(target, code) {
32
- if (typeof code !== "string") {
33
- return Reflect.get(target, code);
34
- }
35
- const item = (...rest) => {
36
- const options = resolveMaybeOptionalOptions(rest);
37
- const config = errors[code];
38
- return new ORPCError(code, {
39
- defined: Boolean(config),
40
- status: config?.status,
41
- message: options.message ?? config?.message,
42
- data: options.data,
43
- cause: options.cause
44
- });
45
- };
46
- return item;
47
- }
48
- });
49
- return proxy;
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 };