@orpc/server 0.0.0-next.d137cdf → 0.0.0-next.d16a1b6
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 +28 -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.mjs +333 -0
- 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.B-ewprcf.d.ts +77 -0
- package/dist/shared/server.BBGuTxHE.mjs +163 -0
- package/dist/shared/server.CA-o8cUY.d.mts +9 -0
- package/dist/shared/server.Cn9ybJtE.d.mts +152 -0
- package/dist/shared/server.Cn9ybJtE.d.ts +152 -0
- package/dist/shared/server.DJrh0Ceu.d.mts +77 -0
- package/dist/shared/server.DPQt9YYq.d.ts +9 -0
- package/dist/shared/server.Q6ZmnTgO.mjs +12 -0
- package/dist/shared/server.V6zT5iYQ.mjs +379 -0
- package/dist/shared/server.pW-fRQNZ.mjs +24 -0
- package/package.json +41 -18
- package/dist/chunk-MZXEMHFS.js +0 -272
- package/dist/fetch.js +0 -110
- package/dist/index.js +0 -467
- package/dist/src/builder.d.ts +0 -53
- package/dist/src/fetch/handle.d.ts +0 -7
- package/dist/src/fetch/handler.d.ts +0 -3
- package/dist/src/fetch/index.d.ts +0 -4
- package/dist/src/fetch/types.d.ts +0 -27
- package/dist/src/index.d.ts +0 -17
- package/dist/src/lazy.d.ts +0 -23
- package/dist/src/middleware.d.ts +0 -26
- package/dist/src/procedure-builder.d.ts +0 -31
- package/dist/src/procedure-caller.d.ts +0 -25
- package/dist/src/procedure-implementer.d.ts +0 -22
- package/dist/src/procedure.d.ts +0 -32
- package/dist/src/router-builder.d.ts +0 -27
- package/dist/src/router-caller.d.ts +0 -25
- package/dist/src/router-implementer.d.ts +0 -24
- package/dist/src/router.d.ts +0 -21
- package/dist/src/types.d.ts +0 -8
- package/dist/src/utils.d.ts +0 -3
package/dist/chunk-MZXEMHFS.js
DELETED
@@ -1,272 +0,0 @@
|
|
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/middleware.ts
|
14
|
-
var decoratedMiddlewareSymbol = Symbol("\u{1F512}decoratedMiddleware");
|
15
|
-
function decorateMiddleware(middleware) {
|
16
|
-
if (Reflect.get(middleware, decoratedMiddlewareSymbol)) {
|
17
|
-
return middleware;
|
18
|
-
}
|
19
|
-
const concat = (concatMiddleware, mapInput2) => {
|
20
|
-
const concatMiddleware_ = mapInput2 ? decorateMiddleware(concatMiddleware).mapInput(mapInput2) : concatMiddleware;
|
21
|
-
return decorateMiddleware(async (input, context, meta, ...rest) => {
|
22
|
-
const input_ = input;
|
23
|
-
const context_ = context;
|
24
|
-
const meta_ = meta;
|
25
|
-
const next = async (options) => {
|
26
|
-
return concatMiddleware_(input_, mergeContext(context_, options.context), meta_, ...rest);
|
27
|
-
};
|
28
|
-
const m1 = await middleware(input_, context_, {
|
29
|
-
...meta_,
|
30
|
-
next
|
31
|
-
}, ...rest);
|
32
|
-
return m1;
|
33
|
-
});
|
34
|
-
};
|
35
|
-
const mapInput = (map) => {
|
36
|
-
return decorateMiddleware(
|
37
|
-
(input, ...rest) => middleware(map(input), ...rest)
|
38
|
-
);
|
39
|
-
};
|
40
|
-
return Object.assign(middleware, {
|
41
|
-
[decoratedMiddlewareSymbol]: true,
|
42
|
-
concat,
|
43
|
-
mapInput
|
44
|
-
});
|
45
|
-
}
|
46
|
-
|
47
|
-
// src/procedure-caller.ts
|
48
|
-
import { executeWithHooks, trim, value } from "@orpc/shared";
|
49
|
-
import { ORPCError } from "@orpc/shared/error";
|
50
|
-
|
51
|
-
// src/procedure.ts
|
52
|
-
import {
|
53
|
-
DecoratedContractProcedure,
|
54
|
-
isContractProcedure
|
55
|
-
} from "@orpc/contract";
|
56
|
-
var Procedure = class {
|
57
|
-
constructor(zz$p) {
|
58
|
-
this.zz$p = zz$p;
|
59
|
-
}
|
60
|
-
};
|
61
|
-
var DECORATED_PROCEDURE_SYMBOL = Symbol("DECORATED_PROCEDURE");
|
62
|
-
function decorateProcedure(procedure) {
|
63
|
-
if (DECORATED_PROCEDURE_SYMBOL in procedure) {
|
64
|
-
return procedure;
|
65
|
-
}
|
66
|
-
return Object.assign(createProcedureCaller({
|
67
|
-
procedure,
|
68
|
-
context: void 0
|
69
|
-
}), {
|
70
|
-
[DECORATED_PROCEDURE_SYMBOL]: true,
|
71
|
-
zz$p: procedure.zz$p,
|
72
|
-
prefix(prefix) {
|
73
|
-
return decorateProcedure({
|
74
|
-
zz$p: {
|
75
|
-
...procedure.zz$p,
|
76
|
-
contract: DecoratedContractProcedure.decorate(
|
77
|
-
procedure.zz$p.contract
|
78
|
-
).prefix(prefix)
|
79
|
-
}
|
80
|
-
});
|
81
|
-
},
|
82
|
-
route(opts) {
|
83
|
-
return decorateProcedure({
|
84
|
-
zz$p: {
|
85
|
-
...procedure.zz$p,
|
86
|
-
contract: DecoratedContractProcedure.decorate(
|
87
|
-
procedure.zz$p.contract
|
88
|
-
).route(opts)
|
89
|
-
}
|
90
|
-
});
|
91
|
-
},
|
92
|
-
use(middleware, mapInput) {
|
93
|
-
const middleware_ = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
|
94
|
-
return decorateProcedure({
|
95
|
-
zz$p: {
|
96
|
-
...procedure.zz$p,
|
97
|
-
middlewares: [middleware_, ...procedure.zz$p.middlewares ?? []]
|
98
|
-
}
|
99
|
-
});
|
100
|
-
}
|
101
|
-
});
|
102
|
-
}
|
103
|
-
function isProcedure(item) {
|
104
|
-
if (item instanceof Procedure)
|
105
|
-
return true;
|
106
|
-
return (typeof item === "object" || typeof item === "function") && item !== null && "zz$p" in item && typeof item.zz$p === "object" && item.zz$p !== null && "contract" in item.zz$p && isContractProcedure(item.zz$p.contract) && "func" in item.zz$p && typeof item.zz$p.func === "function";
|
107
|
-
}
|
108
|
-
|
109
|
-
// src/procedure-caller.ts
|
110
|
-
function createProcedureCaller(options) {
|
111
|
-
const caller = async (input) => {
|
112
|
-
const path = options.path ?? [];
|
113
|
-
const procedure = await loadProcedure(options.procedure);
|
114
|
-
const context = await value(options.context);
|
115
|
-
const execute = async () => {
|
116
|
-
const validInput = (() => {
|
117
|
-
const schema = procedure.zz$p.contract.zz$cp.InputSchema;
|
118
|
-
if (!schema) {
|
119
|
-
return input;
|
120
|
-
}
|
121
|
-
try {
|
122
|
-
return schema.parse(input);
|
123
|
-
} catch (e) {
|
124
|
-
throw new ORPCError({
|
125
|
-
message: "Validation input failed",
|
126
|
-
code: "BAD_REQUEST",
|
127
|
-
cause: e
|
128
|
-
});
|
129
|
-
}
|
130
|
-
})();
|
131
|
-
const middlewares = procedure.zz$p.middlewares ?? [];
|
132
|
-
let currentMidIndex = 0;
|
133
|
-
let currentContext = context;
|
134
|
-
const next = async (nextOptions) => {
|
135
|
-
const mid = middlewares[currentMidIndex];
|
136
|
-
currentMidIndex += 1;
|
137
|
-
currentContext = mergeContext(currentContext, nextOptions.context);
|
138
|
-
if (mid) {
|
139
|
-
return await mid(validInput, currentContext, {
|
140
|
-
path,
|
141
|
-
procedure,
|
142
|
-
next,
|
143
|
-
output: (output3) => ({ output: output3, context: void 0 })
|
144
|
-
});
|
145
|
-
} else {
|
146
|
-
return {
|
147
|
-
output: await await procedure.zz$p.func(validInput, currentContext, {
|
148
|
-
path,
|
149
|
-
procedure
|
150
|
-
}),
|
151
|
-
context: currentContext
|
152
|
-
};
|
153
|
-
}
|
154
|
-
};
|
155
|
-
const output2 = (await next({})).output;
|
156
|
-
const validOutput = await (async () => {
|
157
|
-
const schema = procedure.zz$p.contract.zz$cp.OutputSchema;
|
158
|
-
if (!schema) {
|
159
|
-
return output2;
|
160
|
-
}
|
161
|
-
const result = await schema.safeParseAsync(output2);
|
162
|
-
if (result.error) {
|
163
|
-
throw new ORPCError({
|
164
|
-
message: "Validation output failed",
|
165
|
-
code: "INTERNAL_SERVER_ERROR",
|
166
|
-
cause: result.error
|
167
|
-
});
|
168
|
-
}
|
169
|
-
return result.data;
|
170
|
-
})();
|
171
|
-
return validOutput;
|
172
|
-
};
|
173
|
-
const output = await executeWithHooks({
|
174
|
-
hooks: options,
|
175
|
-
input,
|
176
|
-
context,
|
177
|
-
meta: {
|
178
|
-
path,
|
179
|
-
procedure
|
180
|
-
},
|
181
|
-
execute
|
182
|
-
});
|
183
|
-
return output;
|
184
|
-
};
|
185
|
-
return caller;
|
186
|
-
}
|
187
|
-
async function loadProcedure(procedure) {
|
188
|
-
let loadedProcedure;
|
189
|
-
if (isLazy(procedure)) {
|
190
|
-
loadedProcedure = (await loadLazy(procedure)).default;
|
191
|
-
} else {
|
192
|
-
loadedProcedure = procedure;
|
193
|
-
}
|
194
|
-
if (!isProcedure(loadedProcedure)) {
|
195
|
-
throw new ORPCError({
|
196
|
-
code: "NOT_FOUND",
|
197
|
-
message: "Not found",
|
198
|
-
cause: new Error(trim(`
|
199
|
-
This error should be caught by the typescript compiler.
|
200
|
-
But if you still see this error, it means that you trying to call a lazy router (expected to be a lazy procedure).
|
201
|
-
`))
|
202
|
-
});
|
203
|
-
}
|
204
|
-
return loadedProcedure;
|
205
|
-
}
|
206
|
-
|
207
|
-
// src/lazy.ts
|
208
|
-
var LAZY_LOADER_SYMBOL = Symbol("ORPC_LAZY_LOADER");
|
209
|
-
function createLazy(loader) {
|
210
|
-
return {
|
211
|
-
[LAZY_LOADER_SYMBOL]: loader
|
212
|
-
};
|
213
|
-
}
|
214
|
-
function loadLazy(lazy) {
|
215
|
-
return lazy[LAZY_LOADER_SYMBOL]();
|
216
|
-
}
|
217
|
-
function isLazy(item) {
|
218
|
-
return (typeof item === "object" || typeof item === "function") && item !== null && LAZY_LOADER_SYMBOL in item && typeof item[LAZY_LOADER_SYMBOL] === "function";
|
219
|
-
}
|
220
|
-
function createFlattenLazy(lazy) {
|
221
|
-
const flattenLoader = async () => {
|
222
|
-
let current = await loadLazy(lazy);
|
223
|
-
while (true) {
|
224
|
-
if (!isLazy(current.default)) {
|
225
|
-
break;
|
226
|
-
}
|
227
|
-
current = await loadLazy(current.default);
|
228
|
-
}
|
229
|
-
return current;
|
230
|
-
};
|
231
|
-
const flattenLazy = {
|
232
|
-
[LAZY_LOADER_SYMBOL]: flattenLoader
|
233
|
-
};
|
234
|
-
return flattenLazy;
|
235
|
-
}
|
236
|
-
function decorateLazy(lazy) {
|
237
|
-
const flattenLazy = createFlattenLazy(lazy);
|
238
|
-
const procedureCaller = createProcedureCaller({
|
239
|
-
procedure: flattenLazy,
|
240
|
-
context: void 0
|
241
|
-
});
|
242
|
-
Object.assign(procedureCaller, flattenLazy);
|
243
|
-
const recursive = new Proxy(procedureCaller, {
|
244
|
-
get(target, key) {
|
245
|
-
if (typeof key !== "string") {
|
246
|
-
return Reflect.get(target, key);
|
247
|
-
}
|
248
|
-
return decorateLazy(createLazy(async () => {
|
249
|
-
const current = await loadLazy(flattenLazy);
|
250
|
-
return { default: current.default[key] };
|
251
|
-
}));
|
252
|
-
}
|
253
|
-
});
|
254
|
-
return recursive;
|
255
|
-
}
|
256
|
-
|
257
|
-
export {
|
258
|
-
mergeContext,
|
259
|
-
decorateMiddleware,
|
260
|
-
LAZY_LOADER_SYMBOL,
|
261
|
-
createLazy,
|
262
|
-
loadLazy,
|
263
|
-
isLazy,
|
264
|
-
createFlattenLazy,
|
265
|
-
decorateLazy,
|
266
|
-
createProcedureCaller,
|
267
|
-
loadProcedure,
|
268
|
-
Procedure,
|
269
|
-
decorateProcedure,
|
270
|
-
isProcedure
|
271
|
-
};
|
272
|
-
//# sourceMappingURL=chunk-MZXEMHFS.js.map
|
package/dist/fetch.js
DELETED
@@ -1,110 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
createProcedureCaller,
|
3
|
-
isLazy,
|
4
|
-
isProcedure
|
5
|
-
} from "./chunk-MZXEMHFS.js";
|
6
|
-
|
7
|
-
// src/fetch/handle.ts
|
8
|
-
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;
|
14
|
-
}
|
15
|
-
}
|
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"
|
21
|
-
}
|
22
|
-
});
|
23
|
-
}
|
24
|
-
|
25
|
-
// src/fetch/handler.ts
|
26
|
-
import { ORPC_HEADER, ORPC_HEADER_VALUE } from "@orpc/contract";
|
27
|
-
import { executeWithHooks, 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) {
|
35
|
-
return void 0;
|
36
|
-
}
|
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);
|
42
|
-
if (!match) {
|
43
|
-
throw new ORPCError2({ code: "NOT_FOUND", message: "Not found" });
|
44
|
-
}
|
45
|
-
const input = await deserializeRequest(options.request);
|
46
|
-
const caller = createProcedureCaller({
|
47
|
-
context,
|
48
|
-
procedure: match.procedure,
|
49
|
-
path: match.path
|
50
|
-
});
|
51
|
-
const output = await caller(input);
|
52
|
-
const { body, headers } = serializer.serialize(output);
|
53
|
-
return new Response(body, {
|
54
|
-
status: 200,
|
55
|
-
headers
|
56
|
-
});
|
57
|
-
};
|
58
|
-
try {
|
59
|
-
return await executeWithHooks({
|
60
|
-
hooks: options,
|
61
|
-
context,
|
62
|
-
execute: handler,
|
63
|
-
input: options.request,
|
64
|
-
meta: void 0
|
65
|
-
});
|
66
|
-
} catch (e) {
|
67
|
-
const error = e instanceof ORPCError2 ? e : new ORPCError2({
|
68
|
-
code: "INTERNAL_SERVER_ERROR",
|
69
|
-
message: "Internal server error",
|
70
|
-
cause: e
|
71
|
-
});
|
72
|
-
const { body, headers } = serializer.serialize(error.toJSON());
|
73
|
-
return new Response(body, {
|
74
|
-
status: error.status,
|
75
|
-
headers
|
76
|
-
});
|
77
|
-
}
|
78
|
-
};
|
79
|
-
}
|
80
|
-
function resolveORPCRouter(router, pathname) {
|
81
|
-
const path = trim(pathname, "/").split("/").map(decodeURIComponent);
|
82
|
-
let current = router;
|
83
|
-
for (const segment of path) {
|
84
|
-
if (typeof current !== "object" && typeof current !== "function" || !current) {
|
85
|
-
current = void 0;
|
86
|
-
break;
|
87
|
-
}
|
88
|
-
current = current[segment];
|
89
|
-
}
|
90
|
-
return isProcedure(current) || isLazy(current) ? {
|
91
|
-
procedure: current,
|
92
|
-
path
|
93
|
-
} : void 0;
|
94
|
-
}
|
95
|
-
async function deserializeRequest(request) {
|
96
|
-
try {
|
97
|
-
return await deserializer.deserialize(request);
|
98
|
-
} catch (e) {
|
99
|
-
throw new ORPCError2({
|
100
|
-
code: "BAD_REQUEST",
|
101
|
-
message: "Cannot parse request. Please check the request body and Content-Type header.",
|
102
|
-
cause: e
|
103
|
-
});
|
104
|
-
}
|
105
|
-
}
|
106
|
-
export {
|
107
|
-
createORPCHandler,
|
108
|
-
handleFetchRequest
|
109
|
-
};
|
110
|
-
//# sourceMappingURL=fetch.js.map
|