@orpc/server 0.0.0-next.db1f26d → 0.0.0-next.e361acd
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/chunk-FN62GL22.js +182 -0
- package/dist/fetch.js +47 -42
- package/dist/index.js +368 -318
- package/dist/src/builder.d.ts +25 -43
- package/dist/src/fetch/handle-request.d.ts +7 -0
- package/dist/src/fetch/index.d.ts +2 -2
- package/dist/src/fetch/{handler.d.ts → orpc-handler.d.ts} +1 -1
- package/dist/src/fetch/types.d.ts +12 -19
- package/dist/src/hidden.d.ts +6 -0
- package/dist/src/implementer-chainable.d.ts +10 -0
- package/dist/src/index.d.ts +9 -3
- package/dist/src/lazy-decorated.d.ts +10 -0
- package/dist/src/lazy-utils.d.ts +4 -0
- package/dist/src/lazy.d.ts +6 -11
- package/dist/src/middleware-decorated.d.ts +8 -0
- package/dist/src/middleware.d.ts +3 -6
- package/dist/src/procedure-builder.d.ts +15 -24
- package/dist/src/procedure-client.d.ts +29 -0
- package/dist/src/procedure-decorated.d.ts +14 -0
- package/dist/src/procedure-implementer.d.ts +13 -17
- package/dist/src/procedure.d.ts +15 -24
- package/dist/src/router-builder.d.ts +23 -21
- package/dist/src/router-client.d.ts +25 -0
- package/dist/src/router-implementer.d.ts +18 -21
- package/dist/src/router.d.ts +11 -16
- package/dist/src/types.d.ts +8 -4
- package/package.json +6 -7
- package/dist/chunk-FL4ZAGNE.js +0 -267
- package/dist/src/fetch/handle.d.ts +0 -7
- package/dist/src/procedure-caller.d.ts +0 -20
- 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) && "func" in item["~orpc"] && typeof item["~orpc"].func === "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"].func(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-FN62GL22.js.map
|
package/dist/fetch.js
CHANGED
@@ -1,10 +1,11 @@
|
|
1
1
|
import {
|
2
|
-
|
3
|
-
|
4
|
-
isProcedure
|
5
|
-
|
2
|
+
createProcedureClient,
|
3
|
+
getRouterChild,
|
4
|
+
isProcedure,
|
5
|
+
unlazy
|
6
|
+
} from "./chunk-FN62GL22.js";
|
6
7
|
|
7
|
-
// src/fetch/handle.ts
|
8
|
+
// src/fetch/handle-request.ts
|
8
9
|
import { ORPCError } from "@orpc/shared/error";
|
9
10
|
async function handleFetchRequest(options) {
|
10
11
|
for (const handler of options.handlers) {
|
@@ -22,33 +23,32 @@ async function handleFetchRequest(options) {
|
|
22
23
|
});
|
23
24
|
}
|
24
25
|
|
25
|
-
// src/fetch/handler.ts
|
26
|
-
import {
|
27
|
-
import { trim, value } from "@orpc/shared";
|
26
|
+
// src/fetch/orpc-handler.ts
|
27
|
+
import { executeWithHooks, ORPC_PROTOCOL_HEADER, ORPC_PROTOCOL_VALUE, trim, value } from "@orpc/shared";
|
28
28
|
import { ORPCError as ORPCError2 } from "@orpc/shared/error";
|
29
29
|
import { ORPCDeserializer, ORPCSerializer } from "@orpc/transformer";
|
30
30
|
var serializer = new ORPCSerializer();
|
31
31
|
var deserializer = new ORPCDeserializer();
|
32
32
|
function createORPCHandler() {
|
33
33
|
return async (options) => {
|
34
|
-
if (options.request.headers.get(
|
34
|
+
if (!options.request.headers.get(ORPC_PROTOCOL_HEADER)?.includes(ORPC_PROTOCOL_VALUE)) {
|
35
35
|
return void 0;
|
36
36
|
}
|
37
37
|
const context = await value(options.context);
|
38
38
|
const handler = async () => {
|
39
39
|
const url = new URL(options.request.url);
|
40
40
|
const pathname = `/${trim(url.pathname.replace(options.prefix ?? "", ""), "/")}`;
|
41
|
-
const match =
|
41
|
+
const match = await resolveRouterMatch(options.router, pathname);
|
42
42
|
if (!match) {
|
43
43
|
throw new ORPCError2({ code: "NOT_FOUND", message: "Not found" });
|
44
44
|
}
|
45
|
-
const input = await
|
46
|
-
const caller =
|
45
|
+
const input = await parseRequestInput(options.request);
|
46
|
+
const caller = createProcedureClient({
|
47
47
|
context,
|
48
48
|
procedure: match.procedure,
|
49
49
|
path: match.path
|
50
50
|
});
|
51
|
-
const output = await caller(input);
|
51
|
+
const output = await caller(input, { signal: options.signal });
|
52
52
|
const { body, headers } = serializer.serialize(output);
|
53
53
|
return new Response(body, {
|
54
54
|
status: 200,
|
@@ -56,50 +56,55 @@ function createORPCHandler() {
|
|
56
56
|
});
|
57
57
|
};
|
58
58
|
try {
|
59
|
-
return await
|
59
|
+
return await executeWithHooks({
|
60
|
+
hooks: options,
|
60
61
|
context,
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
message: "Internal server error",
|
67
|
-
cause: e
|
68
|
-
});
|
69
|
-
const { body, headers } = serializer.serialize(error.toJSON());
|
70
|
-
return new Response(body, {
|
71
|
-
status: error.status,
|
72
|
-
headers
|
62
|
+
execute: handler,
|
63
|
+
input: options.request,
|
64
|
+
meta: {
|
65
|
+
signal: options.signal
|
66
|
+
}
|
73
67
|
});
|
68
|
+
} catch (error) {
|
69
|
+
return handleErrorResponse(error);
|
74
70
|
}
|
75
71
|
};
|
76
72
|
}
|
77
|
-
function
|
78
|
-
const
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
break;
|
84
|
-
}
|
85
|
-
current = current[segment];
|
73
|
+
async function resolveRouterMatch(router, pathname) {
|
74
|
+
const pathSegments = trim(pathname, "/").split("/").map(decodeURIComponent);
|
75
|
+
const match = getRouterChild(router, ...pathSegments);
|
76
|
+
const { default: maybeProcedure } = await unlazy(match);
|
77
|
+
if (!isProcedure(maybeProcedure)) {
|
78
|
+
return void 0;
|
86
79
|
}
|
87
|
-
return
|
88
|
-
procedure:
|
89
|
-
path
|
90
|
-
}
|
80
|
+
return {
|
81
|
+
procedure: maybeProcedure,
|
82
|
+
path: pathSegments
|
83
|
+
};
|
91
84
|
}
|
92
|
-
async function
|
85
|
+
async function parseRequestInput(request) {
|
93
86
|
try {
|
94
87
|
return await deserializer.deserialize(request);
|
95
|
-
} catch (
|
88
|
+
} catch (error) {
|
96
89
|
throw new ORPCError2({
|
97
90
|
code: "BAD_REQUEST",
|
98
91
|
message: "Cannot parse request. Please check the request body and Content-Type header.",
|
99
|
-
cause:
|
92
|
+
cause: error
|
100
93
|
});
|
101
94
|
}
|
102
95
|
}
|
96
|
+
function handleErrorResponse(error) {
|
97
|
+
const orpcError = error instanceof ORPCError2 ? error : new ORPCError2({
|
98
|
+
code: "INTERNAL_SERVER_ERROR",
|
99
|
+
message: "Internal server error",
|
100
|
+
cause: error
|
101
|
+
});
|
102
|
+
const { body, headers } = serializer.serialize(orpcError.toJSON());
|
103
|
+
return new Response(body, {
|
104
|
+
status: orpcError.status,
|
105
|
+
headers
|
106
|
+
});
|
107
|
+
}
|
103
108
|
export {
|
104
109
|
createORPCHandler,
|
105
110
|
handleFetchRequest
|