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