@trpc/server 9.24.0 → 9.25.2

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