@trpc/server 10.0.0-alpha.19 → 10.0.0-alpha.21
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/express/dist/trpc-server-adapters-express.cjs.dev.js +5 -5
- package/adapters/express/dist/trpc-server-adapters-express.cjs.prod.js +5 -5
- package/adapters/express/dist/trpc-server-adapters-express.esm.js +5 -5
- package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.dev.js +16 -9
- package/adapters/fastify/dist/trpc-server-adapters-fastify.cjs.prod.js +16 -9
- package/adapters/fastify/dist/trpc-server-adapters-fastify.esm.js +16 -9
- package/adapters/next/dist/trpc-server-adapters-next.cjs.dev.js +3 -3
- package/adapters/next/dist/trpc-server-adapters-next.cjs.prod.js +3 -3
- package/adapters/next/dist/trpc-server-adapters-next.esm.js +3 -3
- package/adapters/node-http/dist/trpc-server-adapters-node-http.cjs.dev.js +3 -3
- package/adapters/node-http/dist/trpc-server-adapters-node-http.cjs.prod.js +3 -3
- package/adapters/node-http/dist/trpc-server-adapters-node-http.esm.js +3 -3
- package/adapters/standalone/dist/trpc-server-adapters-standalone.cjs.dev.js +3 -3
- package/adapters/standalone/dist/trpc-server-adapters-standalone.cjs.prod.js +3 -3
- package/adapters/standalone/dist/trpc-server-adapters-standalone.esm.js +3 -3
- package/adapters/ws/dist/trpc-server-adapters-ws.cjs.dev.js +4 -4
- package/adapters/ws/dist/trpc-server-adapters-ws.cjs.prod.js +4 -4
- package/adapters/ws/dist/trpc-server-adapters-ws.esm.js +4 -4
- package/dist/TRPCError.d.ts +3 -3
- package/dist/TRPCError.d.ts.map +1 -1
- package/dist/adapters/express.js +6 -7
- package/dist/adapters/express.mjs +6 -7
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts +2 -2
- package/dist/adapters/fastify/fastifyRequestHandler.d.ts.map +1 -1
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts +1 -1
- package/dist/adapters/fastify/fastifyTRPCPlugin.d.ts.map +1 -1
- package/dist/adapters/fastify/index.js +18 -12
- package/dist/adapters/fastify/index.mjs +17 -11
- package/dist/adapters/next.d.ts +1 -1
- package/dist/adapters/next.d.ts.map +1 -1
- package/dist/adapters/next.js +4 -5
- package/dist/adapters/next.mjs +4 -5
- package/dist/adapters/node-http/index.js +4 -5
- package/dist/adapters/node-http/index.mjs +4 -5
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts +1 -1
- package/dist/adapters/node-http/nodeHTTPRequestHandler.d.ts.map +1 -1
- package/dist/adapters/node-http/types.d.ts +3 -3
- package/dist/adapters/node-http/types.d.ts.map +1 -1
- package/dist/adapters/standalone.d.ts.map +1 -1
- package/dist/adapters/standalone.js +10 -8
- package/dist/adapters/standalone.mjs +10 -7
- package/dist/adapters/ws.d.ts +3 -3
- package/dist/adapters/ws.d.ts.map +1 -1
- package/dist/adapters/ws.js +5 -5
- package/dist/adapters/ws.mjs +5 -5
- package/dist/assertNotBrowser.d.ts.map +1 -1
- package/dist/{codes-7f46544e.js → codes-4e4d55a9.js} +2 -0
- package/dist/{codes-7a3d4f65.mjs → codes-6c13cf43.mjs} +2 -0
- package/dist/http/internals/getHTTPStatusCode.d.ts +2 -1
- package/dist/http/internals/getHTTPStatusCode.d.ts.map +1 -1
- package/dist/http/internals/types.d.ts +4 -3
- package/dist/http/internals/types.d.ts.map +1 -1
- package/dist/http/resolveHTTPResponse.d.ts +3 -3
- package/dist/http/resolveHTTPResponse.d.ts.map +1 -1
- package/dist/index.js +44 -19
- package/dist/index.mjs +46 -21
- package/dist/internals/BaseHandlerOptions.d.ts +1 -1
- package/dist/internals/BaseHandlerOptions.d.ts.map +1 -1
- package/dist/internals/OnErrorFunction.d.ts +1 -1
- package/dist/internals/OnErrorFunction.d.ts.map +1 -1
- package/dist/internals/errors.d.ts +1 -0
- package/dist/internals/errors.d.ts.map +1 -1
- package/dist/internals/middlewares.d.ts +3 -2
- package/dist/internals/middlewares.d.ts.map +1 -1
- package/dist/internals/procedure.d.ts +44 -32
- package/dist/internals/procedure.d.ts.map +1 -1
- package/dist/{nodeHTTPRequestHandler-f9b7cd2d.mjs → nodeHTTPRequestHandler-13c4bded.mjs} +2 -3
- package/dist/{nodeHTTPRequestHandler-e02d6e5d.js → nodeHTTPRequestHandler-745e43e5.js} +3 -4
- package/dist/{resolveHTTPResponse-d4da195d.js → resolveHTTPResponse-b1167b6a.js} +6 -5
- package/dist/{resolveHTTPResponse-ca8bc869.mjs → resolveHTTPResponse-de40fda7.mjs} +6 -5
- package/dist/router.d.ts +33 -29
- package/dist/router.d.ts.map +1 -1
- package/dist/rpc/codes.d.ts +2 -0
- package/dist/rpc/codes.d.ts.map +1 -1
- package/dist/rpc/index.js +1 -1
- package/dist/rpc/index.mjs +1 -1
- package/dist/{subscription-f36af4f8.js → subscription-4b936ba3.js} +2 -2
- package/dist/{subscription-7ec5884e.mjs → subscription-8a85f253.mjs} +2 -2
- package/dist/{transformTRPCResponse-f9337285.mjs → transformTRPCResponse-9fdeecca.mjs} +50 -38
- package/dist/{transformTRPCResponse-eeeb5300.js → transformTRPCResponse-a57d3226.js} +50 -37
- package/package.json +7 -3
- package/src/TRPCError.ts +3 -3
- package/src/adapters/express.ts +2 -2
- package/src/adapters/fastify/fastifyRequestHandler.ts +6 -8
- package/src/adapters/fastify/fastifyTRPCPlugin.ts +14 -6
- package/src/adapters/next.ts +6 -2
- package/src/adapters/node-http/internals/getPostBody.test.ts +2 -1
- package/src/adapters/node-http/nodeHTTPRequestHandler.ts +5 -6
- package/src/adapters/node-http/types.ts +3 -3
- package/src/adapters/standalone.ts +11 -3
- package/src/adapters/ws.ts +9 -9
- package/src/assertNotBrowser.ts +1 -0
- package/src/http/internals/getHTTPStatusCode.ts +2 -1
- package/src/http/internals/types.ts +6 -7
- package/src/http/resolveHTTPResponse.ts +7 -7
- package/src/internals/BaseHandlerOptions.ts +1 -1
- package/src/internals/OnErrorFunction.ts +1 -1
- package/src/internals/errors.ts +20 -4
- package/src/internals/middlewares.ts +3 -2
- package/src/internals/procedure.ts +175 -68
- package/src/router.ts +165 -49
- package/src/rpc/codes.ts +1 -0
- package/src/subscription.ts +1 -1
- package/ws/dist/trpc-server-ws.cjs.dev.js +2 -2
- package/ws/dist/trpc-server-ws.cjs.prod.js +2 -2
- package/ws/dist/trpc-server-ws.esm.js +2 -2
|
@@ -1,51 +1,55 @@
|
|
|
1
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import { TRPCError } from '../TRPCError';
|
|
2
3
|
import { assertNotBrowser } from '../assertNotBrowser';
|
|
3
4
|
import { ProcedureType } from '../router';
|
|
4
|
-
import {
|
|
5
|
-
import { getErrorFromUnknown } from './errors';
|
|
5
|
+
import { getCauseFromUnknown, getErrorFromUnknown } from './errors';
|
|
6
6
|
import { MiddlewareFunction, middlewareMarker } from './middlewares';
|
|
7
7
|
import { wrapCallSafe } from './wrapCallSafe';
|
|
8
|
+
|
|
8
9
|
assertNotBrowser();
|
|
9
10
|
|
|
10
|
-
export type
|
|
11
|
+
export type ProcedureParserZodEsque<TInput, TOutput> = {
|
|
11
12
|
_input: TInput;
|
|
12
|
-
_output:
|
|
13
|
+
_output: TOutput;
|
|
13
14
|
};
|
|
14
15
|
|
|
15
|
-
export type
|
|
16
|
-
parse: (input: any) =>
|
|
16
|
+
export type ProcedureParserMyZodEsque<T> = {
|
|
17
|
+
parse: (input: any) => T;
|
|
17
18
|
};
|
|
18
19
|
|
|
19
|
-
export type
|
|
20
|
-
create: (input: unknown) =>
|
|
20
|
+
export type ProcedureParserSuperstructEsque<T> = {
|
|
21
|
+
create: (input: unknown) => T;
|
|
21
22
|
};
|
|
22
23
|
|
|
23
|
-
export type
|
|
24
|
+
export type ProcedureParserCustomValidatorEsque<T> = (
|
|
24
25
|
input: unknown,
|
|
25
|
-
) =>
|
|
26
|
+
) => T | Promise<T>;
|
|
26
27
|
|
|
27
|
-
export type
|
|
28
|
-
validateSync: (input: unknown) =>
|
|
28
|
+
export type ProcedureParserYupEsque<T> = {
|
|
29
|
+
validateSync: (input: unknown) => T;
|
|
29
30
|
};
|
|
30
|
-
export type ProcedureInputParser<TInput> =
|
|
31
|
-
| ProcedureInputParserYupEsque<TInput>
|
|
32
|
-
| ProcedureInputParserSuperstructEsque<TInput>
|
|
33
|
-
| ProcedureInputParserCustomValidatorEsque<TInput>
|
|
34
|
-
| ProcedureInputParserMyZodEsque<TInput>;
|
|
35
31
|
|
|
36
|
-
export type
|
|
37
|
-
|
|
32
|
+
export type ProcedureParserWithInputOutput<TInput, TOutput> =
|
|
33
|
+
ProcedureParserZodEsque<TInput, TOutput>;
|
|
38
34
|
|
|
39
|
-
export type
|
|
35
|
+
export type ProcedureParser<T> =
|
|
36
|
+
| ProcedureParserYupEsque<T>
|
|
37
|
+
| ProcedureParserSuperstructEsque<T>
|
|
38
|
+
| ProcedureParserCustomValidatorEsque<T>
|
|
39
|
+
| ProcedureParserMyZodEsque<T>;
|
|
40
|
+
|
|
41
|
+
export type ProcedureResolver<TContext, TInput, TOutput> = (opts: {
|
|
40
42
|
ctx: TContext;
|
|
41
|
-
input:
|
|
43
|
+
input: TInput;
|
|
42
44
|
type: ProcedureType;
|
|
43
45
|
}) => Promise<TOutput> | TOutput;
|
|
44
46
|
|
|
45
|
-
interface ProcedureOptions<TContext,
|
|
46
|
-
middlewares: Array<MiddlewareFunction<any, any>>;
|
|
47
|
-
resolver: ProcedureResolver<TContext,
|
|
48
|
-
inputParser:
|
|
47
|
+
interface ProcedureOptions<TContext, TMeta, TInput, TOutput, TParsedOutput> {
|
|
48
|
+
middlewares: Array<MiddlewareFunction<any, any, any>>;
|
|
49
|
+
resolver: ProcedureResolver<TContext, TInput, TOutput>;
|
|
50
|
+
inputParser: ProcedureParser<TInput>;
|
|
51
|
+
outputParser: ProcedureParser<TParsedOutput>;
|
|
52
|
+
meta: TMeta;
|
|
49
53
|
}
|
|
50
54
|
|
|
51
55
|
/**
|
|
@@ -58,36 +62,33 @@ export interface ProcedureCallOptions<TContext> {
|
|
|
58
62
|
type: ProcedureType;
|
|
59
63
|
}
|
|
60
64
|
|
|
61
|
-
type ParseFn<
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
inputParser: ProcedureInputParser<TParsedInput>,
|
|
66
|
-
): ParseFn<TParsedInput> {
|
|
67
|
-
const parser = inputParser as any;
|
|
65
|
+
type ParseFn<T> = (value: unknown) => T | Promise<T>;
|
|
66
|
+
|
|
67
|
+
function getParseFn<T>(procedureParser: ProcedureParser<T>): ParseFn<T> {
|
|
68
|
+
const parser = procedureParser as any;
|
|
68
69
|
|
|
69
70
|
if (typeof parser === 'function') {
|
|
70
|
-
//
|
|
71
|
+
// ProcedureParserCustomValidatorEsque
|
|
71
72
|
return parser;
|
|
72
73
|
}
|
|
73
74
|
|
|
74
75
|
if (typeof parser.parseAsync === 'function') {
|
|
75
|
-
//
|
|
76
|
+
// ProcedureParserZodEsque
|
|
76
77
|
return parser.parseAsync.bind(parser);
|
|
77
78
|
}
|
|
78
79
|
|
|
79
80
|
if (typeof parser.parse === 'function') {
|
|
80
|
-
//
|
|
81
|
+
// ProcedureParserZodEsque
|
|
81
82
|
return parser.parse.bind(parser);
|
|
82
83
|
}
|
|
83
84
|
|
|
84
85
|
if (typeof parser.validateSync === 'function') {
|
|
85
|
-
//
|
|
86
|
+
// ProcedureParserYupEsque
|
|
86
87
|
return parser.validateSync.bind(parser);
|
|
87
88
|
}
|
|
88
89
|
|
|
89
90
|
if (typeof parser.create === 'function') {
|
|
90
|
-
//
|
|
91
|
+
// ProcedureParserSuperstructEsque
|
|
91
92
|
return parser.create.bind(parser);
|
|
92
93
|
}
|
|
93
94
|
|
|
@@ -97,42 +98,82 @@ function getParseFn<TParsedInput>(
|
|
|
97
98
|
/**
|
|
98
99
|
* @internal
|
|
99
100
|
*/
|
|
100
|
-
export class Procedure<
|
|
101
|
-
|
|
101
|
+
export class Procedure<
|
|
102
|
+
TInputContext,
|
|
103
|
+
TContext,
|
|
104
|
+
TMeta,
|
|
105
|
+
TInput,
|
|
106
|
+
TParsedInput,
|
|
107
|
+
TOutput,
|
|
108
|
+
TParsedOutput,
|
|
109
|
+
TFinalOutput = unknown extends TParsedOutput ? TOutput : TParsedOutput,
|
|
110
|
+
> {
|
|
111
|
+
private middlewares: Readonly<Array<MiddlewareFunction<any, any, any>>>;
|
|
102
112
|
private resolver: ProcedureResolver<TContext, TParsedInput, TOutput>;
|
|
103
|
-
private readonly inputParser:
|
|
104
|
-
private
|
|
113
|
+
private readonly inputParser: ProcedureParser<TParsedInput>;
|
|
114
|
+
private parseInputFn: ParseFn<TParsedInput>;
|
|
115
|
+
private readonly outputParser: ProcedureParser<TFinalOutput>;
|
|
116
|
+
private parseOutputFn: ParseFn<TFinalOutput>;
|
|
117
|
+
public readonly meta: TMeta;
|
|
105
118
|
|
|
106
|
-
constructor(
|
|
119
|
+
constructor(
|
|
120
|
+
opts: ProcedureOptions<
|
|
121
|
+
TContext,
|
|
122
|
+
TMeta,
|
|
123
|
+
TParsedInput,
|
|
124
|
+
TOutput,
|
|
125
|
+
TFinalOutput
|
|
126
|
+
>,
|
|
127
|
+
) {
|
|
107
128
|
this.middlewares = opts.middlewares;
|
|
108
129
|
this.resolver = opts.resolver;
|
|
109
130
|
this.inputParser = opts.inputParser;
|
|
110
|
-
this.
|
|
131
|
+
this.parseInputFn = getParseFn(this.inputParser);
|
|
132
|
+
this.outputParser = opts.outputParser;
|
|
133
|
+
this.parseOutputFn = getParseFn(this.outputParser);
|
|
134
|
+
this.meta = opts.meta;
|
|
111
135
|
}
|
|
112
136
|
|
|
113
137
|
private async parseInput(rawInput: unknown): Promise<TParsedInput> {
|
|
114
138
|
try {
|
|
115
|
-
return await this.
|
|
139
|
+
return await this.parseInputFn(rawInput);
|
|
116
140
|
} catch (cause) {
|
|
117
141
|
throw new TRPCError({
|
|
118
142
|
code: 'BAD_REQUEST',
|
|
119
|
-
cause,
|
|
143
|
+
cause: getCauseFromUnknown(cause),
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
private async parseOutput(rawOutput: TOutput): Promise<TFinalOutput> {
|
|
149
|
+
try {
|
|
150
|
+
return await this.parseOutputFn(rawOutput);
|
|
151
|
+
} catch (cause) {
|
|
152
|
+
throw new TRPCError({
|
|
153
|
+
code: 'INTERNAL_SERVER_ERROR',
|
|
154
|
+
cause: getCauseFromUnknown(cause),
|
|
155
|
+
message: 'Output validation failed',
|
|
120
156
|
});
|
|
121
157
|
}
|
|
122
158
|
}
|
|
123
159
|
|
|
124
160
|
/**
|
|
125
|
-
* Trigger middlewares in order, parse raw input &
|
|
161
|
+
* Trigger middlewares in order, parse raw input, call resolver & parse raw output
|
|
126
162
|
* @internal
|
|
127
163
|
*/
|
|
128
164
|
public async call(
|
|
129
165
|
opts: ProcedureCallOptions<TInputContext>,
|
|
130
|
-
): Promise<
|
|
166
|
+
): Promise<TFinalOutput> {
|
|
131
167
|
// wrap the actual resolver and treat as the last "middleware"
|
|
132
168
|
const middlewaresWithResolver = this.middlewares.concat([
|
|
133
169
|
async ({ ctx }: { ctx: TContext }) => {
|
|
134
170
|
const input = await this.parseInput(opts.rawInput);
|
|
135
|
-
const
|
|
171
|
+
const rawOutput = await this.resolver({
|
|
172
|
+
...opts,
|
|
173
|
+
ctx,
|
|
174
|
+
input,
|
|
175
|
+
});
|
|
176
|
+
const data = await this.parseOutput(rawOutput);
|
|
136
177
|
return {
|
|
137
178
|
marker: middlewareMarker,
|
|
138
179
|
ok: true,
|
|
@@ -151,6 +192,7 @@ export class Procedure<TInputContext, TContext, TInput, TParsedInput, TOutput> {
|
|
|
151
192
|
type: opts.type,
|
|
152
193
|
path: opts.path,
|
|
153
194
|
rawInput: opts.rawInput,
|
|
195
|
+
meta: this.meta,
|
|
154
196
|
next: nextFns[index + 1],
|
|
155
197
|
}),
|
|
156
198
|
);
|
|
@@ -178,7 +220,7 @@ export class Procedure<TInputContext, TContext, TInput, TParsedInput, TOutput> {
|
|
|
178
220
|
throw result.error;
|
|
179
221
|
}
|
|
180
222
|
|
|
181
|
-
return result.data as
|
|
223
|
+
return result.data as TFinalOutput;
|
|
182
224
|
}
|
|
183
225
|
|
|
184
226
|
/**
|
|
@@ -186,15 +228,25 @@ export class Procedure<TInputContext, TContext, TInput, TParsedInput, TOutput> {
|
|
|
186
228
|
* @param middlewares
|
|
187
229
|
*/
|
|
188
230
|
public inheritMiddlewares(
|
|
189
|
-
middlewares: MiddlewareFunction<TInputContext, TContext>[],
|
|
231
|
+
middlewares: MiddlewareFunction<TInputContext, TContext, TMeta>[],
|
|
190
232
|
): this {
|
|
191
233
|
const Constructor: {
|
|
192
|
-
new (
|
|
234
|
+
new (
|
|
235
|
+
opts: ProcedureOptions<
|
|
236
|
+
TContext,
|
|
237
|
+
TMeta,
|
|
238
|
+
TParsedInput,
|
|
239
|
+
TOutput,
|
|
240
|
+
TFinalOutput
|
|
241
|
+
>,
|
|
242
|
+
): Procedure<
|
|
193
243
|
TInputContext,
|
|
194
244
|
TContext,
|
|
245
|
+
TMeta,
|
|
195
246
|
TInput,
|
|
196
247
|
TParsedInput,
|
|
197
|
-
TOutput
|
|
248
|
+
TOutput,
|
|
249
|
+
TParsedOutput
|
|
198
250
|
>;
|
|
199
251
|
} = (this as any).constructor;
|
|
200
252
|
|
|
@@ -202,48 +254,92 @@ export class Procedure<TInputContext, TContext, TInput, TParsedInput, TOutput> {
|
|
|
202
254
|
middlewares: [...middlewares, ...this.middlewares],
|
|
203
255
|
resolver: this.resolver,
|
|
204
256
|
inputParser: this.inputParser,
|
|
257
|
+
outputParser: this.outputParser,
|
|
258
|
+
meta: this.meta,
|
|
205
259
|
});
|
|
206
260
|
|
|
207
261
|
return instance as any;
|
|
208
262
|
}
|
|
209
263
|
}
|
|
210
264
|
|
|
211
|
-
export type CreateProcedureWithInput<TContext,
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
265
|
+
export type CreateProcedureWithInput<TContext, TMeta, TInput, TOutput> = {
|
|
266
|
+
input: ProcedureParser<TInput>;
|
|
267
|
+
output?: ProcedureParser<TOutput>;
|
|
268
|
+
meta?: TMeta;
|
|
269
|
+
resolve: ProcedureResolver<TContext, TInput, TOutput>;
|
|
270
|
+
};
|
|
271
|
+
|
|
216
272
|
export type CreateProcedureWithInputOutputParser<
|
|
217
273
|
TContext,
|
|
274
|
+
TMeta,
|
|
218
275
|
TInput,
|
|
219
276
|
TParsedInput,
|
|
220
277
|
TOutput,
|
|
278
|
+
TParsedOutput,
|
|
221
279
|
> = {
|
|
222
|
-
input:
|
|
280
|
+
input: ProcedureParserWithInputOutput<TInput, TParsedInput>;
|
|
281
|
+
output?: ProcedureParserWithInputOutput<TOutput, TParsedOutput>;
|
|
282
|
+
meta?: TMeta;
|
|
223
283
|
resolve: ProcedureResolver<TContext, TParsedInput, TOutput>;
|
|
224
284
|
};
|
|
225
|
-
|
|
285
|
+
|
|
286
|
+
export type CreateProcedureWithoutInput<
|
|
287
|
+
TContext,
|
|
288
|
+
TMeta,
|
|
289
|
+
TOutput,
|
|
290
|
+
TParsedOutput,
|
|
291
|
+
> = {
|
|
292
|
+
output?:
|
|
293
|
+
| ProcedureParserWithInputOutput<TOutput, TParsedOutput>
|
|
294
|
+
| ProcedureParser<TOutput>;
|
|
295
|
+
meta?: TMeta;
|
|
226
296
|
resolve: ProcedureResolver<TContext, undefined, TOutput>;
|
|
227
297
|
};
|
|
228
298
|
|
|
229
299
|
export type CreateProcedureOptions<
|
|
230
300
|
TContext,
|
|
301
|
+
TMeta = undefined,
|
|
231
302
|
TInput = undefined,
|
|
232
303
|
TParsedInput = undefined,
|
|
233
304
|
TOutput = undefined,
|
|
305
|
+
TParsedOutput = undefined,
|
|
234
306
|
> =
|
|
235
|
-
| CreateProcedureWithInput<TContext, TInput, TParsedInput, TOutput>
|
|
236
307
|
| CreateProcedureWithInputOutputParser<
|
|
237
308
|
TContext,
|
|
309
|
+
TMeta,
|
|
238
310
|
TInput,
|
|
239
311
|
TParsedInput,
|
|
240
|
-
TOutput
|
|
312
|
+
TOutput,
|
|
313
|
+
TParsedOutput
|
|
241
314
|
>
|
|
242
|
-
|
|
|
315
|
+
| CreateProcedureWithInput<TContext, TMeta, TInput, TOutput>
|
|
316
|
+
| CreateProcedureWithoutInput<TContext, TMeta, TOutput, TParsedOutput>;
|
|
243
317
|
|
|
244
|
-
export function createProcedure<
|
|
245
|
-
|
|
246
|
-
|
|
318
|
+
export function createProcedure<
|
|
319
|
+
TContext,
|
|
320
|
+
TMeta,
|
|
321
|
+
TInput,
|
|
322
|
+
TParsedInput,
|
|
323
|
+
TOutput,
|
|
324
|
+
TParsedOutput,
|
|
325
|
+
>(
|
|
326
|
+
opts: CreateProcedureOptions<
|
|
327
|
+
TContext,
|
|
328
|
+
TMeta,
|
|
329
|
+
TInput,
|
|
330
|
+
TParsedInput,
|
|
331
|
+
TOutput,
|
|
332
|
+
TParsedOutput
|
|
333
|
+
>,
|
|
334
|
+
): Procedure<
|
|
335
|
+
unknown,
|
|
336
|
+
TContext,
|
|
337
|
+
TMeta,
|
|
338
|
+
TInput,
|
|
339
|
+
TParsedInput,
|
|
340
|
+
TOutput,
|
|
341
|
+
TParsedOutput
|
|
342
|
+
> {
|
|
247
343
|
const inputParser =
|
|
248
344
|
'input' in opts
|
|
249
345
|
? opts.input
|
|
@@ -257,27 +353,38 @@ export function createProcedure<TContext, TInput, TParsedInput, TOutput>(
|
|
|
257
353
|
return undefined;
|
|
258
354
|
};
|
|
259
355
|
|
|
356
|
+
const outputParser =
|
|
357
|
+
'output' in opts && opts.output
|
|
358
|
+
? opts.output
|
|
359
|
+
: (output: unknown) => output as TOutput;
|
|
360
|
+
|
|
260
361
|
return new Procedure({
|
|
261
362
|
inputParser: inputParser as any,
|
|
262
363
|
resolver: opts.resolve as any,
|
|
263
364
|
middlewares: [],
|
|
365
|
+
outputParser: outputParser as any,
|
|
366
|
+
meta: opts.meta as any,
|
|
264
367
|
});
|
|
265
368
|
}
|
|
266
369
|
|
|
267
370
|
export type inferProcedureFromOptions<
|
|
268
371
|
TInputContext,
|
|
269
|
-
TOptions extends CreateProcedureOptions<any, any, any, any>,
|
|
372
|
+
TOptions extends CreateProcedureOptions<any, any, any, any, any, any>,
|
|
270
373
|
> = TOptions extends CreateProcedureOptions<
|
|
271
374
|
infer TContext,
|
|
375
|
+
infer TMeta,
|
|
272
376
|
infer TInput,
|
|
273
377
|
infer TParsedInput,
|
|
274
|
-
infer TOutput
|
|
378
|
+
infer TOutput,
|
|
379
|
+
infer TParsedOutput
|
|
275
380
|
>
|
|
276
381
|
? Procedure<
|
|
277
382
|
TInputContext,
|
|
278
383
|
TContext,
|
|
384
|
+
TMeta,
|
|
279
385
|
unknown extends TInput ? undefined : TInput,
|
|
280
386
|
unknown extends TParsedInput ? undefined : TParsedInput,
|
|
281
|
-
TOutput
|
|
387
|
+
TOutput,
|
|
388
|
+
TParsedOutput
|
|
282
389
|
>
|
|
283
390
|
: never;
|