@temporary-name/server 1.9.3-alpha.592ae1e02e18c5274906b132313b083116248636 → 1.9.3-alpha.59a0ffc2afa5d7ac78321380832e510591ebe734
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/dist/adapters/aws-lambda/index.d.mts +5 -7
- package/dist/adapters/aws-lambda/index.d.ts +5 -7
- package/dist/adapters/aws-lambda/index.mjs +6 -9
- package/dist/adapters/fetch/index.d.mts +8 -86
- package/dist/adapters/fetch/index.d.ts +8 -86
- package/dist/adapters/fetch/index.mjs +18 -160
- package/dist/adapters/node/index.d.mts +9 -64
- package/dist/adapters/node/index.d.ts +9 -64
- package/dist/adapters/node/index.mjs +16 -125
- package/dist/handler/index.d.mts +28 -0
- package/dist/handler/index.d.ts +28 -0
- package/dist/handler/index.mjs +8 -0
- package/dist/helpers/index.mjs +3 -29
- package/dist/index.d.mts +372 -493
- package/dist/index.d.ts +372 -493
- package/dist/index.mjs +546 -469
- package/dist/openapi/index.d.mts +18 -53
- package/dist/openapi/index.d.ts +18 -53
- package/dist/openapi/index.mjs +384 -378
- package/dist/shared/server.B9A3AnYU.mjs +523 -0
- package/dist/shared/server.Bh0-Oy3H.mjs +156 -0
- package/dist/shared/server.Bmxjwleq.d.ts +39 -0
- package/dist/shared/server.BnAospUv.mjs +334 -0
- package/dist/shared/server.Bx9JTGMu.mjs +433 -0
- package/dist/shared/server.C1RJffw4.mjs +30 -0
- package/dist/shared/server.CjPiuQYH.d.mts +51 -0
- package/dist/shared/server.CjPiuQYH.d.ts +51 -0
- package/dist/shared/server.DLyn62VH.d.mts +39 -0
- package/dist/shared/server.DpIhEnBO.d.mts +515 -0
- package/dist/shared/server.DpIhEnBO.d.ts +515 -0
- package/package.json +13 -30
- package/dist/adapters/standard/index.d.mts +0 -42
- package/dist/adapters/standard/index.d.ts +0 -42
- package/dist/adapters/standard/index.mjs +0 -11
- package/dist/plugins/index.d.mts +0 -160
- package/dist/plugins/index.d.ts +0 -160
- package/dist/plugins/index.mjs +0 -288
- package/dist/shared/server.BEHw7Eyx.mjs +0 -247
- package/dist/shared/server.BKSOrA6h.d.mts +0 -192
- package/dist/shared/server.BKSOrA6h.d.ts +0 -192
- package/dist/shared/server.BKh8I1Ny.mjs +0 -239
- package/dist/shared/server.BeuTpcmO.d.mts +0 -23
- package/dist/shared/server.C1fnTLq0.d.mts +0 -57
- package/dist/shared/server.CQyYNJ1H.d.ts +0 -57
- package/dist/shared/server.DLsti1Pv.mjs +0 -293
- package/dist/shared/server.SLLuK6_v.d.ts +0 -23
package/dist/index.mjs
CHANGED
|
@@ -1,136 +1,81 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export {
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
import { c as
|
|
6
|
-
export { L as LAZY_SYMBOL,
|
|
1
|
+
import { assertNever, splitFirst, onError, isObject, toArray, guard, resolveMaybeOptionalOptions, isTypescriptObject, isPropertyKey } from '@temporary-name/shared';
|
|
2
|
+
export { AsyncIteratorClass, EventPublisher, asyncIteratorToStream as eventIteratorToStream, onError, onFinish, onStart, onSuccess, streamToAsyncIteratorClass as streamToEventIterator } from '@temporary-name/shared';
|
|
3
|
+
import * as z from '@temporary-name/zod';
|
|
4
|
+
import { SchemaClass, object, core, gatingContext } from '@temporary-name/zod';
|
|
5
|
+
import { U as UnauthorizedError, P as Procedure, m as mergePrefix, a as mergeTags, b as enhanceRouter, C as Contract, p as parseEndpointDefinition, c as mergeRoute, d as prefixRoute, f as addMiddleware, I as InternalServerError, N as NotFoundError, F as ForbiddenError, B as BadRequestError, l as lazyInternal, h as getLazyMeta, u as unlazy, j as isProcedure } from './shared/server.Bx9JTGMu.mjs';
|
|
6
|
+
export { A as APIError, y as BadGatewayError, n as ConflictError, q as ContentTooLargeError, G as GatewayTimeoutError, L as LAZY_SYMBOL, M as MethodNotAllowedError, k as NotAcceptableError, x as NotImplementedError, o as PreconditionFailedError, R as RequestTimeoutError, S as ServiceUnavailableError, T as TooManyRequestsError, w as UnprocessableContentError, v as UnsupportedMediaTypeError, V as ValidationError, K as createAccessibleLazyRouter, e as encodeError, W as endpointRegex, g as getDynamicParams, J as getRouter, i as isAPIErrorStatus, X as isDevelopment, D as isLazy, E as isStartWithMiddlewares, z as lazy, H as mergeMiddlewares, r as resolveContractProcedures, s as standardizeHTTPPath, t as toAPIError, O as traverseContractProcedures, Q as unlazyRouter } from './shared/server.Bx9JTGMu.mjs';
|
|
7
|
+
import { g as getCookie } from './shared/server.C1RJffw4.mjs';
|
|
8
|
+
import { c as createProcedureClient } from './shared/server.Bh0-Oy3H.mjs';
|
|
9
|
+
export { m as mergeCurrentContext, a as middlewareOutputFn } from './shared/server.Bh0-Oy3H.mjs';
|
|
10
|
+
import { J as JsonSchemaXNativeType } from './shared/server.B9A3AnYU.mjs';
|
|
11
|
+
export { b as JSON_SCHEMA_INPUT_REGISTRY, c as JSON_SCHEMA_OUTPUT_REGISTRY, a as JSON_SCHEMA_REGISTRY, Z as ZodToJsonSchemaConverter, e as eventIterator, g as getEventIteratorSchemaDetails } from './shared/server.B9A3AnYU.mjs';
|
|
7
12
|
export { getEventMeta, withEventMeta } from '@temporary-name/standard-server';
|
|
8
|
-
import '
|
|
9
|
-
import '
|
|
13
|
+
import 'cookie';
|
|
14
|
+
import '@temporary-name/server/openapi';
|
|
10
15
|
import 'zod/v4/core';
|
|
11
16
|
|
|
12
|
-
function
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
const mapped = decorateMiddleware(
|
|
16
|
-
(options, input, ...rest) => middleware(options, mapInput(input), ...rest)
|
|
17
|
-
);
|
|
18
|
-
return mapped;
|
|
19
|
-
};
|
|
20
|
-
decorated.concat = (concatMiddleware, mapInput) => {
|
|
21
|
-
const mapped = mapInput ? decorateMiddleware(concatMiddleware).mapInput(mapInput) : concatMiddleware;
|
|
22
|
-
const concatted = decorateMiddleware((options, input, output, ...rest) => {
|
|
23
|
-
const merged = middleware(
|
|
24
|
-
{
|
|
25
|
-
...options,
|
|
26
|
-
next: (...[nextOptions1]) => mapped(
|
|
27
|
-
{
|
|
28
|
-
...options,
|
|
29
|
-
context: { ...options.context, ...nextOptions1?.context },
|
|
30
|
-
next: (...[nextOptions2]) => options.next({ context: { ...nextOptions1?.context, ...nextOptions2?.context } })
|
|
31
|
-
},
|
|
32
|
-
input,
|
|
33
|
-
output,
|
|
34
|
-
...rest
|
|
35
|
-
)
|
|
36
|
-
},
|
|
37
|
-
input,
|
|
38
|
-
output,
|
|
39
|
-
...rest
|
|
40
|
-
);
|
|
41
|
-
return merged;
|
|
42
|
-
});
|
|
43
|
-
return concatted;
|
|
44
|
-
};
|
|
45
|
-
return decorated;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function isStartWithMiddlewares(middlewares, compare) {
|
|
49
|
-
if (compare.length > middlewares.length) {
|
|
50
|
-
return false;
|
|
17
|
+
function validateTokenPrefix(prefix, token) {
|
|
18
|
+
if (prefix && !token.startsWith(prefix)) {
|
|
19
|
+
throw new UnauthorizedError({ message: `Invalid auth token. It must start with "${prefix}"` });
|
|
51
20
|
}
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
21
|
+
}
|
|
22
|
+
function validateTokenAuth(config, token, options) {
|
|
23
|
+
const { tokenPrefix, validate } = config;
|
|
24
|
+
validateTokenPrefix(tokenPrefix, token);
|
|
25
|
+
return validate(token, options);
|
|
26
|
+
}
|
|
27
|
+
function runAuth(config, options) {
|
|
28
|
+
switch (config.type) {
|
|
29
|
+
case "header": {
|
|
30
|
+
const authHeader = options.request.headers.get(config.name);
|
|
31
|
+
return authHeader ? validateTokenAuth(config, authHeader, options) : void 0;
|
|
32
|
+
}
|
|
33
|
+
case "query": {
|
|
34
|
+
const { name, tokenPrefix, validate } = config;
|
|
35
|
+
const query = options.request.url.searchParams;
|
|
36
|
+
const token = query.get(name);
|
|
37
|
+
if (!token) return void 0;
|
|
38
|
+
validateTokenPrefix(tokenPrefix, token);
|
|
39
|
+
return validate(token, options);
|
|
40
|
+
}
|
|
41
|
+
case "cookie": {
|
|
42
|
+
const cookie = getCookie(options.request.headers, config.name);
|
|
43
|
+
return cookie ? validateTokenAuth(config, cookie, options) : void 0;
|
|
44
|
+
}
|
|
45
|
+
case "bearer": {
|
|
46
|
+
const authHeader = options.request.headers.get("Authorization");
|
|
47
|
+
if (!authHeader) return void 0;
|
|
48
|
+
const [authType, bearer] = splitFirst(authHeader, " ");
|
|
49
|
+
return authType === "Bearer" ? validateTokenAuth(config, bearer, options) : void 0;
|
|
50
|
+
}
|
|
51
|
+
case "basic": {
|
|
52
|
+
const authHeader = options.request.headers.get("Authorization");
|
|
53
|
+
if (!authHeader) return void 0;
|
|
54
|
+
const [authType, encoded] = splitFirst(authHeader, " ");
|
|
55
|
+
if (authType !== "Basic") return void 0;
|
|
56
|
+
const decoded = Buffer.from(encoded, "base64").toString("utf-8");
|
|
57
|
+
const [username, password] = splitFirst(decoded, ":");
|
|
58
|
+
validateTokenPrefix(config.tokenPrefix, password);
|
|
59
|
+
return config.validate(username, password, options);
|
|
55
60
|
}
|
|
56
|
-
|
|
61
|
+
case "none": {
|
|
57
62
|
return false;
|
|
58
63
|
}
|
|
64
|
+
default: {
|
|
65
|
+
assertNever(config);
|
|
66
|
+
}
|
|
59
67
|
}
|
|
60
|
-
return true;
|
|
61
|
-
}
|
|
62
|
-
function mergeMiddlewares(first, second, options) {
|
|
63
|
-
if (options.dedupeLeading && isStartWithMiddlewares(second, first)) {
|
|
64
|
-
return second;
|
|
65
|
-
}
|
|
66
|
-
return [...first, ...second];
|
|
67
68
|
}
|
|
68
|
-
function
|
|
69
|
-
|
|
69
|
+
function authDescription(config) {
|
|
70
|
+
const { type } = config;
|
|
71
|
+
return type === "basic" ? "a Basic Authentication header" : type === "bearer" ? "a Bearer Authentication header" : type === "header" ? `a header named "${config.name}"` : type === "query" ? `a query parameter named "${config.name}"` : type === "cookie" ? `a cookie named "${config.name}"` : type === "none" ? "no authentication" : assertNever(type);
|
|
70
72
|
}
|
|
71
73
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
* This property holds the defined options.
|
|
75
|
-
*/
|
|
76
|
-
"~orpc";
|
|
77
|
-
constructor(def) {
|
|
78
|
-
this["~orpc"] = def;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
function isProcedure(item) {
|
|
82
|
-
if (item instanceof Procedure) {
|
|
83
|
-
return true;
|
|
84
|
-
}
|
|
85
|
-
return isContractProcedure(item) && "middlewares" in item["~orpc"] && "inputValidationIndex" in item["~orpc"] && "outputValidationIndex" in item["~orpc"] && "handler" in item["~orpc"];
|
|
74
|
+
function mergeMeta(meta1, meta2) {
|
|
75
|
+
return { ...meta1, ...meta2 };
|
|
86
76
|
}
|
|
87
77
|
|
|
88
78
|
class DecoratedProcedure extends Procedure {
|
|
89
|
-
/**
|
|
90
|
-
* Adds type-safe custom errors.
|
|
91
|
-
* The provided errors are spared-merged with any existing errors.
|
|
92
|
-
*
|
|
93
|
-
* @see {@link https://orpc.unnoq.com/docs/error-handling#type%E2%80%90safe-error-handling Type-Safe Error Handling Docs}
|
|
94
|
-
*/
|
|
95
|
-
errors(errors) {
|
|
96
|
-
return new DecoratedProcedure({
|
|
97
|
-
...this["~orpc"],
|
|
98
|
-
errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
|
|
99
|
-
});
|
|
100
|
-
}
|
|
101
|
-
/**
|
|
102
|
-
* Sets or updates the metadata.
|
|
103
|
-
* The provided metadata is spared-merged with any existing metadata.
|
|
104
|
-
*
|
|
105
|
-
* @see {@link https://orpc.unnoq.com/docs/metadata Metadata Docs}
|
|
106
|
-
*/
|
|
107
|
-
meta(meta) {
|
|
108
|
-
return new DecoratedProcedure({
|
|
109
|
-
...this["~orpc"],
|
|
110
|
-
meta: mergeMeta(this["~orpc"].meta, meta)
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
/**
|
|
114
|
-
* Sets or updates the route definition.
|
|
115
|
-
* The provided route is spared-merged with any existing route.
|
|
116
|
-
* This option is typically relevant when integrating with OpenAPI.
|
|
117
|
-
*
|
|
118
|
-
* @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
|
|
119
|
-
* @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
|
|
120
|
-
*/
|
|
121
|
-
route(route) {
|
|
122
|
-
return new DecoratedProcedure({
|
|
123
|
-
...this["~orpc"],
|
|
124
|
-
route: mergeRoute(this["~orpc"].route, route)
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
use(middleware, mapInput) {
|
|
128
|
-
const mapped = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
|
|
129
|
-
return new DecoratedProcedure({
|
|
130
|
-
...this["~orpc"],
|
|
131
|
-
middlewares: addMiddleware(this["~orpc"].middlewares, mapped)
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
79
|
/**
|
|
135
80
|
* Make this procedure callable (works like a function while still being a procedure).
|
|
136
81
|
*
|
|
@@ -149,244 +94,76 @@ class DecoratedProcedure extends Procedure {
|
|
|
149
94
|
}
|
|
150
95
|
}
|
|
151
96
|
|
|
152
|
-
const
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
159
|
-
return Reflect.get(target, key);
|
|
160
|
-
}
|
|
161
|
-
});
|
|
162
|
-
}
|
|
163
|
-
function getHiddenRouterContract(router) {
|
|
164
|
-
return router[HIDDEN_ROUTER_CONTRACT_SYMBOL];
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
function getRouter(router, path) {
|
|
168
|
-
let current = router;
|
|
169
|
-
for (let i = 0; i < path.length; i++) {
|
|
170
|
-
const segment = path[i];
|
|
171
|
-
if (!current) {
|
|
172
|
-
return void 0;
|
|
173
|
-
}
|
|
174
|
-
if (isProcedure(current)) {
|
|
175
|
-
return void 0;
|
|
176
|
-
}
|
|
177
|
-
if (!isLazy(current)) {
|
|
178
|
-
current = current[segment];
|
|
179
|
-
continue;
|
|
180
|
-
}
|
|
181
|
-
const lazied = current;
|
|
182
|
-
const rest = path.slice(i);
|
|
183
|
-
return lazy(async () => {
|
|
184
|
-
const unwrapped = await unlazy(lazied);
|
|
185
|
-
const next = getRouter(unwrapped.default, rest);
|
|
186
|
-
return unlazy(next);
|
|
187
|
-
}, getLazyMeta(lazied));
|
|
188
|
-
}
|
|
189
|
-
return current;
|
|
190
|
-
}
|
|
191
|
-
function createAccessibleLazyRouter(lazied) {
|
|
192
|
-
const recursive = new Proxy(lazied, {
|
|
193
|
-
get(target, key) {
|
|
194
|
-
if (typeof key !== "string") {
|
|
195
|
-
return Reflect.get(target, key);
|
|
196
|
-
}
|
|
197
|
-
const next = getRouter(lazied, [key]);
|
|
198
|
-
return createAccessibleLazyRouter(next);
|
|
199
|
-
}
|
|
200
|
-
});
|
|
201
|
-
return recursive;
|
|
202
|
-
}
|
|
203
|
-
function enhanceRouter(router, options) {
|
|
204
|
-
if (isLazy(router)) {
|
|
205
|
-
const laziedMeta = getLazyMeta(router);
|
|
206
|
-
const enhancedPrefix = laziedMeta?.prefix ? mergePrefix(options.prefix, laziedMeta?.prefix) : options.prefix;
|
|
207
|
-
const enhanced2 = lazy(
|
|
208
|
-
async () => {
|
|
209
|
-
const { default: unlaziedRouter } = await unlazy(router);
|
|
210
|
-
const enhanced3 = enhanceRouter(unlaziedRouter, options);
|
|
211
|
-
return unlazy(enhanced3);
|
|
212
|
-
},
|
|
213
|
-
{
|
|
214
|
-
...laziedMeta,
|
|
215
|
-
prefix: enhancedPrefix
|
|
216
|
-
}
|
|
217
|
-
);
|
|
218
|
-
const accessible = createAccessibleLazyRouter(enhanced2);
|
|
219
|
-
return accessible;
|
|
220
|
-
}
|
|
221
|
-
if (isProcedure(router)) {
|
|
222
|
-
const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares, {
|
|
223
|
-
dedupeLeading: options.dedupeLeadingMiddlewares
|
|
224
|
-
});
|
|
225
|
-
const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
|
|
226
|
-
const enhanced2 = new Procedure({
|
|
227
|
-
...router["~orpc"],
|
|
228
|
-
route: enhanceRoute(router["~orpc"].route, options),
|
|
229
|
-
errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
|
|
230
|
-
middlewares: newMiddlewares,
|
|
231
|
-
inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
|
|
232
|
-
outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
|
|
233
|
-
});
|
|
234
|
-
return enhanced2;
|
|
235
|
-
}
|
|
236
|
-
const enhanced = {};
|
|
237
|
-
for (const key in router) {
|
|
238
|
-
enhanced[key] = enhanceRouter(router[key], options);
|
|
239
|
-
}
|
|
240
|
-
return enhanced;
|
|
241
|
-
}
|
|
242
|
-
function traverseContractProcedures(options, callback, lazyOptions = []) {
|
|
243
|
-
let currentRouter = options.router;
|
|
244
|
-
const hiddenContract = getHiddenRouterContract(options.router);
|
|
245
|
-
if (hiddenContract !== void 0) {
|
|
246
|
-
currentRouter = hiddenContract;
|
|
247
|
-
}
|
|
248
|
-
if (isLazy(currentRouter)) {
|
|
249
|
-
lazyOptions.push({
|
|
250
|
-
router: currentRouter,
|
|
251
|
-
path: options.path
|
|
252
|
-
});
|
|
253
|
-
} else if (isContractProcedure(currentRouter)) {
|
|
254
|
-
callback({
|
|
255
|
-
contract: currentRouter,
|
|
256
|
-
path: options.path
|
|
257
|
-
});
|
|
258
|
-
} else {
|
|
259
|
-
for (const key in currentRouter) {
|
|
260
|
-
traverseContractProcedures(
|
|
261
|
-
{
|
|
262
|
-
router: currentRouter[key],
|
|
263
|
-
path: [...options.path, key]
|
|
264
|
-
},
|
|
265
|
-
callback,
|
|
266
|
-
lazyOptions
|
|
267
|
-
);
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
return lazyOptions;
|
|
271
|
-
}
|
|
272
|
-
async function resolveContractProcedures(options, callback) {
|
|
273
|
-
const pending = [options];
|
|
274
|
-
for (const options2 of pending) {
|
|
275
|
-
const lazyOptions = traverseContractProcedures(options2, callback);
|
|
276
|
-
for (const options3 of lazyOptions) {
|
|
277
|
-
const { default: router } = await unlazy(options3.router);
|
|
278
|
-
pending.push({
|
|
279
|
-
router,
|
|
280
|
-
path: options3.path
|
|
281
|
-
});
|
|
282
|
-
}
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
async function unlazyRouter(router) {
|
|
286
|
-
if (isProcedure(router)) {
|
|
287
|
-
return router;
|
|
288
|
-
}
|
|
289
|
-
const unlazied = {};
|
|
290
|
-
for (const key in router) {
|
|
291
|
-
const item = router[key];
|
|
292
|
-
const { default: unlaziedRouter } = await unlazy(item);
|
|
293
|
-
unlazied[key] = await unlazyRouter(unlaziedRouter);
|
|
294
|
-
}
|
|
295
|
-
return unlazied;
|
|
296
|
-
}
|
|
97
|
+
const initialSchemas = {
|
|
98
|
+
pathSchema: z.strictObject({}),
|
|
99
|
+
querySchema: z.strictObject({}),
|
|
100
|
+
bodySchema: z.strictObject({}),
|
|
101
|
+
outputSchema: z.unknown()
|
|
102
|
+
};
|
|
297
103
|
|
|
298
|
-
class
|
|
299
|
-
|
|
300
|
-
* This property holds the defined options.
|
|
301
|
-
*/
|
|
302
|
-
"~orpc";
|
|
104
|
+
class ProcedureBuilder extends Contract {
|
|
105
|
+
z;
|
|
303
106
|
constructor(def) {
|
|
304
|
-
|
|
107
|
+
super(def);
|
|
108
|
+
this.z = new SchemaClass();
|
|
305
109
|
}
|
|
306
110
|
/**
|
|
307
|
-
*
|
|
308
|
-
*
|
|
309
|
-
* @see {@link https://orpc.unnoq.com/docs/context Context Docs}
|
|
310
|
-
*/
|
|
311
|
-
$context() {
|
|
312
|
-
return this;
|
|
313
|
-
}
|
|
314
|
-
/**
|
|
315
|
-
* Creates a middleware.
|
|
316
|
-
*
|
|
317
|
-
* @see {@link https://orpc.unnoq.com/docs/middleware Middleware Docs}
|
|
318
|
-
*/
|
|
319
|
-
middleware(middleware) {
|
|
320
|
-
return decorateMiddleware(middleware);
|
|
321
|
-
}
|
|
322
|
-
/**
|
|
323
|
-
* Adds type-safe custom errors.
|
|
324
|
-
* The provided errors are spared-merged with any existing errors.
|
|
325
|
-
*
|
|
326
|
-
* @see {@link https://orpc.unnoq.com/docs/error-handling#type%E2%80%90safe-error-handling Type-Safe Error Handling Docs}
|
|
327
|
-
*/
|
|
328
|
-
errors(errors) {
|
|
329
|
-
return new Builder({
|
|
330
|
-
...this["~orpc"],
|
|
331
|
-
errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
|
|
332
|
-
});
|
|
333
|
-
}
|
|
334
|
-
/**
|
|
335
|
-
* Uses a middleware to modify the context or improve the pipeline.
|
|
336
|
-
*
|
|
337
|
-
* @info Supports both normal middleware and inline middleware implementations.
|
|
338
|
-
* @note The current context must be satisfy middleware dependent-context
|
|
339
|
-
* @see {@link https://orpc.unnoq.com/docs/middleware Middleware Docs}
|
|
340
|
-
*/
|
|
341
|
-
use(middleware) {
|
|
342
|
-
return new Builder({
|
|
343
|
-
...this["~orpc"],
|
|
344
|
-
middlewares: addMiddleware(this["~orpc"].middlewares, middleware)
|
|
345
|
-
});
|
|
346
|
-
}
|
|
347
|
-
useGating(gates, isGateEnabled) {
|
|
348
|
-
return this.use(({ next, context }) => {
|
|
349
|
-
return gatingContext.run(
|
|
350
|
-
(gate) => isGateEnabled(gate, context),
|
|
351
|
-
() => next({ context: { isGateEnabled } })
|
|
352
|
-
);
|
|
353
|
-
});
|
|
354
|
-
}
|
|
355
|
-
/**
|
|
356
|
-
* Sets or updates the metadata.
|
|
357
|
-
* The provided metadata is spared-merged with any existing metadata.
|
|
111
|
+
* Sets or updates the metadata for the contract.
|
|
112
|
+
* The provided metadata is spared-merged with any existing metadata in the contract.
|
|
358
113
|
*
|
|
359
114
|
* @see {@link https://orpc.unnoq.com/docs/metadata Metadata Docs}
|
|
360
115
|
*/
|
|
361
116
|
meta(meta) {
|
|
362
|
-
return new
|
|
117
|
+
return new ProcedureBuilder({
|
|
363
118
|
...this["~orpc"],
|
|
364
119
|
meta: mergeMeta(this["~orpc"].meta, meta)
|
|
365
120
|
});
|
|
366
121
|
}
|
|
122
|
+
endpoint(stringsOrEndpoint, ...values) {
|
|
123
|
+
const { method, path, pathSchema } = parseEndpointDefinition(stringsOrEndpoint, values);
|
|
124
|
+
const { prefix } = this["~orpc"];
|
|
125
|
+
const route = { method, path };
|
|
126
|
+
return new ProcedureBuilder({
|
|
127
|
+
...this["~orpc"],
|
|
128
|
+
route: mergeRoute(this["~orpc"].route, prefix ? prefixRoute(route, prefix) : route),
|
|
129
|
+
schemas: {
|
|
130
|
+
...this["~orpc"].schemas,
|
|
131
|
+
pathSchema
|
|
132
|
+
}
|
|
133
|
+
});
|
|
134
|
+
}
|
|
367
135
|
/**
|
|
368
|
-
* Sets or updates the route definition.
|
|
369
|
-
* The provided route is spared-merged with any existing route.
|
|
136
|
+
* Sets or updates the route definition for the contract.
|
|
137
|
+
* The provided route is spared-merged with any existing route in the contract.
|
|
370
138
|
* This option is typically relevant when integrating with OpenAPI.
|
|
371
139
|
*
|
|
372
140
|
* @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
|
|
373
141
|
* @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
|
|
374
142
|
*/
|
|
375
|
-
|
|
376
|
-
|
|
143
|
+
endpointOpts(route) {
|
|
144
|
+
const { prefix } = this["~orpc"];
|
|
145
|
+
return new ProcedureBuilder({
|
|
377
146
|
...this["~orpc"],
|
|
378
|
-
route: mergeRoute(this["~orpc"].route, route)
|
|
147
|
+
route: mergeRoute(this["~orpc"].route, prefix ? prefixRoute(route, prefix) : route)
|
|
379
148
|
});
|
|
380
149
|
}
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
*
|
|
384
|
-
* @see {@link https://orpc.unnoq.com/docs/procedure#input-output-validation Input Validation Docs}
|
|
385
|
-
*/
|
|
386
|
-
input(schema) {
|
|
387
|
-
return new Builder({
|
|
150
|
+
query(schema) {
|
|
151
|
+
return new ProcedureBuilder({
|
|
388
152
|
...this["~orpc"],
|
|
389
|
-
|
|
153
|
+
schemas: {
|
|
154
|
+
...this["~orpc"].schemas,
|
|
155
|
+
querySchema: schema instanceof core.$ZodType ? schema : object(schema)
|
|
156
|
+
},
|
|
157
|
+
inputValidationIndex: this["~orpc"].middlewares.length
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
body(schema) {
|
|
161
|
+
return new ProcedureBuilder({
|
|
162
|
+
...this["~orpc"],
|
|
163
|
+
schemas: {
|
|
164
|
+
...this["~orpc"].schemas,
|
|
165
|
+
bodySchema: schema instanceof core.$ZodType ? schema : object(schema)
|
|
166
|
+
},
|
|
390
167
|
inputValidationIndex: this["~orpc"].middlewares.length
|
|
391
168
|
});
|
|
392
169
|
}
|
|
@@ -396,23 +173,79 @@ class Builder {
|
|
|
396
173
|
* @see {@link https://orpc.unnoq.com/docs/procedure#input-output-validation Output Validation Docs}
|
|
397
174
|
*/
|
|
398
175
|
output(schema) {
|
|
399
|
-
return new
|
|
176
|
+
return new ProcedureBuilder({
|
|
400
177
|
...this["~orpc"],
|
|
401
|
-
|
|
178
|
+
schemas: {
|
|
179
|
+
...this["~orpc"].schemas,
|
|
180
|
+
outputSchema: schema instanceof core.$ZodType ? schema : object(schema)
|
|
181
|
+
},
|
|
402
182
|
outputValidationIndex: this["~orpc"].middlewares.length
|
|
403
183
|
});
|
|
404
184
|
}
|
|
185
|
+
use(middleware) {
|
|
186
|
+
return new this.constructor({
|
|
187
|
+
...this["~orpc"],
|
|
188
|
+
middlewares: addMiddleware(this["~orpc"].middlewares, middleware)
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
useErrors(errorMap) {
|
|
192
|
+
return new this.constructor({
|
|
193
|
+
...this["~orpc"],
|
|
194
|
+
errorMap: { ...this["~orpc"].errorMap, ...errorMap }
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
// The default of false for ValidatedAuthContext is used when you pass in the type 'none'. We use false
|
|
198
|
+
// because we can't use null or undefined (see ValidatedAuthContext) but we still want it to be falsy.
|
|
199
|
+
useAuth(config) {
|
|
200
|
+
const middleware = baseApi.$context().middleware(async (options) => {
|
|
201
|
+
const { next, context } = options;
|
|
202
|
+
if (context.auth) return next();
|
|
203
|
+
const auth = await runAuth(config, options);
|
|
204
|
+
if (auth === void 0) {
|
|
205
|
+
const { authConfigs } = options.procedure["~orpc"];
|
|
206
|
+
if (context.auth !== false && config === authConfigs.at(-1)) {
|
|
207
|
+
let authDescriptions = authConfigs.map(authDescription).join(", ");
|
|
208
|
+
if (authConfigs.length > 1) {
|
|
209
|
+
authDescriptions = `one of: ${authDescriptions}`;
|
|
210
|
+
}
|
|
211
|
+
throw new UnauthorizedError({
|
|
212
|
+
message: `Authentication required. You must provide ${authDescriptions}`
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
return next();
|
|
216
|
+
}
|
|
217
|
+
return next({ context: { auth } });
|
|
218
|
+
});
|
|
219
|
+
return new this.constructor({
|
|
220
|
+
...this["~orpc"],
|
|
221
|
+
authConfigs: [...this["~orpc"].authConfigs, config],
|
|
222
|
+
middlewares: addMiddleware(this["~orpc"].middlewares, middleware)
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
useGating(gates, isGateEnabled) {
|
|
226
|
+
return this.use(({ next, context }) => {
|
|
227
|
+
return gatingContext.run(
|
|
228
|
+
(gate) => isGateEnabled(gate, context),
|
|
229
|
+
() => next({ context: { isGateEnabled } })
|
|
230
|
+
);
|
|
231
|
+
});
|
|
232
|
+
}
|
|
405
233
|
/**
|
|
406
234
|
* Defines the handler of the procedure.
|
|
407
235
|
*
|
|
408
236
|
* @see {@link https://orpc.unnoq.com/docs/procedure Procedure Docs}
|
|
409
237
|
*/
|
|
410
238
|
handler(handler) {
|
|
239
|
+
if (this["~orpc"].schemas.outputSchema === initialSchemas.outputSchema) {
|
|
240
|
+
throw new Error("You must call .output() before calling .handler()");
|
|
241
|
+
}
|
|
411
242
|
return new DecoratedProcedure({
|
|
412
243
|
...this["~orpc"],
|
|
413
244
|
handler
|
|
414
245
|
});
|
|
415
246
|
}
|
|
247
|
+
}
|
|
248
|
+
class BuilderWithMiddlewares extends ProcedureBuilder {
|
|
416
249
|
/**
|
|
417
250
|
* Prefixes all procedures in the router.
|
|
418
251
|
* The provided prefix is post-appended to any existing router prefix.
|
|
@@ -422,7 +255,7 @@ class Builder {
|
|
|
422
255
|
* @see {@link https://orpc.unnoq.com/docs/openapi/routing#route-prefixes OpenAPI Route Prefixes Docs}
|
|
423
256
|
*/
|
|
424
257
|
prefix(prefix) {
|
|
425
|
-
return new
|
|
258
|
+
return new this.constructor({
|
|
426
259
|
...this["~orpc"],
|
|
427
260
|
prefix: mergePrefix(this["~orpc"].prefix, prefix)
|
|
428
261
|
});
|
|
@@ -434,7 +267,7 @@ class Builder {
|
|
|
434
267
|
* @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
|
|
435
268
|
*/
|
|
436
269
|
tag(...tags) {
|
|
437
|
-
return new
|
|
270
|
+
return new this.constructor({
|
|
438
271
|
...this["~orpc"],
|
|
439
272
|
tags: mergeTags(this["~orpc"].tags, tags)
|
|
440
273
|
});
|
|
@@ -447,123 +280,383 @@ class Builder {
|
|
|
447
280
|
router(router) {
|
|
448
281
|
return enhanceRouter(router, this["~orpc"]);
|
|
449
282
|
}
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function decorateMiddleware(middleware) {
|
|
286
|
+
const decorated = ((...args) => middleware(...args));
|
|
287
|
+
decorated.mapInput = (mapInput) => {
|
|
288
|
+
const mapped = decorateMiddleware(
|
|
289
|
+
(options, input, ...rest) => middleware(options, mapInput(input), ...rest)
|
|
290
|
+
);
|
|
291
|
+
return mapped;
|
|
292
|
+
};
|
|
293
|
+
return decorated;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
class Builder extends BuilderWithMiddlewares {
|
|
450
297
|
/**
|
|
451
|
-
*
|
|
452
|
-
* And applies all of the previously defined options to the specified router.
|
|
298
|
+
* Set or override the initial context.
|
|
453
299
|
*
|
|
454
|
-
* @see {@link https://orpc.unnoq.com/docs/
|
|
300
|
+
* @see {@link https://orpc.unnoq.com/docs/context Context Docs}
|
|
455
301
|
*/
|
|
456
|
-
|
|
457
|
-
return
|
|
302
|
+
$context() {
|
|
303
|
+
return this;
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Creates a middleware.
|
|
307
|
+
*
|
|
308
|
+
* @see {@link https://orpc.unnoq.com/docs/middleware Middleware Docs}
|
|
309
|
+
*/
|
|
310
|
+
middleware(middleware) {
|
|
311
|
+
return decorateMiddleware(middleware);
|
|
458
312
|
}
|
|
459
313
|
}
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
}
|
|
478
|
-
const os = createApiBuilder();
|
|
314
|
+
const baseApi = new Builder({
|
|
315
|
+
route: {},
|
|
316
|
+
meta: {},
|
|
317
|
+
errorMap: { BadRequestError, UnauthorizedError, ForbiddenError, NotFoundError, InternalServerError },
|
|
318
|
+
inputValidationIndex: 0,
|
|
319
|
+
outputValidationIndex: 0,
|
|
320
|
+
middlewares: [
|
|
321
|
+
onError((error, _options) => {
|
|
322
|
+
console.dir(error, { depth: null });
|
|
323
|
+
})
|
|
324
|
+
],
|
|
325
|
+
schemas: initialSchemas,
|
|
326
|
+
// NB: this is a relic from orpc -- I'm not convinced there's a need for this (or if there is, that it's
|
|
327
|
+
// the best solution). For now I've removed the interface to configure it externally.
|
|
328
|
+
dedupeLeadingMiddlewares: true,
|
|
329
|
+
authConfigs: []
|
|
330
|
+
});
|
|
479
331
|
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
errorMap: {},
|
|
508
|
-
prefix: void 0,
|
|
509
|
-
tags: void 0,
|
|
510
|
-
dedupeLeadingMiddlewares: true
|
|
511
|
-
});
|
|
512
|
-
return setHiddenRouterContract(adapted, contract);
|
|
513
|
-
};
|
|
514
|
-
} else if (key === "lazyRoute") {
|
|
515
|
-
method = (loader) => {
|
|
516
|
-
const adapted = enhanceRouter(lazy(loader), {
|
|
517
|
-
middlewares,
|
|
518
|
-
errorMap: {},
|
|
519
|
-
prefix: void 0,
|
|
520
|
-
tags: void 0,
|
|
521
|
-
dedupeLeadingMiddlewares: true
|
|
522
|
-
});
|
|
523
|
-
return setHiddenRouterContract(adapted, contract);
|
|
524
|
-
};
|
|
332
|
+
const FLEXIBLE_DATE_FORMAT_REGEX = /^[^-]+-[^-]+-[^-]+$/;
|
|
333
|
+
class experimental_JsonSchemaCoercer {
|
|
334
|
+
coerce(schema, value, options = {}) {
|
|
335
|
+
const [, coerced] = this.#coerce(schema, value, options);
|
|
336
|
+
return coerced;
|
|
337
|
+
}
|
|
338
|
+
#coerce(schema, originalValue, options) {
|
|
339
|
+
if (typeof schema === "boolean") {
|
|
340
|
+
return [schema, originalValue];
|
|
341
|
+
}
|
|
342
|
+
if (Array.isArray(schema.type)) {
|
|
343
|
+
return this.#coerce(
|
|
344
|
+
{
|
|
345
|
+
anyOf: schema.type.map((type) => ({ ...schema, type }))
|
|
346
|
+
},
|
|
347
|
+
originalValue,
|
|
348
|
+
options
|
|
349
|
+
);
|
|
350
|
+
}
|
|
351
|
+
let coerced = originalValue;
|
|
352
|
+
let satisfied = true;
|
|
353
|
+
if (typeof schema.$ref === "string") {
|
|
354
|
+
const refSchema = options?.components?.[schema.$ref];
|
|
355
|
+
if (refSchema !== void 0) {
|
|
356
|
+
const [subSatisfied, subCoerced] = this.#coerce(refSchema, coerced, options);
|
|
357
|
+
coerced = subCoerced;
|
|
358
|
+
satisfied = subSatisfied;
|
|
525
359
|
}
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
360
|
+
}
|
|
361
|
+
const enumValues = schema.const !== void 0 ? [schema.const] : schema.enum;
|
|
362
|
+
if (enumValues !== void 0 && !enumValues.includes(coerced)) {
|
|
363
|
+
if (typeof coerced === "string") {
|
|
364
|
+
const numberValue = this.#stringToNumber(coerced);
|
|
365
|
+
if (enumValues.includes(numberValue)) {
|
|
366
|
+
coerced = numberValue;
|
|
367
|
+
} else {
|
|
368
|
+
const booleanValue = this.#stringToBoolean(coerced);
|
|
369
|
+
if (enumValues.includes(booleanValue)) {
|
|
370
|
+
coerced = booleanValue;
|
|
371
|
+
} else {
|
|
372
|
+
satisfied = false;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
} else {
|
|
376
|
+
satisfied = false;
|
|
529
377
|
}
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
378
|
+
}
|
|
379
|
+
if (typeof schema.type === "string") {
|
|
380
|
+
switch (schema.type) {
|
|
381
|
+
case "null": {
|
|
382
|
+
if (coerced !== null) {
|
|
383
|
+
satisfied = false;
|
|
384
|
+
}
|
|
385
|
+
break;
|
|
386
|
+
}
|
|
387
|
+
case "string": {
|
|
388
|
+
if (typeof coerced !== "string") {
|
|
389
|
+
satisfied = false;
|
|
535
390
|
}
|
|
536
|
-
|
|
391
|
+
break;
|
|
392
|
+
}
|
|
393
|
+
case "number": {
|
|
394
|
+
if (typeof coerced === "string") {
|
|
395
|
+
coerced = this.#stringToNumber(coerced);
|
|
396
|
+
}
|
|
397
|
+
if (typeof coerced !== "number") {
|
|
398
|
+
satisfied = false;
|
|
399
|
+
}
|
|
400
|
+
break;
|
|
401
|
+
}
|
|
402
|
+
case "integer": {
|
|
403
|
+
if (typeof coerced === "string") {
|
|
404
|
+
coerced = this.#stringToInteger(coerced);
|
|
405
|
+
}
|
|
406
|
+
if (typeof coerced !== "number" || !Number.isInteger(coerced)) {
|
|
407
|
+
satisfied = false;
|
|
408
|
+
}
|
|
409
|
+
break;
|
|
410
|
+
}
|
|
411
|
+
case "boolean": {
|
|
412
|
+
if (typeof coerced === "string") {
|
|
413
|
+
coerced = this.#stringToBoolean(coerced);
|
|
414
|
+
}
|
|
415
|
+
if (typeof coerced !== "boolean") {
|
|
416
|
+
satisfied = false;
|
|
417
|
+
}
|
|
418
|
+
break;
|
|
419
|
+
}
|
|
420
|
+
case "array": {
|
|
421
|
+
if (Array.isArray(coerced)) {
|
|
422
|
+
const prefixItemSchemas = "prefixItems" in schema ? toArray(schema.prefixItems) : Array.isArray(schema.items) ? schema.items : [];
|
|
423
|
+
const itemSchema = Array.isArray(schema.items) ? schema.additionalItems : schema.items;
|
|
424
|
+
let shouldUseCoercedItems = false;
|
|
425
|
+
const coercedItems = coerced.map((item, i) => {
|
|
426
|
+
const subSchema = prefixItemSchemas[i] ?? itemSchema;
|
|
427
|
+
if (subSchema === void 0) {
|
|
428
|
+
satisfied = false;
|
|
429
|
+
return item;
|
|
430
|
+
}
|
|
431
|
+
const [subSatisfied, subCoerced] = this.#coerce(subSchema, item, options);
|
|
432
|
+
if (!subSatisfied) {
|
|
433
|
+
satisfied = false;
|
|
434
|
+
}
|
|
435
|
+
if (subCoerced !== item) {
|
|
436
|
+
shouldUseCoercedItems = true;
|
|
437
|
+
}
|
|
438
|
+
return subCoerced;
|
|
439
|
+
});
|
|
440
|
+
if (coercedItems.length < prefixItemSchemas.length) {
|
|
441
|
+
satisfied = false;
|
|
442
|
+
}
|
|
443
|
+
if (shouldUseCoercedItems) {
|
|
444
|
+
coerced = coercedItems;
|
|
445
|
+
}
|
|
446
|
+
} else {
|
|
447
|
+
satisfied = false;
|
|
448
|
+
}
|
|
449
|
+
break;
|
|
450
|
+
}
|
|
451
|
+
case "object": {
|
|
452
|
+
if (Array.isArray(coerced)) {
|
|
453
|
+
coerced = { ...coerced };
|
|
454
|
+
}
|
|
455
|
+
if (isObject(coerced)) {
|
|
456
|
+
let shouldUseCoercedItems = false;
|
|
457
|
+
const coercedItems = {};
|
|
458
|
+
const patternProperties = Object.entries(schema.patternProperties ?? {}).map(
|
|
459
|
+
([key, value]) => [new RegExp(key), value]
|
|
460
|
+
);
|
|
461
|
+
for (const key in coerced) {
|
|
462
|
+
const value = coerced[key];
|
|
463
|
+
const subSchema = schema.properties?.[key] ?? patternProperties.find(([pattern]) => pattern.test(key))?.[1] ?? schema.additionalProperties;
|
|
464
|
+
if (value === void 0 && !schema.required?.includes(key)) {
|
|
465
|
+
coercedItems[key] = value;
|
|
466
|
+
} else if (subSchema === void 0) {
|
|
467
|
+
coercedItems[key] = value;
|
|
468
|
+
satisfied = false;
|
|
469
|
+
} else {
|
|
470
|
+
const [subSatisfied, subCoerced] = this.#coerce(subSchema, value, options);
|
|
471
|
+
coercedItems[key] = subCoerced;
|
|
472
|
+
if (!subSatisfied) {
|
|
473
|
+
satisfied = false;
|
|
474
|
+
}
|
|
475
|
+
if (subCoerced !== value) {
|
|
476
|
+
shouldUseCoercedItems = true;
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
}
|
|
480
|
+
if (schema.required?.some((key) => !Object.hasOwn(coercedItems, key))) {
|
|
481
|
+
satisfied = false;
|
|
482
|
+
}
|
|
483
|
+
if (shouldUseCoercedItems) {
|
|
484
|
+
coerced = coercedItems;
|
|
485
|
+
}
|
|
486
|
+
} else {
|
|
487
|
+
satisfied = false;
|
|
488
|
+
}
|
|
489
|
+
break;
|
|
490
|
+
}
|
|
537
491
|
}
|
|
538
|
-
return nextImpl;
|
|
539
492
|
}
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
493
|
+
if ("x-native-type" in schema && typeof schema["x-native-type"] === "string") {
|
|
494
|
+
switch (schema["x-native-type"]) {
|
|
495
|
+
case JsonSchemaXNativeType.Date: {
|
|
496
|
+
if (typeof coerced === "string") {
|
|
497
|
+
coerced = this.#stringToDate(coerced);
|
|
498
|
+
}
|
|
499
|
+
if (!(coerced instanceof Date)) {
|
|
500
|
+
satisfied = false;
|
|
501
|
+
}
|
|
502
|
+
break;
|
|
503
|
+
}
|
|
504
|
+
case JsonSchemaXNativeType.BigInt: {
|
|
505
|
+
switch (typeof coerced) {
|
|
506
|
+
case "string":
|
|
507
|
+
coerced = this.#stringToBigInt(coerced);
|
|
508
|
+
break;
|
|
509
|
+
case "number":
|
|
510
|
+
coerced = this.#numberToBigInt(coerced);
|
|
511
|
+
break;
|
|
512
|
+
}
|
|
513
|
+
if (typeof coerced !== "bigint") {
|
|
514
|
+
satisfied = false;
|
|
515
|
+
}
|
|
516
|
+
break;
|
|
517
|
+
}
|
|
518
|
+
case JsonSchemaXNativeType.RegExp: {
|
|
519
|
+
if (typeof coerced === "string") {
|
|
520
|
+
coerced = this.#stringToRegExp(coerced);
|
|
521
|
+
}
|
|
522
|
+
if (!(coerced instanceof RegExp)) {
|
|
523
|
+
satisfied = false;
|
|
524
|
+
}
|
|
525
|
+
break;
|
|
526
|
+
}
|
|
527
|
+
case JsonSchemaXNativeType.Url: {
|
|
528
|
+
if (typeof coerced === "string") {
|
|
529
|
+
coerced = this.#stringToURL(coerced);
|
|
530
|
+
}
|
|
531
|
+
if (!(coerced instanceof URL)) {
|
|
532
|
+
satisfied = false;
|
|
533
|
+
}
|
|
534
|
+
break;
|
|
535
|
+
}
|
|
536
|
+
case JsonSchemaXNativeType.Set: {
|
|
537
|
+
if (Array.isArray(coerced)) {
|
|
538
|
+
coerced = this.#arrayToSet(coerced);
|
|
539
|
+
}
|
|
540
|
+
if (!(coerced instanceof Set)) {
|
|
541
|
+
satisfied = false;
|
|
542
|
+
}
|
|
543
|
+
break;
|
|
544
|
+
}
|
|
545
|
+
case JsonSchemaXNativeType.Map: {
|
|
546
|
+
if (Array.isArray(coerced)) {
|
|
547
|
+
coerced = this.#arrayToMap(coerced);
|
|
548
|
+
}
|
|
549
|
+
if (!(coerced instanceof Map)) {
|
|
550
|
+
satisfied = false;
|
|
551
|
+
}
|
|
552
|
+
break;
|
|
553
|
+
}
|
|
550
554
|
}
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
555
|
+
}
|
|
556
|
+
if (schema.allOf) {
|
|
557
|
+
for (const subSchema of schema.allOf) {
|
|
558
|
+
const [subSatisfied, subCoerced] = this.#coerce(subSchema, coerced, options);
|
|
559
|
+
coerced = subCoerced;
|
|
560
|
+
if (!subSatisfied) {
|
|
561
|
+
satisfied = false;
|
|
562
|
+
}
|
|
554
563
|
}
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
564
|
+
}
|
|
565
|
+
for (const key of ["anyOf", "oneOf"]) {
|
|
566
|
+
if (schema[key]) {
|
|
567
|
+
let bestOptions;
|
|
568
|
+
for (const subSchema of schema[key]) {
|
|
569
|
+
const [subSatisfied, subCoerced] = this.#coerce(subSchema, coerced, options);
|
|
570
|
+
if (subSatisfied) {
|
|
571
|
+
if (!bestOptions || subCoerced === coerced) {
|
|
572
|
+
bestOptions = { coerced: subCoerced, satisfied: subSatisfied };
|
|
573
|
+
}
|
|
574
|
+
if (subCoerced === coerced) {
|
|
575
|
+
break;
|
|
576
|
+
}
|
|
577
|
+
}
|
|
558
578
|
}
|
|
559
|
-
|
|
579
|
+
coerced = bestOptions ? bestOptions.coerced : coerced;
|
|
580
|
+
satisfied = bestOptions ? bestOptions.satisfied : false;
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
if (typeof schema.not !== "undefined") {
|
|
584
|
+
const [notSatisfied] = this.#coerce(schema.not, coerced, options);
|
|
585
|
+
if (notSatisfied) {
|
|
586
|
+
satisfied = false;
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
return [satisfied, coerced];
|
|
590
|
+
}
|
|
591
|
+
#stringToNumber(value) {
|
|
592
|
+
const num = Number.parseFloat(value);
|
|
593
|
+
if (Number.isNaN(num) || num !== Number(value)) {
|
|
594
|
+
return value;
|
|
595
|
+
}
|
|
596
|
+
return num;
|
|
597
|
+
}
|
|
598
|
+
#stringToInteger(value) {
|
|
599
|
+
const num = Number.parseInt(value);
|
|
600
|
+
if (Number.isNaN(num) || num !== Number(value)) {
|
|
601
|
+
return value;
|
|
602
|
+
}
|
|
603
|
+
return num;
|
|
604
|
+
}
|
|
605
|
+
#stringToBoolean(value) {
|
|
606
|
+
const lower = value.toLowerCase();
|
|
607
|
+
if (lower === "false" || lower === "off") {
|
|
608
|
+
return false;
|
|
609
|
+
}
|
|
610
|
+
if (lower === "true" || lower === "on") {
|
|
611
|
+
return true;
|
|
612
|
+
}
|
|
613
|
+
return value;
|
|
614
|
+
}
|
|
615
|
+
#stringToBigInt(value) {
|
|
616
|
+
return guard(() => BigInt(value)) ?? value;
|
|
617
|
+
}
|
|
618
|
+
#numberToBigInt(value) {
|
|
619
|
+
return guard(() => BigInt(value)) ?? value;
|
|
620
|
+
}
|
|
621
|
+
#stringToDate(value) {
|
|
622
|
+
const date = new Date(value);
|
|
623
|
+
if (Number.isNaN(date.getTime()) || !FLEXIBLE_DATE_FORMAT_REGEX.test(value)) {
|
|
624
|
+
return value;
|
|
625
|
+
}
|
|
626
|
+
return date;
|
|
627
|
+
}
|
|
628
|
+
#stringToRegExp(value) {
|
|
629
|
+
const match = value.match(/^\/(.*)\/([a-z]*)$/);
|
|
630
|
+
if (match) {
|
|
631
|
+
const [, pattern, flags] = match;
|
|
632
|
+
return guard(() => new RegExp(pattern, flags)) ?? value;
|
|
560
633
|
}
|
|
561
|
-
|
|
562
|
-
|
|
634
|
+
return value;
|
|
635
|
+
}
|
|
636
|
+
#stringToURL(value) {
|
|
637
|
+
return guard(() => new URL(value)) ?? value;
|
|
638
|
+
}
|
|
639
|
+
#arrayToSet(value) {
|
|
640
|
+
const set = new Set(value);
|
|
641
|
+
if (set.size !== value.length) {
|
|
642
|
+
return value;
|
|
643
|
+
}
|
|
644
|
+
return set;
|
|
645
|
+
}
|
|
646
|
+
#arrayToMap(value) {
|
|
647
|
+
if (value.some((item) => !Array.isArray(item) || item.length !== 2)) {
|
|
648
|
+
return value;
|
|
649
|
+
}
|
|
650
|
+
const result = new Map(value);
|
|
651
|
+
if (result.size !== value.length) {
|
|
652
|
+
return value;
|
|
653
|
+
}
|
|
654
|
+
return result;
|
|
655
|
+
}
|
|
563
656
|
}
|
|
564
657
|
|
|
565
658
|
function createAssertedLazyProcedure(lazied) {
|
|
566
|
-
const lazyProcedure =
|
|
659
|
+
const lazyProcedure = lazyInternal(async () => {
|
|
567
660
|
const { default: maybeProcedure } = await unlazy(lazied);
|
|
568
661
|
if (!isProcedure(maybeProcedure)) {
|
|
569
662
|
throw new Error(`
|
|
@@ -576,42 +669,26 @@ function createAssertedLazyProcedure(lazied) {
|
|
|
576
669
|
}, getLazyMeta(lazied));
|
|
577
670
|
return lazyProcedure;
|
|
578
671
|
}
|
|
579
|
-
function createContractedProcedure(procedure, contract) {
|
|
580
|
-
return new Procedure({
|
|
581
|
-
...procedure["~orpc"],
|
|
582
|
-
errorMap: contract["~orpc"].errorMap,
|
|
583
|
-
route: contract["~orpc"].route,
|
|
584
|
-
meta: contract["~orpc"].meta
|
|
585
|
-
});
|
|
586
|
-
}
|
|
587
672
|
function call(procedure, input, ...rest) {
|
|
588
673
|
const options = resolveMaybeOptionalOptions(rest);
|
|
589
674
|
return createProcedureClient(procedure, options)(input, options);
|
|
590
675
|
}
|
|
591
676
|
|
|
592
|
-
function
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
const recursive = new Proxy(procedureCaller, {
|
|
600
|
-
get(target, key) {
|
|
601
|
-
if (typeof key !== "string") {
|
|
602
|
-
return Reflect.get(target, key);
|
|
603
|
-
}
|
|
604
|
-
const next = getRouter(router, [key]);
|
|
605
|
-
if (!next) {
|
|
606
|
-
return Reflect.get(target, key);
|
|
607
|
-
}
|
|
608
|
-
return createRouterClient(next, {
|
|
609
|
-
...rest[0],
|
|
610
|
-
path: [...rest[0]?.path ?? [], key]
|
|
611
|
-
});
|
|
677
|
+
function isSchemaIssue(issue) {
|
|
678
|
+
if (!isTypescriptObject(issue) || typeof issue.message !== "string") {
|
|
679
|
+
return false;
|
|
680
|
+
}
|
|
681
|
+
if (issue.path !== void 0) {
|
|
682
|
+
if (!Array.isArray(issue.path)) {
|
|
683
|
+
return false;
|
|
612
684
|
}
|
|
613
|
-
|
|
614
|
-
|
|
685
|
+
if (!issue.path.every(
|
|
686
|
+
(segment) => isPropertyKey(segment) || isTypescriptObject(segment) && isPropertyKey(segment.key)
|
|
687
|
+
)) {
|
|
688
|
+
return false;
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
return true;
|
|
615
692
|
}
|
|
616
693
|
|
|
617
|
-
export {
|
|
694
|
+
export { BadRequestError, Builder, BuilderWithMiddlewares, Contract, DecoratedProcedure, ForbiddenError, InternalServerError, JsonSchemaXNativeType, NotFoundError, Procedure, ProcedureBuilder, UnauthorizedError, addMiddleware, baseApi, call, createAssertedLazyProcedure, createProcedureClient, decorateMiddleware, enhanceRouter, experimental_JsonSchemaCoercer, getLazyMeta, initialSchemas, isProcedure, isSchemaIssue, lazyInternal, mergeMeta, parseEndpointDefinition, unlazy };
|