@orpc/server 0.0.0-next.ef3ba82 → 0.0.0-next.f107a0e
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 +128 -0
- package/dist/adapters/aws-lambda/index.d.mts +45 -0
- package/dist/adapters/aws-lambda/index.d.ts +45 -0
- package/dist/adapters/aws-lambda/index.mjs +41 -0
- package/dist/adapters/bun-ws/index.d.mts +35 -0
- package/dist/adapters/bun-ws/index.d.ts +35 -0
- package/dist/adapters/bun-ws/index.mjs +51 -0
- package/dist/adapters/crossws/index.d.mts +30 -0
- package/dist/adapters/crossws/index.d.ts +30 -0
- package/dist/adapters/crossws/index.mjs +51 -0
- package/dist/adapters/fetch/index.d.mts +78 -0
- package/dist/adapters/fetch/index.d.ts +78 -0
- package/dist/adapters/fetch/index.mjs +110 -0
- package/dist/adapters/message-port/index.d.mts +28 -0
- package/dist/adapters/message-port/index.d.ts +28 -0
- package/dist/adapters/message-port/index.mjs +41 -0
- package/dist/adapters/node/index.d.mts +77 -0
- package/dist/adapters/node/index.d.ts +77 -0
- package/dist/adapters/node/index.mjs +95 -0
- package/dist/adapters/standard/index.d.mts +26 -0
- package/dist/adapters/standard/index.d.ts +26 -0
- package/dist/adapters/standard/index.mjs +7 -0
- package/dist/adapters/websocket/index.d.mts +27 -0
- package/dist/adapters/websocket/index.d.ts +27 -0
- package/dist/adapters/websocket/index.mjs +38 -0
- package/dist/adapters/ws/index.d.mts +28 -0
- package/dist/adapters/ws/index.d.ts +28 -0
- package/dist/adapters/ws/index.mjs +38 -0
- package/dist/index.d.mts +829 -0
- package/dist/index.d.ts +829 -0
- package/dist/index.mjs +475 -0
- package/dist/plugins/index.d.mts +156 -0
- package/dist/plugins/index.d.ts +156 -0
- package/dist/plugins/index.mjs +253 -0
- package/dist/shared/server.-ACo36I0.d.ts +74 -0
- package/dist/shared/server.BPAWobQg.d.ts +12 -0
- package/dist/shared/server.BVwwTHyO.mjs +9 -0
- package/dist/shared/server.BW-nUGgA.mjs +36 -0
- package/dist/shared/server.Bd52nNaH.d.mts +12 -0
- package/dist/shared/server.BliFSTnG.d.mts +10 -0
- package/dist/shared/server.CHvOkcM3.mjs +186 -0
- package/dist/shared/server.DD2C4ujN.d.mts +192 -0
- package/dist/shared/server.DD2C4ujN.d.ts +192 -0
- package/dist/shared/server.DG7Tamti.mjs +367 -0
- package/dist/shared/server.Dq8xr7PQ.d.mts +74 -0
- package/dist/shared/server.IG2MjhrD.d.ts +10 -0
- package/package.json +75 -17
- package/dist/chunk-CVLK2PBB.js +0 -189
- package/dist/fetch.js +0 -105
- package/dist/index.js +0 -393
- package/dist/src/builder.d.ts +0 -48
- package/dist/src/fetch/handle.d.ts +0 -6
- package/dist/src/fetch/handler.d.ts +0 -2
- package/dist/src/fetch/index.d.ts +0 -3
- package/dist/src/fetch/types.d.ts +0 -34
- package/dist/src/index.d.ts +0 -14
- package/dist/src/middleware.d.ts +0 -25
- package/dist/src/procedure-builder.d.ts +0 -30
- package/dist/src/procedure-caller.d.ts +0 -18
- package/dist/src/procedure-implementer.d.ts +0 -17
- package/dist/src/procedure.d.ts +0 -28
- package/dist/src/router-builder.d.ts +0 -21
- package/dist/src/router-caller.d.ts +0 -21
- package/dist/src/router-implementer.d.ts +0 -19
- package/dist/src/router.d.ts +0 -19
- package/dist/src/types.d.ts +0 -7
- package/dist/src/utils.d.ts +0 -2
package/dist/chunk-CVLK2PBB.js
DELETED
@@ -1,189 +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 { value } from "@orpc/shared";
|
49
|
-
import { ORPCError } from "@orpc/shared/error";
|
50
|
-
import { OpenAPIDeserializer } from "@orpc/transformer";
|
51
|
-
function createProcedureCaller(options) {
|
52
|
-
const path = options.path ?? [];
|
53
|
-
const procedure = options.procedure;
|
54
|
-
const caller = async (input) => {
|
55
|
-
const input_ = (() => {
|
56
|
-
if (!(input instanceof FormData)) {
|
57
|
-
return input;
|
58
|
-
}
|
59
|
-
const transformer = new OpenAPIDeserializer({
|
60
|
-
schema: procedure.zz$p.contract.zz$cp.InputSchema
|
61
|
-
});
|
62
|
-
return transformer.deserializeAsFormData(input);
|
63
|
-
})();
|
64
|
-
const validInput = (() => {
|
65
|
-
const schema = procedure.zz$p.contract.zz$cp.InputSchema;
|
66
|
-
if (!schema) {
|
67
|
-
return input_;
|
68
|
-
}
|
69
|
-
try {
|
70
|
-
return schema.parse(input_);
|
71
|
-
} catch (e) {
|
72
|
-
throw new ORPCError({
|
73
|
-
message: "Validation input failed",
|
74
|
-
code: "BAD_REQUEST",
|
75
|
-
cause: e
|
76
|
-
});
|
77
|
-
}
|
78
|
-
})();
|
79
|
-
const middlewares = procedure.zz$p.middlewares ?? [];
|
80
|
-
let currentMidIndex = 0;
|
81
|
-
let currentContext = await value(options.context);
|
82
|
-
const next = async (nextOptions) => {
|
83
|
-
const mid = middlewares[currentMidIndex];
|
84
|
-
currentMidIndex += 1;
|
85
|
-
currentContext = mergeContext(currentContext, nextOptions.context);
|
86
|
-
if (mid) {
|
87
|
-
return await mid(validInput, currentContext, {
|
88
|
-
path,
|
89
|
-
procedure,
|
90
|
-
next,
|
91
|
-
output: (output2) => ({ output: output2, context: void 0 })
|
92
|
-
});
|
93
|
-
} else {
|
94
|
-
return {
|
95
|
-
output: await await procedure.zz$p.func(validInput, currentContext, {
|
96
|
-
path,
|
97
|
-
procedure
|
98
|
-
}),
|
99
|
-
context: currentContext
|
100
|
-
};
|
101
|
-
}
|
102
|
-
};
|
103
|
-
const output = (await next({})).output;
|
104
|
-
const validOutput = await (async () => {
|
105
|
-
const schema = procedure.zz$p.contract.zz$cp.OutputSchema;
|
106
|
-
if (!schema) {
|
107
|
-
return output;
|
108
|
-
}
|
109
|
-
const result = await schema.safeParseAsync(output);
|
110
|
-
if (result.error) {
|
111
|
-
throw new ORPCError({
|
112
|
-
message: "Validation output failed",
|
113
|
-
code: "INTERNAL_SERVER_ERROR",
|
114
|
-
cause: result.error
|
115
|
-
});
|
116
|
-
}
|
117
|
-
return result.data;
|
118
|
-
})();
|
119
|
-
return validOutput;
|
120
|
-
};
|
121
|
-
return caller;
|
122
|
-
}
|
123
|
-
|
124
|
-
// src/procedure.ts
|
125
|
-
import {
|
126
|
-
DecoratedContractProcedure,
|
127
|
-
isContractProcedure
|
128
|
-
} from "@orpc/contract";
|
129
|
-
var Procedure = class {
|
130
|
-
constructor(zz$p) {
|
131
|
-
this.zz$p = zz$p;
|
132
|
-
}
|
133
|
-
};
|
134
|
-
var DECORATED_PROCEDURE_SYMBOL = Symbol("DECORATED_PROCEDURE");
|
135
|
-
function decorateProcedure(procedure) {
|
136
|
-
if (DECORATED_PROCEDURE_SYMBOL in procedure) {
|
137
|
-
return procedure;
|
138
|
-
}
|
139
|
-
return Object.assign(createProcedureCaller({
|
140
|
-
procedure,
|
141
|
-
context: void 0
|
142
|
-
}), {
|
143
|
-
[DECORATED_PROCEDURE_SYMBOL]: true,
|
144
|
-
zz$p: procedure.zz$p,
|
145
|
-
prefix(prefix) {
|
146
|
-
return decorateProcedure({
|
147
|
-
zz$p: {
|
148
|
-
...procedure.zz$p,
|
149
|
-
contract: DecoratedContractProcedure.decorate(
|
150
|
-
procedure.zz$p.contract
|
151
|
-
).prefix(prefix)
|
152
|
-
}
|
153
|
-
});
|
154
|
-
},
|
155
|
-
route(opts) {
|
156
|
-
return decorateProcedure({
|
157
|
-
zz$p: {
|
158
|
-
...procedure.zz$p,
|
159
|
-
contract: DecoratedContractProcedure.decorate(
|
160
|
-
procedure.zz$p.contract
|
161
|
-
).route(opts)
|
162
|
-
}
|
163
|
-
});
|
164
|
-
},
|
165
|
-
use(middleware, mapInput) {
|
166
|
-
const middleware_ = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
|
167
|
-
return decorateProcedure({
|
168
|
-
zz$p: {
|
169
|
-
...procedure.zz$p,
|
170
|
-
middlewares: [middleware_, ...procedure.zz$p.middlewares ?? []]
|
171
|
-
}
|
172
|
-
});
|
173
|
-
}
|
174
|
-
});
|
175
|
-
}
|
176
|
-
function isProcedure(item) {
|
177
|
-
if (item instanceof Procedure)
|
178
|
-
return true;
|
179
|
-
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";
|
180
|
-
}
|
181
|
-
|
182
|
-
export {
|
183
|
-
mergeContext,
|
184
|
-
decorateMiddleware,
|
185
|
-
createProcedureCaller,
|
186
|
-
Procedure,
|
187
|
-
decorateProcedure,
|
188
|
-
isProcedure
|
189
|
-
};
|
package/dist/fetch.js
DELETED
@@ -1,105 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
createProcedureCaller,
|
3
|
-
isProcedure
|
4
|
-
} from "./chunk-CVLK2PBB.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
|
-
}
|
102
|
-
export {
|
103
|
-
createORPCHandler,
|
104
|
-
handleFetchRequest
|
105
|
-
};
|
package/dist/index.js
DELETED
@@ -1,393 +0,0 @@
|
|
1
|
-
import {
|
2
|
-
Procedure,
|
3
|
-
createProcedureCaller,
|
4
|
-
decorateMiddleware,
|
5
|
-
decorateProcedure,
|
6
|
-
isProcedure,
|
7
|
-
mergeContext
|
8
|
-
} from "./chunk-CVLK2PBB.js";
|
9
|
-
|
10
|
-
// src/builder.ts
|
11
|
-
import {
|
12
|
-
ContractProcedure,
|
13
|
-
isContractProcedure as isContractProcedure2
|
14
|
-
} from "@orpc/contract";
|
15
|
-
|
16
|
-
// src/procedure-builder.ts
|
17
|
-
import {
|
18
|
-
DecoratedContractProcedure
|
19
|
-
} from "@orpc/contract";
|
20
|
-
|
21
|
-
// src/procedure-implementer.ts
|
22
|
-
var ProcedureImplementer = class _ProcedureImplementer {
|
23
|
-
constructor(zz$pi) {
|
24
|
-
this.zz$pi = zz$pi;
|
25
|
-
}
|
26
|
-
use(middleware, mapInput) {
|
27
|
-
const middleware_ = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
|
28
|
-
return new _ProcedureImplementer({
|
29
|
-
...this.zz$pi,
|
30
|
-
middlewares: [...this.zz$pi.middlewares ?? [], middleware_]
|
31
|
-
});
|
32
|
-
}
|
33
|
-
func(func) {
|
34
|
-
return decorateProcedure({
|
35
|
-
zz$p: {
|
36
|
-
middlewares: this.zz$pi.middlewares,
|
37
|
-
contract: this.zz$pi.contract,
|
38
|
-
func
|
39
|
-
}
|
40
|
-
});
|
41
|
-
}
|
42
|
-
};
|
43
|
-
|
44
|
-
// src/procedure-builder.ts
|
45
|
-
var ProcedureBuilder = class _ProcedureBuilder {
|
46
|
-
constructor(zz$pb) {
|
47
|
-
this.zz$pb = zz$pb;
|
48
|
-
}
|
49
|
-
/**
|
50
|
-
* Self chainable
|
51
|
-
*/
|
52
|
-
route(opts) {
|
53
|
-
return new _ProcedureBuilder({
|
54
|
-
...this.zz$pb,
|
55
|
-
contract: DecoratedContractProcedure.decorate(this.zz$pb.contract).route(
|
56
|
-
opts
|
57
|
-
)
|
58
|
-
});
|
59
|
-
}
|
60
|
-
input(schema, example) {
|
61
|
-
return new _ProcedureBuilder({
|
62
|
-
...this.zz$pb,
|
63
|
-
contract: DecoratedContractProcedure.decorate(this.zz$pb.contract).input(
|
64
|
-
schema,
|
65
|
-
example
|
66
|
-
)
|
67
|
-
});
|
68
|
-
}
|
69
|
-
output(schema, example) {
|
70
|
-
return new _ProcedureBuilder({
|
71
|
-
...this.zz$pb,
|
72
|
-
contract: DecoratedContractProcedure.decorate(this.zz$pb.contract).output(
|
73
|
-
schema,
|
74
|
-
example
|
75
|
-
)
|
76
|
-
});
|
77
|
-
}
|
78
|
-
use(middleware, mapInput) {
|
79
|
-
if (!mapInput) {
|
80
|
-
return new ProcedureImplementer({
|
81
|
-
contract: this.zz$pb.contract,
|
82
|
-
middlewares: this.zz$pb.middlewares
|
83
|
-
}).use(middleware);
|
84
|
-
}
|
85
|
-
return new ProcedureImplementer({
|
86
|
-
contract: this.zz$pb.contract,
|
87
|
-
middlewares: this.zz$pb.middlewares
|
88
|
-
}).use(middleware, mapInput);
|
89
|
-
}
|
90
|
-
/**
|
91
|
-
* Convert to Procedure
|
92
|
-
*/
|
93
|
-
func(func) {
|
94
|
-
return decorateProcedure({
|
95
|
-
zz$p: {
|
96
|
-
middlewares: this.zz$pb.middlewares,
|
97
|
-
contract: this.zz$pb.contract,
|
98
|
-
func
|
99
|
-
}
|
100
|
-
});
|
101
|
-
}
|
102
|
-
};
|
103
|
-
|
104
|
-
// src/router-builder.ts
|
105
|
-
import { DecoratedContractProcedure as DecoratedContractProcedure2 } from "@orpc/contract";
|
106
|
-
var RouterBuilder = class _RouterBuilder {
|
107
|
-
constructor(zz$rb) {
|
108
|
-
this.zz$rb = zz$rb;
|
109
|
-
}
|
110
|
-
prefix(prefix) {
|
111
|
-
return new _RouterBuilder({
|
112
|
-
...this.zz$rb,
|
113
|
-
prefix: `${this.zz$rb.prefix ?? ""}${prefix}`
|
114
|
-
});
|
115
|
-
}
|
116
|
-
tags(...tags) {
|
117
|
-
if (!tags.length)
|
118
|
-
return this;
|
119
|
-
return new _RouterBuilder({
|
120
|
-
...this.zz$rb,
|
121
|
-
tags: [...this.zz$rb.tags ?? [], ...tags]
|
122
|
-
});
|
123
|
-
}
|
124
|
-
use(middleware, mapInput) {
|
125
|
-
const middleware_ = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
|
126
|
-
return new _RouterBuilder({
|
127
|
-
...this.zz$rb,
|
128
|
-
middlewares: [...this.zz$rb.middlewares || [], middleware_]
|
129
|
-
});
|
130
|
-
}
|
131
|
-
router(router) {
|
132
|
-
const handled = {};
|
133
|
-
for (const key in router) {
|
134
|
-
const item = router[key];
|
135
|
-
if (isProcedure(item)) {
|
136
|
-
const builderMiddlewares = this.zz$rb.middlewares ?? [];
|
137
|
-
const itemMiddlewares = item.zz$p.middlewares ?? [];
|
138
|
-
const middlewares = [
|
139
|
-
...builderMiddlewares,
|
140
|
-
...itemMiddlewares.filter(
|
141
|
-
(item2) => !builderMiddlewares.includes(item2)
|
142
|
-
)
|
143
|
-
];
|
144
|
-
const contract = DecoratedContractProcedure2.decorate(
|
145
|
-
item.zz$p.contract
|
146
|
-
).addTags(...this.zz$rb.tags ?? []);
|
147
|
-
handled[key] = decorateProcedure({
|
148
|
-
zz$p: {
|
149
|
-
...item.zz$p,
|
150
|
-
contract: this.zz$rb.prefix ? contract.prefix(this.zz$rb.prefix) : contract,
|
151
|
-
middlewares
|
152
|
-
}
|
153
|
-
});
|
154
|
-
} else {
|
155
|
-
handled[key] = this.router(item);
|
156
|
-
}
|
157
|
-
}
|
158
|
-
return handled;
|
159
|
-
}
|
160
|
-
};
|
161
|
-
|
162
|
-
// src/router-implementer.ts
|
163
|
-
import {
|
164
|
-
isContractProcedure
|
165
|
-
} from "@orpc/contract";
|
166
|
-
var RouterImplementer = class {
|
167
|
-
constructor(zz$ri) {
|
168
|
-
this.zz$ri = zz$ri;
|
169
|
-
}
|
170
|
-
router(router) {
|
171
|
-
assertRouterImplementation(this.zz$ri.contract, router);
|
172
|
-
return router;
|
173
|
-
}
|
174
|
-
};
|
175
|
-
function chainRouterImplementer(contract, middlewares) {
|
176
|
-
const result = {};
|
177
|
-
for (const key in contract) {
|
178
|
-
const item = contract[key];
|
179
|
-
if (isContractProcedure(item)) {
|
180
|
-
result[key] = new ProcedureImplementer({
|
181
|
-
contract: item,
|
182
|
-
middlewares
|
183
|
-
});
|
184
|
-
} else {
|
185
|
-
result[key] = chainRouterImplementer(item, middlewares);
|
186
|
-
}
|
187
|
-
}
|
188
|
-
const implementer = new RouterImplementer({ contract });
|
189
|
-
return Object.assign(implementer, result);
|
190
|
-
}
|
191
|
-
function assertRouterImplementation(contract, router, path = []) {
|
192
|
-
for (const key in contract) {
|
193
|
-
const currentPath = [...path, key];
|
194
|
-
const contractItem = contract[key];
|
195
|
-
const routerItem = router[key];
|
196
|
-
if (!routerItem) {
|
197
|
-
throw new Error(
|
198
|
-
`Missing implementation for procedure at [${currentPath.join(".")}]`
|
199
|
-
);
|
200
|
-
}
|
201
|
-
if (isContractProcedure(contractItem)) {
|
202
|
-
if (isProcedure(routerItem)) {
|
203
|
-
if (routerItem.zz$p.contract !== contractItem) {
|
204
|
-
throw new Error(
|
205
|
-
`Mismatch implementation for procedure at [${currentPath.join(".")}]`
|
206
|
-
);
|
207
|
-
}
|
208
|
-
} else {
|
209
|
-
throw new Error(
|
210
|
-
`Mismatch implementation for procedure at [${currentPath.join(".")}]`
|
211
|
-
);
|
212
|
-
}
|
213
|
-
} else {
|
214
|
-
assertRouterImplementation(
|
215
|
-
contractItem,
|
216
|
-
routerItem,
|
217
|
-
currentPath
|
218
|
-
);
|
219
|
-
}
|
220
|
-
}
|
221
|
-
}
|
222
|
-
|
223
|
-
// src/builder.ts
|
224
|
-
var Builder = class _Builder {
|
225
|
-
constructor(zz$b = {}) {
|
226
|
-
this.zz$b = zz$b;
|
227
|
-
}
|
228
|
-
/**
|
229
|
-
* Self chainable
|
230
|
-
*/
|
231
|
-
context() {
|
232
|
-
return this;
|
233
|
-
}
|
234
|
-
use(middleware, mapInput) {
|
235
|
-
const middleware_ = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
|
236
|
-
return new _Builder({
|
237
|
-
...this.zz$b,
|
238
|
-
middlewares: [...this.zz$b.middlewares || [], middleware_]
|
239
|
-
});
|
240
|
-
}
|
241
|
-
/**
|
242
|
-
* Convert to ContractProcedureBuilder
|
243
|
-
*/
|
244
|
-
route(opts) {
|
245
|
-
return new ProcedureBuilder({
|
246
|
-
middlewares: this.zz$b.middlewares,
|
247
|
-
contract: new ContractProcedure({
|
248
|
-
...opts,
|
249
|
-
InputSchema: void 0,
|
250
|
-
OutputSchema: void 0
|
251
|
-
})
|
252
|
-
});
|
253
|
-
}
|
254
|
-
input(schema, example) {
|
255
|
-
return new ProcedureBuilder({
|
256
|
-
middlewares: this.zz$b.middlewares,
|
257
|
-
contract: new ContractProcedure({
|
258
|
-
OutputSchema: void 0,
|
259
|
-
InputSchema: schema,
|
260
|
-
inputExample: example
|
261
|
-
})
|
262
|
-
});
|
263
|
-
}
|
264
|
-
output(schema, example) {
|
265
|
-
return new ProcedureBuilder({
|
266
|
-
middlewares: this.zz$b.middlewares,
|
267
|
-
contract: new ContractProcedure({
|
268
|
-
InputSchema: void 0,
|
269
|
-
OutputSchema: schema,
|
270
|
-
outputExample: example
|
271
|
-
})
|
272
|
-
});
|
273
|
-
}
|
274
|
-
/**
|
275
|
-
* Convert to Procedure
|
276
|
-
*/
|
277
|
-
func(func) {
|
278
|
-
return decorateProcedure({
|
279
|
-
zz$p: {
|
280
|
-
middlewares: this.zz$b.middlewares,
|
281
|
-
contract: new ContractProcedure({
|
282
|
-
InputSchema: void 0,
|
283
|
-
OutputSchema: void 0
|
284
|
-
}),
|
285
|
-
func
|
286
|
-
}
|
287
|
-
});
|
288
|
-
}
|
289
|
-
/**
|
290
|
-
* Convert to ProcedureImplementer | RouterBuilder
|
291
|
-
*/
|
292
|
-
contract(contract) {
|
293
|
-
if (isContractProcedure2(contract)) {
|
294
|
-
return new ProcedureImplementer({
|
295
|
-
contract,
|
296
|
-
middlewares: this.zz$b.middlewares
|
297
|
-
});
|
298
|
-
}
|
299
|
-
return chainRouterImplementer(
|
300
|
-
contract,
|
301
|
-
this.zz$b.middlewares
|
302
|
-
);
|
303
|
-
}
|
304
|
-
/**
|
305
|
-
* Create ExtendedMiddleware
|
306
|
-
*/
|
307
|
-
// TODO: TOutput always any, infer not work at all, because TOutput used inside middleware params,
|
308
|
-
// solution (maybe): create new generic for .output() method
|
309
|
-
middleware(middleware) {
|
310
|
-
return decorateMiddleware(middleware);
|
311
|
-
}
|
312
|
-
prefix(prefix) {
|
313
|
-
return new RouterBuilder({
|
314
|
-
...this.zz$b,
|
315
|
-
prefix
|
316
|
-
});
|
317
|
-
}
|
318
|
-
tags(...tags) {
|
319
|
-
return new RouterBuilder({
|
320
|
-
...this.zz$b,
|
321
|
-
tags
|
322
|
-
});
|
323
|
-
}
|
324
|
-
/**
|
325
|
-
* Create DecoratedRouter
|
326
|
-
*/
|
327
|
-
router(router) {
|
328
|
-
return new RouterBuilder(this.zz$b).router(router);
|
329
|
-
}
|
330
|
-
};
|
331
|
-
|
332
|
-
// src/router.ts
|
333
|
-
import {
|
334
|
-
isContractProcedure as isContractProcedure3
|
335
|
-
} from "@orpc/contract";
|
336
|
-
function toContractRouter(router) {
|
337
|
-
const contract = {};
|
338
|
-
for (const key in router) {
|
339
|
-
const item = router[key];
|
340
|
-
if (isContractProcedure3(item)) {
|
341
|
-
contract[key] = item;
|
342
|
-
} else if (isProcedure(item)) {
|
343
|
-
contract[key] = item.zz$p.contract;
|
344
|
-
} else {
|
345
|
-
contract[key] = toContractRouter(item);
|
346
|
-
}
|
347
|
-
}
|
348
|
-
return contract;
|
349
|
-
}
|
350
|
-
|
351
|
-
// src/router-caller.ts
|
352
|
-
function createRouterCaller(options) {
|
353
|
-
const caller = {};
|
354
|
-
for (const key in options.router) {
|
355
|
-
const path = [...options.basePath ?? [], key];
|
356
|
-
const item = options.router[key];
|
357
|
-
if (isProcedure(item)) {
|
358
|
-
caller[key] = createProcedureCaller({
|
359
|
-
procedure: item,
|
360
|
-
context: options.context,
|
361
|
-
path
|
362
|
-
});
|
363
|
-
} else {
|
364
|
-
caller[key] = createRouterCaller({
|
365
|
-
router: item,
|
366
|
-
context: options.context,
|
367
|
-
basePath: path
|
368
|
-
});
|
369
|
-
}
|
370
|
-
}
|
371
|
-
return caller;
|
372
|
-
}
|
373
|
-
|
374
|
-
// src/index.ts
|
375
|
-
export * from "@orpc/shared/error";
|
376
|
-
var os = new Builder();
|
377
|
-
export {
|
378
|
-
Builder,
|
379
|
-
Procedure,
|
380
|
-
ProcedureBuilder,
|
381
|
-
ProcedureImplementer,
|
382
|
-
RouterImplementer,
|
383
|
-
assertRouterImplementation,
|
384
|
-
chainRouterImplementer,
|
385
|
-
createProcedureCaller,
|
386
|
-
createRouterCaller,
|
387
|
-
decorateMiddleware,
|
388
|
-
decorateProcedure,
|
389
|
-
isProcedure,
|
390
|
-
mergeContext,
|
391
|
-
os,
|
392
|
-
toContractRouter
|
393
|
-
};
|