@orpc/server 0.0.0-next.ad0709a → 0.0.0-next.b0d4662
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 +118 -0
- package/dist/adapters/fetch/index.d.mts +27 -0
- package/dist/adapters/fetch/index.d.ts +27 -0
- package/dist/adapters/fetch/index.mjs +9 -0
- package/dist/adapters/hono/index.d.mts +20 -0
- package/dist/adapters/hono/index.d.ts +20 -0
- package/dist/adapters/hono/index.mjs +32 -0
- package/dist/adapters/next/index.d.mts +27 -0
- package/dist/adapters/next/index.d.ts +27 -0
- package/dist/adapters/next/index.mjs +29 -0
- package/dist/adapters/node/index.d.mts +35 -0
- package/dist/adapters/node/index.d.ts +35 -0
- package/dist/adapters/node/index.mjs +30 -0
- package/dist/adapters/standard/index.d.mts +29 -0
- package/dist/adapters/standard/index.d.ts +29 -0
- package/dist/adapters/standard/index.mjs +7 -0
- package/dist/index.d.mts +255 -0
- package/dist/index.d.ts +255 -0
- package/dist/{index.js → index.mjs} +41 -112
- package/dist/plugins/index.d.mts +31 -0
- package/dist/plugins/index.d.ts +31 -0
- package/dist/plugins/index.mjs +103 -0
- package/dist/shared/server.BBGuTxHE.mjs +163 -0
- package/dist/shared/server.BMaJxq9W.d.mts +9 -0
- package/dist/shared/server.BT-fqIEm.d.mts +77 -0
- package/dist/shared/server.DpdgHO1j.d.ts +9 -0
- package/dist/shared/server.KwueCzFr.mjs +26 -0
- package/dist/shared/server.Q6ZmnTgO.mjs +12 -0
- package/dist/{chunk-NOA3GBJQ.js → shared/server.V6zT5iYQ.mjs} +96 -97
- package/dist/shared/server.ptXwNGQr.d.mts +158 -0
- package/dist/shared/server.ptXwNGQr.d.ts +158 -0
- package/dist/shared/server.xL87pHsk.d.ts +77 -0
- package/package.json +31 -28
- package/dist/chunk-6RSW63UJ.js +0 -136
- package/dist/chunk-JHLUGXCM.js +0 -294
- package/dist/fetch.js +0 -17
- package/dist/hono.js +0 -31
- package/dist/next.js +0 -38
- package/dist/node.js +0 -173
- package/dist/src/adapters/fetch/index.d.ts +0 -4
- package/dist/src/adapters/fetch/rpc-handler.d.ts +0 -10
- package/dist/src/adapters/fetch/types.d.ts +0 -13
- package/dist/src/adapters/fetch/utils.d.ts +0 -6
- package/dist/src/adapters/hono/index.d.ts +0 -3
- package/dist/src/adapters/hono/middleware.d.ts +0 -13
- package/dist/src/adapters/next/index.d.ts +0 -3
- package/dist/src/adapters/next/serve.d.ts +0 -20
- package/dist/src/adapters/node/index.d.ts +0 -4
- package/dist/src/adapters/node/rpc-handler.d.ts +0 -10
- package/dist/src/adapters/node/types.d.ts +0 -21
- package/dist/src/adapters/node/utils.d.ts +0 -5
- package/dist/src/adapters/standard/handler.d.ts +0 -33
- package/dist/src/adapters/standard/index.d.ts +0 -7
- package/dist/src/adapters/standard/rpc-codec.d.ts +0 -15
- package/dist/src/adapters/standard/rpc-handler.d.ts +0 -8
- package/dist/src/adapters/standard/rpc-matcher.d.ts +0 -10
- package/dist/src/adapters/standard/rpc-serializer.d.ts +0 -16
- package/dist/src/adapters/standard/types.d.ts +0 -44
- package/dist/src/builder-variants.d.ts +0 -74
- package/dist/src/builder.d.ts +0 -57
- package/dist/src/config.d.ts +0 -6
- package/dist/src/context.d.ts +0 -9
- package/dist/src/hidden.d.ts +0 -8
- package/dist/src/implementer-procedure.d.ts +0 -30
- package/dist/src/implementer-variants.d.ts +0 -17
- package/dist/src/implementer.d.ts +0 -28
- package/dist/src/index.d.ts +0 -23
- package/dist/src/lazy-utils.d.ts +0 -6
- package/dist/src/lazy.d.ts +0 -22
- package/dist/src/middleware-decorated.d.ts +0 -10
- package/dist/src/middleware-utils.d.ts +0 -5
- package/dist/src/middleware.d.ts +0 -37
- package/dist/src/procedure-client.d.ts +0 -20
- package/dist/src/procedure-decorated.d.ts +0 -21
- package/dist/src/procedure-utils.d.ts +0 -17
- package/dist/src/procedure.d.ts +0 -29
- package/dist/src/router-accessible-lazy.d.ts +0 -8
- package/dist/src/router-client.d.ts +0 -22
- package/dist/src/router.d.ts +0 -29
- package/dist/src/utils.d.ts +0 -24
- package/dist/standard.js +0 -16
@@ -1,33 +1,12 @@
|
|
1
|
-
import {
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
createContractedProcedure,
|
9
|
-
createLazyProcedureFormAnyLazy,
|
10
|
-
createProcedureClient,
|
11
|
-
deepSetLazyRouterPrefix,
|
12
|
-
eachAllContractProcedure,
|
13
|
-
eachContractProcedure,
|
14
|
-
flatLazy,
|
15
|
-
getLazyRouterPrefix,
|
16
|
-
getRouterChild,
|
17
|
-
getRouterContract,
|
18
|
-
isLazy,
|
19
|
-
isProcedure,
|
20
|
-
lazy,
|
21
|
-
middlewareOutputFn,
|
22
|
-
setRouterContract,
|
23
|
-
unlazy
|
24
|
-
} from "./chunk-NOA3GBJQ.js";
|
1
|
+
import { mergeErrorMap, mergeMeta, mergeRoute, mergePrefix, mergeTags, isContractProcedure } from '@orpc/contract';
|
2
|
+
export { ValidationError, eventIterator, type } from '@orpc/contract';
|
3
|
+
import { P as Procedure, d as addMiddleware, c as createProcedureClient, f as adaptRouter, h as flatLazy, l as lazy, s as setRouterContract, i as isProcedure, j as isLazy, k as createLazyProcedureFormAnyLazy, g as getRouterChild } from './shared/server.V6zT5iYQ.mjs';
|
4
|
+
export { L as LAZY_LOADER_SYMBOL, a as convertPathToHttpPath, q as createAccessibleLazyRouter, b as createContractedProcedure, n as deepSetLazyRouterPrefix, r as eachAllContractProcedure, e as eachContractProcedure, o as getLazyRouterPrefix, m as getRouterContract, p as middlewareOutputFn, u as unlazy } from './shared/server.V6zT5iYQ.mjs';
|
5
|
+
export { ORPCError, isDefinedError, safe } from '@orpc/client';
|
6
|
+
export { onError, onFinish, onStart, onSuccess } from '@orpc/shared';
|
7
|
+
export { getEventMeta, withEventMeta } from '@orpc/standard-server';
|
25
8
|
|
26
|
-
|
27
|
-
import { mergeErrorMap as mergeErrorMap2, mergeMeta as mergeMeta2, mergePrefix, mergeRoute as mergeRoute2, mergeTags } from "@orpc/contract";
|
28
|
-
|
29
|
-
// src/config.ts
|
30
|
-
var DEFAULT_CONFIG = {
|
9
|
+
const DEFAULT_CONFIG = {
|
31
10
|
initialInputValidationIndex: 0,
|
32
11
|
initialOutputValidationIndex: 0
|
33
12
|
};
|
@@ -38,7 +17,6 @@ function fallbackConfig(key, value) {
|
|
38
17
|
return value;
|
39
18
|
}
|
40
19
|
|
41
|
-
// src/middleware-decorated.ts
|
42
20
|
function decorateMiddleware(middleware) {
|
43
21
|
const decorated = middleware;
|
44
22
|
decorated.mapInput = (mapInput) => {
|
@@ -50,10 +28,14 @@ function decorateMiddleware(middleware) {
|
|
50
28
|
decorated.concat = (concatMiddleware, mapInput) => {
|
51
29
|
const mapped = mapInput ? decorateMiddleware(concatMiddleware).mapInput(mapInput) : concatMiddleware;
|
52
30
|
const concatted = decorateMiddleware((options, input, output, ...rest) => {
|
53
|
-
const
|
54
|
-
|
55
|
-
|
56
|
-
|
31
|
+
const merged = middleware({
|
32
|
+
...options,
|
33
|
+
next: (...[nextOptions1]) => mapped({
|
34
|
+
...options,
|
35
|
+
context: { ...options.context, ...nextOptions1?.context },
|
36
|
+
next: (...[nextOptions2]) => options.next({ context: { ...nextOptions1?.context, ...nextOptions2?.context } })
|
37
|
+
}, input, output, ...rest)
|
38
|
+
}, input, output, ...rest);
|
57
39
|
return merged;
|
58
40
|
});
|
59
41
|
return concatted;
|
@@ -61,30 +43,28 @@ function decorateMiddleware(middleware) {
|
|
61
43
|
return decorated;
|
62
44
|
}
|
63
45
|
|
64
|
-
|
65
|
-
import { mergeErrorMap, mergeMeta, mergeRoute } from "@orpc/contract";
|
66
|
-
var DecoratedProcedure = class _DecoratedProcedure extends Procedure {
|
46
|
+
class DecoratedProcedure extends Procedure {
|
67
47
|
errors(errors) {
|
68
|
-
return new
|
48
|
+
return new DecoratedProcedure({
|
69
49
|
...this["~orpc"],
|
70
50
|
errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
|
71
51
|
});
|
72
52
|
}
|
73
53
|
meta(meta) {
|
74
|
-
return new
|
54
|
+
return new DecoratedProcedure({
|
75
55
|
...this["~orpc"],
|
76
56
|
meta: mergeMeta(this["~orpc"].meta, meta)
|
77
57
|
});
|
78
58
|
}
|
79
59
|
route(route) {
|
80
|
-
return new
|
60
|
+
return new DecoratedProcedure({
|
81
61
|
...this["~orpc"],
|
82
62
|
route: mergeRoute(this["~orpc"].route, route)
|
83
63
|
});
|
84
64
|
}
|
85
65
|
use(middleware, mapInput) {
|
86
66
|
const mapped = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
|
87
|
-
return new
|
67
|
+
return new DecoratedProcedure({
|
88
68
|
...this["~orpc"],
|
89
69
|
middlewares: addMiddleware(this["~orpc"].middlewares, mapped)
|
90
70
|
});
|
@@ -104,10 +84,9 @@ var DecoratedProcedure = class _DecoratedProcedure extends Procedure {
|
|
104
84
|
actionable(...rest) {
|
105
85
|
return this.callable(...rest);
|
106
86
|
}
|
107
|
-
}
|
87
|
+
}
|
108
88
|
|
109
|
-
|
110
|
-
var Builder = class _Builder {
|
89
|
+
class Builder {
|
111
90
|
"~orpc";
|
112
91
|
constructor(def) {
|
113
92
|
this["~orpc"] = def;
|
@@ -118,7 +97,7 @@ var Builder = class _Builder {
|
|
118
97
|
$config(config) {
|
119
98
|
const inputValidationCount = this["~orpc"].inputValidationIndex - fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex);
|
120
99
|
const outputValidationCount = this["~orpc"].outputValidationIndex - fallbackConfig("initialOutputValidationIndex", this["~orpc"].config.initialOutputValidationIndex);
|
121
|
-
return new
|
100
|
+
return new Builder({
|
122
101
|
...this["~orpc"],
|
123
102
|
config,
|
124
103
|
inputValidationIndex: fallbackConfig("initialInputValidationIndex", config.initialInputValidationIndex) + inputValidationCount,
|
@@ -129,7 +108,7 @@ var Builder = class _Builder {
|
|
129
108
|
* Reset initial context
|
130
109
|
*/
|
131
110
|
$context() {
|
132
|
-
return new
|
111
|
+
return new Builder({
|
133
112
|
...this["~orpc"],
|
134
113
|
middlewares: [],
|
135
114
|
inputValidationIndex: fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex),
|
@@ -140,7 +119,7 @@ var Builder = class _Builder {
|
|
140
119
|
* Reset initial meta
|
141
120
|
*/
|
142
121
|
$meta(initialMeta) {
|
143
|
-
return new
|
122
|
+
return new Builder({
|
144
123
|
...this["~orpc"],
|
145
124
|
meta: initialMeta
|
146
125
|
});
|
@@ -149,7 +128,7 @@ var Builder = class _Builder {
|
|
149
128
|
* Reset initial route
|
150
129
|
*/
|
151
130
|
$route(initialRoute) {
|
152
|
-
return new
|
131
|
+
return new Builder({
|
153
132
|
...this["~orpc"],
|
154
133
|
route: initialRoute
|
155
134
|
});
|
@@ -158,39 +137,39 @@ var Builder = class _Builder {
|
|
158
137
|
return decorateMiddleware(middleware);
|
159
138
|
}
|
160
139
|
errors(errors) {
|
161
|
-
return new
|
140
|
+
return new Builder({
|
162
141
|
...this["~orpc"],
|
163
|
-
errorMap:
|
142
|
+
errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
|
164
143
|
});
|
165
144
|
}
|
166
145
|
use(middleware, mapInput) {
|
167
146
|
const mapped = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
|
168
|
-
return new
|
147
|
+
return new Builder({
|
169
148
|
...this["~orpc"],
|
170
149
|
middlewares: addMiddleware(this["~orpc"].middlewares, mapped)
|
171
150
|
});
|
172
151
|
}
|
173
152
|
meta(meta) {
|
174
|
-
return new
|
153
|
+
return new Builder({
|
175
154
|
...this["~orpc"],
|
176
|
-
meta:
|
155
|
+
meta: mergeMeta(this["~orpc"].meta, meta)
|
177
156
|
});
|
178
157
|
}
|
179
158
|
route(route) {
|
180
|
-
return new
|
159
|
+
return new Builder({
|
181
160
|
...this["~orpc"],
|
182
|
-
route:
|
161
|
+
route: mergeRoute(this["~orpc"].route, route)
|
183
162
|
});
|
184
163
|
}
|
185
164
|
input(schema) {
|
186
|
-
return new
|
165
|
+
return new Builder({
|
187
166
|
...this["~orpc"],
|
188
167
|
inputSchema: schema,
|
189
168
|
inputValidationIndex: fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex) + this["~orpc"].middlewares.length
|
190
169
|
});
|
191
170
|
}
|
192
171
|
output(schema) {
|
193
|
-
return new
|
172
|
+
return new Builder({
|
194
173
|
...this["~orpc"],
|
195
174
|
outputSchema: schema,
|
196
175
|
outputValidationIndex: fallbackConfig("initialOutputValidationIndex", this["~orpc"].config.initialOutputValidationIndex) + this["~orpc"].middlewares.length
|
@@ -203,13 +182,13 @@ var Builder = class _Builder {
|
|
203
182
|
});
|
204
183
|
}
|
205
184
|
prefix(prefix) {
|
206
|
-
return new
|
185
|
+
return new Builder({
|
207
186
|
...this["~orpc"],
|
208
187
|
prefix: mergePrefix(this["~orpc"].prefix, prefix)
|
209
188
|
});
|
210
189
|
}
|
211
190
|
tag(...tags) {
|
212
|
-
return new
|
191
|
+
return new Builder({
|
213
192
|
...this["~orpc"],
|
214
193
|
tags: mergeTags(this["~orpc"].tags, tags)
|
215
194
|
});
|
@@ -220,8 +199,8 @@ var Builder = class _Builder {
|
|
220
199
|
lazy(loader) {
|
221
200
|
return adaptRouter(flatLazy(lazy(loader)), this["~orpc"]);
|
222
201
|
}
|
223
|
-
}
|
224
|
-
|
202
|
+
}
|
203
|
+
const os = new Builder({
|
225
204
|
config: {},
|
226
205
|
route: {},
|
227
206
|
meta: {},
|
@@ -233,13 +212,10 @@ var os = new Builder({
|
|
233
212
|
middlewares: []
|
234
213
|
});
|
235
214
|
|
236
|
-
// src/context.ts
|
237
215
|
function mergeContext(context, other) {
|
238
216
|
return { ...context, ...other };
|
239
217
|
}
|
240
218
|
|
241
|
-
// src/implementer.ts
|
242
|
-
import { isContractProcedure } from "@orpc/contract";
|
243
219
|
function implementerInternal(contract, config, middlewares) {
|
244
220
|
if (isContractProcedure(contract)) {
|
245
221
|
const impl2 = new Builder({
|
@@ -325,12 +301,10 @@ function implement(contract, config = {}) {
|
|
325
301
|
return impl;
|
326
302
|
}
|
327
303
|
|
328
|
-
// src/procedure-utils.ts
|
329
304
|
function call(procedure, input, ...rest) {
|
330
305
|
return createProcedureClient(procedure, ...rest)(input);
|
331
306
|
}
|
332
307
|
|
333
|
-
// src/router-client.ts
|
334
308
|
function createRouterClient(router, ...rest) {
|
335
309
|
if (isProcedure(router)) {
|
336
310
|
const caller = createProcedureClient(router, ...rest);
|
@@ -356,49 +330,4 @@ function createRouterClient(router, ...rest) {
|
|
356
330
|
return recursive;
|
357
331
|
}
|
358
332
|
|
359
|
-
|
360
|
-
import { isDefinedError, ORPCError, safe, type, ValidationError } from "@orpc/contract";
|
361
|
-
import { onError, onFinish, onStart, onSuccess } from "@orpc/shared";
|
362
|
-
export {
|
363
|
-
Builder,
|
364
|
-
DecoratedProcedure,
|
365
|
-
LAZY_LOADER_SYMBOL,
|
366
|
-
ORPCError,
|
367
|
-
Procedure,
|
368
|
-
ValidationError,
|
369
|
-
adaptRouter,
|
370
|
-
call,
|
371
|
-
convertPathToHttpPath,
|
372
|
-
createAccessibleLazyRouter,
|
373
|
-
createContractedProcedure,
|
374
|
-
createLazyProcedureFormAnyLazy,
|
375
|
-
createProcedureClient,
|
376
|
-
createRouterClient,
|
377
|
-
decorateMiddleware,
|
378
|
-
deepSetLazyRouterPrefix,
|
379
|
-
eachAllContractProcedure,
|
380
|
-
eachContractProcedure,
|
381
|
-
fallbackConfig,
|
382
|
-
flatLazy,
|
383
|
-
getLazyRouterPrefix,
|
384
|
-
getRouterChild,
|
385
|
-
getRouterContract,
|
386
|
-
implement,
|
387
|
-
implementerInternal,
|
388
|
-
isDefinedError,
|
389
|
-
isLazy,
|
390
|
-
isProcedure,
|
391
|
-
lazy,
|
392
|
-
mergeContext,
|
393
|
-
middlewareOutputFn,
|
394
|
-
onError,
|
395
|
-
onFinish,
|
396
|
-
onStart,
|
397
|
-
onSuccess,
|
398
|
-
os,
|
399
|
-
safe,
|
400
|
-
setRouterContract,
|
401
|
-
type,
|
402
|
-
unlazy
|
403
|
-
};
|
404
|
-
//# sourceMappingURL=index.js.map
|
333
|
+
export { Builder, DecoratedProcedure, Procedure, adaptRouter, call, createLazyProcedureFormAnyLazy, createProcedureClient, createRouterClient, decorateMiddleware, fallbackConfig, flatLazy, getRouterChild, implement, implementerInternal, isLazy, isProcedure, lazy, mergeContext, os, setRouterContract };
|
@@ -0,0 +1,31 @@
|
|
1
|
+
import { d as StandardHandlerInterceptorOptions, P as Plugin, a as StandardHandlerOptions } from '../shared/server.BT-fqIEm.mjs';
|
2
|
+
export { C as CompositePlugin } from '../shared/server.BT-fqIEm.mjs';
|
3
|
+
import { Value } from '@orpc/shared';
|
4
|
+
import { C as Context } from '../shared/server.ptXwNGQr.mjs';
|
5
|
+
import '@orpc/contract';
|
6
|
+
import '@orpc/standard-server';
|
7
|
+
import '@orpc/client';
|
8
|
+
|
9
|
+
interface CORSOptions<TContext extends Context> {
|
10
|
+
origin?: Value<string | string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<TContext>]>;
|
11
|
+
timingOrigin?: Value<string | string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<TContext>]>;
|
12
|
+
allowMethods?: string[];
|
13
|
+
allowHeaders?: string[];
|
14
|
+
maxAge?: number;
|
15
|
+
credentials?: boolean;
|
16
|
+
exposeHeaders?: string[];
|
17
|
+
}
|
18
|
+
declare class CORSPlugin<TContext extends Context> implements Plugin<TContext> {
|
19
|
+
private readonly options;
|
20
|
+
constructor(options?: Partial<CORSOptions<TContext>>);
|
21
|
+
init(options: StandardHandlerOptions<TContext>): void;
|
22
|
+
}
|
23
|
+
|
24
|
+
interface ResponseHeadersPluginContext {
|
25
|
+
resHeaders?: Headers;
|
26
|
+
}
|
27
|
+
declare class ResponseHeadersPlugin<TContext extends ResponseHeadersPluginContext & Context> implements Plugin<TContext> {
|
28
|
+
init(options: StandardHandlerOptions<TContext>): void;
|
29
|
+
}
|
30
|
+
|
31
|
+
export { type CORSOptions, CORSPlugin, Plugin, ResponseHeadersPlugin, type ResponseHeadersPluginContext };
|
@@ -0,0 +1,31 @@
|
|
1
|
+
import { d as StandardHandlerInterceptorOptions, P as Plugin, a as StandardHandlerOptions } from '../shared/server.xL87pHsk.js';
|
2
|
+
export { C as CompositePlugin } from '../shared/server.xL87pHsk.js';
|
3
|
+
import { Value } from '@orpc/shared';
|
4
|
+
import { C as Context } from '../shared/server.ptXwNGQr.js';
|
5
|
+
import '@orpc/contract';
|
6
|
+
import '@orpc/standard-server';
|
7
|
+
import '@orpc/client';
|
8
|
+
|
9
|
+
interface CORSOptions<TContext extends Context> {
|
10
|
+
origin?: Value<string | string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<TContext>]>;
|
11
|
+
timingOrigin?: Value<string | string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<TContext>]>;
|
12
|
+
allowMethods?: string[];
|
13
|
+
allowHeaders?: string[];
|
14
|
+
maxAge?: number;
|
15
|
+
credentials?: boolean;
|
16
|
+
exposeHeaders?: string[];
|
17
|
+
}
|
18
|
+
declare class CORSPlugin<TContext extends Context> implements Plugin<TContext> {
|
19
|
+
private readonly options;
|
20
|
+
constructor(options?: Partial<CORSOptions<TContext>>);
|
21
|
+
init(options: StandardHandlerOptions<TContext>): void;
|
22
|
+
}
|
23
|
+
|
24
|
+
interface ResponseHeadersPluginContext {
|
25
|
+
resHeaders?: Headers;
|
26
|
+
}
|
27
|
+
declare class ResponseHeadersPlugin<TContext extends ResponseHeadersPluginContext & Context> implements Plugin<TContext> {
|
28
|
+
init(options: StandardHandlerOptions<TContext>): void;
|
29
|
+
}
|
30
|
+
|
31
|
+
export { type CORSOptions, CORSPlugin, Plugin, ResponseHeadersPlugin, type ResponseHeadersPluginContext };
|
@@ -0,0 +1,103 @@
|
|
1
|
+
export { C as CompositePlugin } from '../shared/server.Q6ZmnTgO.mjs';
|
2
|
+
import { value } from '@orpc/shared';
|
3
|
+
|
4
|
+
class CORSPlugin {
|
5
|
+
options;
|
6
|
+
constructor(options) {
|
7
|
+
const defaults = {
|
8
|
+
origin: (origin) => origin,
|
9
|
+
allowMethods: ["GET", "HEAD", "PUT", "POST", "DELETE", "PATCH"]
|
10
|
+
};
|
11
|
+
this.options = {
|
12
|
+
...defaults,
|
13
|
+
...options
|
14
|
+
};
|
15
|
+
}
|
16
|
+
init(options) {
|
17
|
+
options.rootInterceptors ??= [];
|
18
|
+
options.rootInterceptors.unshift(async (interceptorOptions) => {
|
19
|
+
if (interceptorOptions.request.method === "OPTIONS") {
|
20
|
+
const resHeaders = {};
|
21
|
+
if (this.options.maxAge !== void 0) {
|
22
|
+
resHeaders["access-control-max-age"] = this.options.maxAge.toString();
|
23
|
+
}
|
24
|
+
if (this.options.allowMethods?.length) {
|
25
|
+
resHeaders["access-control-allow-methods"] = this.options.allowMethods.join(",");
|
26
|
+
}
|
27
|
+
const allowHeaders = this.options.allowHeaders ?? interceptorOptions.request.headers["access-control-request-headers"];
|
28
|
+
if (Array.isArray(allowHeaders) && allowHeaders.length) {
|
29
|
+
resHeaders["access-control-allow-headers"] = allowHeaders.join(",");
|
30
|
+
} else if (typeof allowHeaders === "string") {
|
31
|
+
resHeaders["access-control-allow-headers"] = allowHeaders;
|
32
|
+
}
|
33
|
+
return {
|
34
|
+
matched: true,
|
35
|
+
response: {
|
36
|
+
status: 204,
|
37
|
+
headers: resHeaders,
|
38
|
+
body: void 0
|
39
|
+
}
|
40
|
+
};
|
41
|
+
}
|
42
|
+
return interceptorOptions.next();
|
43
|
+
});
|
44
|
+
options.rootInterceptors.unshift(async (interceptorOptions) => {
|
45
|
+
const result = await interceptorOptions.next();
|
46
|
+
if (!result.matched) {
|
47
|
+
return result;
|
48
|
+
}
|
49
|
+
const origin = Array.isArray(interceptorOptions.request.headers.origin) ? interceptorOptions.request.headers.origin.join(",") : interceptorOptions.request.headers.origin || "";
|
50
|
+
const allowedOrigin = await value(this.options.origin, origin, interceptorOptions);
|
51
|
+
const allowedOriginArr = Array.isArray(allowedOrigin) ? allowedOrigin : [allowedOrigin];
|
52
|
+
if (allowedOriginArr.includes("*")) {
|
53
|
+
result.response.headers["access-control-allow-origin"] = "*";
|
54
|
+
} else {
|
55
|
+
if (allowedOriginArr.includes(origin)) {
|
56
|
+
result.response.headers["access-control-allow-origin"] = origin;
|
57
|
+
}
|
58
|
+
result.response.headers.vary = interceptorOptions.request.headers.vary ?? "origin";
|
59
|
+
}
|
60
|
+
const allowedTimingOrigin = await value(this.options.timingOrigin, origin, interceptorOptions);
|
61
|
+
const allowedTimingOriginArr = Array.isArray(allowedTimingOrigin) ? allowedTimingOrigin : [allowedTimingOrigin];
|
62
|
+
if (allowedTimingOriginArr.includes("*")) {
|
63
|
+
result.response.headers["timing-allow-origin"] = "*";
|
64
|
+
} else if (allowedTimingOriginArr.includes(origin)) {
|
65
|
+
result.response.headers["timing-allow-origin"] = origin;
|
66
|
+
}
|
67
|
+
if (this.options.credentials) {
|
68
|
+
result.response.headers["access-control-allow-credentials"] = "true";
|
69
|
+
}
|
70
|
+
if (this.options.exposeHeaders?.length) {
|
71
|
+
result.response.headers["access-control-expose-headers"] = this.options.exposeHeaders.join(",");
|
72
|
+
}
|
73
|
+
return result;
|
74
|
+
});
|
75
|
+
}
|
76
|
+
}
|
77
|
+
|
78
|
+
class ResponseHeadersPlugin {
|
79
|
+
init(options) {
|
80
|
+
options.rootInterceptors ??= [];
|
81
|
+
options.rootInterceptors.push(async (interceptorOptions) => {
|
82
|
+
const headers = new Headers();
|
83
|
+
interceptorOptions.context.resHeaders = headers;
|
84
|
+
const result = await interceptorOptions.next();
|
85
|
+
if (!result.matched) {
|
86
|
+
return result;
|
87
|
+
}
|
88
|
+
const responseHeaders = result.response.headers;
|
89
|
+
for (const [key, value] of headers) {
|
90
|
+
if (Array.isArray(responseHeaders[key])) {
|
91
|
+
responseHeaders[key].push(value);
|
92
|
+
} else if (responseHeaders[key] !== void 0) {
|
93
|
+
responseHeaders[key] = [responseHeaders[key], value];
|
94
|
+
} else {
|
95
|
+
responseHeaders[key] = value;
|
96
|
+
}
|
97
|
+
}
|
98
|
+
return result;
|
99
|
+
});
|
100
|
+
}
|
101
|
+
}
|
102
|
+
|
103
|
+
export { CORSPlugin, ResponseHeadersPlugin };
|
@@ -0,0 +1,163 @@
|
|
1
|
+
import { ORPCError, toORPCError } from '@orpc/client';
|
2
|
+
import { intercept, trim, parseEmptyableJSON } from '@orpc/shared';
|
3
|
+
import { C as CompositePlugin } from './server.Q6ZmnTgO.mjs';
|
4
|
+
import { c as createProcedureClient, e as eachContractProcedure, a as convertPathToHttpPath, i as isProcedure, u as unlazy, g as getRouterChild, b as createContractedProcedure } from './server.V6zT5iYQ.mjs';
|
5
|
+
import { RPCSerializer } from '@orpc/client/standard';
|
6
|
+
|
7
|
+
class StandardHandler {
|
8
|
+
constructor(router, matcher, codec, options = {}) {
|
9
|
+
this.matcher = matcher;
|
10
|
+
this.codec = codec;
|
11
|
+
this.options = options;
|
12
|
+
this.plugin = new CompositePlugin(options.plugins);
|
13
|
+
this.plugin.init(this.options);
|
14
|
+
this.matcher.init(router);
|
15
|
+
}
|
16
|
+
plugin;
|
17
|
+
handle(request, ...[options]) {
|
18
|
+
return intercept(
|
19
|
+
this.options.rootInterceptors ?? [],
|
20
|
+
{
|
21
|
+
request,
|
22
|
+
...options,
|
23
|
+
context: options?.context ?? {}
|
24
|
+
// context is optional only when all fields are optional so we can safely force it to have a context
|
25
|
+
},
|
26
|
+
async (interceptorOptions) => {
|
27
|
+
let isDecoding = false;
|
28
|
+
try {
|
29
|
+
return await intercept(
|
30
|
+
this.options.interceptors ?? [],
|
31
|
+
interceptorOptions,
|
32
|
+
async (interceptorOptions2) => {
|
33
|
+
const method = interceptorOptions2.request.method;
|
34
|
+
const url = interceptorOptions2.request.url;
|
35
|
+
const pathname = `/${trim(url.pathname.replace(interceptorOptions2.prefix ?? "", ""), "/")}`;
|
36
|
+
const match = await this.matcher.match(method, pathname);
|
37
|
+
if (!match) {
|
38
|
+
return { matched: false, response: void 0 };
|
39
|
+
}
|
40
|
+
const client = createProcedureClient(match.procedure, {
|
41
|
+
context: interceptorOptions2.context,
|
42
|
+
path: match.path,
|
43
|
+
interceptors: this.options.clientInterceptors
|
44
|
+
});
|
45
|
+
isDecoding = true;
|
46
|
+
const input = await this.codec.decode(request, match.params, match.procedure);
|
47
|
+
isDecoding = false;
|
48
|
+
const lastEventId = Array.isArray(request.headers["last-event-id"]) ? request.headers["last-event-id"].at(-1) : request.headers["last-event-id"];
|
49
|
+
const output = await client(input, { signal: request.signal, lastEventId });
|
50
|
+
const response = this.codec.encode(output, match.procedure);
|
51
|
+
return {
|
52
|
+
matched: true,
|
53
|
+
response
|
54
|
+
};
|
55
|
+
}
|
56
|
+
);
|
57
|
+
} catch (e) {
|
58
|
+
const error = isDecoding ? new ORPCError("BAD_REQUEST", {
|
59
|
+
message: `Malformed request. Ensure the request body is properly formatted and the 'Content-Type' header is set correctly.`,
|
60
|
+
cause: e
|
61
|
+
}) : toORPCError(e);
|
62
|
+
const response = this.codec.encodeError(error);
|
63
|
+
return {
|
64
|
+
matched: true,
|
65
|
+
response
|
66
|
+
};
|
67
|
+
}
|
68
|
+
}
|
69
|
+
);
|
70
|
+
}
|
71
|
+
}
|
72
|
+
|
73
|
+
class RPCCodec {
|
74
|
+
serializer;
|
75
|
+
constructor(options = {}) {
|
76
|
+
this.serializer = options.serializer ?? new RPCSerializer();
|
77
|
+
}
|
78
|
+
async decode(request, _params, _procedure) {
|
79
|
+
const serialized = request.method === "GET" ? parseEmptyableJSON(request.url.searchParams.getAll("data").at(-1)) : await request.body();
|
80
|
+
return this.serializer.deserialize(serialized);
|
81
|
+
}
|
82
|
+
encode(output, _procedure) {
|
83
|
+
return {
|
84
|
+
status: 200,
|
85
|
+
headers: {},
|
86
|
+
body: this.serializer.serialize(output)
|
87
|
+
};
|
88
|
+
}
|
89
|
+
encodeError(error) {
|
90
|
+
return {
|
91
|
+
status: error.status,
|
92
|
+
headers: {},
|
93
|
+
body: this.serializer.serialize(error.toJSON())
|
94
|
+
};
|
95
|
+
}
|
96
|
+
}
|
97
|
+
|
98
|
+
class RPCMatcher {
|
99
|
+
tree = {};
|
100
|
+
pendingRouters = [];
|
101
|
+
init(router, path = []) {
|
102
|
+
const laziedOptions = eachContractProcedure({
|
103
|
+
router,
|
104
|
+
path
|
105
|
+
}, ({ path: path2, contract }) => {
|
106
|
+
const httpPath = convertPathToHttpPath(path2);
|
107
|
+
if (isProcedure(contract)) {
|
108
|
+
this.tree[httpPath] = {
|
109
|
+
path: path2,
|
110
|
+
contract,
|
111
|
+
procedure: contract,
|
112
|
+
// this mean dev not used contract-first so we can used contract as procedure directly
|
113
|
+
router
|
114
|
+
};
|
115
|
+
} else {
|
116
|
+
this.tree[httpPath] = {
|
117
|
+
path: path2,
|
118
|
+
contract,
|
119
|
+
procedure: void 0,
|
120
|
+
router
|
121
|
+
};
|
122
|
+
}
|
123
|
+
});
|
124
|
+
this.pendingRouters.push(...laziedOptions.map((option) => ({
|
125
|
+
...option,
|
126
|
+
httpPathPrefix: convertPathToHttpPath(option.path)
|
127
|
+
})));
|
128
|
+
}
|
129
|
+
async match(_method, pathname) {
|
130
|
+
if (this.pendingRouters.length) {
|
131
|
+
const newPendingRouters = [];
|
132
|
+
for (const pendingRouter of this.pendingRouters) {
|
133
|
+
if (pathname.startsWith(pendingRouter.httpPathPrefix)) {
|
134
|
+
const { default: router } = await unlazy(pendingRouter.lazied);
|
135
|
+
this.init(router, pendingRouter.path);
|
136
|
+
} else {
|
137
|
+
newPendingRouters.push(pendingRouter);
|
138
|
+
}
|
139
|
+
}
|
140
|
+
this.pendingRouters = newPendingRouters;
|
141
|
+
}
|
142
|
+
const match = this.tree[pathname];
|
143
|
+
if (!match) {
|
144
|
+
return void 0;
|
145
|
+
}
|
146
|
+
if (!match.procedure) {
|
147
|
+
const { default: maybeProcedure } = await unlazy(getRouterChild(match.router, ...match.path));
|
148
|
+
if (!isProcedure(maybeProcedure)) {
|
149
|
+
throw new Error(`
|
150
|
+
[Contract-First] Missing or invalid implementation for procedure at path: ${convertPathToHttpPath(match.path)}.
|
151
|
+
Ensure that the procedure is correctly defined and matches the expected contract.
|
152
|
+
`);
|
153
|
+
}
|
154
|
+
match.procedure = createContractedProcedure(match.contract, maybeProcedure);
|
155
|
+
}
|
156
|
+
return {
|
157
|
+
path: match.path,
|
158
|
+
procedure: match.procedure
|
159
|
+
};
|
160
|
+
}
|
161
|
+
}
|
162
|
+
|
163
|
+
export { RPCCodec as R, StandardHandler as S, RPCMatcher as a };
|
@@ -0,0 +1,9 @@
|
|
1
|
+
import { C as Context } from './server.ptXwNGQr.mjs';
|
2
|
+
import { a as StandardHandlerOptions, b as StandardMatcher, c as StandardCodec } from './server.BT-fqIEm.mjs';
|
3
|
+
|
4
|
+
interface RPCHandlerOptions<T extends Context> extends StandardHandlerOptions<T> {
|
5
|
+
matcher?: StandardMatcher;
|
6
|
+
codec?: StandardCodec;
|
7
|
+
}
|
8
|
+
|
9
|
+
export type { RPCHandlerOptions as R };
|