@orpc/server 0.0.0-next.32cb70c → 0.0.0-next.352022d

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.
Files changed (72) hide show
  1. package/README.md +118 -0
  2. package/dist/adapters/fetch/index.d.mts +26 -0
  3. package/dist/adapters/fetch/index.d.ts +26 -0
  4. package/dist/adapters/fetch/index.mjs +9 -0
  5. package/dist/adapters/hono/index.d.mts +19 -0
  6. package/dist/adapters/hono/index.d.ts +19 -0
  7. package/dist/adapters/hono/index.mjs +32 -0
  8. package/dist/adapters/next/index.d.mts +26 -0
  9. package/dist/adapters/next/index.d.ts +26 -0
  10. package/dist/{next.js → adapters/next/index.mjs} +11 -18
  11. package/dist/adapters/node/index.d.mts +34 -0
  12. package/dist/adapters/node/index.d.ts +34 -0
  13. package/dist/adapters/node/index.mjs +31 -0
  14. package/dist/adapters/standard/index.d.mts +25 -0
  15. package/dist/adapters/standard/index.d.ts +25 -0
  16. package/dist/adapters/standard/index.mjs +6 -0
  17. package/dist/index.d.mts +269 -0
  18. package/dist/index.d.ts +269 -0
  19. package/dist/{index.js → index.mjs} +68 -119
  20. package/dist/plugins/index.d.mts +31 -0
  21. package/dist/plugins/index.d.ts +31 -0
  22. package/dist/plugins/index.mjs +103 -0
  23. package/dist/shared/server.CM3tWr3C.d.mts +75 -0
  24. package/dist/shared/server.CPteJIPP.d.mts +143 -0
  25. package/dist/shared/server.CPteJIPP.d.ts +143 -0
  26. package/dist/shared/server.CXu_v2vM.mjs +28 -0
  27. package/dist/shared/server.DmW25ynm.d.ts +75 -0
  28. package/dist/shared/server.N2ttmv6r.mjs +158 -0
  29. package/dist/shared/server.Q6ZmnTgO.mjs +12 -0
  30. package/dist/shared/server.kPUGzsdw.mjs +357 -0
  31. package/package.json +36 -24
  32. package/dist/chunk-ESTRJAOX.js +0 -299
  33. package/dist/chunk-KK4SDLC7.js +0 -320
  34. package/dist/chunk-WUOGVGWG.js +0 -1
  35. package/dist/fetch.js +0 -15
  36. package/dist/hono.js +0 -30
  37. package/dist/node.js +0 -87
  38. package/dist/src/adapters/fetch/index.d.ts +0 -6
  39. package/dist/src/adapters/fetch/orpc-handler.d.ts +0 -20
  40. package/dist/src/adapters/fetch/orpc-payload-codec.d.ts +0 -16
  41. package/dist/src/adapters/fetch/orpc-procedure-matcher.d.ts +0 -12
  42. package/dist/src/adapters/fetch/super-json.d.ts +0 -12
  43. package/dist/src/adapters/fetch/types.d.ts +0 -21
  44. package/dist/src/adapters/hono/index.d.ts +0 -3
  45. package/dist/src/adapters/hono/middleware.d.ts +0 -12
  46. package/dist/src/adapters/next/index.d.ts +0 -3
  47. package/dist/src/adapters/next/serve.d.ts +0 -19
  48. package/dist/src/adapters/node/index.d.ts +0 -5
  49. package/dist/src/adapters/node/orpc-handler.d.ts +0 -12
  50. package/dist/src/adapters/node/request-listener.d.ts +0 -28
  51. package/dist/src/adapters/node/types.d.ts +0 -22
  52. package/dist/src/builder-variants.d.ts +0 -74
  53. package/dist/src/builder.d.ts +0 -57
  54. package/dist/src/config.d.ts +0 -6
  55. package/dist/src/context.d.ts +0 -9
  56. package/dist/src/hidden.d.ts +0 -8
  57. package/dist/src/implementer-procedure.d.ts +0 -30
  58. package/dist/src/implementer-variants.d.ts +0 -16
  59. package/dist/src/implementer.d.ts +0 -27
  60. package/dist/src/index.d.ts +0 -21
  61. package/dist/src/lazy-utils.d.ts +0 -6
  62. package/dist/src/lazy.d.ts +0 -22
  63. package/dist/src/middleware-decorated.d.ts +0 -10
  64. package/dist/src/middleware-utils.d.ts +0 -5
  65. package/dist/src/middleware.d.ts +0 -37
  66. package/dist/src/procedure-client.d.ts +0 -20
  67. package/dist/src/procedure-decorated.d.ts +0 -21
  68. package/dist/src/procedure-utils.d.ts +0 -17
  69. package/dist/src/procedure.d.ts +0 -29
  70. package/dist/src/router-accessible-lazy.d.ts +0 -8
  71. package/dist/src/router-client.d.ts +0 -22
  72. package/dist/src/router.d.ts +0 -29
@@ -1,29 +1,12 @@
1
- import {
2
- LAZY_LOADER_SYMBOL,
3
- Procedure,
4
- adaptRouter,
5
- addMiddleware,
6
- createAccessibleLazyRouter,
7
- createLazyProcedureFormAnyLazy,
8
- createProcedureClient,
9
- deepSetLazyRouterPrefix,
10
- flatLazy,
11
- getLazyRouterPrefix,
12
- getRouterChild,
13
- getRouterContract,
14
- isLazy,
15
- isProcedure,
16
- lazy,
17
- middlewareOutputFn,
18
- setRouterContract,
19
- unlazy
20
- } from "./chunk-KK4SDLC7.js";
1
+ import { mergeErrorMap, mergeMeta, mergeRoute, mergePrefix, mergeTags, isContractProcedure, getContractRouter } from '@orpc/contract';
2
+ export { ValidationError, eventIterator, type } from '@orpc/contract';
3
+ import { P as Procedure, d as addMiddleware, c as createProcedureClient, e as enhanceRouter, l as lazy, s as setHiddenRouterContract, i as isProcedure, f as isLazy, h as createAssertedLazyProcedure, g as getRouter } from './shared/server.kPUGzsdw.mjs';
4
+ export { L as LAZY_SYMBOL, k as call, o as createAccessibleLazyRouter, b as createContractedProcedure, n as getHiddenRouterContract, j as getLazyMeta, m as middlewareOutputFn, r as resolveContractProcedures, a as toHttpPath, t as traverseContractProcedures, u as unlazy, p as unlazyRouter } from './shared/server.kPUGzsdw.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';
21
8
 
22
- // src/builder.ts
23
- import { mergeErrorMap as mergeErrorMap2, mergeMeta as mergeMeta2, mergePrefix, mergeRoute as mergeRoute2, mergeTags } from "@orpc/contract";
24
-
25
- // src/config.ts
26
- var DEFAULT_CONFIG = {
9
+ const DEFAULT_CONFIG = {
27
10
  initialInputValidationIndex: 0,
28
11
  initialOutputValidationIndex: 0
29
12
  };
@@ -34,7 +17,6 @@ function fallbackConfig(key, value) {
34
17
  return value;
35
18
  }
36
19
 
37
- // src/middleware-decorated.ts
38
20
  function decorateMiddleware(middleware) {
39
21
  const decorated = middleware;
40
22
  decorated.mapInput = (mapInput) => {
@@ -46,10 +28,14 @@ function decorateMiddleware(middleware) {
46
28
  decorated.concat = (concatMiddleware, mapInput) => {
47
29
  const mapped = mapInput ? decorateMiddleware(concatMiddleware).mapInput(mapInput) : concatMiddleware;
48
30
  const concatted = decorateMiddleware((options, input, output, ...rest) => {
49
- const next = async (...[nextOptions]) => {
50
- return mapped({ ...options, context: { ...nextOptions?.context, ...options.context } }, input, output, ...rest);
51
- };
52
- const merged = middleware({ ...options, next }, input, output, ...rest);
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);
53
39
  return merged;
54
40
  });
55
41
  return concatted;
@@ -57,30 +43,28 @@ function decorateMiddleware(middleware) {
57
43
  return decorated;
58
44
  }
59
45
 
60
- // src/procedure-decorated.ts
61
- import { mergeErrorMap, mergeMeta, mergeRoute } from "@orpc/contract";
62
- var DecoratedProcedure = class _DecoratedProcedure extends Procedure {
46
+ class DecoratedProcedure extends Procedure {
63
47
  errors(errors) {
64
- return new _DecoratedProcedure({
48
+ return new DecoratedProcedure({
65
49
  ...this["~orpc"],
66
50
  errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
67
51
  });
68
52
  }
69
53
  meta(meta) {
70
- return new _DecoratedProcedure({
54
+ return new DecoratedProcedure({
71
55
  ...this["~orpc"],
72
56
  meta: mergeMeta(this["~orpc"].meta, meta)
73
57
  });
74
58
  }
75
59
  route(route) {
76
- return new _DecoratedProcedure({
60
+ return new DecoratedProcedure({
77
61
  ...this["~orpc"],
78
62
  route: mergeRoute(this["~orpc"].route, route)
79
63
  });
80
64
  }
81
65
  use(middleware, mapInput) {
82
66
  const mapped = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
83
- return new _DecoratedProcedure({
67
+ return new DecoratedProcedure({
84
68
  ...this["~orpc"],
85
69
  middlewares: addMiddleware(this["~orpc"].middlewares, mapped)
86
70
  });
@@ -100,10 +84,9 @@ var DecoratedProcedure = class _DecoratedProcedure extends Procedure {
100
84
  actionable(...rest) {
101
85
  return this.callable(...rest);
102
86
  }
103
- };
87
+ }
104
88
 
105
- // src/builder.ts
106
- var Builder = class _Builder {
89
+ class Builder {
107
90
  "~orpc";
108
91
  constructor(def) {
109
92
  this["~orpc"] = def;
@@ -114,7 +97,7 @@ var Builder = class _Builder {
114
97
  $config(config) {
115
98
  const inputValidationCount = this["~orpc"].inputValidationIndex - fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex);
116
99
  const outputValidationCount = this["~orpc"].outputValidationIndex - fallbackConfig("initialOutputValidationIndex", this["~orpc"].config.initialOutputValidationIndex);
117
- return new _Builder({
100
+ return new Builder({
118
101
  ...this["~orpc"],
119
102
  config,
120
103
  inputValidationIndex: fallbackConfig("initialInputValidationIndex", config.initialInputValidationIndex) + inputValidationCount,
@@ -125,7 +108,7 @@ var Builder = class _Builder {
125
108
  * Reset initial context
126
109
  */
127
110
  $context() {
128
- return new _Builder({
111
+ return new Builder({
129
112
  ...this["~orpc"],
130
113
  middlewares: [],
131
114
  inputValidationIndex: fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex),
@@ -136,7 +119,7 @@ var Builder = class _Builder {
136
119
  * Reset initial meta
137
120
  */
138
121
  $meta(initialMeta) {
139
- return new _Builder({
122
+ return new Builder({
140
123
  ...this["~orpc"],
141
124
  meta: initialMeta
142
125
  });
@@ -145,48 +128,54 @@ var Builder = class _Builder {
145
128
  * Reset initial route
146
129
  */
147
130
  $route(initialRoute) {
148
- return new _Builder({
131
+ return new Builder({
149
132
  ...this["~orpc"],
150
133
  route: initialRoute
151
134
  });
152
135
  }
136
+ $input(initialInputSchema) {
137
+ return new Builder({
138
+ ...this["~orpc"],
139
+ inputSchema: initialInputSchema
140
+ });
141
+ }
153
142
  middleware(middleware) {
154
143
  return decorateMiddleware(middleware);
155
144
  }
156
145
  errors(errors) {
157
- return new _Builder({
146
+ return new Builder({
158
147
  ...this["~orpc"],
159
- errorMap: mergeErrorMap2(this["~orpc"].errorMap, errors)
148
+ errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
160
149
  });
161
150
  }
162
151
  use(middleware, mapInput) {
163
152
  const mapped = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
164
- return new _Builder({
153
+ return new Builder({
165
154
  ...this["~orpc"],
166
155
  middlewares: addMiddleware(this["~orpc"].middlewares, mapped)
167
156
  });
168
157
  }
169
158
  meta(meta) {
170
- return new _Builder({
159
+ return new Builder({
171
160
  ...this["~orpc"],
172
- meta: mergeMeta2(this["~orpc"].meta, meta)
161
+ meta: mergeMeta(this["~orpc"].meta, meta)
173
162
  });
174
163
  }
175
164
  route(route) {
176
- return new _Builder({
165
+ return new Builder({
177
166
  ...this["~orpc"],
178
- route: mergeRoute2(this["~orpc"].route, route)
167
+ route: mergeRoute(this["~orpc"].route, route)
179
168
  });
180
169
  }
181
170
  input(schema) {
182
- return new _Builder({
171
+ return new Builder({
183
172
  ...this["~orpc"],
184
173
  inputSchema: schema,
185
174
  inputValidationIndex: fallbackConfig("initialInputValidationIndex", this["~orpc"].config.initialInputValidationIndex) + this["~orpc"].middlewares.length
186
175
  });
187
176
  }
188
177
  output(schema) {
189
- return new _Builder({
178
+ return new Builder({
190
179
  ...this["~orpc"],
191
180
  outputSchema: schema,
192
181
  outputValidationIndex: fallbackConfig("initialOutputValidationIndex", this["~orpc"].config.initialOutputValidationIndex) + this["~orpc"].middlewares.length
@@ -199,43 +188,38 @@ var Builder = class _Builder {
199
188
  });
200
189
  }
201
190
  prefix(prefix) {
202
- return new _Builder({
191
+ return new Builder({
203
192
  ...this["~orpc"],
204
193
  prefix: mergePrefix(this["~orpc"].prefix, prefix)
205
194
  });
206
195
  }
207
196
  tag(...tags) {
208
- return new _Builder({
197
+ return new Builder({
209
198
  ...this["~orpc"],
210
199
  tags: mergeTags(this["~orpc"].tags, tags)
211
200
  });
212
201
  }
213
202
  router(router) {
214
- return adaptRouter(router, this["~orpc"]);
203
+ return enhanceRouter(router, this["~orpc"]);
215
204
  }
216
205
  lazy(loader) {
217
- return adaptRouter(flatLazy(lazy(loader)), this["~orpc"]);
206
+ return enhanceRouter(lazy(loader), this["~orpc"]);
218
207
  }
219
- };
220
- var os = new Builder({
208
+ }
209
+ const os = new Builder({
221
210
  config: {},
222
211
  route: {},
223
212
  meta: {},
224
213
  errorMap: {},
225
- inputSchema: void 0,
226
- outputSchema: void 0,
227
214
  inputValidationIndex: fallbackConfig("initialInputValidationIndex"),
228
215
  outputValidationIndex: fallbackConfig("initialOutputValidationIndex"),
229
216
  middlewares: []
230
217
  });
231
218
 
232
- // src/context.ts
233
219
  function mergeContext(context, other) {
234
220
  return { ...context, ...other };
235
221
  }
236
222
 
237
- // src/implementer.ts
238
- import { isContractProcedure } from "@orpc/contract";
239
223
  function implementerInternal(contract, config, middlewares) {
240
224
  if (isContractProcedure(contract)) {
241
225
  const impl2 = new Builder({
@@ -249,6 +233,9 @@ function implementerInternal(contract, config, middlewares) {
249
233
  }
250
234
  const impl = new Proxy(contract, {
251
235
  get: (target, key) => {
236
+ if (typeof key !== "string") {
237
+ return Reflect.get(target, key);
238
+ }
252
239
  let method;
253
240
  if (key === "middleware") {
254
241
  method = (mid) => decorateMiddleware(mid);
@@ -262,23 +249,27 @@ function implementerInternal(contract, config, middlewares) {
262
249
  };
263
250
  } else if (key === "router") {
264
251
  method = (router) => {
265
- const adapted = adaptRouter(router, {
252
+ const adapted = enhanceRouter(router, {
266
253
  middlewares,
267
- errorMap: {}
254
+ errorMap: {},
255
+ prefix: void 0,
256
+ tags: void 0
268
257
  });
269
- return setRouterContract(adapted, contract);
258
+ return setHiddenRouterContract(adapted, contract);
270
259
  };
271
260
  } else if (key === "lazy") {
272
261
  method = (loader) => {
273
- const adapted = adaptRouter(flatLazy(lazy(loader)), {
262
+ const adapted = enhanceRouter(lazy(loader), {
274
263
  middlewares,
275
- errorMap: {}
264
+ errorMap: {},
265
+ prefix: void 0,
266
+ tags: void 0
276
267
  });
277
- return setRouterContract(adapted, contract);
268
+ return setHiddenRouterContract(adapted, contract);
278
269
  };
279
270
  }
280
- const next = Reflect.get(target, key);
281
- if (!next || typeof next !== "function" && typeof next !== "object") {
271
+ const next = getContractRouter(target, [key]);
272
+ if (!next) {
282
273
  return method ?? next;
283
274
  }
284
275
  const nextImpl = implementerInternal(next, config, middlewares);
@@ -321,28 +312,21 @@ function implement(contract, config = {}) {
321
312
  return impl;
322
313
  }
323
314
 
324
- // src/procedure-utils.ts
325
- function call(procedure, input, ...rest) {
326
- return createProcedureClient(procedure, ...rest)(input);
327
- }
328
-
329
- // src/router-client.ts
330
- function createRouterClient(router, ...rest) {
315
+ function createRouterClient(router, ...[options]) {
331
316
  if (isProcedure(router)) {
332
- const caller = createProcedureClient(router, ...rest);
317
+ const caller = createProcedureClient(router, options);
333
318
  return caller;
334
319
  }
335
- const procedureCaller = isLazy(router) ? createProcedureClient(createLazyProcedureFormAnyLazy(router), ...rest) : {};
320
+ const procedureCaller = isLazy(router) ? createProcedureClient(createAssertedLazyProcedure(router), options) : {};
336
321
  const recursive = new Proxy(procedureCaller, {
337
322
  get(target, key) {
338
323
  if (typeof key !== "string") {
339
324
  return Reflect.get(target, key);
340
325
  }
341
- const next = getRouterChild(router, key);
326
+ const next = getRouter(router, [key]);
342
327
  if (!next) {
343
328
  return Reflect.get(target, key);
344
329
  }
345
- const [options] = rest;
346
330
  return createRouterClient(next, {
347
331
  ...options,
348
332
  path: [...options?.path ?? [], key]
@@ -352,39 +336,4 @@ function createRouterClient(router, ...rest) {
352
336
  return recursive;
353
337
  }
354
338
 
355
- // src/index.ts
356
- import { isDefinedError, ORPCError, safe, type } from "@orpc/contract";
357
- export {
358
- Builder,
359
- DecoratedProcedure,
360
- LAZY_LOADER_SYMBOL,
361
- ORPCError,
362
- Procedure,
363
- adaptRouter,
364
- call,
365
- createAccessibleLazyRouter,
366
- createLazyProcedureFormAnyLazy,
367
- createProcedureClient,
368
- createRouterClient,
369
- decorateMiddleware,
370
- deepSetLazyRouterPrefix,
371
- fallbackConfig,
372
- flatLazy,
373
- getLazyRouterPrefix,
374
- getRouterChild,
375
- getRouterContract,
376
- implement,
377
- implementerInternal,
378
- isDefinedError,
379
- isLazy,
380
- isProcedure,
381
- lazy,
382
- mergeContext,
383
- middlewareOutputFn,
384
- os,
385
- safe,
386
- setRouterContract,
387
- type,
388
- unlazy
389
- };
390
- //# sourceMappingURL=index.js.map
339
+ export { Builder, DecoratedProcedure, Procedure, createAssertedLazyProcedure, createProcedureClient, createRouterClient, decorateMiddleware, enhanceRouter, fallbackConfig, getRouter, implement, implementerInternal, isLazy, isProcedure, lazy, mergeContext, os, setHiddenRouterContract };
@@ -0,0 +1,31 @@
1
+ import { a as StandardHandlerInterceptorOptions, H as HandlerPlugin, b as StandardHandlerOptions } from '../shared/server.CM3tWr3C.mjs';
2
+ export { C as CompositePlugin } from '../shared/server.CM3tWr3C.mjs';
3
+ import { Value } from '@orpc/shared';
4
+ import { C as Context } from '../shared/server.CPteJIPP.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 | readonly string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<TContext>]>;
11
+ timingOrigin?: Value<string | readonly string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<TContext>]>;
12
+ allowMethods?: readonly string[];
13
+ allowHeaders?: readonly string[];
14
+ maxAge?: number;
15
+ credentials?: boolean;
16
+ exposeHeaders?: readonly string[];
17
+ }
18
+ declare class CORSPlugin<TContext extends Context> implements HandlerPlugin<TContext> {
19
+ private readonly options;
20
+ constructor(options?: 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 HandlerPlugin<TContext> {
28
+ init(options: StandardHandlerOptions<TContext>): void;
29
+ }
30
+
31
+ export { type CORSOptions, CORSPlugin, HandlerPlugin, ResponseHeadersPlugin, type ResponseHeadersPluginContext };
@@ -0,0 +1,31 @@
1
+ import { a as StandardHandlerInterceptorOptions, H as HandlerPlugin, b as StandardHandlerOptions } from '../shared/server.DmW25ynm.js';
2
+ export { C as CompositePlugin } from '../shared/server.DmW25ynm.js';
3
+ import { Value } from '@orpc/shared';
4
+ import { C as Context } from '../shared/server.CPteJIPP.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 | readonly string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<TContext>]>;
11
+ timingOrigin?: Value<string | readonly string[] | null | undefined, [origin: string, options: StandardHandlerInterceptorOptions<TContext>]>;
12
+ allowMethods?: readonly string[];
13
+ allowHeaders?: readonly string[];
14
+ maxAge?: number;
15
+ credentials?: boolean;
16
+ exposeHeaders?: readonly string[];
17
+ }
18
+ declare class CORSPlugin<TContext extends Context> implements HandlerPlugin<TContext> {
19
+ private readonly options;
20
+ constructor(options?: 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 HandlerPlugin<TContext> {
28
+ init(options: StandardHandlerOptions<TContext>): void;
29
+ }
30
+
31
+ export { type CORSOptions, CORSPlugin, HandlerPlugin, 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,75 @@
1
+ import { HTTPPath, AnySchema, Meta, InferSchemaOutput, ErrorFromErrorMap } from '@orpc/contract';
2
+ import { Interceptor, MaybeOptionalOptions } from '@orpc/shared';
3
+ import { StandardResponse, StandardLazyRequest } from '@orpc/standard-server';
4
+ import { a as AnyRouter, A as AnyProcedure, C as Context, P as ProcedureClientInterceptorOptions, R as Router } from './server.CPteJIPP.mjs';
5
+ import { ORPCError } from '@orpc/client';
6
+
7
+ type StandardParams = Record<string, string>;
8
+ type StandardMatchResult = {
9
+ path: readonly string[];
10
+ procedure: AnyProcedure;
11
+ params?: StandardParams;
12
+ } | undefined;
13
+ interface StandardMatcher {
14
+ init(router: AnyRouter): void;
15
+ match(method: string, pathname: HTTPPath): Promise<StandardMatchResult>;
16
+ }
17
+ interface StandardCodec {
18
+ encode(output: unknown, procedure: AnyProcedure): StandardResponse;
19
+ encodeError(error: ORPCError<any, any>): StandardResponse;
20
+ decode(request: StandardLazyRequest, params: StandardParams | undefined, procedure: AnyProcedure): Promise<unknown>;
21
+ }
22
+
23
+ type StandardHandleOptions<T extends Context> = {
24
+ prefix?: HTTPPath;
25
+ } & (Record<never, never> extends T ? {
26
+ context?: T;
27
+ } : {
28
+ context: T;
29
+ });
30
+ type StandardHandleResult = {
31
+ matched: true;
32
+ response: StandardResponse;
33
+ } | {
34
+ matched: false;
35
+ response: undefined;
36
+ };
37
+ type StandardHandlerInterceptorOptions<T extends Context> = StandardHandleOptions<T> & {
38
+ context: T;
39
+ request: StandardLazyRequest;
40
+ };
41
+ interface StandardHandlerOptions<TContext extends Context> {
42
+ plugins?: HandlerPlugin<TContext>[];
43
+ /**
44
+ * Interceptors at the request level, helpful when you want catch errors
45
+ */
46
+ interceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult, unknown>[];
47
+ /**
48
+ * Interceptors at the root level, helpful when you want override the request/response
49
+ */
50
+ rootInterceptors?: Interceptor<StandardHandlerInterceptorOptions<TContext>, StandardHandleResult, unknown>[];
51
+ /**
52
+ *
53
+ * Interceptors for procedure client.
54
+ */
55
+ clientInterceptors?: Interceptor<ProcedureClientInterceptorOptions<TContext, AnySchema, Record<never, never>, Meta>, InferSchemaOutput<AnySchema>, ErrorFromErrorMap<Record<never, never>>>[];
56
+ }
57
+ declare class StandardHandler<T extends Context> {
58
+ private readonly matcher;
59
+ private readonly codec;
60
+ private readonly options;
61
+ private readonly plugin;
62
+ constructor(router: Router<any, T>, matcher: StandardMatcher, codec: StandardCodec, options: NoInfer<StandardHandlerOptions<T>>);
63
+ handle(request: StandardLazyRequest, ...[options]: MaybeOptionalOptions<StandardHandleOptions<T>>): Promise<StandardHandleResult>;
64
+ }
65
+
66
+ interface HandlerPlugin<TContext extends Context> {
67
+ init?(options: StandardHandlerOptions<TContext>): void;
68
+ }
69
+ declare class CompositePlugin<TContext extends Context> implements HandlerPlugin<TContext> {
70
+ private readonly plugins;
71
+ constructor(plugins?: HandlerPlugin<TContext>[]);
72
+ init(options: StandardHandlerOptions<TContext>): void;
73
+ }
74
+
75
+ export { CompositePlugin as C, type HandlerPlugin as H, type StandardHandleOptions as S, type StandardHandlerInterceptorOptions as a, type StandardHandlerOptions as b, type StandardCodec as c, type StandardParams as d, type StandardMatcher as e, type StandardMatchResult as f, type StandardHandleResult as g, StandardHandler as h };