@temporary-name/server 1.9.3-alpha.21b0289906d115f2f3db137ea407a6d8d50b5ad6 → 1.9.3-alpha.2957dbc009ec31fa21575f028b83c96651cba827

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 (39) hide show
  1. package/dist/adapters/aws-lambda/index.d.mts +3 -4
  2. package/dist/adapters/aws-lambda/index.d.ts +3 -4
  3. package/dist/adapters/aws-lambda/index.mjs +3 -3
  4. package/dist/adapters/fetch/index.d.mts +7 -84
  5. package/dist/adapters/fetch/index.d.ts +7 -84
  6. package/dist/adapters/fetch/index.mjs +15 -154
  7. package/dist/adapters/node/index.d.mts +7 -61
  8. package/dist/adapters/node/index.d.ts +7 -61
  9. package/dist/adapters/node/index.mjs +13 -119
  10. package/dist/adapters/standard/index.d.mts +4 -5
  11. package/dist/adapters/standard/index.d.ts +4 -5
  12. package/dist/adapters/standard/index.mjs +4 -4
  13. package/dist/helpers/index.mjs +3 -29
  14. package/dist/index.d.mts +60 -191
  15. package/dist/index.d.ts +60 -191
  16. package/dist/index.mjs +96 -104
  17. package/dist/openapi/index.d.mts +9 -26
  18. package/dist/openapi/index.d.ts +9 -26
  19. package/dist/openapi/index.mjs +6 -74
  20. package/dist/shared/{server.DmGicgbG.mjs → server.B7tjiDal.mjs} +46 -105
  21. package/dist/shared/server.C1RJffw4.mjs +30 -0
  22. package/dist/shared/server.CQIFwyhc.mjs +40 -0
  23. package/dist/shared/server.CpS0m3at.mjs +403 -0
  24. package/dist/shared/server.DPD7R7h_.d.mts +226 -0
  25. package/dist/shared/server.DPD7R7h_.d.ts +226 -0
  26. package/dist/shared/server.DfUs5c4R.d.ts +41 -0
  27. package/dist/shared/server.L8lRAYBR.d.mts +41 -0
  28. package/package.json +10 -27
  29. package/dist/plugins/index.d.mts +0 -159
  30. package/dist/plugins/index.d.ts +0 -159
  31. package/dist/plugins/index.mjs +0 -288
  32. package/dist/shared/server.7aL9gcoU.d.mts +0 -23
  33. package/dist/shared/server.BL2R5jcp.d.mts +0 -228
  34. package/dist/shared/server.BL2R5jcp.d.ts +0 -228
  35. package/dist/shared/server.CttFCjkj.mjs +0 -255
  36. package/dist/shared/server.D6Qs_UcF.d.mts +0 -55
  37. package/dist/shared/server.DFptr1Nz.d.ts +0 -23
  38. package/dist/shared/server.DpoO_ER_.d.ts +0 -55
  39. package/dist/shared/server.JtIZ8YG7.mjs +0 -237
@@ -1,7 +1,7 @@
1
- import { isContractProcedure, mergePrefix, mergeErrorMap, enhanceRoute, validateORPCError, ValidationError } from '@temporary-name/contract';
2
- import { resolveMaybeOptionalOptions, ORPCError, toArray, value, runWithSpan, intercept, isAsyncIteratorObject, overlayProxy, asyncIteratorWithSpan } from '@temporary-name/shared';
1
+ import { mergePrefix, enhanceRoute, ValidationError } from '@temporary-name/contract';
2
+ import { resolveMaybeOptionalOptions, toArray, value, runWithSpan, isAsyncIteratorObject, overlayProxy, asyncIteratorWithSpan, ORPCError } from '@temporary-name/shared';
3
3
  import { HibernationEventIterator, mapEventIterator } from '@temporary-name/standard-server';
4
- import { safeParseAsync } from '@temporary-name/zod';
4
+ import { safeDecodeAsync, safeEncodeAsync } from '@temporary-name/zod';
5
5
 
6
6
  function isStartWithMiddlewares(middlewares, compare) {
7
7
  if (compare.length > middlewares.length) {
@@ -27,7 +27,7 @@ function addMiddleware(middlewares, addition) {
27
27
  return [...middlewares, addition];
28
28
  }
29
29
 
30
- class Procedure {
30
+ class Contract {
31
31
  /**
32
32
  * This property holds the defined options.
33
33
  */
@@ -36,55 +36,17 @@ class Procedure {
36
36
  this["~orpc"] = def;
37
37
  }
38
38
  }
39
+ class Procedure extends Contract {
40
+ }
39
41
  function isProcedure(item) {
40
- if (item instanceof Procedure) {
41
- return true;
42
- }
43
- return isContractProcedure(item) && "middlewares" in item["~orpc"] && "handler" in item["~orpc"];
42
+ return item instanceof Procedure || // This is so we'll return true for Proxy-wrapped Procedures e.g. as returned by `callable`
43
+ (typeof item === "object" || typeof item === "function") && item !== null && "~orpc" in item && typeof item["~orpc"] === "object" && item["~orpc"] !== null && "route" in item["~orpc"] && "meta" in item["~orpc"] && "middlewares" in item["~orpc"] && "inputValidationIndex" in item["~orpc"] && "outputValidationIndex" in item["~orpc"] && "handler" in item["~orpc"];
44
44
  }
45
45
 
46
46
  function mergeCurrentContext(context, other) {
47
47
  return { ...context, ...other };
48
48
  }
49
49
 
50
- function createORPCErrorConstructorMap(errors) {
51
- const proxy = new Proxy(errors, {
52
- get(target, code) {
53
- if (typeof code !== "string") {
54
- return Reflect.get(target, code);
55
- }
56
- const item = (...rest) => {
57
- const options = resolveMaybeOptionalOptions(rest);
58
- const config = errors[code];
59
- return new ORPCError(code, {
60
- defined: Boolean(config),
61
- status: config?.status,
62
- message: options.message ?? config?.message,
63
- data: options.data,
64
- cause: options.cause
65
- });
66
- };
67
- return item;
68
- }
69
- });
70
- return proxy;
71
- }
72
-
73
- const HIDDEN_ROUTER_CONTRACT_SYMBOL = Symbol("ORPC_HIDDEN_ROUTER_CONTRACT");
74
- function setHiddenRouterContract(router, contract) {
75
- return new Proxy(router, {
76
- get(target, key) {
77
- if (key === HIDDEN_ROUTER_CONTRACT_SYMBOL) {
78
- return contract;
79
- }
80
- return Reflect.get(target, key);
81
- }
82
- });
83
- }
84
- function getHiddenRouterContract(router) {
85
- return router[HIDDEN_ROUTER_CONTRACT_SYMBOL];
86
- }
87
-
88
50
  function getRouter(router, path) {
89
51
  let current = router;
90
52
  for (let i = 0; i < path.length; i++) {
@@ -147,7 +109,6 @@ function enhanceRouter(router, options) {
147
109
  const enhanced2 = new Procedure({
148
110
  ...router["~orpc"],
149
111
  route: enhanceRoute(router["~orpc"].route, options),
150
- errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
151
112
  middlewares: newMiddlewares,
152
113
  inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
153
114
  outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
@@ -161,21 +122,19 @@ function enhanceRouter(router, options) {
161
122
  return enhanced;
162
123
  }
163
124
  function traverseContractProcedures(options, callback, lazyOptions = []) {
164
- let currentRouter = options.router;
165
- const hiddenContract = getHiddenRouterContract(options.router);
166
- if (hiddenContract !== void 0) {
167
- currentRouter = hiddenContract;
168
- }
125
+ const currentRouter = options.router;
169
126
  if (isLazy(currentRouter)) {
170
127
  lazyOptions.push({
171
128
  router: currentRouter,
172
129
  path: options.path
173
130
  });
174
- } else if (isContractProcedure(currentRouter)) {
131
+ } else if (currentRouter instanceof Contract) {
175
132
  callback({
176
133
  contract: currentRouter,
177
134
  path: options.path
178
135
  });
136
+ } else if (typeof currentRouter === "string") {
137
+ throw new Error("Unexpected: got string instead of router");
179
138
  } else {
180
139
  for (const key in currentRouter) {
181
140
  traverseContractProcedures(
@@ -228,7 +187,6 @@ function lazyInternal(loader, meta = {}) {
228
187
  function lazy(prefix, loader) {
229
188
  return enhanceRouter(lazyInternal(loader), {
230
189
  middlewares: [],
231
- errorMap: {},
232
190
  dedupeLeadingMiddlewares: true,
233
191
  prefix
234
192
  });
@@ -254,64 +212,44 @@ function createProcedureClient(lazyableProcedure, ...rest) {
254
212
  const { default: procedure } = await unlazy(lazyableProcedure);
255
213
  const clientContext = callerOptions?.context ?? {};
256
214
  const context = await value(options.context ?? {}, clientContext);
257
- const errors = createORPCErrorConstructorMap(procedure["~orpc"].errorMap);
258
- const validateError = async (e) => {
259
- if (e instanceof ORPCError) {
260
- return await validateORPCError(procedure["~orpc"].errorMap, e);
215
+ const output = await runWithSpan({ name: "call_procedure", signal: callerOptions?.signal }, (span) => {
216
+ span?.setAttribute("procedure.path", [...path]);
217
+ return executeProcedureInternal(procedure, input, {
218
+ context,
219
+ path,
220
+ procedure,
221
+ request: callerOptions?.request,
222
+ signal: callerOptions?.signal,
223
+ lastEventId: callerOptions?.lastEventId
224
+ });
225
+ });
226
+ if (isAsyncIteratorObject(output)) {
227
+ if (output instanceof HibernationEventIterator) {
228
+ return output;
261
229
  }
262
- return e;
263
- };
264
- try {
265
- const output = await runWithSpan({ name: "call_procedure", signal: callerOptions?.signal }, (span) => {
266
- span?.setAttribute("procedure.path", [...path]);
267
- return intercept(
268
- toArray(options.interceptors),
230
+ return overlayProxy(
231
+ output,
232
+ mapEventIterator(
233
+ asyncIteratorWithSpan(
234
+ { name: "consume_event_iterator_output", signal: callerOptions?.signal },
235
+ output
236
+ ),
269
237
  {
270
- context,
271
- input,
272
- errors,
273
- path,
274
- procedure,
275
- request: callerOptions?.request,
276
- signal: callerOptions?.signal,
277
- lastEventId: callerOptions?.lastEventId
278
- },
279
- (interceptorOptions) => {
280
- const { input: input2, ...opts } = interceptorOptions;
281
- return executeProcedureInternal(interceptorOptions.procedure, input2, opts);
238
+ value: (v) => v,
239
+ error: async (e) => e
282
240
  }
283
- );
284
- });
285
- if (isAsyncIteratorObject(output)) {
286
- if (output instanceof HibernationEventIterator) {
287
- return output;
288
- }
289
- return overlayProxy(
290
- output,
291
- mapEventIterator(
292
- asyncIteratorWithSpan(
293
- { name: "consume_event_iterator_output", signal: callerOptions?.signal },
294
- output
295
- ),
296
- {
297
- value: (v) => v,
298
- error: (e) => validateError(e)
299
- }
300
- )
301
- );
302
- }
303
- return output;
304
- } catch (e) {
305
- throw await validateError(e);
241
+ )
242
+ );
306
243
  }
244
+ return output;
307
245
  };
308
246
  }
309
247
  async function validateInput(procedure, input) {
310
248
  const schemas = procedure["~orpc"].schemas;
311
249
  return runWithSpan({ name: "validate_input" }, async () => {
312
- const resultBody = await safeParseAsync(schemas.bodySchema, input.body);
313
- const resultPath = await safeParseAsync(schemas.pathSchema, input.path);
314
- const resultQuery = await safeParseAsync(schemas.querySchema, input.query);
250
+ const resultBody = await safeDecodeAsync(schemas.bodySchema, input.body, { parseType: "body" });
251
+ const resultPath = await safeDecodeAsync(schemas.pathSchema, input.path, { parseType: "path" });
252
+ const resultQuery = await safeDecodeAsync(schemas.querySchema, input.query, { parseType: "query" });
315
253
  const issues = [];
316
254
  if (!resultBody.success) {
317
255
  issues.push(...resultBody.error.issues.map((i) => ({ ...i, path: ["body", ...i.path] })));
@@ -349,7 +287,7 @@ async function validateOutput(procedure, output) {
349
287
  return output;
350
288
  }
351
289
  return runWithSpan({ name: "validate_output" }, async () => {
352
- const result = await safeParseAsync(schema, output);
290
+ const result = await safeEncodeAsync(schema, output, { parseType: "output" });
353
291
  if (!result.success) {
354
292
  throw new ORPCError("INTERNAL_SERVER_ERROR", {
355
293
  message: "Output validation failed",
@@ -390,6 +328,9 @@ async function executeProcedureInternal(procedure, input, options) {
390
328
  const nextContext = nextOptions?.context ?? {};
391
329
  return {
392
330
  output: await next(index + 1, mergeCurrentContext(context, nextContext), currentInput),
331
+ // NB: Pretty sure this isn't used (or meant to be used) at runtime, it's just there
332
+ // to get type inference in the builder (via the caller returning the output of next() in
333
+ // the middleware function)
393
334
  context: nextContext
394
335
  };
395
336
  }
@@ -410,4 +351,4 @@ async function executeProcedureInternal(procedure, input, options) {
410
351
  return next(0, options.context, input);
411
352
  }
412
353
 
413
- export { LAZY_SYMBOL as L, Procedure as P, addMiddleware as a, isLazy as b, createProcedureClient as c, getRouter as d, enhanceRouter as e, createORPCErrorConstructorMap as f, getLazyMeta as g, lazy as h, isProcedure as i, middlewareOutputFn as j, isStartWithMiddlewares as k, lazyInternal as l, mergeCurrentContext as m, mergeMiddlewares as n, getHiddenRouterContract as o, createAccessibleLazyRouter as p, unlazyRouter as q, resolveContractProcedures as r, setHiddenRouterContract as s, traverseContractProcedures as t, unlazy as u };
354
+ export { Contract as C, LAZY_SYMBOL as L, Procedure as P, addMiddleware as a, isLazy as b, createProcedureClient as c, getRouter as d, enhanceRouter as e, lazy as f, getLazyMeta as g, middlewareOutputFn as h, isProcedure as i, isStartWithMiddlewares as j, mergeMiddlewares as k, lazyInternal as l, mergeCurrentContext as m, createAccessibleLazyRouter as n, unlazyRouter as o, resolveContractProcedures as r, traverseContractProcedures as t, unlazy as u };
@@ -0,0 +1,30 @@
1
+ import { parse, serialize } from 'cookie';
2
+
3
+ function setCookie(headers, name, value, options = {}) {
4
+ if (headers === void 0) {
5
+ return;
6
+ }
7
+ const cookieString = serialize(name, value, {
8
+ path: "/",
9
+ ...options
10
+ });
11
+ headers.append("Set-Cookie", cookieString);
12
+ }
13
+ function getCookie(headers, name, options = {}) {
14
+ if (headers === void 0) {
15
+ return void 0;
16
+ }
17
+ const cookieHeader = headers.get("cookie");
18
+ if (cookieHeader === null) {
19
+ return void 0;
20
+ }
21
+ return parse(cookieHeader, options)[name];
22
+ }
23
+ function deleteCookie(headers, name, options = {}) {
24
+ return setCookie(headers, name, "", {
25
+ ...options,
26
+ maxAge: 0
27
+ });
28
+ }
29
+
30
+ export { deleteCookie as d, getCookie as g, setCookie as s };
@@ -0,0 +1,40 @@
1
+ import { isObject } from '@temporary-name/shared';
2
+
3
+ function jsonSerialize(data, hasBlobRef = { value: false }) {
4
+ if (data instanceof Blob) {
5
+ hasBlobRef.value = true;
6
+ return [data, hasBlobRef.value];
7
+ }
8
+ if (data instanceof Set) {
9
+ return jsonSerialize(Array.from(data), hasBlobRef);
10
+ }
11
+ if (data instanceof Map) {
12
+ return jsonSerialize(Array.from(data.entries()), hasBlobRef);
13
+ }
14
+ if (Array.isArray(data)) {
15
+ const json = data.map((v) => v === void 0 ? null : jsonSerialize(v, hasBlobRef)[0]);
16
+ return [json, hasBlobRef.value];
17
+ }
18
+ if (isObject(data)) {
19
+ const json = {};
20
+ for (const k in data) {
21
+ if (k === "toJSON" && typeof data[k] === "function") {
22
+ continue;
23
+ }
24
+ json[k] = jsonSerialize(data[k], hasBlobRef)[0];
25
+ }
26
+ return [json, hasBlobRef.value];
27
+ }
28
+ if (typeof data === "bigint" || data instanceof RegExp || data instanceof URL) {
29
+ return [data.toString(), hasBlobRef.value];
30
+ }
31
+ if (data instanceof Date) {
32
+ return [Number.isNaN(data.getTime()) ? null : data.toISOString(), hasBlobRef.value];
33
+ }
34
+ if (Number.isNaN(data)) {
35
+ return [null, hasBlobRef.value];
36
+ }
37
+ return [data, hasBlobRef.value];
38
+ }
39
+
40
+ export { jsonSerialize as j };