@orpc/server 2.0.0-beta.49 → 2.0.0-beta.50
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/dist/adapters/aws-lambda/handler.d.ts +17 -15
- package/dist/adapters/aws-lambda/handler.js +23 -23
- package/dist/adapters/aws-lambda/index.d.ts +4 -16
- package/dist/adapters/aws-lambda/index.js +2 -2
- package/dist/adapters/aws-lambda/rpc-handler.d.ts +9 -9
- package/dist/adapters/aws-lambda/rpc-handler.js +20 -17
- package/dist/adapters/crossws/handler.d.ts +44 -42
- package/dist/adapters/crossws/handler.js +53 -60
- package/dist/adapters/crossws/index.d.ts +3 -2
- package/dist/adapters/crossws/index.js +2 -2
- package/dist/adapters/crossws/rpc-handler.d.ts +10 -10
- package/dist/adapters/crossws/rpc-handler.js +17 -14
- package/dist/adapters/fastify/handler.d.ts +17 -15
- package/dist/adapters/fastify/handler.js +20 -20
- package/dist/adapters/fastify/index.d.ts +3 -2
- package/dist/adapters/fastify/index.js +2 -2
- package/dist/adapters/fastify/rpc-handler.d.ts +9 -9
- package/dist/adapters/fastify/rpc-handler.js +16 -13
- package/dist/adapters/fetch/handler.d.ts +19 -17
- package/dist/adapters/fetch/handler.js +22 -22
- package/dist/adapters/fetch/index.d.ts +4 -7
- package/dist/adapters/fetch/index.js +3 -7
- package/dist/adapters/fetch/rpc-handler.d.ts +9 -9
- package/dist/adapters/fetch/rpc-handler.js +18 -15
- package/dist/adapters/message-port/handler.d.ts +58 -60
- package/dist/adapters/message-port/handler.js +72 -86
- package/dist/adapters/message-port/index.d.ts +3 -2
- package/dist/adapters/message-port/index.js +2 -2
- package/dist/adapters/message-port/rpc-handler.d.ts +9 -9
- package/dist/adapters/message-port/rpc-handler.js +17 -14
- package/dist/adapters/node/handler.d.ts +17 -15
- package/dist/adapters/node/handler.js +20 -20
- package/dist/adapters/node/index.d.ts +4 -7
- package/dist/adapters/node/index.js +3 -7
- package/dist/adapters/node/rpc-handler.d.ts +9 -9
- package/dist/adapters/node/rpc-handler.js +17 -14
- package/dist/adapters/standard/codec.d.ts +13 -13
- package/dist/adapters/standard/handler.d.ts +49 -49
- package/dist/adapters/standard/handler.js +205 -205
- package/dist/adapters/standard/index.d.ts +7 -6
- package/dist/adapters/standard/index.js +4 -5
- package/dist/adapters/standard/plugin.d.ts +25 -31
- package/dist/adapters/standard/plugin.js +14 -16
- package/dist/adapters/standard/rpc-handler-codec.d.ts +39 -43
- package/dist/adapters/standard/rpc-handler-codec.js +50 -51
- package/dist/adapters/standard/rpc-matcher.d.ts +40 -40
- package/dist/adapters/standard/rpc-matcher.js +103 -149
- package/dist/adapters/standard/utils.d.ts +8 -8
- package/dist/adapters/standard/utils.js +7 -5
- package/dist/adapters/standard-peer/index.d.ts +2 -1
- package/dist/adapters/standard-peer/index.js +1 -1
- package/dist/adapters/standard-peer/utils.d.ts +6 -4
- package/dist/adapters/standard-peer/utils.js +12 -6
- package/dist/adapters/websocket/handler.d.ts +52 -50
- package/dist/adapters/websocket/handler.js +75 -85
- package/dist/adapters/websocket/index.d.ts +3 -2
- package/dist/adapters/websocket/index.js +2 -2
- package/dist/adapters/websocket/rpc-handler.d.ts +9 -9
- package/dist/adapters/websocket/rpc-handler.js +17 -14
- package/dist/builder-variants.d.ts +195 -195
- package/dist/builder.d.ts +103 -104
- package/dist/builder.js +198 -224
- package/dist/constants.d.ts +1 -1
- package/dist/constants.js +4 -2
- package/dist/context.d.ts +3 -3
- package/dist/extensions/callable.d.ts +11 -11
- package/dist/extensions/callable.js +6 -5
- package/dist/helpers/base64url.d.ts +1 -1
- package/dist/helpers/base64url.js +44 -49
- package/dist/helpers/cookie.d.ts +10 -11
- package/dist/helpers/cookie.js +56 -63
- package/dist/helpers/encryption.d.ts +1 -1
- package/dist/helpers/encryption.js +80 -70
- package/dist/helpers/index.d.ts +5 -4
- package/dist/helpers/index.js +4 -4
- package/dist/helpers/signing.d.ts +1 -1
- package/dist/helpers/signing.js +72 -82
- package/dist/implementer-procedure.d.ts +12 -13
- package/dist/implementer-procedure.js +37 -44
- package/dist/implementer-router.d.ts +30 -36
- package/dist/implementer-router.js +92 -111
- package/dist/implementer.d.ts +8 -8
- package/dist/implementer.js +28 -35
- package/dist/index.d.ts +22 -61
- package/dist/index.js +16 -48
- package/dist/lazy.d.ts +14 -14
- package/dist/lazy.js +19 -30
- package/dist/middleware-decorated.d.ts +8 -9
- package/dist/middleware-decorated.js +66 -70
- package/dist/middleware.d.ts +34 -34
- package/dist/plugins/batch.d.ts +75 -75
- package/dist/plugins/batch.js +345 -379
- package/dist/plugins/cors.d.ts +60 -64
- package/dist/plugins/cors.js +118 -111
- package/dist/plugins/get-method-csrf-protection.d.ts +8 -6
- package/dist/plugins/get-method-csrf-protection.js +47 -63
- package/dist/plugins/head-method.d.ts +11 -9
- package/dist/plugins/head-method.js +97 -94
- package/dist/plugins/index.d.ts +15 -39
- package/dist/plugins/index.js +14 -29
- package/dist/plugins/method-override.d.ts +30 -28
- package/dist/plugins/method-override.js +53 -49
- package/dist/plugins/request-compression.d.ts +11 -9
- package/dist/plugins/request-compression.js +52 -79
- package/dist/plugins/request-headers.d.ts +10 -8
- package/dist/plugins/request-headers.js +27 -29
- package/dist/plugins/request-limit.d.ts +21 -19
- package/dist/plugins/request-limit.js +60 -67
- package/dist/plugins/response-compression.d.ts +42 -39
- package/dist/plugins/response-compression.js +150 -202
- package/dist/plugins/response-headers.d.ts +15 -13
- package/dist/plugins/response-headers.js +43 -46
- package/dist/plugins/rethrow.d.ts +25 -23
- package/dist/plugins/rethrow.js +46 -58
- package/dist/plugins/signal-sync.d.ts +7 -5
- package/dist/plugins/signal-sync.js +71 -86
- package/dist/plugins/timeout.d.ts +29 -27
- package/dist/plugins/timeout.js +61 -58
- package/dist/procedure-client.d.ts +14 -15
- package/dist/procedure-client.js +149 -166
- package/dist/procedure-decorated.d.ts +9 -9
- package/dist/procedure-decorated.js +36 -44
- package/dist/procedure-utils.d.ts +10 -30
- package/dist/procedure-utils.js +39 -40
- package/dist/procedure.d.ts +60 -60
- package/dist/procedure.js +18 -24
- package/dist/router-client.d.ts +9 -11
- package/dist/router-client.js +33 -41
- package/dist/router-hidden.js +10 -12
- package/dist/router-utils.d.ts +13 -30
- package/dist/router-utils.js +146 -191
- package/dist/router.d.ts +12 -26
- package/package.json +10 -10
- package/dist/adapters/standard/codec.js +0 -1
- package/dist/builder-variants.js +0 -1
- package/dist/context.js +0 -1
- package/dist/middleware.js +0 -1
- package/dist/router-hidden.d.ts +0 -5
- package/dist/router.js +0 -1
package/dist/procedure-client.js
CHANGED
|
@@ -1,175 +1,158 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { createORPCErrorConstructorMap, reconcileORPCError
|
|
3
|
-
import { getTracer, intercept, isAsyncIteratorObject, mergeTwoLevels, override, resolveMaybeOptionalOptions, runWithSpan, toArray, traceAsyncIterator, traceReadableStream, value
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
* Remember use `override` for ReadableStream to remain other special properties
|
|
56
|
-
*/
|
|
57
|
-
return override(output, traceReadableStream('consume_octet_stream_output', output));
|
|
58
|
-
}
|
|
59
|
-
return output;
|
|
60
|
-
}
|
|
61
|
-
catch (e) {
|
|
62
|
-
throw await reconcileError(e);
|
|
63
|
-
}
|
|
64
|
-
};
|
|
1
|
+
import { unlazy } from "./lazy.js";
|
|
2
|
+
import { ValidationError, createORPCErrorConstructorMap, reconcileORPCError } from "@orpc/contract";
|
|
3
|
+
import { getTracer, intercept, isAsyncIteratorObject, mergeTwoLevels, override, resolveMaybeOptionalOptions, runWithSpan, toArray, traceAsyncIterator, traceReadableStream, value } from "@orpc/shared";
|
|
4
|
+
import { ORPCError, wrapAsyncIteratorPreservingEventMeta } from "@orpc/client";
|
|
5
|
+
// src/procedure-client.ts
|
|
6
|
+
function createProcedureClient(lazyableProcedure, ...rest) {
|
|
7
|
+
const options = resolveMaybeOptionalOptions(rest);
|
|
8
|
+
return async (...[input, callerOptions]) => {
|
|
9
|
+
const path = toArray(options.path);
|
|
10
|
+
const { default: procedure } = await unlazy(lazyableProcedure);
|
|
11
|
+
const clientContext = callerOptions?.context ?? {};
|
|
12
|
+
const context = await value(options.context, clientContext) ?? {};
|
|
13
|
+
const errors = createORPCErrorConstructorMap(procedure["~orpc"].errorMap);
|
|
14
|
+
const reconcileError = async (e) => {
|
|
15
|
+
if (e instanceof ORPCError) return await reconcileORPCError(procedure["~orpc"].errorMap, e);
|
|
16
|
+
return e;
|
|
17
|
+
};
|
|
18
|
+
try {
|
|
19
|
+
const output = await runWithSpan("call_procedure", (span) => {
|
|
20
|
+
span?.setAttribute("procedure.path", path);
|
|
21
|
+
return intercept(options.interceptors, {
|
|
22
|
+
context,
|
|
23
|
+
input,
|
|
24
|
+
errors,
|
|
25
|
+
path,
|
|
26
|
+
procedure,
|
|
27
|
+
signal: callerOptions?.signal,
|
|
28
|
+
lastEventId: callerOptions?.lastEventId
|
|
29
|
+
}, (interceptorOptions) => executeProcedureInternal(interceptorOptions.procedure, interceptorOptions));
|
|
30
|
+
});
|
|
31
|
+
if (isAsyncIteratorObject(output))
|
|
32
|
+
/**
|
|
33
|
+
* traceAsyncIterator/wrapAsyncIteratorPreservingEventMeta return AsyncIteratorClass
|
|
34
|
+
* which is backwards compatible with AsyncIteratorObject.
|
|
35
|
+
*
|
|
36
|
+
* @warning
|
|
37
|
+
* If remove this return, can be breaking change
|
|
38
|
+
* because AsyncIteratorClass convert `.throw` to `.return` (rarely used)
|
|
39
|
+
*
|
|
40
|
+
* @warning
|
|
41
|
+
* Remember use `override` for AsyncIteratorObject to remain other special properties
|
|
42
|
+
*/
|
|
43
|
+
return override(output, wrapAsyncIteratorPreservingEventMeta(getTracer() ? traceAsyncIterator("consume_async_iterator_object_output", output) : output, { mapError: reconcileError }));
|
|
44
|
+
else if (getTracer() && output instanceof ReadableStream)
|
|
45
|
+
/**
|
|
46
|
+
* @warning
|
|
47
|
+
* Remember use `override` for ReadableStream to remain other special properties
|
|
48
|
+
*/
|
|
49
|
+
return override(output, traceReadableStream("consume_octet_stream_output", output));
|
|
50
|
+
return output;
|
|
51
|
+
} catch (e) {
|
|
52
|
+
throw await reconcileError(e);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
65
55
|
}
|
|
66
56
|
async function validateInput(i, schema, input) {
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
return result.value;
|
|
84
|
-
});
|
|
57
|
+
return runWithSpan(`validate_input.${i}`, async (span) => {
|
|
58
|
+
span?.setAttribute("input_schema.index", i);
|
|
59
|
+
const result = await schema["~standard"].validate(input);
|
|
60
|
+
if (result.issues) throw new ORPCError("BAD_REQUEST", {
|
|
61
|
+
message: "Input validation failed",
|
|
62
|
+
data: { issues: result.issues },
|
|
63
|
+
cause: new ValidationError({
|
|
64
|
+
message: "Input validation failed",
|
|
65
|
+
issues: result.issues,
|
|
66
|
+
invalidData: input
|
|
67
|
+
})
|
|
68
|
+
});
|
|
69
|
+
return result.value;
|
|
70
|
+
});
|
|
85
71
|
}
|
|
86
72
|
async function validateOutput(i, schema, output) {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
return result.value;
|
|
101
|
-
});
|
|
73
|
+
return runWithSpan(`validate_output.${i}`, async (span) => {
|
|
74
|
+
span?.setAttribute("output_schema.index", i);
|
|
75
|
+
const result = await schema["~standard"].validate(output);
|
|
76
|
+
if (result.issues) throw new ORPCError("INTERNAL_SERVER_ERROR", {
|
|
77
|
+
message: "Output validation failed",
|
|
78
|
+
cause: new ValidationError({
|
|
79
|
+
message: "Output validation failed",
|
|
80
|
+
issues: result.issues,
|
|
81
|
+
invalidData: output
|
|
82
|
+
})
|
|
83
|
+
});
|
|
84
|
+
return result.value;
|
|
85
|
+
});
|
|
102
86
|
}
|
|
103
87
|
const middlewareDone = (...rest) => {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
};
|
|
88
|
+
const options = resolveMaybeOptionalOptions(rest);
|
|
89
|
+
return {
|
|
90
|
+
output: options.output,
|
|
91
|
+
context: options.context ?? {}
|
|
92
|
+
};
|
|
110
93
|
};
|
|
111
94
|
async function executeProcedureInternal(procedure, options) {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
return output;
|
|
95
|
+
const inputSchemas = toArray(procedure["~orpc"].inputSchemas);
|
|
96
|
+
const outputSchemas = toArray(procedure["~orpc"].outputSchemas);
|
|
97
|
+
const orderedMiddlewares = procedure["~orpc"].orderedMiddlewares;
|
|
98
|
+
const next = async (midIndex, context, input, signal) => {
|
|
99
|
+
let currentInput = input;
|
|
100
|
+
let middlewareInput = input;
|
|
101
|
+
const startInputIndex = midIndex === 0 ? 0 : orderedMiddlewares[midIndex - 1].inputSchemasLengthAtUse ?? 0;
|
|
102
|
+
const endInputIndex = midIndex === orderedMiddlewares.length ? inputSchemas.length : orderedMiddlewares[midIndex].inputSchemasLengthAtUse ?? 0;
|
|
103
|
+
/**
|
|
104
|
+
* Each schema after the first validates the original input with the results so far applied, then
|
|
105
|
+
* its result is merged in, so no schema loses a field an earlier one stripped. Values that are not
|
|
106
|
+
* plain objects stay piped, which keeps schemas like `asyncIteratorObject` wrapping each other.
|
|
107
|
+
*/
|
|
108
|
+
if (!procedure["~orpc"].disableInputValidation) {
|
|
109
|
+
for (let i = startInputIndex; i < endInputIndex; i++) {
|
|
110
|
+
const validated = await validateInput(i, inputSchemas[i], i !== 0 ? mergeTwoLevels(options.input, currentInput) : currentInput);
|
|
111
|
+
currentInput = i !== 0 ? mergeTwoLevels(currentInput, validated) : validated;
|
|
112
|
+
}
|
|
113
|
+
middlewareInput = endInputIndex > 0 && endInputIndex < inputSchemas.length ? mergeTwoLevels(options.input, currentInput) : currentInput;
|
|
114
|
+
}
|
|
115
|
+
let currentOutput;
|
|
116
|
+
let currentContext = context;
|
|
117
|
+
if (midIndex < orderedMiddlewares.length) {
|
|
118
|
+
const { middleware } = orderedMiddlewares[midIndex];
|
|
119
|
+
const result = await runWithSpan(`middleware.${middleware.name}`, async (span) => {
|
|
120
|
+
span?.setAttribute("middleware.index", midIndex);
|
|
121
|
+
return await middleware({
|
|
122
|
+
...options,
|
|
123
|
+
context,
|
|
124
|
+
signal,
|
|
125
|
+
next: (...rest) => {
|
|
126
|
+
const nextOptions = resolveMaybeOptionalOptions(rest);
|
|
127
|
+
const nextContext = nextOptions.context ?? {};
|
|
128
|
+
return next(midIndex + 1, {
|
|
129
|
+
...context,
|
|
130
|
+
...nextContext
|
|
131
|
+
}, currentInput, nextOptions.signal ?? signal);
|
|
132
|
+
},
|
|
133
|
+
lastEventId: options.lastEventId
|
|
134
|
+
}, middlewareInput, middlewareDone);
|
|
135
|
+
});
|
|
136
|
+
currentOutput = result.output;
|
|
137
|
+
currentContext = {
|
|
138
|
+
...context,
|
|
139
|
+
...result.context
|
|
140
|
+
};
|
|
141
|
+
} else currentOutput = await runWithSpan("handler", () => procedure["~orpc"].handler({
|
|
142
|
+
...options,
|
|
143
|
+
context,
|
|
144
|
+
input: currentInput,
|
|
145
|
+
signal
|
|
146
|
+
}, currentInput));
|
|
147
|
+
const startOutputIndex = midIndex === 0 ? 0 : orderedMiddlewares[midIndex - 1].outputSchemasLengthAtUse ?? 0;
|
|
148
|
+
const endOutputIndex = midIndex === orderedMiddlewares.length ? outputSchemas.length : orderedMiddlewares[midIndex].outputSchemasLengthAtUse ?? 0;
|
|
149
|
+
if (!procedure["~orpc"].disableOutputValidation) for (let i = endOutputIndex - 1; i >= startOutputIndex; i--) currentOutput = await validateOutput(i, outputSchemas[i], currentOutput);
|
|
150
|
+
return {
|
|
151
|
+
output: currentOutput,
|
|
152
|
+
context: currentContext
|
|
153
|
+
};
|
|
154
|
+
};
|
|
155
|
+
const { output } = await next(0, options.context, options.input, options.signal);
|
|
156
|
+
return output;
|
|
175
157
|
}
|
|
158
|
+
export { createProcedureClient };
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import {
|
|
1
|
+
import { Context, MergedContext, MergedInitialContext } from "./context.js";
|
|
2
|
+
import { Middleware } from "./middleware.js";
|
|
3
|
+
import { Procedure } from "./procedure.js";
|
|
4
|
+
import { AnySchema, ErrorMap, InferSchemaInput, InferSchemaOutput, MergedErrorMap, MetaPlugin } from "@orpc/contract";
|
|
5
|
+
import { IntersectPick } from "@orpc/shared";
|
|
6
6
|
export declare class DecoratedProcedure<TInitialContext extends Context, TInjectedContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> extends Procedure<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, TErrorMap> {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
7
|
+
meta(...plugins: MetaPlugin<TInputSchema, TOutputSchema, TErrorMap>[]): DecoratedProcedure<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, TErrorMap>;
|
|
8
|
+
errors<T extends ErrorMap>(errors: T): DecoratedProcedure<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, MergedErrorMap<TErrorMap, T>>;
|
|
9
|
+
use<$OutContext extends IntersectPick<MergedContext<TInitialContext, TInjectedContext>, $OutContext>, $InContext extends Context = MergedContext<TInitialContext, TInjectedContext>, $ErrorMap extends ErrorMap = TErrorMap>(middleware: Middleware<$InContext | MergedContext<TInitialContext, TInjectedContext>, $OutContext, InferSchemaOutput<TInputSchema>, InferSchemaInput<TOutputSchema>, $ErrorMap>): DecoratedProcedure<MergedInitialContext<TInitialContext, TInjectedContext, $InContext>, MergedContext<TInjectedContext, $OutContext>, TInputSchema, TOutputSchema, MergedErrorMap<$ErrorMap, TErrorMap>>;
|
|
10
|
+
}
|
|
@@ -1,44 +1,36 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
],
|
|
38
|
-
});
|
|
39
|
-
if (middleware['~orpc']?.metaPlugins) {
|
|
40
|
-
procedure = procedure.meta(...middleware['~orpc']?.metaPlugins);
|
|
41
|
-
}
|
|
42
|
-
return procedure;
|
|
43
|
-
}
|
|
44
|
-
}
|
|
1
|
+
import { Procedure } from "./procedure.js";
|
|
2
|
+
import { getHiddenMetaPlugins, mergeErrorMap, resolveMetaPlugins } from "@orpc/contract";
|
|
3
|
+
// src/procedure-decorated.ts
|
|
4
|
+
var DecoratedProcedure = class DecoratedProcedure extends Procedure {
|
|
5
|
+
meta(...plugins) {
|
|
6
|
+
const [meta, metaPlugins] = resolveMetaPlugins(this["~orpc"].meta, this["~orpc"].metaPlugins, plugins);
|
|
7
|
+
return new DecoratedProcedure({
|
|
8
|
+
...this["~orpc"],
|
|
9
|
+
meta,
|
|
10
|
+
metaPlugins
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
errors(errors) {
|
|
14
|
+
let procedure = new DecoratedProcedure({
|
|
15
|
+
...this["~orpc"],
|
|
16
|
+
errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
|
|
17
|
+
});
|
|
18
|
+
const plugins = getHiddenMetaPlugins(errors);
|
|
19
|
+
if (plugins) procedure = procedure.meta(...plugins);
|
|
20
|
+
return procedure;
|
|
21
|
+
}
|
|
22
|
+
use(middleware) {
|
|
23
|
+
let procedure = new DecoratedProcedure({
|
|
24
|
+
...this["~orpc"],
|
|
25
|
+
errorMap: mergeErrorMap(middleware["~orpc"]?.errorMap, this["~orpc"].errorMap),
|
|
26
|
+
orderedMiddlewares: [...this["~orpc"].orderedMiddlewares, {
|
|
27
|
+
middleware,
|
|
28
|
+
inputSchemasLengthAtUse: this["~orpc"].inputSchemas?.length,
|
|
29
|
+
outputSchemasLengthAtUse: this["~orpc"].outputSchemas?.length
|
|
30
|
+
}]
|
|
31
|
+
});
|
|
32
|
+
if (middleware["~orpc"]?.metaPlugins) procedure = procedure.meta(...middleware["~orpc"]?.metaPlugins);
|
|
33
|
+
return procedure;
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
export { DecoratedProcedure };
|
|
@@ -1,32 +1,12 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
import {
|
|
7
|
-
import
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
export declare function createGuardedProcedureLazy(lazy: Lazy<unknown>): Lazy<AnyProcedure>;
|
|
11
|
-
/**
|
|
12
|
-
* Create a new procedure that ensure the contract is applied to the procedure.
|
|
13
|
-
*
|
|
14
|
-
* .input/.output/.handler mismatches are already caught by the `implementer.router`,
|
|
15
|
-
* but .errors (in case implemented procedure error map is more than enough) and .meta cannot be
|
|
16
|
-
* validated that way - so they must be overridden here to ensure the client always sees what the contract defines.
|
|
17
|
-
*/
|
|
18
|
-
export declare function createContractProcedure<TInitialContext extends Context, TInjectedContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap>(procedure: Procedure<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, any>, contract: ProcedureContract<any, any, TErrorMap>): Procedure<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, TErrorMap>;
|
|
19
|
-
export type CallOptions<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> = ProcedureClientOptions<TInitialContext, TOutputSchema, TErrorMap, object> & Omit<ClientOptions<object>, 'context'>;
|
|
20
|
-
export type CallRest<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> = object extends CallOptions<TInitialContext, TOutputSchema, TErrorMap> ? undefined extends InferSchemaInput<TInputSchema> ? [
|
|
21
|
-
input?: InferSchemaInput<TInputSchema>,
|
|
22
|
-
options?: CallOptions<TInitialContext, TOutputSchema, TErrorMap>
|
|
23
|
-
] : [
|
|
24
|
-
input: InferSchemaInput<TInputSchema>,
|
|
25
|
-
options?: CallOptions<TInitialContext, TOutputSchema, TErrorMap>
|
|
26
|
-
] : [
|
|
27
|
-
input: InferSchemaInput<TInputSchema>,
|
|
28
|
-
options: CallOptions<TInitialContext, TOutputSchema, TErrorMap>
|
|
29
|
-
];
|
|
1
|
+
import { Context } from "./context.js";
|
|
2
|
+
import { Lazyable } from "./lazy.js";
|
|
3
|
+
import { Procedure } from "./procedure.js";
|
|
4
|
+
import { ProcedureClientOptions } from "./procedure-client.js";
|
|
5
|
+
import { AnySchema, ErrorMap, InferSchemaInput, InferSchemaOutput, ORPCErrorFromErrorMap } from "@orpc/contract";
|
|
6
|
+
import { PromiseWithError, ThrowableError as ThrowableError$1 } from "@orpc/shared";
|
|
7
|
+
import { ClientOptions } from "@orpc/client";
|
|
8
|
+
export type CallOptions<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> = ProcedureClientOptions<TInitialContext, TOutputSchema, TErrorMap, object> & Omit<ClientOptions<object>, "context">;
|
|
9
|
+
export type CallRest<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap> = object extends CallOptions<TInitialContext, TOutputSchema, TErrorMap> ? undefined extends InferSchemaInput<TInputSchema> ? [input?: InferSchemaInput<TInputSchema>, options?: CallOptions<TInitialContext, TOutputSchema, TErrorMap>] : [input: InferSchemaInput<TInputSchema>, options?: CallOptions<TInitialContext, TOutputSchema, TErrorMap>] : [input: InferSchemaInput<TInputSchema>, options: CallOptions<TInitialContext, TOutputSchema, TErrorMap>];
|
|
30
10
|
/**
|
|
31
11
|
* Quickly call a procedure without creating a client.
|
|
32
12
|
*
|
|
@@ -38,4 +18,4 @@ export type CallRest<TInitialContext extends Context, TInputSchema extends AnySc
|
|
|
38
18
|
*
|
|
39
19
|
* @see {@link https://orpc.dev/docs/client/server-side#one-off-calls | Server-Side Clients - One-Off Calls}
|
|
40
20
|
*/
|
|
41
|
-
export declare function call<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, TErrorMap>>, ...[input, options
|
|
21
|
+
export declare function call<TInitialContext extends Context, TInputSchema extends AnySchema, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TInputSchema, TOutputSchema, TErrorMap>>, ...[input, options]: CallRest<TInitialContext, TInputSchema, TOutputSchema, TErrorMap>): PromiseWithError<InferSchemaOutput<TOutputSchema>, ORPCErrorFromErrorMap<TErrorMap> | ThrowableError$1>;
|
package/dist/procedure-utils.js
CHANGED
|
@@ -1,49 +1,48 @@
|
|
|
1
|
-
import { Lazy, unlazy } from
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
import { Lazy, unlazy } from "./lazy.js";
|
|
2
|
+
import { Procedure } from "./procedure.js";
|
|
3
|
+
import { createProcedureClient } from "./procedure-client.js";
|
|
4
|
+
// src/procedure-utils.ts
|
|
5
|
+
function createGuardedProcedureLazy(lazy) {
|
|
6
|
+
return new Lazy({
|
|
7
|
+
...lazy["~orpc"],
|
|
8
|
+
async loader() {
|
|
9
|
+
const { default: maybeProcedure } = await unlazy(lazy);
|
|
10
|
+
if (!(maybeProcedure instanceof Procedure)) throw new TypeError(`
|
|
11
11
|
Expected a lazy<procedure> but got lazy<unknown>.
|
|
12
12
|
This should be caught by TypeScript compilation.
|
|
13
13
|
Please report this issue if you think this is a bug.
|
|
14
14
|
`);
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
});
|
|
19
|
-
return guarded;
|
|
15
|
+
return { default: maybeProcedure };
|
|
16
|
+
}
|
|
17
|
+
});
|
|
20
18
|
}
|
|
21
19
|
/**
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
20
|
+
* Create a new procedure that ensure the contract is applied to the procedure.
|
|
21
|
+
*
|
|
22
|
+
* .input/.output/.handler mismatches are already caught by the `implementer.router`,
|
|
23
|
+
* but .errors (in case implemented procedure error map is more than enough) and .meta cannot be
|
|
24
|
+
* validated that way - so they must be overridden here to ensure the client always sees what the contract defines.
|
|
25
|
+
*/
|
|
26
|
+
function createContractProcedure(procedure, contract) {
|
|
27
|
+
return new Procedure({
|
|
28
|
+
...procedure["~orpc"],
|
|
29
|
+
errorMap: contract["~orpc"].errorMap,
|
|
30
|
+
meta: contract["~orpc"].meta,
|
|
31
|
+
metaPlugins: contract["~orpc"].metaPlugins
|
|
32
|
+
});
|
|
35
33
|
}
|
|
36
34
|
/**
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
35
|
+
* Quickly call a procedure without creating a client.
|
|
36
|
+
*
|
|
37
|
+
* @example
|
|
38
|
+
* ```ts
|
|
39
|
+
* const output = await call(getting, 'input')
|
|
40
|
+
* const output = await call(getting, 'input', { context: { db: 'postgres' } })
|
|
41
|
+
* ```
|
|
42
|
+
*
|
|
43
|
+
* @see {@link https://orpc.dev/docs/client/server-side#one-off-calls | Server-Side Clients - One-Off Calls}
|
|
44
|
+
*/
|
|
45
|
+
function call(lazyableProcedure, ...[input = void 0, options = {}]) {
|
|
46
|
+
return createProcedureClient(lazyableProcedure, options)(input, options);
|
|
49
47
|
}
|
|
48
|
+
export { call, createContractProcedure, createGuardedProcedureLazy };
|