@orpc/server 2.0.0-beta.46 → 2.0.0-beta.48
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/README.md +6 -4
- package/dist/adapters/aws-lambda/handler.d.mts +25 -0
- package/dist/adapters/aws-lambda/handler.mjs +21 -0
- package/dist/adapters/aws-lambda/index.d.mts +14 -49
- package/dist/adapters/aws-lambda/index.mjs +2 -39
- package/dist/adapters/aws-lambda/rpc-handler.d.mts +18 -0
- package/dist/adapters/aws-lambda/rpc-handler.mjs +10 -0
- package/dist/adapters/crossws/handler.d.mts +41 -0
- package/dist/adapters/crossws/handler.mjs +38 -0
- package/dist/adapters/crossws/index.d.mts +2 -63
- package/dist/adapters/crossws/index.mjs +2 -68
- package/dist/adapters/crossws/rpc-handler.d.mts +15 -0
- package/dist/adapters/crossws/rpc-handler.mjs +10 -0
- package/dist/adapters/fastify/handler.d.mts +21 -0
- package/dist/adapters/fastify/handler.mjs +20 -0
- package/dist/adapters/fastify/index.d.mts +2 -42
- package/dist/adapters/fastify/index.mjs +2 -39
- package/dist/adapters/fastify/rpc-handler.d.mts +14 -0
- package/dist/adapters/fastify/rpc-handler.mjs +10 -0
- package/dist/adapters/fetch/handler.d.mts +23 -0
- package/dist/adapters/fetch/handler.mjs +22 -0
- package/dist/adapters/fetch/index.d.mts +6 -46
- package/dist/adapters/fetch/index.mjs +4 -41
- package/dist/adapters/fetch/rpc-handler.d.mts +16 -0
- package/dist/adapters/fetch/rpc-handler.mjs +10 -0
- package/dist/adapters/message-port/handler.d.mts +57 -0
- package/dist/adapters/message-port/handler.mjs +62 -0
- package/dist/adapters/message-port/index.d.mts +2 -77
- package/dist/adapters/message-port/index.mjs +2 -87
- package/dist/adapters/message-port/rpc-handler.d.mts +15 -0
- package/dist/adapters/message-port/rpc-handler.mjs +10 -0
- package/dist/adapters/node/handler.d.mts +21 -0
- package/dist/adapters/node/handler.mjs +20 -0
- package/dist/adapters/node/index.d.mts +6 -43
- package/dist/adapters/node/index.mjs +4 -40
- package/dist/adapters/node/rpc-handler.d.mts +15 -0
- package/dist/adapters/node/rpc-handler.mjs +10 -0
- package/dist/adapters/standard/codec.d.mts +16 -0
- package/dist/adapters/standard/codec.mjs +1 -0
- package/dist/adapters/standard/handler.d.mts +68 -0
- package/dist/adapters/standard/handler.mjs +185 -0
- package/dist/adapters/standard/index.d.mts +6 -95
- package/dist/adapters/standard/index.mjs +5 -8
- package/dist/adapters/standard/plugin.d.mts +32 -0
- package/dist/adapters/standard/plugin.mjs +16 -0
- package/dist/adapters/standard/rpc-handler-codec.d.mts +42 -0
- package/dist/adapters/standard/rpc-handler-codec.mjs +51 -0
- package/dist/adapters/standard/rpc-matcher.d.mts +53 -0
- package/dist/adapters/standard/rpc-matcher.mjs +140 -0
- package/dist/adapters/standard/utils.d.mts +10 -0
- package/dist/adapters/standard/utils.mjs +6 -0
- package/dist/adapters/standard-peer/index.d.mts +1 -11
- package/dist/adapters/standard-peer/index.mjs +1 -10
- package/dist/adapters/standard-peer/utils.d.mts +4 -0
- package/dist/adapters/standard-peer/utils.mjs +11 -0
- package/dist/adapters/websocket/handler.d.mts +55 -0
- package/dist/adapters/websocket/handler.mjs +55 -0
- package/dist/adapters/websocket/index.d.mts +2 -77
- package/dist/adapters/websocket/index.mjs +2 -87
- package/dist/adapters/websocket/rpc-handler.d.mts +15 -0
- package/dist/adapters/websocket/rpc-handler.mjs +10 -0
- package/dist/builder-variants.d.mts +267 -0
- package/dist/builder-variants.mjs +1 -0
- package/dist/builder.d.mts +141 -0
- package/dist/builder.mjs +145 -0
- package/dist/constants.d.mts +2 -0
- package/dist/constants.mjs +2 -0
- package/dist/context.d.mts +13 -0
- package/dist/context.mjs +1 -0
- package/dist/extensions/callable.d.mts +16 -9
- package/dist/extensions/callable.mjs +5 -9
- package/dist/helpers/base64url.d.mts +31 -0
- package/dist/helpers/base64url.mjs +25 -0
- package/dist/helpers/cookie.d.mts +50 -0
- package/dist/helpers/cookie.mjs +35 -0
- package/dist/helpers/encryption.d.mts +28 -0
- package/dist/helpers/encryption.mjs +62 -0
- package/dist/helpers/index.d.mts +4 -167
- package/dist/helpers/index.mjs +4 -200
- package/dist/helpers/signing.d.mts +50 -0
- package/dist/helpers/signing.mjs +43 -0
- package/dist/implementer-procedure.d.mts +36 -0
- package/dist/implementer-procedure.mjs +38 -0
- package/dist/implementer-router.d.mts +78 -0
- package/dist/implementer-router.mjs +77 -0
- package/dist/implementer.d.mts +22 -0
- package/dist/implementer.mjs +30 -0
- package/dist/index.d.mts +57 -476
- package/dist/index.mjs +25 -458
- package/dist/lazy.d.mts +20 -0
- package/dist/lazy.mjs +22 -0
- package/dist/middleware-decorated.d.mts +27 -0
- package/dist/middleware-decorated.mjs +65 -0
- package/dist/middleware.d.mts +70 -0
- package/dist/middleware.mjs +1 -0
- package/dist/plugins/batch.d.mts +104 -0
- package/dist/plugins/batch.mjs +373 -0
- package/dist/plugins/cors.d.mts +67 -0
- package/dist/plugins/cors.mjs +134 -0
- package/dist/plugins/get-method-csrf-protection.d.mts +23 -0
- package/dist/plugins/get-method-csrf-protection.mjs +45 -0
- package/dist/plugins/head-method.d.mts +23 -0
- package/dist/plugins/head-method.mjs +95 -0
- package/dist/plugins/index.d.mts +39 -432
- package/dist/plugins/index.mjs +23 -967
- package/dist/plugins/method-override.d.mts +38 -0
- package/dist/plugins/method-override.mjs +50 -0
- package/dist/plugins/request-compression.d.mts +16 -0
- package/dist/plugins/request-compression.mjs +67 -0
- package/dist/plugins/request-headers.d.mts +22 -0
- package/dist/plugins/request-headers.mjs +21 -0
- package/dist/plugins/request-limit.d.mts +30 -0
- package/dist/plugins/request-limit.mjs +53 -0
- package/dist/plugins/response-compression.d.mts +59 -0
- package/dist/plugins/response-compression.mjs +172 -0
- package/dist/plugins/response-headers.d.mts +30 -0
- package/dist/plugins/response-headers.mjs +34 -0
- package/dist/plugins/rethrow.d.mts +33 -0
- package/dist/plugins/rethrow.mjs +46 -0
- package/dist/plugins/signal-sync.d.mts +13 -0
- package/dist/plugins/signal-sync.mjs +84 -0
- package/dist/plugins/timeout.d.mts +37 -0
- package/dist/plugins/timeout.mjs +56 -0
- package/dist/procedure-client.d.mts +41 -0
- package/dist/procedure-client.mjs +157 -0
- package/dist/procedure-decorated.d.mts +20 -0
- package/dist/procedure-decorated.mjs +41 -0
- package/dist/procedure-utils.d.mts +52 -0
- package/dist/procedure-utils.mjs +33 -0
- package/dist/procedure.d.mts +93 -0
- package/dist/procedure.mjs +20 -0
- package/dist/router-client.d.mts +27 -0
- package/dist/router-client.mjs +35 -0
- package/dist/router-hidden.d.mts +5 -0
- package/dist/router-hidden.mjs +12 -0
- package/dist/router-utils.d.mts +59 -0
- package/dist/router-utils.mjs +191 -0
- package/dist/router.d.mts +61 -0
- package/dist/router.mjs +1 -0
- package/package.json +11 -11
- package/dist/adapters/aws-lambda/index.d.ts +0 -51
- package/dist/adapters/crossws/index.d.ts +0 -63
- package/dist/adapters/fastify/index.d.ts +0 -42
- package/dist/adapters/fetch/index.d.ts +0 -47
- package/dist/adapters/message-port/index.d.ts +0 -77
- package/dist/adapters/node/index.d.ts +0 -44
- package/dist/adapters/standard/index.d.ts +0 -95
- package/dist/adapters/standard-peer/index.d.ts +0 -11
- package/dist/adapters/websocket/index.d.ts +0 -77
- package/dist/extensions/callable.d.ts +0 -10
- package/dist/helpers/index.d.ts +0 -167
- package/dist/index.d.ts +0 -476
- package/dist/plugins/index.d.ts +0 -432
- package/dist/shared/server.1ExWFMze.mjs +0 -69
- package/dist/shared/server.9B1jEm5a.d.mts +0 -105
- package/dist/shared/server.BB464y-6.mjs +0 -207
- package/dist/shared/server.BC5BIu02.d.mts +0 -73
- package/dist/shared/server.BSYGskId.d.ts +0 -14
- package/dist/shared/server.BvG3boBj.d.mts +0 -14
- package/dist/shared/server.CTV_mC2P.d.mts +0 -34
- package/dist/shared/server.CYbIpHbH.mjs +0 -239
- package/dist/shared/server.CgBF3Vj1.d.ts +0 -34
- package/dist/shared/server.Chq5FpXf.mjs +0 -353
- package/dist/shared/server.DFPxP2v3.d.mts +0 -166
- package/dist/shared/server.DFPxP2v3.d.ts +0 -166
- package/dist/shared/server.DOg7ke6y.d.ts +0 -105
- package/dist/shared/server.Dz7j9BEh.d.ts +0 -73
- package/dist/shared/server.W91HSRkE.mjs +0 -9
- package/dist/shared/server.sBQSgqK3.mjs +0 -41
package/dist/index.mjs
CHANGED
|
@@ -1,463 +1,30 @@
|
|
|
1
|
-
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
export
|
|
5
|
-
|
|
6
|
-
export
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export
|
|
10
|
-
|
|
11
|
-
export
|
|
12
|
-
export
|
|
1
|
+
export * from "./builder.mjs";
|
|
2
|
+
export * from "./builder-variants.mjs";
|
|
3
|
+
export * from "./constants.mjs";
|
|
4
|
+
export * from "./context.mjs";
|
|
5
|
+
export * from "./implementer.mjs";
|
|
6
|
+
export * from "./implementer-procedure.mjs";
|
|
7
|
+
export { SharedRouterImplementer } from "./implementer-router.mjs";
|
|
8
|
+
export * from "./lazy.mjs";
|
|
9
|
+
export * from "./middleware.mjs";
|
|
10
|
+
export * from "./procedure.mjs";
|
|
11
|
+
export * from "./procedure-client.mjs";
|
|
12
|
+
export * from "./procedure-decorated.mjs";
|
|
13
|
+
export { call } from "./procedure-utils.mjs";
|
|
14
|
+
export * from "./router.mjs";
|
|
15
|
+
export * from "./router-client.mjs";
|
|
16
|
+
export { getRouter, resolveContractProcedure, unlazyRouter, walkProcedureContractsAsync, walkProcedureContractsSync } from "./router-utils.mjs";
|
|
17
|
+
export { COMMON_ERROR_STATUS_MAP, isDefinedError, ORPCError, RPCJsonSerializer, RPCSerializer, safe, toORPCError } from "@orpc/client";
|
|
18
|
+
export { asyncIteratorObject, defineMeta, error, eventIterator, type, ValidationError } from "@orpc/contract";
|
|
19
|
+
export { AsyncIteratorClass, asyncIteratorToStream, asyncIteratorToUnproxiedDataStream,
|
|
13
20
|
|
|
14
|
-
|
|
15
|
-
const decorated = ((...args) => middleware(...args));
|
|
16
|
-
decorated["~orpc"] = middleware["~orpc"];
|
|
17
|
-
Object.defineProperty(decorated, "name", {
|
|
18
|
-
value: middleware.name
|
|
19
|
-
});
|
|
20
|
-
decorated.adaptInput = (adapt) => {
|
|
21
|
-
const mapped = decorateMiddleware(
|
|
22
|
-
(opts, input, ...args) => {
|
|
23
|
-
return middleware(opts, adapt(input), ...args);
|
|
24
|
-
}
|
|
25
|
-
);
|
|
26
|
-
mapped["~orpc"] = decorated["~orpc"];
|
|
27
|
-
Object.defineProperty(mapped, "name", {
|
|
28
|
-
value: decorated.name
|
|
29
|
-
});
|
|
30
|
-
return mapped;
|
|
31
|
-
};
|
|
32
|
-
decorated.errors = (errors) => {
|
|
33
|
-
const newMiddleware = decorateMiddleware(decorated);
|
|
34
|
-
newMiddleware["~orpc"] = {
|
|
35
|
-
...decorated["~orpc"],
|
|
36
|
-
errorMap: mergeErrorMap(decorated["~orpc"]?.errorMap, errors)
|
|
37
|
-
};
|
|
38
|
-
return newMiddleware;
|
|
39
|
-
};
|
|
40
|
-
decorated.use = (usedMiddleware) => {
|
|
41
|
-
const merged = decorateMiddleware((opts, ...args) => {
|
|
42
|
-
return middleware(
|
|
43
|
-
{
|
|
44
|
-
...opts,
|
|
45
|
-
async next(nextOpts1) {
|
|
46
|
-
const signal = nextOpts1?.signal ?? opts.signal;
|
|
47
|
-
const result = await usedMiddleware({
|
|
48
|
-
...opts,
|
|
49
|
-
context: { ...opts.context, ...nextOpts1?.context },
|
|
50
|
-
signal,
|
|
51
|
-
next(nextOpts2, ...args2) {
|
|
52
|
-
return opts.next({
|
|
53
|
-
context: {
|
|
54
|
-
...nextOpts1?.context,
|
|
55
|
-
...nextOpts2?.context
|
|
56
|
-
},
|
|
57
|
-
signal: nextOpts2?.signal ?? signal
|
|
58
|
-
}, ...args2);
|
|
59
|
-
}
|
|
60
|
-
}, ...args);
|
|
61
|
-
return {
|
|
62
|
-
...result,
|
|
63
|
-
context: {
|
|
64
|
-
...nextOpts1?.context,
|
|
65
|
-
...result.context
|
|
66
|
-
}
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
},
|
|
70
|
-
...args
|
|
71
|
-
);
|
|
72
|
-
});
|
|
73
|
-
merged["~orpc"] = {
|
|
74
|
-
...decorated["~orpc"],
|
|
75
|
-
errorMap: mergeErrorMap(usedMiddleware["~orpc"]?.errorMap, decorated["~orpc"]?.errorMap),
|
|
76
|
-
metaPlugins: [
|
|
77
|
-
...toArray(decorated["~orpc"]?.metaPlugins),
|
|
78
|
-
...toArray(usedMiddleware["~orpc"]?.metaPlugins)
|
|
79
|
-
]
|
|
80
|
-
};
|
|
81
|
-
Object.defineProperty(merged, "name", {
|
|
82
|
-
value: `${decorated.name} + ${usedMiddleware.name}`
|
|
83
|
-
});
|
|
84
|
-
return merged;
|
|
85
|
-
};
|
|
86
|
-
return decorated;
|
|
87
|
-
}
|
|
21
|
+
asyncIteratorToStream as eventIteratorToStream,
|
|
88
22
|
|
|
89
|
-
|
|
90
|
-
"~orpc";
|
|
91
|
-
/**
|
|
92
|
-
* Private constructor to prevent direct instantiation.
|
|
93
|
-
* Use the static `create` method to initialize a new instance with a safe initial definition.
|
|
94
|
-
*/
|
|
95
|
-
constructor(definition) {
|
|
96
|
-
this["~orpc"] = definition;
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* Creates a fresh builder with an empty definition.
|
|
100
|
-
* Prefer the exported `os` instance over calling this directly.
|
|
101
|
-
*
|
|
102
|
-
* @see {@link https://orpc.dev/docs/procedure | Procedure}
|
|
103
|
-
*/
|
|
104
|
-
static create() {
|
|
105
|
-
return new Builder({
|
|
106
|
-
errorMap: {},
|
|
107
|
-
meta: {},
|
|
108
|
-
orderedMiddlewares: []
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* Declares the initial context type that must be provided when executing
|
|
113
|
-
* procedures built from this builder.
|
|
114
|
-
*
|
|
115
|
-
* @see {@link https://orpc.dev/docs/context#initial-context | Context - Initial Context}
|
|
116
|
-
*/
|
|
117
|
-
$context() {
|
|
118
|
-
return this;
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* Overrides the procedure configuration, such as disabling runtime
|
|
122
|
-
* input/output validation.
|
|
123
|
-
*
|
|
124
|
-
* @see {@link https://orpc.dev/docs/recipes/validation-customization | Validation Customization}
|
|
125
|
-
*/
|
|
126
|
-
$config(config) {
|
|
127
|
-
return new Builder({
|
|
128
|
-
...this["~orpc"],
|
|
129
|
-
...config
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Applies metadata plugins to procedures built from this builder.
|
|
134
|
-
*
|
|
135
|
-
* @see {@link https://orpc.dev/docs/metadata | Metadata}
|
|
136
|
-
*/
|
|
137
|
-
meta(...plugins) {
|
|
138
|
-
const [meta, metaPlugins] = resolveMetaPlugins(
|
|
139
|
-
this["~orpc"].meta,
|
|
140
|
-
this["~orpc"].metaPlugins,
|
|
141
|
-
plugins
|
|
142
|
-
);
|
|
143
|
-
return new Builder({
|
|
144
|
-
...this["~orpc"],
|
|
145
|
-
meta,
|
|
146
|
-
metaPlugins
|
|
147
|
-
});
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Defines typesafe errors that procedures built from this builder can throw
|
|
151
|
-
* via the `errors` utility in handlers and middleware.
|
|
152
|
-
*
|
|
153
|
-
* @see {@link https://orpc.dev/docs/error-handling#typesafe-errors | Error Handling - Typesafe Errors}
|
|
154
|
-
*/
|
|
155
|
-
errors(errors) {
|
|
156
|
-
let builder = new Builder({
|
|
157
|
-
...this["~orpc"],
|
|
158
|
-
errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
|
|
159
|
-
});
|
|
160
|
-
const plugins = getHiddenMetaPlugins(errors);
|
|
161
|
-
if (plugins) {
|
|
162
|
-
builder = builder.meta(...plugins);
|
|
163
|
-
}
|
|
164
|
-
return builder;
|
|
165
|
-
}
|
|
166
|
-
/**
|
|
167
|
-
* Applies a middleware that runs before the handler of every procedure
|
|
168
|
-
* built from this builder.
|
|
169
|
-
*
|
|
170
|
-
* @see {@link https://orpc.dev/docs/middleware | Middleware}
|
|
171
|
-
*/
|
|
172
|
-
use(middleware) {
|
|
173
|
-
let builder = new Builder({
|
|
174
|
-
...this["~orpc"],
|
|
175
|
-
errorMap: mergeErrorMap(middleware["~orpc"]?.errorMap ?? {}, this["~orpc"].errorMap),
|
|
176
|
-
orderedMiddlewares: [...this["~orpc"].orderedMiddlewares, {
|
|
177
|
-
middleware,
|
|
178
|
-
inputSchemasLengthAtUse: this["~orpc"].inputSchemas?.length,
|
|
179
|
-
outputSchemasLengthAtUse: this["~orpc"].outputSchemas?.length
|
|
180
|
-
}]
|
|
181
|
-
});
|
|
182
|
-
if (middleware["~orpc"]?.metaPlugins) {
|
|
183
|
-
builder = builder.meta(...middleware["~orpc"]?.metaPlugins);
|
|
184
|
-
}
|
|
185
|
-
return builder;
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* Creates a standalone middleware that can be composed and applied to any
|
|
189
|
-
* compatible builder or procedure with `.use`.
|
|
190
|
-
*
|
|
191
|
-
* @see {@link https://orpc.dev/docs/middleware | Middleware}
|
|
192
|
-
*/
|
|
193
|
-
middleware(middleware) {
|
|
194
|
-
const allMiddlewares = [
|
|
195
|
-
...this["~orpc"].orderedMiddlewares.map(({ middleware: middleware2 }) => middleware2),
|
|
196
|
-
middleware
|
|
197
|
-
];
|
|
198
|
-
let current = decorateMiddleware(allMiddlewares.shift());
|
|
199
|
-
for (const mid of allMiddlewares) {
|
|
200
|
-
current = current.use(mid);
|
|
201
|
-
}
|
|
202
|
-
current["~orpc"] = {
|
|
203
|
-
...current["~orpc"],
|
|
204
|
-
errorMap: this["~orpc"].errorMap,
|
|
205
|
-
metaPlugins: [
|
|
206
|
-
...toArray(this["~orpc"].metaPlugins),
|
|
207
|
-
...toArray(middleware["~orpc"]?.metaPlugins)
|
|
208
|
-
]
|
|
209
|
-
};
|
|
210
|
-
return current;
|
|
211
|
-
}
|
|
212
|
-
/**
|
|
213
|
-
* Defines the input schema used to validate and type the procedure input.
|
|
214
|
-
*
|
|
215
|
-
* @see {@link https://orpc.dev/docs/procedure#inputoutput-validation | Procedure - Input/Output Validation}
|
|
216
|
-
*/
|
|
217
|
-
input(schema) {
|
|
218
|
-
let builder = new Builder({
|
|
219
|
-
...this["~orpc"],
|
|
220
|
-
inputSchemas: [...toArray(this["~orpc"].inputSchemas), schema]
|
|
221
|
-
});
|
|
222
|
-
const plugins = getHiddenMetaPlugins(schema);
|
|
223
|
-
if (plugins) {
|
|
224
|
-
builder = builder.meta(...plugins);
|
|
225
|
-
}
|
|
226
|
-
return builder;
|
|
227
|
-
}
|
|
228
|
-
/**
|
|
229
|
-
* Defines the output schema used to validate and type the procedure output.
|
|
230
|
-
*
|
|
231
|
-
* @see {@link https://orpc.dev/docs/procedure#inputoutput-validation | Procedure - Input/Output Validation}
|
|
232
|
-
*/
|
|
233
|
-
output(schema) {
|
|
234
|
-
let builder = new Builder({
|
|
235
|
-
...this["~orpc"],
|
|
236
|
-
outputSchemas: [...toArray(this["~orpc"].outputSchemas), schema]
|
|
237
|
-
});
|
|
238
|
-
const plugins = getHiddenMetaPlugins(schema);
|
|
239
|
-
if (plugins) {
|
|
240
|
-
builder = builder.meta(...plugins);
|
|
241
|
-
}
|
|
242
|
-
return builder;
|
|
243
|
-
}
|
|
244
|
-
/**
|
|
245
|
-
* Defines the function that implements the procedure and completes the
|
|
246
|
-
* chain, returning a callable procedure.
|
|
247
|
-
*
|
|
248
|
-
* @see {@link https://orpc.dev/docs/procedure | Procedure}
|
|
249
|
-
*/
|
|
250
|
-
handler(handler) {
|
|
251
|
-
let procedure = new DecoratedProcedure({
|
|
252
|
-
...this["~orpc"],
|
|
253
|
-
handler
|
|
254
|
-
});
|
|
255
|
-
const plugins = getHiddenMetaPlugins(handler);
|
|
256
|
-
if (plugins) {
|
|
257
|
-
procedure = procedure.meta(...plugins);
|
|
258
|
-
}
|
|
259
|
-
return procedure;
|
|
260
|
-
}
|
|
261
|
-
/**
|
|
262
|
-
* Applies the builder's middleware, errors, and metadata to every procedure
|
|
263
|
-
* in the given router.
|
|
264
|
-
*
|
|
265
|
-
* @see {@link https://orpc.dev/docs/router#extending-router | Router - Extending Router}
|
|
266
|
-
*/
|
|
267
|
-
router(router) {
|
|
268
|
-
return augmentRouter(router, {
|
|
269
|
-
...this["~orpc"],
|
|
270
|
-
middlewares: this["~orpc"].orderedMiddlewares.map(({ middleware }) => middleware)
|
|
271
|
-
});
|
|
272
|
-
}
|
|
273
|
-
/**
|
|
274
|
-
* Like `.router`, but loads the router lazily on first access, which helps
|
|
275
|
-
* reduce startup time for large applications.
|
|
276
|
-
*
|
|
277
|
-
* @see {@link https://orpc.dev/docs/router#lazy-router | Router - Lazy Router}
|
|
278
|
-
*/
|
|
279
|
-
lazy(loader) {
|
|
280
|
-
return new Lazy({
|
|
281
|
-
loader: async () => {
|
|
282
|
-
const { default: router } = await loader();
|
|
283
|
-
return {
|
|
284
|
-
default: this.router(router)
|
|
285
|
-
};
|
|
286
|
-
},
|
|
287
|
-
meta: this["~orpc"].meta,
|
|
288
|
-
metaPlugins: this["~orpc"].metaPlugins
|
|
289
|
-
});
|
|
290
|
-
}
|
|
291
|
-
}
|
|
292
|
-
const os = Builder.create();
|
|
23
|
+
asyncIteratorToUnproxiedDataStream as eventIteratorToUnproxiedDataStream, onAsyncIteratorObjectError, onError, onFinish, onReadableStreamError, onStart, onSuccess, streamToAsyncIteratorObject,
|
|
293
24
|
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
constructor(definition) {
|
|
297
|
-
this["~orpc"] = definition;
|
|
298
|
-
}
|
|
299
|
-
"use"(middleware) {
|
|
300
|
-
return new ProcedureImplementer({
|
|
301
|
-
...this["~orpc"],
|
|
302
|
-
orderedMiddlewares: [...this["~orpc"].orderedMiddlewares, {
|
|
303
|
-
middleware,
|
|
304
|
-
inputSchemasLengthAtUse: this["~orpc"].inputSchemas?.length,
|
|
305
|
-
outputSchemasLengthAtUse: this["~orpc"].outputSchemas?.length
|
|
306
|
-
}]
|
|
307
|
-
});
|
|
308
|
-
}
|
|
309
|
-
"handler"(handler) {
|
|
310
|
-
return new ImplementedProcedure({
|
|
311
|
-
...this["~orpc"],
|
|
312
|
-
handler
|
|
313
|
-
});
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
class ImplementedProcedure extends Procedure {
|
|
317
|
-
use(middleware) {
|
|
318
|
-
return new ImplementedProcedure({
|
|
319
|
-
...this["~orpc"],
|
|
320
|
-
orderedMiddlewares: [...this["~orpc"].orderedMiddlewares, {
|
|
321
|
-
middleware,
|
|
322
|
-
inputSchemasLengthAtUse: this["~orpc"].inputSchemas?.length,
|
|
323
|
-
outputSchemasLengthAtUse: this["~orpc"].outputSchemas?.length
|
|
324
|
-
}]
|
|
325
|
-
});
|
|
326
|
-
}
|
|
327
|
-
}
|
|
25
|
+
streamToAsyncIteratorObject as streamToEventIterator } from "@orpc/shared";
|
|
26
|
+
export { ErrorEvent,
|
|
328
27
|
|
|
329
|
-
|
|
330
|
-
constructor(contract, config, middlewares) {
|
|
331
|
-
this.contract = contract;
|
|
332
|
-
this.config = config;
|
|
333
|
-
this.middlewares = middlewares;
|
|
334
|
-
}
|
|
335
|
-
static create(contract, config, middlewares = []) {
|
|
336
|
-
return new SharedRouterImplementer(
|
|
337
|
-
contract,
|
|
338
|
-
config,
|
|
339
|
-
middlewares
|
|
340
|
-
);
|
|
341
|
-
}
|
|
342
|
-
use(middleware) {
|
|
343
|
-
return createRouterImplementerInternal(this.contract, this.config, [...this.middlewares, middleware]);
|
|
344
|
-
}
|
|
345
|
-
middleware(middleware) {
|
|
346
|
-
const allMiddlewares = [
|
|
347
|
-
...this.middlewares,
|
|
348
|
-
middleware
|
|
349
|
-
];
|
|
350
|
-
let current = decorateMiddleware(allMiddlewares.shift());
|
|
351
|
-
for (const mid of allMiddlewares) {
|
|
352
|
-
current = current.use(mid);
|
|
353
|
-
}
|
|
354
|
-
return current;
|
|
355
|
-
}
|
|
356
|
-
router(router) {
|
|
357
|
-
if (this.middlewares.length) {
|
|
358
|
-
router = augmentImplementedRouter(router, {
|
|
359
|
-
...this.config,
|
|
360
|
-
middlewares: this.middlewares
|
|
361
|
-
});
|
|
362
|
-
}
|
|
363
|
-
return withHiddenRouterContract(router, this.contract);
|
|
364
|
-
}
|
|
365
|
-
lazy(loader) {
|
|
366
|
-
return new Lazy({
|
|
367
|
-
loader: async () => {
|
|
368
|
-
const { default: router } = await loader();
|
|
369
|
-
return { default: this.router(router) };
|
|
370
|
-
},
|
|
371
|
-
meta: {}
|
|
372
|
-
});
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
function createRouterImplementer(contract, config) {
|
|
376
|
-
return createRouterImplementerInternal(contract, config, []);
|
|
377
|
-
}
|
|
378
|
-
function createRouterImplementerInternal(contract, config, middlewares) {
|
|
379
|
-
if (contract instanceof ProcedureContract) {
|
|
380
|
-
return new ProcedureImplementer({
|
|
381
|
-
...config,
|
|
382
|
-
...contract["~orpc"],
|
|
383
|
-
orderedMiddlewares: middlewares.map((middleware) => ({ middleware }))
|
|
384
|
-
});
|
|
385
|
-
}
|
|
386
|
-
const implementer = {};
|
|
387
|
-
for (const [key, child] of Object.entries(contract)) {
|
|
388
|
-
implementer[key] = createRouterImplementerInternal(child, config, middlewares);
|
|
389
|
-
}
|
|
390
|
-
const shared = bindMethods(SharedRouterImplementer.create(contract, config, middlewares));
|
|
391
|
-
for (const [key, method] of Object.entries(shared)) {
|
|
392
|
-
if (key in implementer) {
|
|
393
|
-
const child = implementer[key];
|
|
394
|
-
implementer[key] = new Proxy(method, {
|
|
395
|
-
get(_, p) {
|
|
396
|
-
return Reflect.get(child, p);
|
|
397
|
-
}
|
|
398
|
-
});
|
|
399
|
-
} else {
|
|
400
|
-
implementer[key] = method;
|
|
401
|
-
}
|
|
402
|
-
}
|
|
403
|
-
return implementer;
|
|
404
|
-
}
|
|
28
|
+
getEventMeta, unwrapEvent,
|
|
405
29
|
|
|
406
|
-
|
|
407
|
-
const routerImplementer = createRouterImplementer(contract, config);
|
|
408
|
-
const implementer = new Proxy(routerImplementer, {
|
|
409
|
-
get(_, p) {
|
|
410
|
-
let method;
|
|
411
|
-
if (p === "$context") {
|
|
412
|
-
method = () => implementer;
|
|
413
|
-
} else if (p === "$config") {
|
|
414
|
-
method = (incoming) => implement(contract, { ...config, ...incoming });
|
|
415
|
-
}
|
|
416
|
-
const value = Reflect.get(routerImplementer, p);
|
|
417
|
-
if (method) {
|
|
418
|
-
if (!isTypescriptObject(value)) {
|
|
419
|
-
return method;
|
|
420
|
-
}
|
|
421
|
-
return new Proxy(method, {
|
|
422
|
-
get(_2, p2) {
|
|
423
|
-
return Reflect.get(value, p2);
|
|
424
|
-
}
|
|
425
|
-
});
|
|
426
|
-
}
|
|
427
|
-
return value;
|
|
428
|
-
}
|
|
429
|
-
});
|
|
430
|
-
return implementer;
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
function createRouterClient(router, ...rest) {
|
|
434
|
-
const options = resolveMaybeOptionalOptions(rest);
|
|
435
|
-
if (router instanceof Procedure) {
|
|
436
|
-
return createProcedureClient(router, options);
|
|
437
|
-
}
|
|
438
|
-
const procedureCaller = router instanceof Lazy ? createProcedureClient(createGuardedProcedureLazy(router), options) : {};
|
|
439
|
-
const cache = /* @__PURE__ */ new Map();
|
|
440
|
-
const recursive = new Proxy(procedureCaller, {
|
|
441
|
-
get(target, key) {
|
|
442
|
-
if (typeof key !== "string" || router instanceof Lazy && RECURSIVE_CLIENT_UNWRAP_KEYS.has(key)) {
|
|
443
|
-
return Reflect.get(target, key);
|
|
444
|
-
}
|
|
445
|
-
let nextClient = cache.get(key);
|
|
446
|
-
if (nextClient === void 0) {
|
|
447
|
-
const next = getRouter(router, [key]);
|
|
448
|
-
if (!next) {
|
|
449
|
-
return Reflect.get(target, key);
|
|
450
|
-
}
|
|
451
|
-
nextClient = createRouterClient(next, {
|
|
452
|
-
...options,
|
|
453
|
-
path: [...toArray(options.path), key]
|
|
454
|
-
});
|
|
455
|
-
cache.set(key, nextClient);
|
|
456
|
-
}
|
|
457
|
-
return nextClient;
|
|
458
|
-
}
|
|
459
|
-
});
|
|
460
|
-
return recursive;
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
export { Builder, DecoratedProcedure, ImplementedProcedure, Lazy, Procedure, ProcedureImplementer, SharedRouterImplementer, createProcedureClient, createRouterClient, getRouter, implement, os };
|
|
30
|
+
withEventMeta } from "@standard-server/core";
|
package/dist/lazy.d.mts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { AnyMetaPlugin, Meta } from "@orpc/contract";
|
|
2
|
+
export interface LazyDefinition<T> {
|
|
3
|
+
meta: Meta;
|
|
4
|
+
metaPlugins?: AnyMetaPlugin[] | undefined;
|
|
5
|
+
loader: () => Promise<{
|
|
6
|
+
default: T;
|
|
7
|
+
}>;
|
|
8
|
+
}
|
|
9
|
+
export declare class Lazy<T> {
|
|
10
|
+
"~orpc": LazyDefinition<T>;
|
|
11
|
+
constructor(def: LazyDefinition<T>);
|
|
12
|
+
/**
|
|
13
|
+
* Checks if the given instance satisfies the {@link Lazy} class/interface.
|
|
14
|
+
*/
|
|
15
|
+
static [Symbol.hasInstance](instance: unknown): boolean;
|
|
16
|
+
}
|
|
17
|
+
export type Lazyable<T> = T | Lazy<T>;
|
|
18
|
+
export declare function unlazy<T extends Lazyable<any>>(maybeLazy: T): Promise<{
|
|
19
|
+
default: T extends Lazy<infer U> ? U : T;
|
|
20
|
+
}>;
|
package/dist/lazy.mjs
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { getConstructor, isTypescriptObject } from "@orpc/shared";
|
|
2
|
+
export class Lazy {
|
|
3
|
+
"~orpc";
|
|
4
|
+
constructor(def) {
|
|
5
|
+
this["~orpc"] = def;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
static [Symbol.hasInstance](instance) {
|
|
9
|
+
if (this !== Lazy) {
|
|
10
|
+
|
|
11
|
+
return Function.prototype[Symbol.hasInstance].call(this, instance);
|
|
12
|
+
}
|
|
13
|
+
const constructor = getConstructor(instance);
|
|
14
|
+
if (constructor === Lazy) {
|
|
15
|
+
return true;
|
|
16
|
+
}
|
|
17
|
+
return isTypescriptObject(instance) && isTypescriptObject(instance["~orpc"]) && isTypescriptObject(instance["~orpc"].meta) && (instance["~orpc"].metaPlugins === undefined || Array.isArray(instance["~orpc"].metaPlugins)) && typeof instance["~orpc"].loader === "function";
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export function unlazy(maybeLazy) {
|
|
21
|
+
return maybeLazy instanceof Lazy ? maybeLazy["~orpc"].loader() : Promise.resolve({ default: maybeLazy });
|
|
22
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ErrorMap, MergedErrorMap } from "@orpc/contract";
|
|
2
|
+
import type { IntersectPick } from "@orpc/shared";
|
|
3
|
+
import type { Context, MergedContext, MergedInitialContext } from "./context.mjs";
|
|
4
|
+
import type { Middleware } from "./middleware.mjs";
|
|
5
|
+
export interface DecoratedMiddleware<
|
|
6
|
+
TInContext extends Context,
|
|
7
|
+
TOutContext extends Context,
|
|
8
|
+
TInput,
|
|
9
|
+
TOutput,
|
|
10
|
+
TErrorMap extends ErrorMap
|
|
11
|
+
> extends Middleware<TInContext, TOutContext, TInput, TOutput, TErrorMap> {
|
|
12
|
+
adaptInput<T>(adapt: (input: T) => TInput): DecoratedMiddleware<TInContext, TOutContext, T, TOutput, TErrorMap>;
|
|
13
|
+
errors<T extends ErrorMap>(errors: T): DecoratedMiddleware<TInContext, TOutContext, TInput, TOutput, MergedErrorMap<TErrorMap, T>>;
|
|
14
|
+
use<
|
|
15
|
+
$OutContext extends IntersectPick<MergedContext<TInContext, TOutContext>, $OutContext>,
|
|
16
|
+
$Input extends TInput,
|
|
17
|
+
$InContext extends Context = MergedContext<TInContext, TOutContext>,
|
|
18
|
+
$ErrorMap extends ErrorMap = TErrorMap
|
|
19
|
+
>(middleware: Middleware<$InContext | MergedContext<TInContext, TOutContext>, $OutContext, $Input, TOutput, $ErrorMap>): DecoratedMiddleware<MergedInitialContext<TInContext, TOutContext, $InContext>, MergedContext<TOutContext, $OutContext>, $Input, TOutput, MergedErrorMap<$ErrorMap, TErrorMap>>;
|
|
20
|
+
}
|
|
21
|
+
export declare function decorateMiddleware<
|
|
22
|
+
TInContext extends Context,
|
|
23
|
+
TOutContext extends Context,
|
|
24
|
+
TInput,
|
|
25
|
+
TOutput,
|
|
26
|
+
TErrorMap extends ErrorMap
|
|
27
|
+
>(middleware: Middleware<TInContext, TOutContext, TInput, TOutput, TErrorMap>): DecoratedMiddleware<TInContext, TOutContext, TInput, TOutput, TErrorMap>;
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { mergeErrorMap } from "@orpc/contract";
|
|
2
|
+
import { toArray } from "@orpc/shared";
|
|
3
|
+
export function decorateMiddleware(middleware) {
|
|
4
|
+
const decorated = ((...args) => middleware(...args));
|
|
5
|
+
decorated["~orpc"] = middleware["~orpc"];
|
|
6
|
+
Object.defineProperty(decorated, "name", { value: middleware.name });
|
|
7
|
+
decorated.adaptInput = (adapt) => {
|
|
8
|
+
const mapped = decorateMiddleware((opts, input, ...args) => {
|
|
9
|
+
return middleware(opts, adapt(input), ...args);
|
|
10
|
+
});
|
|
11
|
+
mapped["~orpc"] = decorated["~orpc"];
|
|
12
|
+
Object.defineProperty(mapped, "name", { value: decorated.name });
|
|
13
|
+
return mapped;
|
|
14
|
+
};
|
|
15
|
+
decorated.errors = (errors) => {
|
|
16
|
+
const newMiddleware = decorateMiddleware(decorated);
|
|
17
|
+
newMiddleware["~orpc"] = {
|
|
18
|
+
...decorated["~orpc"],
|
|
19
|
+
errorMap: mergeErrorMap(decorated["~orpc"]?.errorMap, errors)
|
|
20
|
+
};
|
|
21
|
+
return newMiddleware;
|
|
22
|
+
};
|
|
23
|
+
decorated.use = (usedMiddleware) => {
|
|
24
|
+
const merged = decorateMiddleware((opts, ...args) => {
|
|
25
|
+
return middleware({
|
|
26
|
+
...opts,
|
|
27
|
+
async next(nextOpts1) {
|
|
28
|
+
const signal = nextOpts1?.signal ?? opts.signal;
|
|
29
|
+
const result = await usedMiddleware({
|
|
30
|
+
...opts,
|
|
31
|
+
context: {
|
|
32
|
+
...opts.context,
|
|
33
|
+
...nextOpts1?.context
|
|
34
|
+
},
|
|
35
|
+
signal,
|
|
36
|
+
next(nextOpts2, ...args) {
|
|
37
|
+
return opts.next({
|
|
38
|
+
context: {
|
|
39
|
+
...nextOpts1?.context,
|
|
40
|
+
...nextOpts2?.context
|
|
41
|
+
},
|
|
42
|
+
signal: nextOpts2?.signal ?? signal
|
|
43
|
+
}, ...args);
|
|
44
|
+
}
|
|
45
|
+
}, ...args);
|
|
46
|
+
return {
|
|
47
|
+
...result,
|
|
48
|
+
context: {
|
|
49
|
+
...nextOpts1?.context,
|
|
50
|
+
...result.context
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
}, ...args);
|
|
55
|
+
});
|
|
56
|
+
merged["~orpc"] = {
|
|
57
|
+
...decorated["~orpc"],
|
|
58
|
+
errorMap: mergeErrorMap(usedMiddleware["~orpc"]?.errorMap, decorated["~orpc"]?.errorMap),
|
|
59
|
+
metaPlugins: [...toArray(decorated["~orpc"]?.metaPlugins), ...toArray(usedMiddleware["~orpc"]?.metaPlugins)]
|
|
60
|
+
};
|
|
61
|
+
Object.defineProperty(merged, "name", { value: `${decorated.name} + ${usedMiddleware.name}` });
|
|
62
|
+
return merged;
|
|
63
|
+
};
|
|
64
|
+
return decorated;
|
|
65
|
+
}
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import type { AnyMetaPlugin, ErrorMap, ORPCErrorConstructorMap } from "@orpc/contract";
|
|
2
|
+
import type { MaybeOptionalOptions, Promisable } from "@orpc/shared";
|
|
3
|
+
import type { Context } from "./context.mjs";
|
|
4
|
+
import type { AnyProcedure } from "./procedure.mjs";
|
|
5
|
+
export type MiddlewareResult<
|
|
6
|
+
TOutContext extends Context,
|
|
7
|
+
TOutput
|
|
8
|
+
> = Promisable<{
|
|
9
|
+
output: TOutput;
|
|
10
|
+
context: TOutContext;
|
|
11
|
+
}>;
|
|
12
|
+
export type MiddlewareNextOptions<TOutContext extends Context> = (object extends TOutContext ? {
|
|
13
|
+
context?: TOutContext | undefined;
|
|
14
|
+
} : {
|
|
15
|
+
context: TOutContext;
|
|
16
|
+
}) & {
|
|
17
|
+
/** Overrides the signal for the rest of the chain. */
|
|
18
|
+
signal?: AbortSignal | undefined;
|
|
19
|
+
};
|
|
20
|
+
export interface MiddlewareNext<TOutput> {
|
|
21
|
+
<T extends Context = object>(...rest: MaybeOptionalOptions<MiddlewareNextOptions<T>>): MiddlewareResult<T, TOutput>;
|
|
22
|
+
}
|
|
23
|
+
export type MiddlewareDoneOptions<
|
|
24
|
+
TOutContext,
|
|
25
|
+
TOutput
|
|
26
|
+
> = (object extends TOutContext ? {
|
|
27
|
+
context?: TOutContext | undefined;
|
|
28
|
+
} : {
|
|
29
|
+
context: TOutContext;
|
|
30
|
+
}) & {
|
|
31
|
+
output: TOutput;
|
|
32
|
+
};
|
|
33
|
+
export interface MiddlewareDone<TOutput> {
|
|
34
|
+
/**
|
|
35
|
+
* Create a successful result and terminate the middleware chain early.
|
|
36
|
+
*/
|
|
37
|
+
<TOutContext extends Context = object>(...rest: MaybeOptionalOptions<MiddlewareDoneOptions<TOutContext, TOutput>>): MiddlewareResult<TOutContext, TOutput>;
|
|
38
|
+
}
|
|
39
|
+
export interface MiddlewareOptions<
|
|
40
|
+
TInContext extends Context,
|
|
41
|
+
TOutput,
|
|
42
|
+
TErrorConstructorMap extends ORPCErrorConstructorMap<any>
|
|
43
|
+
> {
|
|
44
|
+
context: TInContext;
|
|
45
|
+
path: string[];
|
|
46
|
+
procedure: AnyProcedure;
|
|
47
|
+
signal?: AbortSignal | undefined;
|
|
48
|
+
lastEventId: string | undefined;
|
|
49
|
+
/**
|
|
50
|
+
* Invoke to continue the middleware chain.
|
|
51
|
+
*/
|
|
52
|
+
next: MiddlewareNext<TOutput>;
|
|
53
|
+
errors: TErrorConstructorMap;
|
|
54
|
+
}
|
|
55
|
+
export interface MiddlewareDefinition<TErrorMap extends ErrorMap> {
|
|
56
|
+
errorMap?: TErrorMap | undefined;
|
|
57
|
+
metaPlugins?: AnyMetaPlugin[] | undefined;
|
|
58
|
+
}
|
|
59
|
+
export interface Middleware<
|
|
60
|
+
TInContext extends Context,
|
|
61
|
+
TOutContext extends Context,
|
|
62
|
+
TInput,
|
|
63
|
+
TOutput,
|
|
64
|
+
TErrorMap extends ErrorMap
|
|
65
|
+
> {
|
|
66
|
+
/** this property should be optional to support inline middleware */
|
|
67
|
+
"~orpc"?: MiddlewareDefinition<TErrorMap> | undefined;
|
|
68
|
+
(opts: MiddlewareOptions<TInContext, TOutput, ORPCErrorConstructorMap<TErrorMap>>, input: TInput, done: MiddlewareDone<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
|
|
69
|
+
}
|
|
70
|
+
export type AnyMiddleware = Middleware<any, any, any, any, any>;
|