@trpc/server 9.20.3 → 9.23.0

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 (101) hide show
  1. package/adapters/express/dist/trpc-server-adapters-express.cjs.dev.js +6 -6
  2. package/adapters/express/dist/trpc-server-adapters-express.cjs.prod.js +6 -6
  3. package/adapters/express/dist/trpc-server-adapters-express.esm.js +6 -6
  4. package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.dev.js +17 -10
  5. package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.prod.js +17 -10
  6. package/adapters/fastify/dist/trpc-server-adapters-fastify.esm.js +17 -10
  7. package/adapters/next/dist/trpc-server-adapters-next.cjs.dev.js +4 -4
  8. package/adapters/next/dist/trpc-server-adapters-next.cjs.prod.js +4 -4
  9. package/adapters/next/dist/trpc-server-adapters-next.esm.js +4 -4
  10. package/adapters/node-http/dist/trpc-server-adapters-node-http.cjs.dev.js +4 -4
  11. package/adapters/node-http/dist/trpc-server-adapters-node-http.cjs.prod.js +4 -4
  12. package/adapters/node-http/dist/trpc-server-adapters-node-http.esm.js +4 -4
  13. package/adapters/standalone/dist/trpc-server-adapters-standalone.cjs.dev.js +4 -4
  14. package/adapters/standalone/dist/trpc-server-adapters-standalone.cjs.prod.js +4 -4
  15. package/adapters/standalone/dist/trpc-server-adapters-standalone.esm.js +4 -4
  16. package/adapters/ws/dist/trpc-server-adapters-ws.cjs.dev.js +4 -4
  17. package/adapters/ws/dist/trpc-server-adapters-ws.cjs.prod.js +4 -4
  18. package/adapters/ws/dist/trpc-server-adapters-ws.esm.js +4 -4
  19. package/dist/{codes-33cef953.cjs.dev.js → codes-130e62df.cjs.dev.js} +2 -0
  20. package/dist/{codes-fe07bf82.esm.js → codes-5678cc97.esm.js} +2 -0
  21. package/dist/{codes-e0df67c4.cjs.prod.js → codes-aff770a3.cjs.prod.js} +2 -0
  22. package/dist/declarations/src/TRPCError.d.ts +1 -1
  23. package/dist/declarations/src/TRPCError.d.ts.map +1 -1
  24. package/dist/declarations/src/adapters/fastify/fastifyRequestHandler.d.ts +2 -2
  25. package/dist/declarations/src/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
  26. package/dist/declarations/src/adapters/fastify/fastifyTRPCPlugin.d.ts +1 -1
  27. package/dist/declarations/src/adapters/fastify/fastifyTRPCPlugin.d.ts.map +1 -1
  28. package/dist/declarations/src/adapters/next.d.ts +1 -1
  29. package/dist/declarations/src/adapters/next.d.ts.map +1 -1
  30. package/dist/declarations/src/adapters/node-http/nodeHTTPRequestHandler.d.ts +1 -1
  31. package/dist/declarations/src/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
  32. package/dist/declarations/src/adapters/standalone.d.ts.map +1 -1
  33. package/dist/declarations/src/adapters/ws.d.ts.map +1 -1
  34. package/dist/declarations/src/deprecated/createHttpServer.d.ts +1 -1
  35. package/dist/declarations/src/deprecated/createHttpServer.d.ts.map +1 -1
  36. package/dist/declarations/src/deprecated/requestHandler.d.ts +1 -1
  37. package/dist/declarations/src/deprecated/requestHandler.d.ts.map +1 -1
  38. package/dist/declarations/src/http/internals/getHTTPStatusCode.d.ts +2 -1
  39. package/dist/declarations/src/http/internals/getHTTPStatusCode.d.ts.map +1 -1
  40. package/dist/declarations/src/http/internals/types.d.ts +1 -1
  41. package/dist/declarations/src/http/internals/types.d.ts.map +1 -1
  42. package/dist/declarations/src/http/resolveHTTPResponse.d.ts +2 -2
  43. package/dist/declarations/src/http/resolveHTTPResponse.d.ts.map +1 -1
  44. package/dist/declarations/src/internals/BaseHandlerOptions.d.ts +1 -1
  45. package/dist/declarations/src/internals/BaseHandlerOptions.d.ts.map +1 -1
  46. package/dist/declarations/src/internals/OnErrorFunction.d.ts +1 -1
  47. package/dist/declarations/src/internals/OnErrorFunction.d.ts.map +1 -1
  48. package/dist/declarations/src/internals/middlewares.d.ts +3 -2
  49. package/dist/declarations/src/internals/middlewares.d.ts.map +1 -1
  50. package/dist/declarations/src/internals/procedure.d.ts +47 -34
  51. package/dist/declarations/src/internals/procedure.d.ts.map +1 -1
  52. package/dist/declarations/src/router.d.ts +34 -30
  53. package/dist/declarations/src/router.d.ts.map +1 -1
  54. package/dist/declarations/src/rpc/codes.d.ts +2 -0
  55. package/dist/declarations/src/rpc/codes.d.ts.map +1 -1
  56. package/dist/declarations/src/types.d.ts +8 -0
  57. package/dist/declarations/src/types.d.ts.map +1 -1
  58. package/dist/{nodeHTTPRequestHandler-2cc444d8.cjs.dev.js → nodeHTTPRequestHandler-36ddaba2.cjs.dev.js} +2 -2
  59. package/dist/{nodeHTTPRequestHandler-d00c1126.esm.js → nodeHTTPRequestHandler-372a4a4f.esm.js} +2 -2
  60. package/dist/{nodeHTTPRequestHandler-8ddacc19.cjs.prod.js → nodeHTTPRequestHandler-91e7ca3d.cjs.prod.js} +2 -2
  61. package/dist/{resolveHTTPResponse-562c3d62.cjs.prod.js → resolveHTTPResponse-02e61299.cjs.prod.js} +3 -2
  62. package/dist/{resolveHTTPResponse-3867844c.cjs.dev.js → resolveHTTPResponse-55b2c9bd.cjs.dev.js} +3 -2
  63. package/dist/{resolveHTTPResponse-85f3f5df.esm.js → resolveHTTPResponse-ddb56677.esm.js} +3 -2
  64. package/dist/{subscription-fbda2888.esm.js → subscription-2ea469a3.esm.js} +2 -2
  65. package/dist/{subscription-c2e0bfbc.cjs.prod.js → subscription-7de48383.cjs.dev.js} +2 -2
  66. package/dist/{subscription-b8831081.cjs.dev.js → subscription-f03bec71.cjs.prod.js} +2 -2
  67. package/dist/{transformTRPCResponse-8248515e.cjs.dev.js → transformTRPCResponse-1e125b28.cjs.prod.js} +38 -38
  68. package/dist/{transformTRPCResponse-f73dc4a2.esm.js → transformTRPCResponse-45c225c3.esm.js} +38 -38
  69. package/dist/{transformTRPCResponse-dcbf66c5.cjs.prod.js → transformTRPCResponse-c654ea92.cjs.dev.js} +38 -38
  70. package/dist/trpc-server.cjs.dev.js +45 -19
  71. package/dist/trpc-server.cjs.prod.js +45 -19
  72. package/dist/trpc-server.esm.js +47 -21
  73. package/package.json +4 -4
  74. package/rpc/dist/trpc-server-rpc.cjs.dev.js +1 -1
  75. package/rpc/dist/trpc-server-rpc.cjs.prod.js +1 -1
  76. package/rpc/dist/trpc-server-rpc.esm.js +1 -1
  77. package/src/TRPCError.ts +1 -1
  78. package/src/adapters/express.ts +2 -2
  79. package/src/adapters/fastify/fastifyRequestHandler.ts +7 -8
  80. package/src/adapters/fastify/fastifyTRPCPlugin.ts +14 -6
  81. package/src/adapters/next.ts +6 -2
  82. package/src/adapters/node-http/internals/getPostBody.test.ts +2 -1
  83. package/src/adapters/node-http/nodeHTTPRequestHandler.ts +5 -5
  84. package/src/adapters/standalone.ts +1 -0
  85. package/src/adapters/ws.ts +4 -4
  86. package/src/deprecated/createHttpServer.ts +2 -2
  87. package/src/deprecated/requestHandler.ts +2 -2
  88. package/src/http/internals/getHTTPStatusCode.ts +2 -1
  89. package/src/http/internals/types.ts +2 -2
  90. package/src/http/resolveHTTPResponse.ts +3 -3
  91. package/src/internals/BaseHandlerOptions.ts +1 -1
  92. package/src/internals/OnErrorFunction.ts +1 -1
  93. package/src/internals/middlewares.ts +3 -2
  94. package/src/internals/procedure.ts +176 -68
  95. package/src/router.ts +172 -52
  96. package/src/rpc/codes.ts +1 -0
  97. package/src/subscription.ts +1 -1
  98. package/src/types.ts +7 -0
  99. package/ws/dist/trpc-server-ws.cjs.dev.js +2 -2
  100. package/ws/dist/trpc-server-ws.cjs.prod.js +2 -2
  101. package/ws/dist/trpc-server-ws.esm.js +2 -2
@@ -2,12 +2,12 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var resolveHTTPResponse = require('./resolveHTTPResponse-562c3d62.cjs.prod.js');
6
- var subscription = require('./subscription-c2e0bfbc.cjs.prod.js');
7
- var transformTRPCResponse = require('./transformTRPCResponse-dcbf66c5.cjs.prod.js');
5
+ var resolveHTTPResponse = require('./resolveHTTPResponse-02e61299.cjs.prod.js');
6
+ var subscription = require('./subscription-f03bec71.cjs.prod.js');
7
+ var transformTRPCResponse = require('./transformTRPCResponse-1e125b28.cjs.prod.js');
8
8
  var adapters_standalone_dist_trpcServerAdaptersStandalone = require('../adapters/standalone/dist/trpc-server-adapters-standalone.cjs.prod.js');
9
- var nodeHTTPRequestHandler = require('./nodeHTTPRequestHandler-8ddacc19.cjs.prod.js');
10
- var codes = require('./codes-e0df67c4.cjs.prod.js');
9
+ var nodeHTTPRequestHandler = require('./nodeHTTPRequestHandler-91e7ca3d.cjs.prod.js');
10
+ var codes = require('./codes-aff770a3.cjs.prod.js');
11
11
  require('events');
12
12
  require('http');
13
13
  require('url');
@@ -32,6 +32,7 @@ const createHttpHandler = adapters_standalone_dist_trpcServerAdaptersStandalone.
32
32
  /**
33
33
  * @deprecated use `nodeHTTPRequestHandler` from `@trpc/server/adapters/node-http`
34
34
  */
35
+
35
36
  const requestHandler = nodeHTTPRequestHandler.nodeHTTPRequestHandler;
36
37
  /**
37
38
  * @deprecated use `NodeHTTPCreateContextFn` from `@trpc/server/adapters/node-http`
@@ -61,31 +62,31 @@ async function wrapCallSafe(fn) {
61
62
  /* eslint-disable @typescript-eslint/no-explicit-any */
62
63
  resolveHTTPResponse.assertNotBrowser();
63
64
 
64
- function getParseFn(inputParser) {
65
- const parser = inputParser;
65
+ function getParseFn(procedureParser) {
66
+ const parser = procedureParser;
66
67
 
67
68
  if (typeof parser === 'function') {
68
- // ProcedureInputParserCustomValidatorEsque
69
+ // ProcedureParserCustomValidatorEsque
69
70
  return parser;
70
71
  }
71
72
 
72
73
  if (typeof parser.parseAsync === 'function') {
73
- // ProcedureInputParserZodEsque
74
+ // ProcedureParserZodEsque
74
75
  return parser.parseAsync.bind(parser);
75
76
  }
76
77
 
77
78
  if (typeof parser.parse === 'function') {
78
- // ProcedureInputParserZodEsque
79
+ // ProcedureParserZodEsque
79
80
  return parser.parse.bind(parser);
80
81
  }
81
82
 
82
83
  if (typeof parser.validateSync === 'function') {
83
- // ProcedureInputParserYupEsque
84
+ // ProcedureParserYupEsque
84
85
  return parser.validateSync.bind(parser);
85
86
  }
86
87
 
87
88
  if (typeof parser.create === 'function') {
88
- // ProcedureInputParserSuperstructEsque
89
+ // ProcedureParserSuperstructEsque
89
90
  return parser.create.bind(parser);
90
91
  }
91
92
 
@@ -101,16 +102,22 @@ class Procedure {
101
102
  this.middlewares = void 0;
102
103
  this.resolver = void 0;
103
104
  this.inputParser = void 0;
104
- this.parse = void 0;
105
+ this.parseInputFn = void 0;
106
+ this.outputParser = void 0;
107
+ this.parseOutputFn = void 0;
108
+ this.meta = void 0;
105
109
  this.middlewares = opts.middlewares;
106
110
  this.resolver = opts.resolver;
107
111
  this.inputParser = opts.inputParser;
108
- this.parse = getParseFn(this.inputParser);
112
+ this.parseInputFn = getParseFn(this.inputParser);
113
+ this.outputParser = opts.outputParser;
114
+ this.parseOutputFn = getParseFn(this.outputParser);
115
+ this.meta = opts.meta;
109
116
  }
110
117
 
111
118
  async parseInput(rawInput) {
112
119
  try {
113
- return await this.parse(rawInput);
120
+ return await this.parseInputFn(rawInput);
114
121
  } catch (cause) {
115
122
  throw new transformTRPCResponse.TRPCError({
116
123
  code: 'BAD_REQUEST',
@@ -118,8 +125,20 @@ class Procedure {
118
125
  });
119
126
  }
120
127
  }
128
+
129
+ async parseOutput(rawOutput) {
130
+ try {
131
+ return await this.parseOutputFn(rawOutput);
132
+ } catch (cause) {
133
+ throw new transformTRPCResponse.TRPCError({
134
+ code: 'INTERNAL_SERVER_ERROR',
135
+ cause,
136
+ message: 'Output validation failed'
137
+ });
138
+ }
139
+ }
121
140
  /**
122
- * Trigger middlewares in order, parse raw input & call resolver
141
+ * Trigger middlewares in order, parse raw input, call resolver & parse raw output
123
142
  * @internal
124
143
  */
125
144
 
@@ -130,10 +149,11 @@ class Procedure {
130
149
  ctx
131
150
  }) => {
132
151
  const input = await this.parseInput(opts.rawInput);
133
- const data = await this.resolver({ ...opts,
152
+ const rawOutput = await this.resolver({ ...opts,
134
153
  ctx,
135
154
  input
136
155
  });
156
+ const data = await this.parseOutput(rawOutput);
137
157
  return {
138
158
  marker: middlewareMarker,
139
159
  ok: true,
@@ -149,6 +169,7 @@ class Procedure {
149
169
  type: opts.type,
150
170
  path: opts.path,
151
171
  rawInput: opts.rawInput,
172
+ meta: this.meta,
152
173
  next: nextFns[index + 1]
153
174
  }));
154
175
 
@@ -190,7 +211,9 @@ class Procedure {
190
211
  const instance = new Constructor({
191
212
  middlewares: [...middlewares, ...this.middlewares],
192
213
  resolver: this.resolver,
193
- inputParser: this.inputParser
214
+ inputParser: this.inputParser,
215
+ outputParser: this.outputParser,
216
+ meta: this.meta
194
217
  });
195
218
  return instance;
196
219
  }
@@ -207,10 +230,13 @@ function createProcedure(opts) {
207
230
 
208
231
  return undefined;
209
232
  };
233
+ const outputParser = 'output' in opts && opts.output ? opts.output : output => output;
210
234
  return new Procedure({
211
235
  inputParser: inputParser,
212
236
  resolver: opts.resolve,
213
- middlewares: []
237
+ middlewares: [],
238
+ outputParser: outputParser,
239
+ meta: opts.meta
214
240
  });
215
241
  }
216
242
 
@@ -1,11 +1,11 @@
1
- import { a as assertNotBrowser, g as getHTTPStatusCodeFromError } from './resolveHTTPResponse-85f3f5df.esm.js';
2
- export { a as assertNotBrowser, r as resolveHTTPResponse } from './resolveHTTPResponse-85f3f5df.esm.js';
3
- export { S as Subscription, s as subscriptionPullFactory } from './subscription-fbda2888.esm.js';
4
- import { T as TRPCError, g as getErrorFromUnknown } from './transformTRPCResponse-f73dc4a2.esm.js';
5
- export { T as TRPCError } from './transformTRPCResponse-f73dc4a2.esm.js';
1
+ import { a as assertNotBrowser, g as getHTTPStatusCodeFromError } from './resolveHTTPResponse-ddb56677.esm.js';
2
+ export { a as assertNotBrowser, r as resolveHTTPResponse } from './resolveHTTPResponse-ddb56677.esm.js';
3
+ export { S as Subscription, s as subscriptionPullFactory } from './subscription-2ea469a3.esm.js';
4
+ import { T as TRPCError, g as getErrorFromUnknown } from './transformTRPCResponse-45c225c3.esm.js';
5
+ export { T as TRPCError } from './transformTRPCResponse-45c225c3.esm.js';
6
6
  import { createHTTPServer, createHTTPHandler } from '../adapters/standalone/dist/trpc-server-adapters-standalone.esm.js';
7
- import { n as nodeHTTPRequestHandler } from './nodeHTTPRequestHandler-d00c1126.esm.js';
8
- import { T as TRPC_ERROR_CODES_BY_KEY } from './codes-fe07bf82.esm.js';
7
+ import { n as nodeHTTPRequestHandler } from './nodeHTTPRequestHandler-372a4a4f.esm.js';
8
+ import { T as TRPC_ERROR_CODES_BY_KEY } from './codes-5678cc97.esm.js';
9
9
  import 'events';
10
10
  import 'http';
11
11
  import 'url';
@@ -30,6 +30,7 @@ const createHttpHandler = createHTTPHandler;
30
30
  /**
31
31
  * @deprecated use `nodeHTTPRequestHandler` from `@trpc/server/adapters/node-http`
32
32
  */
33
+
33
34
  const requestHandler = nodeHTTPRequestHandler;
34
35
  /**
35
36
  * @deprecated use `NodeHTTPCreateContextFn` from `@trpc/server/adapters/node-http`
@@ -59,31 +60,31 @@ async function wrapCallSafe(fn) {
59
60
  /* eslint-disable @typescript-eslint/no-explicit-any */
60
61
  assertNotBrowser();
61
62
 
62
- function getParseFn(inputParser) {
63
- const parser = inputParser;
63
+ function getParseFn(procedureParser) {
64
+ const parser = procedureParser;
64
65
 
65
66
  if (typeof parser === 'function') {
66
- // ProcedureInputParserCustomValidatorEsque
67
+ // ProcedureParserCustomValidatorEsque
67
68
  return parser;
68
69
  }
69
70
 
70
71
  if (typeof parser.parseAsync === 'function') {
71
- // ProcedureInputParserZodEsque
72
+ // ProcedureParserZodEsque
72
73
  return parser.parseAsync.bind(parser);
73
74
  }
74
75
 
75
76
  if (typeof parser.parse === 'function') {
76
- // ProcedureInputParserZodEsque
77
+ // ProcedureParserZodEsque
77
78
  return parser.parse.bind(parser);
78
79
  }
79
80
 
80
81
  if (typeof parser.validateSync === 'function') {
81
- // ProcedureInputParserYupEsque
82
+ // ProcedureParserYupEsque
82
83
  return parser.validateSync.bind(parser);
83
84
  }
84
85
 
85
86
  if (typeof parser.create === 'function') {
86
- // ProcedureInputParserSuperstructEsque
87
+ // ProcedureParserSuperstructEsque
87
88
  return parser.create.bind(parser);
88
89
  }
89
90
 
@@ -99,16 +100,22 @@ class Procedure {
99
100
  this.middlewares = void 0;
100
101
  this.resolver = void 0;
101
102
  this.inputParser = void 0;
102
- this.parse = void 0;
103
+ this.parseInputFn = void 0;
104
+ this.outputParser = void 0;
105
+ this.parseOutputFn = void 0;
106
+ this.meta = void 0;
103
107
  this.middlewares = opts.middlewares;
104
108
  this.resolver = opts.resolver;
105
109
  this.inputParser = opts.inputParser;
106
- this.parse = getParseFn(this.inputParser);
110
+ this.parseInputFn = getParseFn(this.inputParser);
111
+ this.outputParser = opts.outputParser;
112
+ this.parseOutputFn = getParseFn(this.outputParser);
113
+ this.meta = opts.meta;
107
114
  }
108
115
 
109
116
  async parseInput(rawInput) {
110
117
  try {
111
- return await this.parse(rawInput);
118
+ return await this.parseInputFn(rawInput);
112
119
  } catch (cause) {
113
120
  throw new TRPCError({
114
121
  code: 'BAD_REQUEST',
@@ -116,8 +123,20 @@ class Procedure {
116
123
  });
117
124
  }
118
125
  }
126
+
127
+ async parseOutput(rawOutput) {
128
+ try {
129
+ return await this.parseOutputFn(rawOutput);
130
+ } catch (cause) {
131
+ throw new TRPCError({
132
+ code: 'INTERNAL_SERVER_ERROR',
133
+ cause,
134
+ message: 'Output validation failed'
135
+ });
136
+ }
137
+ }
119
138
  /**
120
- * Trigger middlewares in order, parse raw input & call resolver
139
+ * Trigger middlewares in order, parse raw input, call resolver & parse raw output
121
140
  * @internal
122
141
  */
123
142
 
@@ -128,10 +147,11 @@ class Procedure {
128
147
  ctx
129
148
  }) => {
130
149
  const input = await this.parseInput(opts.rawInput);
131
- const data = await this.resolver({ ...opts,
150
+ const rawOutput = await this.resolver({ ...opts,
132
151
  ctx,
133
152
  input
134
153
  });
154
+ const data = await this.parseOutput(rawOutput);
135
155
  return {
136
156
  marker: middlewareMarker,
137
157
  ok: true,
@@ -147,6 +167,7 @@ class Procedure {
147
167
  type: opts.type,
148
168
  path: opts.path,
149
169
  rawInput: opts.rawInput,
170
+ meta: this.meta,
150
171
  next: nextFns[index + 1]
151
172
  }));
152
173
 
@@ -188,7 +209,9 @@ class Procedure {
188
209
  const instance = new Constructor({
189
210
  middlewares: [...middlewares, ...this.middlewares],
190
211
  resolver: this.resolver,
191
- inputParser: this.inputParser
212
+ inputParser: this.inputParser,
213
+ outputParser: this.outputParser,
214
+ meta: this.meta
192
215
  });
193
216
  return instance;
194
217
  }
@@ -205,10 +228,13 @@ function createProcedure(opts) {
205
228
 
206
229
  return undefined;
207
230
  };
231
+ const outputParser = 'output' in opts && opts.output ? opts.output : output => output;
208
232
  return new Procedure({
209
233
  inputParser: inputParser,
210
234
  resolver: opts.resolve,
211
- middlewares: []
235
+ middlewares: [],
236
+ outputParser: outputParser,
237
+ meta: opts.meta
212
238
  });
213
239
  }
214
240
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trpc/server",
3
- "version": "9.20.3",
3
+ "version": "9.23.0",
4
4
  "description": "tRPC Server",
5
5
  "author": "KATT",
6
6
  "license": "MIT",
@@ -48,7 +48,7 @@
48
48
  "devDependencies": {
49
49
  "@testing-library/jest-dom": "^5.14.1",
50
50
  "@testing-library/react": "^12.0.0",
51
- "@testing-library/user-event": "^13.2.1",
51
+ "@testing-library/user-event": "^14.1.0",
52
52
  "@types/express": "^4.17.12",
53
53
  "@types/hash-sum": "^1.0.0",
54
54
  "@types/ws": "^8.2.0",
@@ -62,7 +62,7 @@
62
62
  "jest": "^27.1.0",
63
63
  "konn": "^0.7.0",
64
64
  "myzod": "^1.3.1",
65
- "next": "^12.0.1",
65
+ "next": "12.1.4",
66
66
  "node-fetch": "^2.6.1",
67
67
  "superstruct": "^0.15.2",
68
68
  "typescript": "4.4.4",
@@ -70,5 +70,5 @@
70
70
  "yup": "^0.32.8",
71
71
  "zod": "^3.0.0"
72
72
  },
73
- "gitHead": "79f545134b8a30b7b3f866dc2dd219303d9e8c05"
73
+ "gitHead": "f672e66bfa5b19dbfa4c981eb0819b0147f8f404"
74
74
  }
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var codes = require('../../dist/codes-33cef953.cjs.dev.js');
5
+ var codes = require('../../dist/codes-130e62df.cjs.dev.js');
6
6
 
7
7
 
8
8
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var codes = require('../../dist/codes-e0df67c4.cjs.prod.js');
5
+ var codes = require('../../dist/codes-aff770a3.cjs.prod.js');
6
6
 
7
7
 
8
8
 
@@ -1 +1 @@
1
- export { T as TRPC_ERROR_CODES_BY_KEY, a as TRPC_ERROR_CODES_BY_NUMBER } from '../../dist/codes-fe07bf82.esm.js';
1
+ export { T as TRPC_ERROR_CODES_BY_KEY, a as TRPC_ERROR_CODES_BY_NUMBER } from '../../dist/codes-5678cc97.esm.js';
package/src/TRPCError.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { TRPC_ERROR_CODE_KEY } from './rpc/codes';
2
1
  import { getMessageFromUnkownError } from './internals/errors';
2
+ import { TRPC_ERROR_CODE_KEY } from './rpc/codes';
3
3
 
4
4
  export class TRPCError extends Error {
5
5
  /**
@@ -15,10 +15,10 @@ export type CreateExpressContextOptions = NodeHTTPCreateContextFnOptions<
15
15
  export function createExpressMiddleware<TRouter extends AnyRouter>(
16
16
  opts: NodeHTTPHandlerOptions<TRouter, express.Request, express.Response>,
17
17
  ): express.Handler {
18
- return (req, res) => {
18
+ return async (req, res) => {
19
19
  const endpoint = req.path.slice(1);
20
20
 
21
- nodeHTTPRequestHandler({
21
+ await nodeHTTPRequestHandler({
22
22
  ...opts,
23
23
  req,
24
24
  res,
@@ -1,13 +1,13 @@
1
- import { URLSearchParams } from 'url';
2
1
  import { FastifyReply, FastifyRequest } from 'fastify';
2
+ import { URLSearchParams } from 'url';
3
3
  import { assertNotBrowser } from '../../assertNotBrowser';
4
- import { NodeHTTPCreateContextOption } from '../node-http';
5
- import { AnyRouter, inferRouterContext } from '../../router';
6
- import { resolveHTTPResponse } from '../../http/resolveHTTPResponse';
7
4
  import {
8
5
  HTTPBaseHandlerOptions,
9
6
  HTTPRequest,
10
7
  } from '../../http/internals/types';
8
+ import { resolveHTTPResponse } from '../../http/resolveHTTPResponse';
9
+ import { AnyRouter, inferRouterContext } from '../../router';
10
+ import { NodeHTTPCreateContextOption } from '../node-http';
11
11
 
12
12
  assertNotBrowser();
13
13
 
@@ -67,13 +67,12 @@ export async function fastifyRequestHandler<
67
67
  if ('status' in result && (!res.statusCode || res.statusCode === 200)) {
68
68
  res.statusCode = result.status;
69
69
  }
70
-
71
70
  for (const [key, value] of Object.entries(result.headers ?? {})) {
72
71
  if (typeof value === 'undefined') {
73
72
  continue;
74
73
  }
75
- res.header(key, value);
76
- }
77
74
 
78
- res.send(result.body);
75
+ void res.header(key, value);
76
+ }
77
+ await res.send(result.body);
79
78
  }
@@ -1,10 +1,10 @@
1
1
  /// <reference types="fastify-websocket" />
2
2
  import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
3
- import { fastifyRequestHandler } from './fastifyRequestHandler';
4
- import { NodeHTTPCreateContextFnOptions } from '../node-http';
5
- import { applyWSSHandler, WSSHandlerOptions } from '../ws';
6
3
  import { FastifyHandlerOptions } from '.';
7
4
  import { AnyRouter } from '../..';
5
+ import { NodeHTTPCreateContextFnOptions } from '../node-http';
6
+ import { WSSHandlerOptions, applyWSSHandler } from '../ws';
7
+ import { fastifyRequestHandler } from './fastifyRequestHandler';
8
8
 
9
9
  export interface FastifyTRPCPluginOptions<TRouter extends AnyRouter> {
10
10
  prefix?: string;
@@ -30,9 +30,17 @@ export function fastifyTRPCPlugin<TRouter extends AnyRouter>(
30
30
  },
31
31
  );
32
32
 
33
- fastify.all(`${opts.prefix ?? ''}/:path`, (req, res) => {
33
+ let prefix = opts.prefix ?? '';
34
+
35
+ // https://github.com/fastify/fastify-plugin/blob/fe079bef6557a83794bf437e14b9b9edb8a74104/plugin.js#L11
36
+ // @ts-expect-error property 'default' does not exists on type ...
37
+ if (typeof fastifyTRPCPlugin.default !== 'function') {
38
+ prefix = ''; // handled by fastify internally
39
+ }
40
+
41
+ fastify.all(`${prefix}/:path`, async (req, res) => {
34
42
  const path = (req.params as any).path;
35
- fastifyRequestHandler({ ...opts.trpcOptions, req, res, path });
43
+ await fastifyRequestHandler({ ...opts.trpcOptions, req, res, path });
36
44
  });
37
45
 
38
46
  if (opts.useWSS) {
@@ -41,7 +49,7 @@ export function fastifyTRPCPlugin<TRouter extends AnyRouter>(
41
49
  wss: fastify.websocketServer,
42
50
  });
43
51
  // eslint-disable-next-line @typescript-eslint/no-empty-function
44
- fastify.get(opts.prefix ?? '/', { websocket: true }, () => {});
52
+ fastify.get(prefix ?? '/', { websocket: true }, () => {});
45
53
  }
46
54
 
47
55
  done();
@@ -1,9 +1,13 @@
1
1
  /* eslint-disable @typescript-eslint/no-explicit-any */
2
2
  // eslint-disable-next-line @typescript-eslint/ban-ts-comment
3
- import type { NextApiHandler, NextApiRequest, NextApiResponse } from 'next';
3
+ import type {
4
+ NextApiHandler,
5
+ NextApiRequest,
6
+ NextApiResponse,
7
+ } from 'next/types';
8
+ import { TRPCError } from '../TRPCError';
4
9
  import { AnyRouter } from '../router';
5
10
  import { TRPCErrorResponse } from '../rpc';
6
- import { TRPCError } from '../TRPCError';
7
11
  import { nodeHTTPRequestHandler } from './node-http';
8
12
  import {
9
13
  NodeHTTPCreateContextFnOptions,
@@ -1,5 +1,6 @@
1
- import { getPostBody } from './getPostBody';
2
1
  import { EventEmitter } from 'events';
2
+ import { getPostBody } from './getPostBody';
3
+
3
4
  test('has body', async () => {
4
5
  const body = {};
5
6
  const resolvedBody = await getPostBody({ req: { body } } as any);
@@ -1,15 +1,15 @@
1
1
  /* eslint-disable @typescript-eslint/no-non-null-assertion */
2
2
  import { URLSearchParams } from 'url';
3
3
  import { assertNotBrowser } from '../../assertNotBrowser';
4
+ import { HTTPRequest } from '../../http/internals/types';
5
+ import { resolveHTTPResponse } from '../../http/resolveHTTPResponse';
6
+ import { AnyRouter, inferRouterContext } from '../../router';
7
+ import { getPostBody } from './internals/getPostBody';
4
8
  import {
9
+ NodeHTTPHandlerOptions,
5
10
  NodeHTTPRequest,
6
11
  NodeHTTPResponse,
7
- NodeHTTPHandlerOptions,
8
12
  } from './types';
9
- import { AnyRouter, inferRouterContext } from '../../router';
10
- import { getPostBody } from './internals/getPostBody';
11
- import { HTTPRequest } from '../../http/internals/types';
12
- import { resolveHTTPResponse } from '../../http/resolveHTTPResponse';
13
13
 
14
14
  assertNotBrowser();
15
15
 
@@ -1,4 +1,5 @@
1
1
  /* eslint-disable @typescript-eslint/no-non-null-assertion */
2
+
2
3
  /* eslint-disable @typescript-eslint/no-explicit-any */
3
4
  import http from 'http';
4
5
  import url from 'url';
@@ -1,10 +1,11 @@
1
1
  import http from 'http';
2
2
  import ws from 'ws';
3
+ import { TRPCError } from '../TRPCError';
3
4
  import { BaseHandlerOptions } from '../internals/BaseHandlerOptions';
4
5
  import { callProcedure } from '../internals/callProcedure';
5
6
  import { getErrorFromUnknown } from '../internals/errors';
6
7
  import { transformTRPCResponse } from '../internals/transformTRPCResponse';
7
- import { AnyRouter, inferRouterContext, ProcedureType } from '../router';
8
+ import { AnyRouter, ProcedureType, inferRouterContext } from '../router';
8
9
  import {
9
10
  TRPCErrorResponse,
10
11
  TRPCReconnectNotification,
@@ -13,7 +14,6 @@ import {
13
14
  } from '../rpc';
14
15
  import { Subscription } from '../subscription';
15
16
  import { CombinedDataTransformer } from '../transformer';
16
- import { TRPCError } from '../TRPCError';
17
17
  import { NodeHTTPCreateContextOption } from './node-http';
18
18
 
19
19
  /* istanbul ignore next */
@@ -96,7 +96,7 @@ export function applyWSSHandler<TRouter extends AnyRouter>(
96
96
  const { wss, createContext, router } = opts;
97
97
 
98
98
  const { transformer } = router._def;
99
- wss.on('connection', (client, req) => {
99
+ wss.on('connection', async (client, req) => {
100
100
  const clientSubscriptions = new Map<
101
101
  number | string,
102
102
  Subscription<TRouter>
@@ -285,7 +285,7 @@ export function applyWSSHandler<TRouter extends AnyRouter>(
285
285
  });
286
286
  }
287
287
  }
288
- createContextAsync();
288
+ await createContextAsync();
289
289
  });
290
290
 
291
291
  return {
@@ -1,9 +1,9 @@
1
1
  // this has been moved to separate package
2
2
  import {
3
- createHTTPHandler,
3
+ CreateHTTPContextOptions,
4
4
  CreateHTTPHandlerOptions,
5
+ createHTTPHandler,
5
6
  createHTTPServer,
6
- CreateHTTPContextOptions,
7
7
  } from '../adapters/standalone';
8
8
  import { AnyRouter } from '../router';
9
9
 
@@ -1,9 +1,9 @@
1
+ import type { AnyRouter } from '../';
1
2
  import {
2
- nodeHTTPRequestHandler as requestHandlerInner,
3
3
  NodeHTTPCreateContextFn,
4
4
  NodeHTTPCreateContextFnOptions,
5
+ nodeHTTPRequestHandler as requestHandlerInner,
5
6
  } from '../adapters/node-http';
6
- import type { AnyRouter } from '../';
7
7
 
8
8
  /**
9
9
  * @deprecated use `nodeHTTPRequestHandler` from `@trpc/server/adapters/node-http`
@@ -1,6 +1,6 @@
1
- import { TRPCResponse, TRPC_ERROR_CODES_BY_KEY } from '../../rpc';
2
1
  import { TRPCError } from '../../TRPCError';
3
2
  import { invert } from '../../internals/invert';
3
+ import { TRPCResponse, TRPC_ERROR_CODES_BY_KEY } from '../../rpc';
4
4
 
5
5
  export const TRPC_ERROR_CODES_BY_NUMBER = invert(TRPC_ERROR_CODES_BY_KEY);
6
6
  type ValueOf<T> = T[keyof T];
@@ -17,6 +17,7 @@ const JSONRPC2_TO_HTTP_CODE: Record<
17
17
  UNAUTHORIZED: 401,
18
18
  FORBIDDEN: 403,
19
19
  TIMEOUT: 408,
20
+ CONFLICT: 409,
20
21
  CLIENT_CLOSED_REQUEST: 499,
21
22
  PRECONDITION_FAILED: 412,
22
23
  PAYLOAD_TOO_LARGE: 413,
@@ -1,11 +1,11 @@
1
1
  import {
2
2
  AnyRouter,
3
3
  Dict,
4
- inferRouterContext,
5
- inferRouterError,
6
4
  ProcedureType,
7
5
  ResponseMeta,
8
6
  TRPCError,
7
+ inferRouterContext,
8
+ inferRouterError,
9
9
  } from '@trpc/server';
10
10
  import { URLSearchParams } from 'url';
11
11
  import { BaseHandlerOptions } from '../../internals/BaseHandlerOptions';
@@ -1,22 +1,22 @@
1
1
  /* eslint-disable @typescript-eslint/no-non-null-assertion */
2
2
  import { Maybe } from '@trpc/server';
3
+ import { TRPCError } from '../TRPCError';
3
4
  import { callProcedure } from '../internals/callProcedure';
4
5
  import { getErrorFromUnknown } from '../internals/errors';
5
6
  import { transformTRPCResponse } from '../internals/transformTRPCResponse';
6
7
  import {
7
8
  AnyRouter,
9
+ ProcedureType,
8
10
  inferRouterContext,
9
11
  inferRouterError,
10
- ProcedureType,
11
12
  } from '../router';
12
13
  import { TRPCErrorResponse, TRPCResponse, TRPCResultResponse } from '../rpc';
13
- import { TRPCError } from '../TRPCError';
14
14
  import { getHTTPStatusCode } from './internals/getHTTPStatusCode';
15
15
  import {
16
+ HTTPBaseHandlerOptions,
16
17
  HTTPHeaders,
17
18
  HTTPRequest,
18
19
  HTTPResponse,
19
- HTTPBaseHandlerOptions,
20
20
  } from './internals/types';
21
21
 
22
22
  const HTTP_METHOD_PROCEDURE_TYPE_MAP: Record<
@@ -1,5 +1,5 @@
1
- import { OnErrorFunction } from './OnErrorFunction';
2
1
  import { AnyRouter } from '../router';
2
+ import { OnErrorFunction } from './OnErrorFunction';
3
3
 
4
4
  /**
5
5
  * Base interface for any response handler
@@ -1,5 +1,5 @@
1
- import { AnyRouter, inferRouterContext, ProcedureType } from '../router';
2
1
  import { TRPCError } from '../TRPCError';
2
+ import { AnyRouter, ProcedureType, inferRouterContext } from '../router';
3
3
 
4
4
  export type OnErrorFunction<TRouter extends AnyRouter, TRequest> = (opts: {
5
5
  error: TRPCError;