@orpc/server 0.0.0-next.c59d67c → 0.0.0-next.caefe3a

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 (69) hide show
  1. package/dist/chunk-M65OCXNG.js +136 -0
  2. package/dist/chunk-OEPDHGQ4.js +378 -0
  3. package/dist/chunk-OWMUECGT.js +111 -0
  4. package/dist/chunk-PZ44ZPVC.js +308 -0
  5. package/dist/fetch.js +14 -102
  6. package/dist/hono.js +42 -0
  7. package/dist/index.js +332 -322
  8. package/dist/next.js +39 -0
  9. package/dist/node.js +175 -0
  10. package/dist/plugins.js +11 -0
  11. package/dist/src/adapters/fetch/index.d.ts +4 -0
  12. package/dist/src/adapters/fetch/rpc-handler.d.ts +10 -0
  13. package/dist/src/adapters/fetch/types.d.ts +13 -0
  14. package/dist/src/adapters/fetch/utils.d.ts +6 -0
  15. package/dist/src/adapters/hono/index.d.ts +3 -0
  16. package/dist/src/adapters/hono/middleware.d.ts +13 -0
  17. package/dist/src/adapters/next/index.d.ts +3 -0
  18. package/dist/src/adapters/next/serve.d.ts +20 -0
  19. package/dist/src/adapters/node/index.d.ts +4 -0
  20. package/dist/src/adapters/node/rpc-handler.d.ts +10 -0
  21. package/dist/src/adapters/node/types.d.ts +21 -0
  22. package/dist/src/adapters/node/utils.d.ts +5 -0
  23. package/dist/src/adapters/standard/handler.d.ts +47 -0
  24. package/dist/src/adapters/standard/index.d.ts +7 -0
  25. package/dist/src/adapters/standard/rpc-codec.d.ts +15 -0
  26. package/dist/src/adapters/standard/rpc-handler.d.ts +8 -0
  27. package/dist/src/adapters/standard/rpc-matcher.d.ts +10 -0
  28. package/dist/src/adapters/standard/rpc-serializer.d.ts +16 -0
  29. package/dist/src/adapters/standard/types.d.ts +44 -0
  30. package/dist/src/builder-variants.d.ts +74 -0
  31. package/dist/src/builder.d.ts +47 -39
  32. package/dist/src/config.d.ts +6 -0
  33. package/dist/src/context.d.ts +9 -0
  34. package/dist/src/hidden.d.ts +8 -0
  35. package/dist/src/implementer-procedure.d.ts +30 -0
  36. package/dist/src/implementer-variants.d.ts +17 -0
  37. package/dist/src/implementer.d.ts +28 -0
  38. package/dist/src/index.d.ts +17 -9
  39. package/dist/src/lazy-utils.d.ts +6 -0
  40. package/dist/src/lazy.d.ts +22 -0
  41. package/dist/src/middleware-decorated.d.ts +10 -0
  42. package/dist/src/middleware-utils.d.ts +5 -0
  43. package/dist/src/middleware.d.ts +28 -17
  44. package/dist/src/plugins/base.d.ts +11 -0
  45. package/dist/src/plugins/cors.d.ts +18 -0
  46. package/dist/src/plugins/index.d.ts +4 -0
  47. package/dist/src/plugins/response-headers.d.ts +10 -0
  48. package/dist/src/procedure-client.d.ts +31 -0
  49. package/dist/src/procedure-decorated.d.ts +21 -0
  50. package/dist/src/procedure-utils.d.ts +17 -0
  51. package/dist/src/procedure.d.ts +25 -25
  52. package/dist/src/router-accessible-lazy.d.ts +8 -0
  53. package/dist/src/router-client.d.ts +10 -0
  54. package/dist/src/router.d.ts +25 -16
  55. package/dist/src/utils.d.ts +23 -2
  56. package/dist/standard.js +17 -0
  57. package/package.json +34 -9
  58. package/dist/chunk-TDFYNRZV.js +0 -190
  59. package/dist/src/fetch/handle.d.ts +0 -7
  60. package/dist/src/fetch/handler.d.ts +0 -3
  61. package/dist/src/fetch/index.d.ts +0 -4
  62. package/dist/src/fetch/types.d.ts +0 -35
  63. package/dist/src/procedure-builder.d.ts +0 -31
  64. package/dist/src/procedure-caller.d.ts +0 -19
  65. package/dist/src/procedure-implementer.d.ts +0 -18
  66. package/dist/src/router-builder.d.ts +0 -22
  67. package/dist/src/router-caller.d.ts +0 -22
  68. package/dist/src/router-implementer.d.ts +0 -20
  69. package/dist/src/types.d.ts +0 -8
@@ -0,0 +1,308 @@
1
+ import {
2
+ convertPathToHttpPath,
3
+ createContractedProcedure,
4
+ createProcedureClient,
5
+ eachContractProcedure,
6
+ getRouterChild,
7
+ isProcedure,
8
+ unlazy
9
+ } from "./chunk-OEPDHGQ4.js";
10
+ import {
11
+ CompositePlugin
12
+ } from "./chunk-OWMUECGT.js";
13
+
14
+ // src/adapters/standard/handler.ts
15
+ import { toORPCError } from "@orpc/contract";
16
+ import { intercept, trim } from "@orpc/shared";
17
+ var StandardHandler = class {
18
+ constructor(router, matcher, codec, options = {}) {
19
+ this.matcher = matcher;
20
+ this.codec = codec;
21
+ this.options = options;
22
+ this.plugin = new CompositePlugin(options?.plugins);
23
+ this.plugin.init(this.options);
24
+ this.matcher.init(router);
25
+ }
26
+ plugin;
27
+ handle(request, ...[options]) {
28
+ const handleOptions = options ?? {};
29
+ handleOptions.context ??= {};
30
+ return intercept(
31
+ this.options.interceptorsRoot ?? [],
32
+ { request, ...handleOptions },
33
+ async (interceptorRootOptions) => {
34
+ try {
35
+ return await intercept(
36
+ this.options.interceptors ?? [],
37
+ interceptorRootOptions,
38
+ async ({ request: request2, context }) => {
39
+ const method = request2.method;
40
+ const url = request2.url;
41
+ const pathname = `/${trim(url.pathname.replace(options?.prefix ?? "", ""), "/")}`;
42
+ const match = await this.matcher.match(method, pathname);
43
+ if (!match) {
44
+ return { matched: false, response: void 0 };
45
+ }
46
+ const client = createProcedureClient(match.procedure, {
47
+ context,
48
+ path: match.path
49
+ });
50
+ const input = await this.codec.decode(request2, match.params, match.procedure);
51
+ const output = await client(input, { signal: request2.signal });
52
+ const response = this.codec.encode(output, match.procedure);
53
+ return {
54
+ matched: true,
55
+ response
56
+ };
57
+ }
58
+ );
59
+ } catch (e) {
60
+ const error = toORPCError(e);
61
+ const response = this.codec.encodeError(error);
62
+ return {
63
+ matched: true,
64
+ response
65
+ };
66
+ }
67
+ }
68
+ );
69
+ }
70
+ };
71
+
72
+ // src/adapters/standard/rpc-serializer.ts
73
+ import { findDeepMatches, isPlainObject, set } from "@orpc/shared";
74
+ var RPCSerializer = class {
75
+ serialize(data) {
76
+ if (data instanceof Blob) {
77
+ return data;
78
+ }
79
+ const serializedJSON = serializeRPCJson(data);
80
+ const { maps, values: blobs } = findDeepMatches((v) => v instanceof Blob, serializedJSON.json);
81
+ if (blobs.length === 0) {
82
+ return serializedJSON;
83
+ }
84
+ const form = new FormData();
85
+ form.set("data", JSON.stringify(serializedJSON));
86
+ form.set("maps", JSON.stringify(maps));
87
+ for (const i in blobs) {
88
+ form.set(i, blobs[i]);
89
+ }
90
+ return form;
91
+ }
92
+ deserialize(serialized) {
93
+ if (serialized instanceof Blob) {
94
+ return serialized;
95
+ }
96
+ if (!(serialized instanceof FormData)) {
97
+ return deserializeRPCJson(serialized);
98
+ }
99
+ const data = JSON.parse(serialized.get("data"));
100
+ const maps = JSON.parse(serialized.get("maps"));
101
+ for (const i in maps) {
102
+ data.json = set(data.json, maps[i], serialized.get(i));
103
+ }
104
+ return deserializeRPCJson(data);
105
+ }
106
+ };
107
+ function serializeRPCJson(value, segments = [], meta = []) {
108
+ if (typeof value === "bigint") {
109
+ meta.push(["bigint", segments]);
110
+ return { json: value.toString(), meta };
111
+ }
112
+ if (value instanceof Date) {
113
+ meta.push(["date", segments]);
114
+ const data = Number.isNaN(value.getTime()) ? "Invalid Date" : value.toISOString();
115
+ return { json: data, meta };
116
+ }
117
+ if (Number.isNaN(value)) {
118
+ meta.push(["nan", segments]);
119
+ return { json: "NaN", meta };
120
+ }
121
+ if (value instanceof RegExp) {
122
+ meta.push(["regexp", segments]);
123
+ return { json: value.toString(), meta };
124
+ }
125
+ if (value instanceof URL) {
126
+ meta.push(["url", segments]);
127
+ return { json: value.toString(), meta };
128
+ }
129
+ if (isPlainObject(value)) {
130
+ const json = {};
131
+ for (const k in value) {
132
+ json[k] = serializeRPCJson(value[k], [...segments, k], meta).json;
133
+ }
134
+ return { json, meta };
135
+ }
136
+ if (Array.isArray(value)) {
137
+ const json = value.map((v, i) => {
138
+ if (v === void 0) {
139
+ meta.push(["undefined", [...segments, i]]);
140
+ return null;
141
+ }
142
+ return serializeRPCJson(v, [...segments, i], meta).json;
143
+ });
144
+ return { json, meta };
145
+ }
146
+ if (value instanceof Set) {
147
+ const result = serializeRPCJson(Array.from(value), segments, meta);
148
+ meta.push(["set", segments]);
149
+ return result;
150
+ }
151
+ if (value instanceof Map) {
152
+ const result = serializeRPCJson(Array.from(value.entries()), segments, meta);
153
+ meta.push(["map", segments]);
154
+ return result;
155
+ }
156
+ return { json: value, meta };
157
+ }
158
+ function deserializeRPCJson({
159
+ json,
160
+ meta
161
+ }) {
162
+ if (meta.length === 0) {
163
+ return json;
164
+ }
165
+ const ref = { data: json };
166
+ for (const [type, segments] of meta) {
167
+ let currentRef = ref;
168
+ let preSegment = "data";
169
+ for (let i = 0; i < segments.length; i++) {
170
+ currentRef = currentRef[preSegment];
171
+ preSegment = segments[i];
172
+ }
173
+ switch (type) {
174
+ case "nan":
175
+ currentRef[preSegment] = Number.NaN;
176
+ break;
177
+ case "bigint":
178
+ currentRef[preSegment] = BigInt(currentRef[preSegment]);
179
+ break;
180
+ case "date":
181
+ currentRef[preSegment] = new Date(currentRef[preSegment]);
182
+ break;
183
+ case "regexp": {
184
+ const [, pattern, flags] = currentRef[preSegment].match(/^\/(.*)\/([a-z]*)$/);
185
+ currentRef[preSegment] = new RegExp(pattern, flags);
186
+ break;
187
+ }
188
+ case "url":
189
+ currentRef[preSegment] = new URL(currentRef[preSegment]);
190
+ break;
191
+ case "undefined":
192
+ currentRef[preSegment] = void 0;
193
+ break;
194
+ case "map":
195
+ currentRef[preSegment] = new Map(currentRef[preSegment]);
196
+ break;
197
+ case "set":
198
+ currentRef[preSegment] = new Set(currentRef[preSegment]);
199
+ break;
200
+ /* v8 ignore next 3 */
201
+ default: {
202
+ const _expected = type;
203
+ }
204
+ }
205
+ }
206
+ return ref.data;
207
+ }
208
+
209
+ // src/adapters/standard/rpc-codec.ts
210
+ var RPCCodec = class {
211
+ serializer;
212
+ constructor(options = {}) {
213
+ this.serializer = options.serializer ?? new RPCSerializer();
214
+ }
215
+ async decode(request, _params, _procedure) {
216
+ const serialized = request.method === "GET" ? JSON.parse(request.url.searchParams.getAll("data").at(-1)) : await request.body();
217
+ return this.serializer.deserialize(serialized);
218
+ }
219
+ encode(output, _procedure) {
220
+ return {
221
+ status: 200,
222
+ headers: {},
223
+ body: this.serializer.serialize(output)
224
+ };
225
+ }
226
+ encodeError(error) {
227
+ return {
228
+ status: error.status,
229
+ headers: {},
230
+ body: this.serializer.serialize(error.toJSON())
231
+ };
232
+ }
233
+ };
234
+
235
+ // src/adapters/standard/rpc-matcher.ts
236
+ var RPCMatcher = class {
237
+ tree = {};
238
+ pendingRouters = [];
239
+ init(router, path = []) {
240
+ const laziedOptions = eachContractProcedure({
241
+ router,
242
+ path
243
+ }, ({ path: path2, contract }) => {
244
+ const httpPath = convertPathToHttpPath(path2);
245
+ if (isProcedure(contract)) {
246
+ this.tree[httpPath] = {
247
+ path: path2,
248
+ contract,
249
+ procedure: contract,
250
+ // this mean dev not used contract-first so we can used contract as procedure directly
251
+ router
252
+ };
253
+ } else {
254
+ this.tree[httpPath] = {
255
+ path: path2,
256
+ contract,
257
+ procedure: void 0,
258
+ router
259
+ };
260
+ }
261
+ });
262
+ this.pendingRouters.push(...laziedOptions.map((option) => ({
263
+ ...option,
264
+ httpPathPrefix: convertPathToHttpPath(option.path)
265
+ })));
266
+ }
267
+ async match(_method, pathname) {
268
+ if (this.pendingRouters.length) {
269
+ const newPendingRouters = [];
270
+ for (const pendingRouter of this.pendingRouters) {
271
+ if (pathname.startsWith(pendingRouter.httpPathPrefix)) {
272
+ const { default: router } = await unlazy(pendingRouter.lazied);
273
+ this.init(router, pendingRouter.path);
274
+ } else {
275
+ newPendingRouters.push(pendingRouter);
276
+ }
277
+ }
278
+ this.pendingRouters = newPendingRouters;
279
+ }
280
+ const match = this.tree[pathname];
281
+ if (!match) {
282
+ return void 0;
283
+ }
284
+ if (!match.procedure) {
285
+ const { default: maybeProcedure } = await unlazy(getRouterChild(match.router, ...match.path));
286
+ if (!isProcedure(maybeProcedure)) {
287
+ throw new Error(`
288
+ [Contract-First] Missing or invalid implementation for procedure at path: ${convertPathToHttpPath(match.path)}.
289
+ Ensure that the procedure is correctly defined and matches the expected contract.
290
+ `);
291
+ }
292
+ match.procedure = createContractedProcedure(match.contract, maybeProcedure);
293
+ }
294
+ return {
295
+ path: match.path,
296
+ procedure: match.procedure
297
+ };
298
+ }
299
+ };
300
+
301
+ export {
302
+ StandardHandler,
303
+ RPCSerializer,
304
+ serializeRPCJson,
305
+ RPCCodec,
306
+ RPCMatcher
307
+ };
308
+ //# sourceMappingURL=chunk-PZ44ZPVC.js.map
package/dist/fetch.js CHANGED
@@ -1,106 +1,18 @@
1
1
  import {
2
- createProcedureCaller,
3
- isProcedure
4
- } from "./chunk-TDFYNRZV.js";
5
-
6
- // src/fetch/handle.ts
7
- import { ORPCError } from "@orpc/shared/error";
8
- async function handleFetchRequest(options) {
9
- for (const handler of options.handlers) {
10
- const response = await handler(options);
11
- if (response) {
12
- return response;
13
- }
14
- }
15
- const error = new ORPCError({ code: "NOT_FOUND", message: "Not found" });
16
- return new Response(JSON.stringify(error.toJSON()), {
17
- status: error.status,
18
- headers: {
19
- "Content-Type": "application/json"
20
- }
21
- });
22
- }
23
-
24
- // src/fetch/handler.ts
25
- import { ORPC_HEADER, ORPC_HEADER_VALUE } from "@orpc/contract";
26
- import { trim, value } from "@orpc/shared";
27
- import { ORPCError as ORPCError2 } from "@orpc/shared/error";
28
- import { ORPCDeserializer, ORPCSerializer } from "@orpc/transformer";
29
- var serializer = new ORPCSerializer();
30
- var deserializer = new ORPCDeserializer();
31
- function createORPCHandler() {
32
- return async (options) => {
33
- if (options.request.headers.get(ORPC_HEADER) !== ORPC_HEADER_VALUE) {
34
- return void 0;
35
- }
36
- const context = await value(options.context);
37
- const handler = async () => {
38
- const url = new URL(options.request.url);
39
- const pathname = `/${trim(url.pathname.replace(options.prefix ?? "", ""), "/")}`;
40
- const match = resolveORPCRouter(options.router, pathname);
41
- if (!match) {
42
- throw new ORPCError2({ code: "NOT_FOUND", message: "Not found" });
43
- }
44
- const input = await deserializeRequest(options.request);
45
- const caller = createProcedureCaller({
46
- context,
47
- procedure: match.procedure,
48
- path: match.path
49
- });
50
- const output = await caller(input);
51
- const { body, headers } = serializer.serialize(output);
52
- return new Response(body, {
53
- status: 200,
54
- headers
55
- });
56
- };
57
- try {
58
- return await options.hooks?.(
59
- context,
60
- { next: handler, response: (response) => response }
61
- ) ?? await handler();
62
- } catch (e) {
63
- const error = e instanceof ORPCError2 ? e : new ORPCError2({
64
- code: "INTERNAL_SERVER_ERROR",
65
- message: "Internal server error",
66
- cause: e
67
- });
68
- const { body, headers } = serializer.serialize(error.toJSON());
69
- return new Response(body, {
70
- status: error.status,
71
- headers
72
- });
73
- }
74
- };
75
- }
76
- function resolveORPCRouter(router, pathname) {
77
- const path = trim(pathname, "/").split("/").map(decodeURIComponent);
78
- let current = router;
79
- for (const segment of path) {
80
- if ((typeof current !== "object" || current === null) && typeof current !== "function") {
81
- current = void 0;
82
- break;
83
- }
84
- current = current[segment];
85
- }
86
- return isProcedure(current) ? {
87
- procedure: current,
88
- path
89
- } : void 0;
90
- }
91
- async function deserializeRequest(request) {
92
- try {
93
- return await deserializer.deserialize(request);
94
- } catch (e) {
95
- throw new ORPCError2({
96
- code: "BAD_REQUEST",
97
- message: "Cannot parse request. Please check the request body and Content-Type header.",
98
- cause: e
99
- });
100
- }
101
- }
2
+ RPCHandler,
3
+ fetchReToStandardBody,
4
+ fetchRequestToStandardRequest,
5
+ standardBodyToFetchBody,
6
+ standardResponseToFetchResponse
7
+ } from "./chunk-M65OCXNG.js";
8
+ import "./chunk-PZ44ZPVC.js";
9
+ import "./chunk-OEPDHGQ4.js";
10
+ import "./chunk-OWMUECGT.js";
102
11
  export {
103
- createORPCHandler,
104
- handleFetchRequest
12
+ RPCHandler,
13
+ fetchReToStandardBody,
14
+ fetchRequestToStandardRequest,
15
+ standardBodyToFetchBody,
16
+ standardResponseToFetchResponse
105
17
  };
106
18
  //# sourceMappingURL=fetch.js.map
package/dist/hono.js ADDED
@@ -0,0 +1,42 @@
1
+ import {
2
+ RPCHandler,
3
+ fetchReToStandardBody,
4
+ fetchRequestToStandardRequest,
5
+ standardBodyToFetchBody,
6
+ standardResponseToFetchResponse
7
+ } from "./chunk-M65OCXNG.js";
8
+ import "./chunk-PZ44ZPVC.js";
9
+ import "./chunk-OEPDHGQ4.js";
10
+ import "./chunk-OWMUECGT.js";
11
+
12
+ // src/adapters/hono/middleware.ts
13
+ import { value } from "@orpc/shared";
14
+ function createMiddleware(handler, ...[options]) {
15
+ return async (c, next) => {
16
+ const bodyProps = /* @__PURE__ */ new Set(["arrayBuffer", "blob", "formData", "json", "text"]);
17
+ const request = c.req.method === "GET" || c.req.method === "HEAD" ? c.req.raw : new Proxy(c.req.raw, {
18
+ // https://github.com/honojs/middleware/blob/main/packages/trpc-server/src/index.ts#L39
19
+ get(target, prop) {
20
+ if (bodyProps.has(prop)) {
21
+ return () => c.req[prop]();
22
+ }
23
+ return Reflect.get(target, prop, target);
24
+ }
25
+ });
26
+ const context = await value(options?.context ?? {}, c);
27
+ const { matched, response } = await handler.handle(request, { ...options, context });
28
+ if (matched) {
29
+ return c.body(response.body, response);
30
+ }
31
+ await next();
32
+ };
33
+ }
34
+ export {
35
+ RPCHandler,
36
+ createMiddleware,
37
+ fetchReToStandardBody,
38
+ fetchRequestToStandardRequest,
39
+ standardBodyToFetchBody,
40
+ standardResponseToFetchResponse
41
+ };
42
+ //# sourceMappingURL=hono.js.map