@trpc/server 10.0.0-proxy-alpha.76 → 10.0.0-proxy-alpha.78

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 (56) hide show
  1. package/dist/{TRPCError-281ac25a.mjs → TRPCError-e99e7cbe.mjs} +1 -1
  2. package/dist/adapters/aws-lambda/index.js +2 -2
  3. package/dist/adapters/aws-lambda/index.mjs +3 -3
  4. package/dist/adapters/express.js +4 -3
  5. package/dist/adapters/express.mjs +5 -4
  6. package/dist/adapters/fastify/index.js +3 -2
  7. package/dist/adapters/fastify/index.mjs +4 -3
  8. package/dist/adapters/fetch/index.js +3 -2
  9. package/dist/adapters/fetch/index.mjs +4 -3
  10. package/dist/adapters/next.js +4 -3
  11. package/dist/adapters/next.mjs +5 -4
  12. package/dist/adapters/node-http/index.js +4 -3
  13. package/dist/adapters/node-http/index.mjs +5 -4
  14. package/dist/adapters/standalone.js +4 -3
  15. package/dist/adapters/standalone.mjs +5 -4
  16. package/dist/adapters/ws.mjs +1 -1
  17. package/dist/core/index.d.ts +1 -1
  18. package/dist/core/index.d.ts.map +1 -1
  19. package/dist/core/internals/__generated__/mergeRoutersGeneric.d.ts +0 -40
  20. package/dist/core/internals/__generated__/mergeRoutersGeneric.d.ts.map +1 -1
  21. package/dist/core/internals/procedureBuilder.d.ts.map +1 -1
  22. package/dist/core/procedure.d.ts +3 -0
  23. package/dist/core/procedure.d.ts.map +1 -1
  24. package/dist/core/router.d.ts +17 -32
  25. package/dist/core/router.d.ts.map +1 -1
  26. package/dist/core/types.d.ts +5 -5
  27. package/dist/core/types.d.ts.map +1 -1
  28. package/dist/deprecated/interop.d.ts +20 -43
  29. package/dist/deprecated/interop.d.ts.map +1 -1
  30. package/dist/http/resolveHTTPResponse.d.ts.map +1 -1
  31. package/dist/index.js +23 -23
  32. package/dist/index.mjs +5 -5
  33. package/dist/{nodeHTTPRequestHandler-c3dbb39e.js → nodeHTTPRequestHandler-7295e689.js} +1 -1
  34. package/dist/{nodeHTTPRequestHandler-c30139c8.mjs → nodeHTTPRequestHandler-e4a030ab.mjs} +2 -2
  35. package/dist/{router-6b03ff5f.mjs → resolveHTTPResponse-3955937e.mjs} +411 -601
  36. package/dist/{router-da6086c3.js → resolveHTTPResponse-9a70b9b2.js} +414 -601
  37. package/dist/router-4f222389.mjs +450 -0
  38. package/dist/router-fee6849a.js +452 -0
  39. package/dist/shared/index.d.ts +0 -1
  40. package/dist/shared/index.d.ts.map +1 -1
  41. package/dist/subscription.mjs +1 -1
  42. package/dist/types.d.ts +0 -10
  43. package/dist/types.d.ts.map +1 -1
  44. package/package.json +2 -2
  45. package/src/core/index.ts +4 -0
  46. package/src/core/internals/__generated__/mergeRoutersGeneric.ts +0 -40
  47. package/src/core/internals/procedureBuilder.ts +9 -6
  48. package/src/core/procedure.ts +3 -1
  49. package/src/core/router.ts +22 -38
  50. package/src/core/types.ts +7 -18
  51. package/src/deprecated/interop.ts +30 -78
  52. package/src/http/resolveHTTPResponse.ts +8 -2
  53. package/src/shared/index.ts +0 -2
  54. package/src/types.ts +0 -12
  55. package/dist/resolveHTTPResponse-1b339cff.mjs +0 -260
  56. package/dist/resolveHTTPResponse-d7a8a210.js +0 -264
@@ -9,11 +9,11 @@ import {
9
9
  } from '../core/middleware';
10
10
  import {
11
11
  MutationProcedure,
12
- Procedure as NewProcedure,
13
12
  QueryProcedure,
14
13
  SubscriptionProcedure,
15
14
  } from '../core/procedure';
16
15
  import {
16
+ ProcedureRecord as NewProcedureRecord,
17
17
  Router as NewRouter,
18
18
  RouterDef,
19
19
  createRouterFactory,
@@ -53,26 +53,16 @@ type convertProcedureParams<
53
53
  >
54
54
  : never;
55
55
 
56
- /**
57
- * @deprecated
58
- */
59
- export type LegacyV9ProcedureTag = {
60
- _old: true;
61
- };
62
-
63
56
  type MigrateProcedure<
64
57
  TConfig extends RootConfig,
65
58
  TProcedure extends AnyOldProcedure,
66
59
  TType extends ProcedureType,
67
60
  > = TType extends 'query'
68
- ? QueryProcedure<convertProcedureParams<TConfig, TProcedure>> &
69
- LegacyV9ProcedureTag
61
+ ? QueryProcedure<convertProcedureParams<TConfig, TProcedure>>
70
62
  : TType extends 'mutation'
71
- ? MutationProcedure<convertProcedureParams<TConfig, TProcedure>> &
72
- LegacyV9ProcedureTag
63
+ ? MutationProcedure<convertProcedureParams<TConfig, TProcedure>>
73
64
  : TType extends 'subscription'
74
- ? SubscriptionProcedure<convertProcedureParams<TConfig, TProcedure>> &
75
- LegacyV9ProcedureTag
65
+ ? SubscriptionProcedure<convertProcedureParams<TConfig, TProcedure>>
76
66
  : never;
77
67
 
78
68
  export type MigrateProcedureRecord<
@@ -117,43 +107,7 @@ export type MigrateRouter<
117
107
  TErrorShape extends TRPCErrorShape<any>,
118
108
  > = NewRouter<
119
109
  {
120
- legacy: {
121
- queries: MigrateProcedureRecord<
122
- CreateRootConfig<{
123
- ctx: TInputContext;
124
- errorShape: TErrorShape;
125
- meta: TMeta;
126
- transformer: CombinedDataTransformer;
127
- }>,
128
- TQueries,
129
- 'query'
130
- >;
131
- mutations: MigrateProcedureRecord<
132
- CreateRootConfig<{
133
- ctx: TInputContext;
134
- errorShape: TErrorShape;
135
- meta: TMeta;
136
- transformer: CombinedDataTransformer;
137
- }>,
138
- TMutations,
139
- 'mutation'
140
- >;
141
- subscriptions: MigrateProcedureRecord<
142
- CreateRootConfig<{
143
- ctx: TInputContext;
144
- errorShape: TErrorShape;
145
- meta: TMeta;
146
- transformer: CombinedDataTransformer;
147
- }>,
148
- TSubscriptions,
149
- 'subscription'
150
- >;
151
- };
152
- } & RouterDef<
153
- TInputContext,
154
- TErrorShape,
155
- TMeta,
156
- MigrateProcedureRecord<
110
+ queries: MigrateProcedureRecord<
157
111
  CreateRootConfig<{
158
112
  ctx: TInputContext;
159
113
  errorShape: TErrorShape;
@@ -162,28 +116,28 @@ export type MigrateRouter<
162
116
  }>,
163
117
  TQueries,
164
118
  'query'
165
- > &
166
- MigrateProcedureRecord<
167
- CreateRootConfig<{
168
- ctx: TInputContext;
169
- errorShape: TErrorShape;
170
- meta: TMeta;
171
- transformer: CombinedDataTransformer;
172
- }>,
173
- TMutations,
174
- 'mutation'
175
- > &
176
- MigrateProcedureRecord<
177
- CreateRootConfig<{
178
- ctx: TInputContext;
179
- errorShape: TErrorShape;
180
- meta: TMeta;
181
- transformer: CombinedDataTransformer;
182
- }>,
183
- TSubscriptions,
184
- 'subscription'
185
- >
186
- >
119
+ >;
120
+ mutations: MigrateProcedureRecord<
121
+ CreateRootConfig<{
122
+ ctx: TInputContext;
123
+ errorShape: TErrorShape;
124
+ meta: TMeta;
125
+ transformer: CombinedDataTransformer;
126
+ }>,
127
+ TMutations,
128
+ 'mutation'
129
+ >;
130
+ subscriptions: MigrateProcedureRecord<
131
+ CreateRootConfig<{
132
+ ctx: TInputContext;
133
+ errorShape: TErrorShape;
134
+ meta: TMeta;
135
+ transformer: CombinedDataTransformer;
136
+ }>,
137
+ TSubscriptions,
138
+ 'subscription'
139
+ >;
140
+ } & RouterDef<TInputContext, TErrorShape, TMeta, {}>
187
141
  >;
188
142
 
189
143
  export type MigrateOldRouter<TRouter extends AnyOldRouter> =
@@ -242,11 +196,9 @@ export function migrateRouter<TOldRouter extends AnyOldRouter>(
242
196
  const errorFormatter = oldRouter._def.errorFormatter;
243
197
  const transformer = oldRouter._def.transformer;
244
198
 
245
- type ProcRecord = Record<string, NewProcedure<any>>;
246
-
247
- const queries: ProcRecord = {};
248
- const mutations: ProcRecord = {};
249
- const subscriptions: ProcRecord = {};
199
+ const queries: NewProcedureRecord = {};
200
+ const mutations: NewProcedureRecord = {};
201
+ const subscriptions: NewProcedureRecord = {};
250
202
  for (const [name, procedure] of Object.entries(oldRouter._def.queries)) {
251
203
  queries[name] = migrateProcedure(procedure as any, 'query');
252
204
  }
@@ -2,6 +2,7 @@
2
2
  import {
3
3
  AnyRouter,
4
4
  ProcedureType,
5
+ callProcedure,
5
6
  inferRouterContext,
6
7
  inferRouterError,
7
8
  } from '../core';
@@ -180,8 +181,13 @@ export async function resolveHTTPResponse<
180
181
  const input = inputs[index];
181
182
 
182
183
  try {
183
- const caller = router.createCaller(ctx);
184
- const output = await caller[type](path, input as any);
184
+ const output = await callProcedure({
185
+ procedures: router._def.procedures,
186
+ path,
187
+ rawInput: input,
188
+ ctx,
189
+ type,
190
+ });
185
191
  return {
186
192
  input,
187
193
  path,
@@ -1,3 +1 @@
1
1
  export * from './createProxy';
2
-
3
- export type { LegacyV9ProcedureTag } from '../deprecated/interop';
package/src/types.ts CHANGED
@@ -63,15 +63,3 @@ type FilterKeys<T extends object, K> = {
63
63
  * @internal
64
64
  */
65
65
  export type Filter<T extends object, K> = Pick<T, FilterKeys<T, K>>;
66
-
67
- /**
68
- * @internal
69
- */
70
- export type NeverKeys<T> = {
71
- [TKey in keyof T]: T[TKey] extends never ? TKey : never;
72
- }[keyof T];
73
-
74
- /**
75
- * @internal
76
- */
77
- export type OmitNeverKeys<T> = Omit<T, NeverKeys<T>>;
@@ -1,260 +0,0 @@
1
- import { T as TRPCError, g as getTRPCErrorFromUnknown, a as getCauseFromUnknown } from './TRPCError-281ac25a.mjs';
2
- import { t as transformTRPCResponse } from './transformTRPCResponse-c8138d5f.mjs';
3
- import { T as TRPC_ERROR_CODES_BY_KEY } from './codes-aaa7d554.mjs';
4
-
5
- /* istanbul ignore file */ function assertNotBrowser() {
6
- if (typeof window !== 'undefined' && !('Deno' in window) && process.env.NODE_ENV !== 'test' && process.env.JEST_WORKER_ID === undefined) {
7
- throw new Error('Imported server-only code in the browser');
8
- }
9
- }
10
-
11
- function invert(obj) {
12
- const newObj = Object.create(null);
13
- for(const key in obj){
14
- const v = obj[key];
15
- newObj[v] = key;
16
- }
17
- return newObj;
18
- }
19
-
20
- const TRPC_ERROR_CODES_BY_NUMBER = invert(TRPC_ERROR_CODES_BY_KEY);
21
- const JSONRPC2_TO_HTTP_CODE = {
22
- PARSE_ERROR: 400,
23
- BAD_REQUEST: 400,
24
- NOT_FOUND: 404,
25
- INTERNAL_SERVER_ERROR: 500,
26
- UNAUTHORIZED: 401,
27
- FORBIDDEN: 403,
28
- TIMEOUT: 408,
29
- CONFLICT: 409,
30
- CLIENT_CLOSED_REQUEST: 499,
31
- PRECONDITION_FAILED: 412,
32
- PAYLOAD_TOO_LARGE: 413,
33
- METHOD_NOT_SUPPORTED: 405
34
- };
35
- function getStatusCodeFromKey(code) {
36
- return JSONRPC2_TO_HTTP_CODE[code] ?? 500;
37
- }
38
- function getHTTPStatusCode(json) {
39
- const arr = Array.isArray(json) ? json : [
40
- json
41
- ];
42
- const httpStatuses = new Set(arr.map((res)=>{
43
- if ('error' in res) {
44
- const data = res.error.data;
45
- if (typeof data.httpStatus === 'number') {
46
- return data.httpStatus;
47
- }
48
- const code = TRPC_ERROR_CODES_BY_NUMBER[res.error.code];
49
- return getStatusCodeFromKey(code);
50
- }
51
- return 200;
52
- }));
53
- if (httpStatuses.size !== 1) {
54
- return 207;
55
- }
56
- const httpStatus = httpStatuses.values().next().value;
57
- return httpStatus;
58
- }
59
- function getHTTPStatusCodeFromError(error) {
60
- const { code } = error;
61
- return getStatusCodeFromKey(code);
62
- }
63
-
64
- const HTTP_METHOD_PROCEDURE_TYPE_MAP = {
65
- GET: 'query',
66
- POST: 'mutation'
67
- };
68
- function getRawProcedureInputOrThrow(req) {
69
- try {
70
- if (req.method === 'GET') {
71
- if (!req.query.has('input')) {
72
- return undefined;
73
- }
74
- const raw = req.query.get('input');
75
- return JSON.parse(raw);
76
- }
77
- if (typeof req.body === 'string') {
78
- // A mutation with no inputs will have req.body === ''
79
- return req.body.length === 0 ? undefined : JSON.parse(req.body);
80
- }
81
- return req.body;
82
- } catch (err) {
83
- throw new TRPCError({
84
- code: 'PARSE_ERROR',
85
- cause: getCauseFromUnknown(err)
86
- });
87
- }
88
- }
89
- async function resolveHTTPResponse(opts) {
90
- const { createContext , onError , router , req } = opts;
91
- const batchingEnabled = opts.batching?.enabled ?? true;
92
- if (req.method === 'HEAD') {
93
- // can be used for lambda warmup
94
- return {
95
- status: 204
96
- };
97
- }
98
- const type = HTTP_METHOD_PROCEDURE_TYPE_MAP[req.method] ?? 'unknown';
99
- let ctx = undefined;
100
- let paths = undefined;
101
- const isBatchCall = !!req.query.get('batch');
102
- function endResponse(untransformedJSON, errors) {
103
- let status = getHTTPStatusCode(untransformedJSON);
104
- const headers = {
105
- 'Content-Type': 'application/json'
106
- };
107
- const meta = opts.responseMeta?.({
108
- ctx,
109
- paths,
110
- type,
111
- data: Array.isArray(untransformedJSON) ? untransformedJSON : [
112
- untransformedJSON
113
- ],
114
- errors
115
- }) ?? {};
116
- for (const [key, value] of Object.entries(meta.headers ?? {})){
117
- headers[key] = value;
118
- }
119
- if (meta.status) {
120
- status = meta.status;
121
- }
122
- const transformedJSON = transformTRPCResponse(router, untransformedJSON);
123
- const body = JSON.stringify(transformedJSON);
124
- return {
125
- body,
126
- status,
127
- headers
128
- };
129
- }
130
- try {
131
- if (opts.error) {
132
- throw opts.error;
133
- }
134
- if (isBatchCall && !batchingEnabled) {
135
- throw new Error(`Batching is not enabled on the server`);
136
- }
137
- if (type === 'subscription') {
138
- throw new TRPCError({
139
- message: 'Subscriptions should use wsLink',
140
- code: 'METHOD_NOT_SUPPORTED'
141
- });
142
- }
143
- if (type === 'unknown') {
144
- throw new TRPCError({
145
- message: `Unexpected request method ${req.method}`,
146
- code: 'METHOD_NOT_SUPPORTED'
147
- });
148
- }
149
- const rawInput = getRawProcedureInputOrThrow(req);
150
- paths = isBatchCall ? opts.path.split(',') : [
151
- opts.path
152
- ];
153
- ctx = await createContext();
154
- const deserializeInputValue = (rawValue)=>{
155
- return typeof rawValue !== 'undefined' ? router._def.transformer.input.deserialize(rawValue) : rawValue;
156
- };
157
- const getInputs = ()=>{
158
- if (!isBatchCall) {
159
- return {
160
- 0: deserializeInputValue(rawInput)
161
- };
162
- }
163
- if (rawInput == null || typeof rawInput !== 'object' || Array.isArray(rawInput)) {
164
- throw new TRPCError({
165
- code: 'BAD_REQUEST',
166
- message: '"input" needs to be an object when doing a batch call'
167
- });
168
- }
169
- const input = {};
170
- for(const key in rawInput){
171
- const k = key;
172
- const rawValue = rawInput[k];
173
- const value = deserializeInputValue(rawValue);
174
- input[k] = value;
175
- }
176
- return input;
177
- };
178
- const inputs = getInputs();
179
- const rawResults = await Promise.all(paths.map(async (path, index)=>{
180
- const input = inputs[index];
181
- try {
182
- const caller = router.createCaller(ctx);
183
- const output = await caller[type](path, input);
184
- return {
185
- input,
186
- path,
187
- data: output
188
- };
189
- } catch (cause) {
190
- const error = getTRPCErrorFromUnknown(cause);
191
- onError?.({
192
- error,
193
- path,
194
- input,
195
- ctx,
196
- type: type,
197
- req
198
- });
199
- return {
200
- input,
201
- path,
202
- error
203
- };
204
- }
205
- }));
206
- const errors = rawResults.flatMap((obj)=>obj.error ? [
207
- obj.error
208
- ] : []);
209
- const resultEnvelopes = rawResults.map((obj)=>{
210
- const { path , input } = obj;
211
- if (obj.error) {
212
- return {
213
- error: router.getErrorShape({
214
- error: obj.error,
215
- type,
216
- path,
217
- input,
218
- ctx
219
- })
220
- };
221
- } else {
222
- return {
223
- result: {
224
- data: obj.data
225
- }
226
- };
227
- }
228
- });
229
- const result = isBatchCall ? resultEnvelopes : resultEnvelopes[0];
230
- return endResponse(result, errors);
231
- } catch (cause) {
232
- // we get here if
233
- // - batching is called when it's not enabled
234
- // - `createContext()` throws
235
- // - post body is too large
236
- // - input deserialization fails
237
- const error = getTRPCErrorFromUnknown(cause);
238
- onError?.({
239
- error,
240
- path: undefined,
241
- input: undefined,
242
- ctx,
243
- type: type,
244
- req
245
- });
246
- return endResponse({
247
- error: router.getErrorShape({
248
- error,
249
- type,
250
- path: undefined,
251
- input: undefined,
252
- ctx
253
- })
254
- }, [
255
- error
256
- ]);
257
- }
258
- }
259
-
260
- export { assertNotBrowser as a, getHTTPStatusCodeFromError as g, resolveHTTPResponse as r };