@orpc/server 0.0.0-next.aa57fb6 → 0.0.0-next.b6be6f0

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 (35) hide show
  1. package/dist/chunk-37HIYNDO.js +182 -0
  2. package/dist/fetch.js +282 -74
  3. package/dist/index.js +370 -320
  4. package/dist/src/builder.d.ts +26 -44
  5. package/dist/src/fetch/composite-handler.d.ts +8 -0
  6. package/dist/src/fetch/index.d.ts +4 -2
  7. package/dist/src/fetch/orpc-handler.d.ts +20 -0
  8. package/dist/src/fetch/orpc-payload-codec.d.ts +16 -0
  9. package/dist/src/fetch/orpc-procedure-matcher.d.ts +12 -0
  10. package/dist/src/fetch/super-json.d.ts +12 -0
  11. package/dist/src/fetch/types.d.ts +14 -33
  12. package/dist/src/hidden.d.ts +6 -0
  13. package/dist/src/implementer-chainable.d.ts +10 -0
  14. package/dist/src/index.d.ts +9 -3
  15. package/dist/src/lazy-decorated.d.ts +10 -0
  16. package/dist/src/lazy-utils.d.ts +4 -0
  17. package/dist/src/lazy.d.ts +6 -11
  18. package/dist/src/middleware-decorated.d.ts +8 -0
  19. package/dist/src/middleware.d.ts +3 -6
  20. package/dist/src/procedure-builder.d.ts +15 -24
  21. package/dist/src/procedure-client.d.ts +34 -0
  22. package/dist/src/procedure-decorated.d.ts +14 -0
  23. package/dist/src/procedure-implementer.d.ts +13 -17
  24. package/dist/src/procedure.d.ts +15 -24
  25. package/dist/src/router-builder.d.ts +23 -21
  26. package/dist/src/router-client.d.ts +25 -0
  27. package/dist/src/router-implementer.d.ts +18 -21
  28. package/dist/src/router.d.ts +11 -16
  29. package/dist/src/types.d.ts +8 -4
  30. package/package.json +4 -9
  31. package/dist/chunk-FL4ZAGNE.js +0 -267
  32. package/dist/src/fetch/handle.d.ts +0 -7
  33. package/dist/src/fetch/handler.d.ts +0 -3
  34. package/dist/src/procedure-caller.d.ts +0 -20
  35. package/dist/src/router-caller.d.ts +0 -22
@@ -0,0 +1,182 @@
1
+ // src/utils.ts
2
+ function mergeContext(a, b) {
3
+ if (!a)
4
+ return b;
5
+ if (!b)
6
+ return a;
7
+ return {
8
+ ...a,
9
+ ...b
10
+ };
11
+ }
12
+
13
+ // src/procedure.ts
14
+ import { isContractProcedure } from "@orpc/contract";
15
+ var Procedure = class {
16
+ "~type" = "Procedure";
17
+ "~orpc";
18
+ constructor(def) {
19
+ this["~orpc"] = def;
20
+ }
21
+ };
22
+ function isProcedure(item) {
23
+ if (item instanceof Procedure) {
24
+ return true;
25
+ }
26
+ return (typeof item === "object" || typeof item === "function") && item !== null && "~type" in item && item["~type"] === "Procedure" && "~orpc" in item && typeof item["~orpc"] === "object" && item["~orpc"] !== null && "contract" in item["~orpc"] && isContractProcedure(item["~orpc"].contract) && "handler" in item["~orpc"] && typeof item["~orpc"].handler === "function";
27
+ }
28
+
29
+ // src/lazy.ts
30
+ var LAZY_LOADER_SYMBOL = Symbol("ORPC_LAZY_LOADER");
31
+ function lazy(loader) {
32
+ return {
33
+ [LAZY_LOADER_SYMBOL]: loader
34
+ };
35
+ }
36
+ function isLazy(item) {
37
+ return (typeof item === "object" || typeof item === "function") && item !== null && LAZY_LOADER_SYMBOL in item && typeof item[LAZY_LOADER_SYMBOL] === "function";
38
+ }
39
+ function unlazy(lazied) {
40
+ return isLazy(lazied) ? lazied[LAZY_LOADER_SYMBOL]() : Promise.resolve({ default: lazied });
41
+ }
42
+ function flatLazy(lazied) {
43
+ const flattenLoader = async () => {
44
+ let current = await unlazy(lazied);
45
+ while (true) {
46
+ if (!isLazy(current.default)) {
47
+ break;
48
+ }
49
+ current = await unlazy(current.default);
50
+ }
51
+ return current;
52
+ };
53
+ return lazy(flattenLoader);
54
+ }
55
+
56
+ // src/procedure-client.ts
57
+ import { executeWithHooks, value } from "@orpc/shared";
58
+ import { ORPCError } from "@orpc/shared/error";
59
+ function createProcedureClient(options) {
60
+ return async (...[input, callerOptions]) => {
61
+ const path = options.path ?? [];
62
+ const { default: procedure } = await unlazy(options.procedure);
63
+ const context = await value(options.context);
64
+ const meta = {
65
+ path,
66
+ procedure,
67
+ signal: callerOptions?.signal
68
+ };
69
+ const executeWithValidation = async () => {
70
+ const validInput = await validateInput(procedure, input);
71
+ const output = await executeMiddlewareChain(
72
+ procedure,
73
+ validInput,
74
+ context,
75
+ meta
76
+ );
77
+ return validateOutput(procedure, output);
78
+ };
79
+ return executeWithHooks({
80
+ hooks: options,
81
+ input,
82
+ context,
83
+ meta,
84
+ execute: executeWithValidation
85
+ });
86
+ };
87
+ }
88
+ async function validateInput(procedure, input) {
89
+ const schema = procedure["~orpc"].contract["~orpc"].InputSchema;
90
+ if (!schema)
91
+ return input;
92
+ const result = await schema["~standard"].validate(input);
93
+ if (result.issues) {
94
+ throw new ORPCError({
95
+ message: "Input validation failed",
96
+ code: "BAD_REQUEST",
97
+ issues: result.issues
98
+ });
99
+ }
100
+ return result.value;
101
+ }
102
+ async function validateOutput(procedure, output) {
103
+ const schema = procedure["~orpc"].contract["~orpc"].OutputSchema;
104
+ if (!schema)
105
+ return output;
106
+ const result = await schema["~standard"].validate(output);
107
+ if (result.issues) {
108
+ throw new ORPCError({
109
+ message: "Output validation failed",
110
+ code: "INTERNAL_SERVER_ERROR",
111
+ issues: result.issues
112
+ });
113
+ }
114
+ return result.value;
115
+ }
116
+ async function executeMiddlewareChain(procedure, input, context, meta) {
117
+ const middlewares = procedure["~orpc"].middlewares ?? [];
118
+ let currentMidIndex = 0;
119
+ let currentContext = context;
120
+ const next = async (nextOptions) => {
121
+ const mid = middlewares[currentMidIndex];
122
+ currentMidIndex += 1;
123
+ currentContext = mergeContext(currentContext, nextOptions.context);
124
+ if (mid) {
125
+ return await mid(input, currentContext, {
126
+ ...meta,
127
+ next,
128
+ output: (output) => ({ output, context: void 0 })
129
+ });
130
+ }
131
+ const result = {
132
+ output: await procedure["~orpc"].handler(input, currentContext, meta),
133
+ context: currentContext
134
+ };
135
+ return result;
136
+ };
137
+ return (await next({})).output;
138
+ }
139
+
140
+ // src/router.ts
141
+ function getRouterChild(router, ...path) {
142
+ let current = router;
143
+ for (let i = 0; i < path.length; i++) {
144
+ const segment = path[i];
145
+ if (!current) {
146
+ return void 0;
147
+ }
148
+ if (isProcedure(current)) {
149
+ return void 0;
150
+ }
151
+ if (!isLazy(current)) {
152
+ current = current[segment];
153
+ continue;
154
+ }
155
+ const lazied = current;
156
+ const rest = path.slice(i);
157
+ const newLazy = lazy(async () => {
158
+ const unwrapped = await unlazy(lazied);
159
+ if (!unwrapped.default) {
160
+ return unwrapped;
161
+ }
162
+ const next = getRouterChild(unwrapped.default, ...rest);
163
+ return { default: next };
164
+ });
165
+ return flatLazy(newLazy);
166
+ }
167
+ return current;
168
+ }
169
+
170
+ export {
171
+ mergeContext,
172
+ Procedure,
173
+ isProcedure,
174
+ LAZY_LOADER_SYMBOL,
175
+ lazy,
176
+ isLazy,
177
+ unlazy,
178
+ flatLazy,
179
+ createProcedureClient,
180
+ getRouterChild
181
+ };
182
+ //# sourceMappingURL=chunk-37HIYNDO.js.map
package/dist/fetch.js CHANGED
@@ -1,107 +1,315 @@
1
1
  import {
2
- createProcedureCaller,
3
- isLazy,
4
- isProcedure
5
- } from "./chunk-FL4ZAGNE.js";
2
+ createProcedureClient,
3
+ getRouterChild,
4
+ isProcedure,
5
+ unlazy
6
+ } from "./chunk-37HIYNDO.js";
6
7
 
7
- // src/fetch/handle.ts
8
+ // src/fetch/composite-handler.ts
9
+ var CompositeHandler = class {
10
+ constructor(handlers) {
11
+ this.handlers = handlers;
12
+ }
13
+ async fetch(request, ...opt) {
14
+ for (const handler of this.handlers) {
15
+ if (handler.condition(request)) {
16
+ return handler.fetch(request, ...opt);
17
+ }
18
+ }
19
+ return new Response("None of the handlers can handle the request.", {
20
+ status: 404
21
+ });
22
+ }
23
+ };
24
+
25
+ // src/fetch/orpc-handler.ts
26
+ import { executeWithHooks, ORPC_HANDLER_HEADER, ORPC_HANDLER_VALUE, trim as trim2 } from "@orpc/shared";
27
+ import { ORPCError as ORPCError2 } from "@orpc/shared/error";
28
+
29
+ // src/fetch/orpc-payload-codec.ts
30
+ import { findDeepMatches, set } from "@orpc/shared";
8
31
  import { ORPCError } from "@orpc/shared/error";
9
- async function handleFetchRequest(options) {
10
- for (const handler of options.handlers) {
11
- const response = await handler(options);
12
- if (response) {
13
- return response;
32
+
33
+ // ../../node_modules/.pnpm/is-what@5.0.2/node_modules/is-what/dist/getType.js
34
+ function getType(payload) {
35
+ return Object.prototype.toString.call(payload).slice(8, -1);
36
+ }
37
+
38
+ // ../../node_modules/.pnpm/is-what@5.0.2/node_modules/is-what/dist/isPlainObject.js
39
+ function isPlainObject(payload) {
40
+ if (getType(payload) !== "Object")
41
+ return false;
42
+ const prototype = Object.getPrototypeOf(payload);
43
+ return !!prototype && prototype.constructor === Object && prototype === Object.prototype;
44
+ }
45
+
46
+ // src/fetch/super-json.ts
47
+ function serialize(value, segments = [], meta = []) {
48
+ if (typeof value === "bigint") {
49
+ meta.push(["bigint", segments]);
50
+ return { data: value.toString(), meta };
51
+ }
52
+ if (value instanceof Date) {
53
+ meta.push(["date", segments]);
54
+ const data = Number.isNaN(value.getTime()) ? "Invalid Date" : value.toISOString();
55
+ return { data, meta };
56
+ }
57
+ if (Number.isNaN(value)) {
58
+ meta.push(["nan", segments]);
59
+ return { data: "NaN", meta };
60
+ }
61
+ if (value instanceof RegExp) {
62
+ meta.push(["regexp", segments]);
63
+ return { data: value.toString(), meta };
64
+ }
65
+ if (value instanceof URL) {
66
+ meta.push(["url", segments]);
67
+ return { data: value.toString(), meta };
68
+ }
69
+ if (isPlainObject(value)) {
70
+ const data = {};
71
+ for (const k in value) {
72
+ data[k] = serialize(value[k], [...segments, k], meta).data;
14
73
  }
74
+ return { data, meta };
75
+ }
76
+ if (Array.isArray(value)) {
77
+ const data = value.map((v, i) => {
78
+ if (v === void 0) {
79
+ meta.push(["undefined", [...segments, i]]);
80
+ return null;
81
+ }
82
+ return serialize(v, [...segments, i], meta).data;
83
+ });
84
+ return { data, meta };
85
+ }
86
+ if (value instanceof Set) {
87
+ const result = serialize(Array.from(value), segments, meta);
88
+ meta.push(["set", segments]);
89
+ return result;
90
+ }
91
+ if (value instanceof Map) {
92
+ const result = serialize(Array.from(value.entries()), segments, meta);
93
+ meta.push(["map", segments]);
94
+ return result;
95
+ }
96
+ return { data: value, meta };
97
+ }
98
+ function deserialize({
99
+ data,
100
+ meta
101
+ }) {
102
+ if (meta.length === 0) {
103
+ return data;
15
104
  }
16
- const error = new ORPCError({ code: "NOT_FOUND", message: "Not found" });
17
- return new Response(JSON.stringify(error.toJSON()), {
18
- status: error.status,
19
- headers: {
20
- "Content-Type": "application/json"
105
+ const ref = { data };
106
+ for (const [type, segments] of meta) {
107
+ let currentRef = ref;
108
+ let preSegment = "data";
109
+ for (let i = 0; i < segments.length; i++) {
110
+ currentRef = currentRef[preSegment];
111
+ preSegment = segments[i];
112
+ }
113
+ switch (type) {
114
+ case "nan":
115
+ currentRef[preSegment] = Number.NaN;
116
+ break;
117
+ case "bigint":
118
+ currentRef[preSegment] = BigInt(currentRef[preSegment]);
119
+ break;
120
+ case "date":
121
+ currentRef[preSegment] = new Date(currentRef[preSegment]);
122
+ break;
123
+ case "regexp": {
124
+ const [, pattern, flags] = currentRef[preSegment].match(/^\/(.*)\/([a-z]*)$/);
125
+ currentRef[preSegment] = new RegExp(pattern, flags);
126
+ break;
127
+ }
128
+ case "url":
129
+ currentRef[preSegment] = new URL(currentRef[preSegment]);
130
+ break;
131
+ case "undefined":
132
+ currentRef[preSegment] = void 0;
133
+ break;
134
+ case "map":
135
+ currentRef[preSegment] = new Map(currentRef[preSegment]);
136
+ break;
137
+ case "set":
138
+ currentRef[preSegment] = new Set(currentRef[preSegment]);
139
+ break;
140
+ /* v8 ignore next 3 */
141
+ default: {
142
+ const _expected = type;
143
+ }
21
144
  }
22
- });
145
+ }
146
+ return ref.data;
23
147
  }
24
148
 
25
- // src/fetch/handler.ts
26
- import { ORPC_HEADER, ORPC_HEADER_VALUE } from "@orpc/contract";
27
- import { trim, value } from "@orpc/shared";
28
- import { ORPCError as ORPCError2 } from "@orpc/shared/error";
29
- import { ORPCDeserializer, ORPCSerializer } from "@orpc/transformer";
30
- var serializer = new ORPCSerializer();
31
- var deserializer = new ORPCDeserializer();
32
- function createORPCHandler() {
33
- return async (options) => {
34
- if (options.request.headers.get(ORPC_HEADER) !== ORPC_HEADER_VALUE) {
149
+ // src/fetch/orpc-payload-codec.ts
150
+ var ORPCPayloadCodec = class {
151
+ /**
152
+ * If method is GET, the payload will be encoded as query string.
153
+ * If method is GET and payload contain file, the method will be fallback to fallbackMethod. (fallbackMethod = GET will force to use GET method)
154
+ */
155
+ encode(payload, method = "POST", fallbackMethod = "POST") {
156
+ const { data, meta } = serialize(payload);
157
+ const { maps, values } = findDeepMatches((v) => v instanceof Blob, data);
158
+ if (method === "GET" && (values.length === 0 || fallbackMethod === "GET")) {
159
+ const query = new URLSearchParams({
160
+ data: JSON.stringify(data),
161
+ meta: JSON.stringify(meta)
162
+ });
163
+ return {
164
+ query,
165
+ method: "GET"
166
+ };
167
+ }
168
+ const nonGETMethod = method === "GET" ? fallbackMethod : method;
169
+ if (values.length > 0) {
170
+ const form = new FormData();
171
+ if (data !== void 0) {
172
+ form.append("data", JSON.stringify(data));
173
+ }
174
+ form.append("meta", JSON.stringify(meta));
175
+ form.append("maps", JSON.stringify(maps));
176
+ for (const i in values) {
177
+ const value = values[i];
178
+ form.append(i, value);
179
+ }
180
+ return {
181
+ body: form,
182
+ method: nonGETMethod
183
+ };
184
+ }
185
+ return {
186
+ body: JSON.stringify({ data, meta }),
187
+ headers: new Headers({
188
+ "content-type": "application/json"
189
+ }),
190
+ method: nonGETMethod
191
+ };
192
+ }
193
+ async decode(re) {
194
+ try {
195
+ if ("method" in re && re.method === "GET") {
196
+ const url = new URL(re.url);
197
+ const query = url.searchParams;
198
+ const data = JSON.parse(query.getAll("data").at(-1));
199
+ const meta = JSON.parse(query.getAll("meta").at(-1));
200
+ return deserialize({
201
+ data,
202
+ meta
203
+ });
204
+ }
205
+ if (re.headers.get("content-type")?.startsWith("multipart/form-data")) {
206
+ const form = await re.formData();
207
+ const rawData = form.get("data");
208
+ const rawMeta = form.get("meta");
209
+ const rawMaps = form.get("maps");
210
+ let data = JSON.parse(rawData);
211
+ const meta = JSON.parse(rawMeta);
212
+ const maps = JSON.parse(rawMaps);
213
+ for (const i in maps) {
214
+ data = set(data, maps[i], form.get(i));
215
+ }
216
+ return deserialize({
217
+ data,
218
+ meta
219
+ });
220
+ }
221
+ const json = await re.json();
222
+ return deserialize(json);
223
+ } catch (e) {
224
+ throw new ORPCError({
225
+ code: "BAD_REQUEST",
226
+ message: "Cannot parse request/response. Please check the request/response body and Content-Type header.",
227
+ cause: e
228
+ });
229
+ }
230
+ }
231
+ };
232
+
233
+ // src/fetch/orpc-procedure-matcher.ts
234
+ import { trim } from "@orpc/shared";
235
+ var ORPCProcedureMatcher = class {
236
+ constructor(router) {
237
+ this.router = router;
238
+ }
239
+ async match(pathname) {
240
+ const path = trim(pathname, "/").split("/").map(decodeURIComponent);
241
+ const match = getRouterChild(this.router, ...path);
242
+ const { default: maybeProcedure } = await unlazy(match);
243
+ if (!isProcedure(maybeProcedure)) {
35
244
  return void 0;
36
245
  }
37
- const context = await value(options.context);
38
- const handler = async () => {
39
- const url = new URL(options.request.url);
40
- const pathname = `/${trim(url.pathname.replace(options.prefix ?? "", ""), "/")}`;
41
- const match = resolveORPCRouter(options.router, pathname);
246
+ return {
247
+ procedure: maybeProcedure,
248
+ path
249
+ };
250
+ }
251
+ };
252
+
253
+ // src/fetch/orpc-handler.ts
254
+ var ORPCHandler = class {
255
+ constructor(router, options) {
256
+ this.router = router;
257
+ this.options = options;
258
+ this.procedureMatcher = options?.procedureMatcher ?? new ORPCProcedureMatcher(router);
259
+ this.payloadCodec = options?.payloadCodec ?? new ORPCPayloadCodec();
260
+ }
261
+ procedureMatcher;
262
+ payloadCodec;
263
+ condition(request) {
264
+ return Boolean(request.headers.get(ORPC_HANDLER_HEADER)?.includes(ORPC_HANDLER_VALUE));
265
+ }
266
+ async fetch(request, ...[options]) {
267
+ const context = options?.context;
268
+ const execute = async () => {
269
+ const url = new URL(request.url);
270
+ const pathname = `/${trim2(url.pathname.replace(options?.prefix ?? "", ""), "/")}`;
271
+ const match = await this.procedureMatcher.match(pathname);
42
272
  if (!match) {
43
273
  throw new ORPCError2({ code: "NOT_FOUND", message: "Not found" });
44
274
  }
45
- const input = await deserializeRequest(options.request);
46
- const caller = createProcedureCaller({
275
+ const input = await this.payloadCodec.decode(request);
276
+ const client = createProcedureClient({
47
277
  context,
48
278
  procedure: match.procedure,
49
279
  path: match.path
50
280
  });
51
- const output = await caller(input);
52
- const { body, headers } = serializer.serialize(output);
53
- return new Response(body, {
54
- status: 200,
55
- headers
56
- });
281
+ const output = await client(input, { signal: options?.signal });
282
+ const { body, headers } = this.payloadCodec.encode(output);
283
+ return new Response(body, { headers });
57
284
  };
58
285
  try {
59
- return await options.hooks?.(
286
+ return await executeWithHooks({
60
287
  context,
61
- { next: handler, response: (response) => response }
62
- ) ?? await handler();
288
+ execute,
289
+ input: request,
290
+ hooks: this.options,
291
+ meta: {
292
+ signal: options?.signal
293
+ }
294
+ });
63
295
  } catch (e) {
64
296
  const error = e instanceof ORPCError2 ? e : new ORPCError2({
65
297
  code: "INTERNAL_SERVER_ERROR",
66
298
  message: "Internal server error",
67
299
  cause: e
68
300
  });
69
- const { body, headers } = serializer.serialize(error.toJSON());
301
+ const { body, headers } = this.payloadCodec.encode(error.toJSON());
70
302
  return new Response(body, {
71
- status: error.status,
72
- headers
303
+ headers,
304
+ status: error.status
73
305
  });
74
306
  }
75
- };
76
- }
77
- function resolveORPCRouter(router, pathname) {
78
- const path = trim(pathname, "/").split("/").map(decodeURIComponent);
79
- let current = router;
80
- for (const segment of path) {
81
- if (typeof current !== "object" && typeof current !== "function" || !current) {
82
- current = void 0;
83
- break;
84
- }
85
- current = current[segment];
86
- }
87
- return isProcedure(current) || isLazy(current) ? {
88
- procedure: current,
89
- path
90
- } : void 0;
91
- }
92
- async function deserializeRequest(request) {
93
- try {
94
- return await deserializer.deserialize(request);
95
- } catch (e) {
96
- throw new ORPCError2({
97
- code: "BAD_REQUEST",
98
- message: "Cannot parse request. Please check the request body and Content-Type header.",
99
- cause: e
100
- });
101
307
  }
102
- }
308
+ };
103
309
  export {
104
- createORPCHandler,
105
- handleFetchRequest
310
+ CompositeHandler,
311
+ ORPCHandler,
312
+ ORPCPayloadCodec,
313
+ ORPCProcedureMatcher
106
314
  };
107
315
  //# sourceMappingURL=fetch.js.map