@trpc/server 9.23.6 → 9.25.1

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 (51) hide show
  1. package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.d.ts +1 -0
  2. package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.dev.js +170 -0
  3. package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.js +7 -0
  4. package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.cjs.prod.js +170 -0
  5. package/adapters/aws-lambda/dist/trpc-server-adapters-aws-lambda.esm.js +166 -0
  6. package/adapters/aws-lambda/package.json +4 -0
  7. package/adapters/lambda/dist/trpc-server-adapters-lambda.cjs.d.ts +1 -0
  8. package/adapters/lambda/dist/trpc-server-adapters-lambda.cjs.dev.js +25 -0
  9. package/adapters/lambda/dist/trpc-server-adapters-lambda.cjs.js +7 -0
  10. package/adapters/lambda/dist/trpc-server-adapters-lambda.cjs.prod.js +25 -0
  11. package/adapters/lambda/dist/trpc-server-adapters-lambda.esm.js +21 -0
  12. package/adapters/lambda/package.json +4 -0
  13. package/dist/declarations/src/adapters/aws-lambda/index.d.ts +14 -0
  14. package/dist/declarations/src/adapters/aws-lambda/index.d.ts.map +1 -0
  15. package/dist/declarations/src/adapters/aws-lambda/utils.d.ts +34 -0
  16. package/dist/declarations/src/adapters/aws-lambda/utils.d.ts.map +1 -0
  17. package/dist/declarations/src/adapters/lambda/index.d.ts +12 -0
  18. package/dist/declarations/src/adapters/lambda/index.d.ts.map +1 -0
  19. package/dist/declarations/src/adapters/ws.d.ts +1 -1
  20. package/dist/declarations/src/deprecated/{LegacyRouter.d.ts → legacyRouter.d.ts} +1 -1
  21. package/dist/declarations/src/deprecated/legacyRouter.d.ts.map +1 -0
  22. package/dist/declarations/src/http/index.d.ts +1 -1
  23. package/dist/declarations/src/http/internals/types.d.ts +2 -3
  24. package/dist/declarations/src/http/internals/types.d.ts.map +1 -1
  25. package/dist/declarations/src/http/{ResponseMeta.d.ts → responseMeta.d.ts} +1 -1
  26. package/dist/declarations/src/http/responseMeta.d.ts.map +1 -0
  27. package/dist/declarations/src/index.d.ts +1 -1
  28. package/dist/declarations/src/internals/{BaseHandlerOptions.d.ts → baseHandlerOptions.d.ts} +2 -2
  29. package/dist/declarations/src/internals/{BaseHandlerOptions.d.ts.map → baseHandlerOptions.d.ts.map} +1 -1
  30. package/dist/declarations/src/internals/{OnErrorFunction.d.ts → onErrorFunction.d.ts} +1 -1
  31. package/dist/declarations/src/internals/{OnErrorFunction.d.ts.map → onErrorFunction.d.ts.map} +1 -1
  32. package/dist/router-2f54c292.cjs.dev.js +507 -0
  33. package/dist/router-459409c8.cjs.prod.js +503 -0
  34. package/dist/router-93491dae.esm.js +504 -0
  35. package/dist/trpc-server.cjs.dev.js +5 -503
  36. package/dist/trpc-server.cjs.prod.js +5 -499
  37. package/dist/trpc-server.esm.js +3 -503
  38. package/package.json +5 -2
  39. package/src/adapters/aws-lambda/index.ts +156 -0
  40. package/src/adapters/aws-lambda/utils.ts +91 -0
  41. package/src/adapters/lambda/index.ts +18 -0
  42. package/src/adapters/ws.ts +1 -1
  43. package/src/deprecated/{LegacyRouter.ts → legacyRouter.ts} +0 -0
  44. package/src/http/index.ts +1 -1
  45. package/src/http/internals/types.ts +2 -2
  46. package/src/http/{ResponseMeta.ts → responseMeta.ts} +0 -0
  47. package/src/index.ts +1 -1
  48. package/src/internals/{BaseHandlerOptions.ts → baseHandlerOptions.ts} +1 -1
  49. package/src/internals/{OnErrorFunction.ts → onErrorFunction.ts} +0 -0
  50. package/dist/declarations/src/deprecated/LegacyRouter.d.ts.map +0 -1
  51. package/dist/declarations/src/http/ResponseMeta.d.ts.map +0 -1
@@ -0,0 +1,504 @@
1
+ import { a as assertNotBrowser, g as getHTTPStatusCodeFromError } from './resolveHTTPResponse-ddb56677.esm.js';
2
+ import { T as TRPCError, g as getErrorFromUnknown } from './transformTRPCResponse-45c225c3.esm.js';
3
+ import { T as TRPC_ERROR_CODES_BY_KEY } from './codes-5678cc97.esm.js';
4
+
5
+ assertNotBrowser();
6
+
7
+ const middlewareMarker = 'middlewareMarker';
8
+
9
+ /* eslint-disable @typescript-eslint/no-explicit-any */
10
+ assertNotBrowser();
11
+
12
+ function getParseFn(procedureParser) {
13
+ const parser = procedureParser;
14
+
15
+ if (typeof parser === 'function') {
16
+ // ProcedureParserCustomValidatorEsque
17
+ return parser;
18
+ }
19
+
20
+ if (typeof parser.parseAsync === 'function') {
21
+ // ProcedureParserZodEsque
22
+ return parser.parseAsync.bind(parser);
23
+ }
24
+
25
+ if (typeof parser.parse === 'function') {
26
+ // ProcedureParserZodEsque
27
+ return parser.parse.bind(parser);
28
+ }
29
+
30
+ if (typeof parser.validateSync === 'function') {
31
+ // ProcedureParserYupEsque
32
+ return parser.validateSync.bind(parser);
33
+ }
34
+
35
+ if (typeof parser.create === 'function') {
36
+ // ProcedureParserSuperstructEsque
37
+ return parser.create.bind(parser);
38
+ }
39
+
40
+ throw new Error('Could not find a validator fn');
41
+ }
42
+ /**
43
+ * @internal
44
+ */
45
+
46
+
47
+ class Procedure {
48
+ constructor(opts) {
49
+ this.middlewares = void 0;
50
+ this.resolver = void 0;
51
+ this.inputParser = void 0;
52
+ this.parseInputFn = void 0;
53
+ this.outputParser = void 0;
54
+ this.parseOutputFn = void 0;
55
+ this.meta = void 0;
56
+ this.middlewares = opts.middlewares;
57
+ this.resolver = opts.resolver;
58
+ this.inputParser = opts.inputParser;
59
+ this.parseInputFn = getParseFn(this.inputParser);
60
+ this.outputParser = opts.outputParser;
61
+ this.parseOutputFn = getParseFn(this.outputParser);
62
+ this.meta = opts.meta;
63
+ }
64
+
65
+ async parseInput(rawInput) {
66
+ try {
67
+ return await this.parseInputFn(rawInput);
68
+ } catch (cause) {
69
+ throw new TRPCError({
70
+ code: 'BAD_REQUEST',
71
+ cause
72
+ });
73
+ }
74
+ }
75
+
76
+ async parseOutput(rawOutput) {
77
+ try {
78
+ return await this.parseOutputFn(rawOutput);
79
+ } catch (cause) {
80
+ throw new TRPCError({
81
+ code: 'INTERNAL_SERVER_ERROR',
82
+ cause,
83
+ message: 'Output validation failed'
84
+ });
85
+ }
86
+ }
87
+ /**
88
+ * Trigger middlewares in order, parse raw input, call resolver & parse raw output
89
+ * @internal
90
+ */
91
+
92
+
93
+ async call(opts) {
94
+ // wrap the actual resolver and treat as the last "middleware"
95
+ const middlewaresWithResolver = this.middlewares.concat([async ({
96
+ ctx
97
+ }) => {
98
+ const input = await this.parseInput(opts.rawInput);
99
+ const rawOutput = await this.resolver({ ...opts,
100
+ ctx,
101
+ input
102
+ });
103
+ const data = await this.parseOutput(rawOutput);
104
+ return {
105
+ marker: middlewareMarker,
106
+ ok: true,
107
+ data,
108
+ ctx
109
+ };
110
+ }]); // run the middlewares recursively with the resolver as the last one
111
+
112
+ const callRecursive = async (callOpts = {
113
+ index: 0,
114
+ ctx: opts.ctx
115
+ }) => {
116
+ try {
117
+ const result = await middlewaresWithResolver[callOpts.index]({
118
+ ctx: callOpts.ctx,
119
+ type: opts.type,
120
+ path: opts.path,
121
+ rawInput: opts.rawInput,
122
+ meta: this.meta,
123
+ next: async nextOpts => {
124
+ return await callRecursive({
125
+ index: callOpts.index + 1,
126
+ ctx: nextOpts ? nextOpts.ctx : callOpts.ctx
127
+ });
128
+ }
129
+ });
130
+ return result;
131
+ } catch (cause) {
132
+ return {
133
+ ctx: callOpts.ctx,
134
+ ok: false,
135
+ error: getErrorFromUnknown(cause),
136
+ marker: middlewareMarker
137
+ };
138
+ }
139
+ }; // there's always at least one "next" since we wrap this.resolver in a middleware
140
+
141
+
142
+ const result = await callRecursive();
143
+
144
+ if (!result) {
145
+ throw new TRPCError({
146
+ code: 'INTERNAL_SERVER_ERROR',
147
+ message: 'No result from middlewares - did you forget to `return next()`?'
148
+ });
149
+ }
150
+
151
+ if (!result.ok) {
152
+ // re-throw original error
153
+ throw result.error;
154
+ }
155
+
156
+ return result.data;
157
+ }
158
+ /**
159
+ * Create new procedure with passed middlewares
160
+ * @param middlewares
161
+ */
162
+
163
+
164
+ inheritMiddlewares(middlewares) {
165
+ const Constructor = this.constructor;
166
+ const instance = new Constructor({
167
+ middlewares: [...middlewares, ...this.middlewares],
168
+ resolver: this.resolver,
169
+ inputParser: this.inputParser,
170
+ outputParser: this.outputParser,
171
+ meta: this.meta
172
+ });
173
+ return instance;
174
+ }
175
+
176
+ }
177
+ function createProcedure(opts) {
178
+ const inputParser = 'input' in opts ? opts.input : input => {
179
+ if (input != null) {
180
+ throw new TRPCError({
181
+ code: 'BAD_REQUEST',
182
+ message: 'No input expected'
183
+ });
184
+ }
185
+
186
+ return undefined;
187
+ };
188
+ const outputParser = 'output' in opts && opts.output ? opts.output : output => output;
189
+ return new Procedure({
190
+ inputParser: inputParser,
191
+ resolver: opts.resolve,
192
+ middlewares: [],
193
+ outputParser: outputParser,
194
+ meta: opts.meta
195
+ });
196
+ }
197
+
198
+ /* eslint-disable @typescript-eslint/ban-types */
199
+ assertNotBrowser();
200
+ /**
201
+ * @public
202
+ */
203
+
204
+ function getDataTransformer(transformer) {
205
+ if ('input' in transformer) {
206
+ return transformer;
207
+ }
208
+
209
+ return {
210
+ input: transformer,
211
+ output: transformer
212
+ };
213
+ }
214
+ /**
215
+ * @internal
216
+ */
217
+
218
+
219
+ const PROCEDURE_DEFINITION_MAP = {
220
+ query: 'queries',
221
+ mutation: 'mutations',
222
+ subscription: 'subscriptions'
223
+ };
224
+ /**
225
+ * @internal
226
+ */
227
+
228
+ function safeObject(...args) {
229
+ return Object.assign(Object.create(null), ...args);
230
+ }
231
+
232
+ const defaultFormatter = ({
233
+ shape
234
+ }) => {
235
+ return shape;
236
+ };
237
+
238
+ const defaultTransformer = {
239
+ input: {
240
+ serialize: obj => obj,
241
+ deserialize: obj => obj
242
+ },
243
+ output: {
244
+ serialize: obj => obj,
245
+ deserialize: obj => obj
246
+ }
247
+ };
248
+
249
+ /**
250
+ * @internal The type signature of this class may change without warning.
251
+ */
252
+ class Router {
253
+ constructor(def) {
254
+ var _def$queries, _def$mutations, _def$subscriptions, _def$middlewares, _def$errorFormatter, _def$transformer;
255
+
256
+ this._def = void 0;
257
+ this._def = {
258
+ queries: (_def$queries = def === null || def === void 0 ? void 0 : def.queries) !== null && _def$queries !== void 0 ? _def$queries : safeObject(),
259
+ mutations: (_def$mutations = def === null || def === void 0 ? void 0 : def.mutations) !== null && _def$mutations !== void 0 ? _def$mutations : safeObject(),
260
+ subscriptions: (_def$subscriptions = def === null || def === void 0 ? void 0 : def.subscriptions) !== null && _def$subscriptions !== void 0 ? _def$subscriptions : safeObject(),
261
+ middlewares: (_def$middlewares = def === null || def === void 0 ? void 0 : def.middlewares) !== null && _def$middlewares !== void 0 ? _def$middlewares : [],
262
+ errorFormatter: (_def$errorFormatter = def === null || def === void 0 ? void 0 : def.errorFormatter) !== null && _def$errorFormatter !== void 0 ? _def$errorFormatter : defaultFormatter,
263
+ transformer: (_def$transformer = def === null || def === void 0 ? void 0 : def.transformer) !== null && _def$transformer !== void 0 ? _def$transformer : defaultTransformer
264
+ };
265
+ }
266
+
267
+ static prefixProcedures(procedures, prefix) {
268
+ const eps = safeObject();
269
+
270
+ for (const key in procedures) {
271
+ eps[prefix + key] = procedures[key];
272
+ }
273
+
274
+ return eps;
275
+ }
276
+
277
+ query(path, procedure) {
278
+ const router = new Router({
279
+ queries: safeObject({
280
+ [path]: createProcedure(procedure)
281
+ })
282
+ });
283
+ return this.merge(router);
284
+ }
285
+
286
+ mutation(path, procedure) {
287
+ const router = new Router({
288
+ mutations: safeObject({
289
+ [path]: createProcedure(procedure)
290
+ })
291
+ });
292
+ return this.merge(router);
293
+ }
294
+ /**
295
+ * @beta Might change without a major version bump
296
+ */
297
+
298
+
299
+ subscription(path, procedure) {
300
+ const router = new Router({
301
+ subscriptions: safeObject({
302
+ [path]: createProcedure(procedure)
303
+ })
304
+ });
305
+ return this.merge(router);
306
+ }
307
+ /**
308
+ * Merge router with other router
309
+ * @param router
310
+ */
311
+
312
+
313
+ merge(prefixOrRouter, maybeRouter) {
314
+ let prefix = '';
315
+ let childRouter;
316
+
317
+ if (typeof prefixOrRouter === 'string' && maybeRouter instanceof Router) {
318
+ prefix = prefixOrRouter;
319
+ childRouter = maybeRouter;
320
+ } else if (prefixOrRouter instanceof Router) {
321
+ childRouter = prefixOrRouter;
322
+ }
323
+ /* istanbul ignore next */
324
+ else {
325
+ throw new Error('Invalid args');
326
+ }
327
+
328
+ const duplicateQueries = Object.keys(childRouter._def.queries).filter(key => !!this._def['queries'][prefix + key]);
329
+ const duplicateMutations = Object.keys(childRouter._def.mutations).filter(key => !!this._def['mutations'][prefix + key]);
330
+ const duplicateSubscriptions = Object.keys(childRouter._def.subscriptions).filter(key => !!this._def['subscriptions'][prefix + key]);
331
+ const duplicates = [...duplicateQueries, ...duplicateMutations, ...duplicateSubscriptions];
332
+
333
+ if (duplicates.length) {
334
+ throw new Error(`Duplicate endpoint(s): ${duplicates.join(', ')}`);
335
+ }
336
+
337
+ const mergeProcedures = defs => {
338
+ const newDefs = safeObject();
339
+
340
+ for (const key in defs) {
341
+ const procedure = defs[key];
342
+ const newProcedure = procedure.inheritMiddlewares(this._def.middlewares);
343
+ newDefs[key] = newProcedure;
344
+ }
345
+
346
+ return Router.prefixProcedures(newDefs, prefix);
347
+ };
348
+
349
+ return new Router({ ...this._def,
350
+ queries: safeObject(this._def.queries, mergeProcedures(childRouter._def.queries)),
351
+ mutations: safeObject(this._def.mutations, mergeProcedures(childRouter._def.mutations)),
352
+ subscriptions: safeObject(this._def.subscriptions, mergeProcedures(childRouter._def.subscriptions))
353
+ });
354
+ }
355
+ /**
356
+ * Invoke procedure. Only for internal use within library.
357
+ */
358
+
359
+
360
+ async call(opts) {
361
+ const {
362
+ type,
363
+ path
364
+ } = opts;
365
+ const defTarget = PROCEDURE_DEFINITION_MAP[type];
366
+ const defs = this._def[defTarget];
367
+ const procedure = defs[path];
368
+
369
+ if (!procedure) {
370
+ throw new TRPCError({
371
+ code: 'NOT_FOUND',
372
+ message: `No "${type}"-procedure on path "${path}"`
373
+ });
374
+ }
375
+
376
+ return procedure.call(opts);
377
+ }
378
+
379
+ createCaller(ctx) {
380
+ return {
381
+ query: (path, ...args) => {
382
+ return this.call({
383
+ type: 'query',
384
+ ctx,
385
+ path,
386
+ rawInput: args[0]
387
+ });
388
+ },
389
+ mutation: (path, ...args) => {
390
+ return this.call({
391
+ type: 'mutation',
392
+ ctx,
393
+ path,
394
+ rawInput: args[0]
395
+ });
396
+ },
397
+ subscription: (path, ...args) => {
398
+ return this.call({
399
+ type: 'subscription',
400
+ ctx,
401
+ path,
402
+ rawInput: args[0]
403
+ });
404
+ }
405
+ };
406
+ }
407
+ /**
408
+ * Function to be called before any procedure is invoked
409
+ * @link https://trpc.io/docs/middlewares
410
+ */
411
+
412
+
413
+ middleware(middleware) {
414
+ return new Router({ ...this._def,
415
+ middlewares: [...this._def.middlewares, middleware]
416
+ });
417
+ }
418
+ /**
419
+ * Format errors
420
+ * @link https://trpc.io/docs/error-formatting
421
+ */
422
+
423
+
424
+ formatError(errorFormatter) {
425
+ if (this._def.errorFormatter !== defaultFormatter) {
426
+ throw new Error('You seem to have double `formatError()`-calls in your router tree');
427
+ }
428
+
429
+ return new Router({ ...this._def,
430
+ errorFormatter: errorFormatter
431
+ });
432
+ }
433
+
434
+ getErrorShape(opts) {
435
+ const {
436
+ path,
437
+ error
438
+ } = opts;
439
+ const {
440
+ code
441
+ } = opts.error;
442
+ const shape = {
443
+ message: error.message,
444
+ code: TRPC_ERROR_CODES_BY_KEY[code],
445
+ data: {
446
+ code,
447
+ httpStatus: getHTTPStatusCodeFromError(error)
448
+ }
449
+ };
450
+
451
+ if (process.env.NODE_ENV !== 'production' && typeof opts.error.stack === 'string') {
452
+ shape.data.stack = opts.error.stack;
453
+ }
454
+
455
+ if (typeof path === 'string') {
456
+ shape.data.path = path;
457
+ }
458
+
459
+ return this._def.errorFormatter({ ...opts,
460
+ shape
461
+ });
462
+ }
463
+ /**
464
+ * Add data transformer to serialize/deserialize input args + output
465
+ * @link https://trpc.io/docs/data-transformers
466
+ */
467
+
468
+
469
+ transformer(_transformer) {
470
+ const transformer = getDataTransformer(_transformer);
471
+
472
+ if (this._def.transformer !== defaultTransformer) {
473
+ throw new Error('You seem to have double `transformer()`-calls in your router tree');
474
+ }
475
+
476
+ return new Router({ ...this._def,
477
+ transformer
478
+ });
479
+ }
480
+ /**
481
+ * Flattens the generics of TQueries/TMutations/TSubscriptions.
482
+ * ⚠️ Experimental - might disappear. ⚠️
483
+ *
484
+ * @alpha
485
+ */
486
+
487
+
488
+ flat() {
489
+ return this;
490
+ }
491
+
492
+ }
493
+ /**
494
+ * Subclass of `VNextRouter` with `TInputContext` and `TContext` set to the same type, for backcompat.
495
+ *
496
+ * @deprecated
497
+ */
498
+
499
+ class LegacyRouter extends Router {}
500
+ function router() {
501
+ return new Router();
502
+ }
503
+
504
+ export { LegacyRouter as L, router as r };