@temporary-name/server 1.9.3-alpha.4275e976ddda4d8be107c2cfde9899bdea9a337d → 1.9.3-alpha.4c6c68e5af7355d5a6dff1179055586e1093ac2d
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 +12 -8
- package/dist/adapters/aws-lambda/index.d.ts +12 -8
- package/dist/adapters/aws-lambda/index.mjs +12 -4
- package/dist/adapters/fetch/index.d.mts +15 -87
- package/dist/adapters/fetch/index.d.ts +15 -87
- package/dist/adapters/fetch/index.mjs +23 -161
- package/dist/adapters/node/index.d.mts +15 -64
- package/dist/adapters/node/index.d.ts +15 -64
- package/dist/adapters/node/index.mjs +21 -126
- package/dist/adapters/standard/index.d.mts +27 -14
- package/dist/adapters/standard/index.d.ts +27 -14
- package/dist/adapters/standard/index.mjs +9 -101
- package/dist/helpers/index.mjs +3 -29
- package/dist/index.d.mts +120 -583
- package/dist/index.d.ts +120 -583
- package/dist/index.mjs +213 -449
- package/dist/openapi/index.d.mts +204 -0
- package/dist/openapi/index.d.ts +204 -0
- package/dist/openapi/index.mjs +692 -0
- package/dist/shared/server.BbUmcWIW.d.mts +41 -0
- package/dist/shared/server.BfraJHay.d.mts +373 -0
- package/dist/shared/server.BfraJHay.d.ts +373 -0
- package/dist/shared/server.C1RJffw4.mjs +30 -0
- package/dist/shared/server.CHugaZ_Y.d.ts +41 -0
- package/dist/shared/server.CQIFwyhc.mjs +40 -0
- package/dist/shared/server.CYa9puL2.mjs +403 -0
- package/dist/shared/server.ChOv1yG3.mjs +319 -0
- package/dist/shared/server.Cza0RB3u.mjs +160 -0
- package/dist/shared/server.YUvuxHty.mjs +48 -0
- package/package.json +18 -30
- 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.BEQrAa3A.mjs +0 -207
- package/dist/shared/server.Bo94xDTv.d.mts +0 -73
- package/dist/shared/server.Btxrgkj5.d.ts +0 -73
- package/dist/shared/server.C1YnHvvf.d.mts +0 -192
- package/dist/shared/server.C1YnHvvf.d.ts +0 -192
- package/dist/shared/server.D6K9uoPI.mjs +0 -35
- package/dist/shared/server.DZ5BIITo.mjs +0 -9
- package/dist/shared/server.X0YaZxSJ.mjs +0 -13
package/dist/index.mjs
CHANGED
|
@@ -1,137 +1,72 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export {
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
import { g as
|
|
6
|
-
import {
|
|
7
|
-
export { L as LAZY_SYMBOL,
|
|
1
|
+
import { assertNever, splitFirst, ORPCError, onError, resolveMaybeOptionalOptions, isTypescriptObject, isPropertyKey } from '@temporary-name/shared';
|
|
2
|
+
export { AsyncIteratorClass, EventPublisher, ORPCError, asyncIteratorToStream as eventIteratorToStream, isDefinedError, 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 { g as getCookie } from './shared/server.C1RJffw4.mjs';
|
|
6
|
+
import { P as Procedure, m as mergePrefix, a as mergeTags, e as enhanceRouter, C as Contract, p as parseEndpointDefinition, b as mergeRoute, c as prefixRoute, d as addMiddleware, l as lazyInternal, f as getLazyMeta, u as unlazy, i as isProcedure, h as isLazy, j as getRouter } from './shared/server.ChOv1yG3.mjs';
|
|
7
|
+
export { L as LAZY_SYMBOL, V as ValidationError, q as createAccessibleLazyRouter, w as endpointRegex, g as getDynamicParams, n as isStartWithMiddlewares, k as lazy, o as mergeMiddlewares, r as resolveContractProcedures, s as standardizeHTTPPath, t as traverseContractProcedures, v as unlazyRouter } from './shared/server.ChOv1yG3.mjs';
|
|
8
|
+
import { c as createProcedureClient } from './shared/server.Cza0RB3u.mjs';
|
|
9
|
+
export { m as mergeCurrentContext, a as middlewareOutputFn } from './shared/server.Cza0RB3u.mjs';
|
|
10
|
+
export { e as eventIterator, g as getEventIteratorSchemaDetails } from './shared/server.YUvuxHty.mjs';
|
|
8
11
|
export { getEventMeta, withEventMeta } from '@temporary-name/standard-server';
|
|
9
|
-
import '
|
|
10
|
-
import 'zod';
|
|
11
|
-
import 'zod/v4/core';
|
|
12
|
+
import 'cookie';
|
|
12
13
|
|
|
13
|
-
function
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
const mapped = decorateMiddleware(
|
|
17
|
-
(options, input, ...rest) => middleware(options, mapInput(input), ...rest)
|
|
18
|
-
);
|
|
19
|
-
return mapped;
|
|
20
|
-
};
|
|
21
|
-
decorated.concat = (concatMiddleware, mapInput) => {
|
|
22
|
-
const mapped = mapInput ? decorateMiddleware(concatMiddleware).mapInput(mapInput) : concatMiddleware;
|
|
23
|
-
const concatted = decorateMiddleware((options, input, output, ...rest) => {
|
|
24
|
-
const merged = middleware(
|
|
25
|
-
{
|
|
26
|
-
...options,
|
|
27
|
-
next: (...[nextOptions1]) => mapped(
|
|
28
|
-
{
|
|
29
|
-
...options,
|
|
30
|
-
context: { ...options.context, ...nextOptions1?.context },
|
|
31
|
-
next: (...[nextOptions2]) => options.next({ context: { ...nextOptions1?.context, ...nextOptions2?.context } })
|
|
32
|
-
},
|
|
33
|
-
input,
|
|
34
|
-
output,
|
|
35
|
-
...rest
|
|
36
|
-
)
|
|
37
|
-
},
|
|
38
|
-
input,
|
|
39
|
-
output,
|
|
40
|
-
...rest
|
|
41
|
-
);
|
|
42
|
-
return merged;
|
|
43
|
-
});
|
|
44
|
-
return concatted;
|
|
45
|
-
};
|
|
46
|
-
return decorated;
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function isStartWithMiddlewares(middlewares, compare) {
|
|
50
|
-
if (compare.length > middlewares.length) {
|
|
51
|
-
return false;
|
|
52
|
-
}
|
|
53
|
-
for (let i = 0; i < middlewares.length; i++) {
|
|
54
|
-
if (compare[i] === void 0) {
|
|
55
|
-
return true;
|
|
56
|
-
}
|
|
57
|
-
if (middlewares[i] !== compare[i]) {
|
|
58
|
-
return false;
|
|
59
|
-
}
|
|
14
|
+
function validateTokenPrefix(prefix, token) {
|
|
15
|
+
if (prefix && !token.startsWith(prefix)) {
|
|
16
|
+
throw new ORPCError("UNAUTHORIZED", { message: `Invalid auth token. It must start with "${prefix}"` });
|
|
60
17
|
}
|
|
61
|
-
return true;
|
|
62
18
|
}
|
|
63
|
-
function
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
return [...first, ...second];
|
|
19
|
+
function validateTokenAuth(config, token, options) {
|
|
20
|
+
const { tokenPrefix, validate } = config;
|
|
21
|
+
validateTokenPrefix(tokenPrefix, token);
|
|
22
|
+
return validate(token, options);
|
|
68
23
|
}
|
|
69
|
-
function
|
|
70
|
-
|
|
24
|
+
function authByQuery(config, options) {
|
|
25
|
+
const { name, tokenPrefix, validate } = config;
|
|
26
|
+
const query = options.request.url.searchParams;
|
|
27
|
+
const token = query.get(name);
|
|
28
|
+
if (!token) return void 0;
|
|
29
|
+
validateTokenPrefix(tokenPrefix, token);
|
|
30
|
+
return validate(token, options);
|
|
71
31
|
}
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
* This property holds the defined options.
|
|
76
|
-
*/
|
|
77
|
-
"~orpc";
|
|
78
|
-
constructor(def) {
|
|
79
|
-
this["~orpc"] = def;
|
|
80
|
-
}
|
|
32
|
+
function authByHeader(config, options) {
|
|
33
|
+
const authHeader = options.request.headers.get(config.name);
|
|
34
|
+
return authHeader ? validateTokenAuth(config, authHeader, options) : void 0;
|
|
81
35
|
}
|
|
82
|
-
function
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
36
|
+
function authByCookie(config, options) {
|
|
37
|
+
const cookie = getCookie(options.request.headers, config.name);
|
|
38
|
+
return cookie ? validateTokenAuth(config, cookie, options) : void 0;
|
|
39
|
+
}
|
|
40
|
+
function authByBearer(config, options) {
|
|
41
|
+
const authHeader = options.request.headers.get("Authorization");
|
|
42
|
+
if (!authHeader) return void 0;
|
|
43
|
+
const [authType, bearer] = splitFirst(authHeader, " ");
|
|
44
|
+
return authType === "Bearer" ? validateTokenAuth(config, bearer, options) : void 0;
|
|
45
|
+
}
|
|
46
|
+
function authByBasic(config, options) {
|
|
47
|
+
const authHeader = options.request.headers.get("Authorization");
|
|
48
|
+
if (!authHeader) return void 0;
|
|
49
|
+
const [authType, encoded] = splitFirst(authHeader, " ");
|
|
50
|
+
if (authType !== "Basic") return void 0;
|
|
51
|
+
const decoded = Buffer.from(encoded, "base64").toString("utf-8");
|
|
52
|
+
const [username, password] = splitFirst(decoded, ":");
|
|
53
|
+
validateTokenPrefix(config.tokenPrefix, password);
|
|
54
|
+
return config.validate(username, password, options);
|
|
55
|
+
}
|
|
56
|
+
function authByType(config, options) {
|
|
57
|
+
const { type } = config;
|
|
58
|
+
return type === "header" ? authByHeader(config, options) : type === "query" ? authByQuery(config, options) : type === "cookie" ? authByCookie(config, options) : type === "bearer" ? authByBearer(config, options) : type === "basic" ? authByBasic(config, options) : type === "none" ? false : assertNever(type);
|
|
59
|
+
}
|
|
60
|
+
function authDescription(config) {
|
|
61
|
+
const { type } = config;
|
|
62
|
+
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);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function mergeMeta(meta1, meta2) {
|
|
66
|
+
return { ...meta1, ...meta2 };
|
|
87
67
|
}
|
|
88
68
|
|
|
89
69
|
class DecoratedProcedure extends Procedure {
|
|
90
|
-
/**
|
|
91
|
-
* Adds type-safe custom errors.
|
|
92
|
-
* The provided errors are spared-merged with any existing errors.
|
|
93
|
-
*
|
|
94
|
-
* @see {@link https://orpc.unnoq.com/docs/error-handling#type%E2%80%90safe-error-handling Type-Safe Error Handling Docs}
|
|
95
|
-
*/
|
|
96
|
-
errors(errors) {
|
|
97
|
-
return new DecoratedProcedure({
|
|
98
|
-
...this["~orpc"],
|
|
99
|
-
errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
/**
|
|
103
|
-
* Sets or updates the metadata.
|
|
104
|
-
* The provided metadata is spared-merged with any existing metadata.
|
|
105
|
-
*
|
|
106
|
-
* @see {@link https://orpc.unnoq.com/docs/metadata Metadata Docs}
|
|
107
|
-
*/
|
|
108
|
-
meta(meta) {
|
|
109
|
-
return new DecoratedProcedure({
|
|
110
|
-
...this["~orpc"],
|
|
111
|
-
meta: mergeMeta(this["~orpc"].meta, meta)
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Sets or updates the route definition.
|
|
116
|
-
* The provided route is spared-merged with any existing route.
|
|
117
|
-
* This option is typically relevant when integrating with OpenAPI.
|
|
118
|
-
*
|
|
119
|
-
* @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
|
|
120
|
-
* @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
|
|
121
|
-
*/
|
|
122
|
-
route(route) {
|
|
123
|
-
return new DecoratedProcedure({
|
|
124
|
-
...this["~orpc"],
|
|
125
|
-
route: mergeRoute(this["~orpc"].route, route)
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
use(middleware, mapInput) {
|
|
129
|
-
const mapped = mapInput ? decorateMiddleware(middleware).mapInput(mapInput) : middleware;
|
|
130
|
-
return new DecoratedProcedure({
|
|
131
|
-
...this["~orpc"],
|
|
132
|
-
middlewares: addMiddleware(this["~orpc"].middlewares, mapped)
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
70
|
/**
|
|
136
71
|
* Make this procedure callable (works like a function while still being a procedure).
|
|
137
72
|
*
|
|
@@ -150,244 +85,76 @@ class DecoratedProcedure extends Procedure {
|
|
|
150
85
|
}
|
|
151
86
|
}
|
|
152
87
|
|
|
153
|
-
const
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
}
|
|
160
|
-
return Reflect.get(target, key);
|
|
161
|
-
}
|
|
162
|
-
});
|
|
163
|
-
}
|
|
164
|
-
function getHiddenRouterContract(router) {
|
|
165
|
-
return router[HIDDEN_ROUTER_CONTRACT_SYMBOL];
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
function getRouter(router, path) {
|
|
169
|
-
let current = router;
|
|
170
|
-
for (let i = 0; i < path.length; i++) {
|
|
171
|
-
const segment = path[i];
|
|
172
|
-
if (!current) {
|
|
173
|
-
return void 0;
|
|
174
|
-
}
|
|
175
|
-
if (isProcedure(current)) {
|
|
176
|
-
return void 0;
|
|
177
|
-
}
|
|
178
|
-
if (!isLazy(current)) {
|
|
179
|
-
current = current[segment];
|
|
180
|
-
continue;
|
|
181
|
-
}
|
|
182
|
-
const lazied = current;
|
|
183
|
-
const rest = path.slice(i);
|
|
184
|
-
return lazy(async () => {
|
|
185
|
-
const unwrapped = await unlazy(lazied);
|
|
186
|
-
const next = getRouter(unwrapped.default, rest);
|
|
187
|
-
return unlazy(next);
|
|
188
|
-
}, getLazyMeta(lazied));
|
|
189
|
-
}
|
|
190
|
-
return current;
|
|
191
|
-
}
|
|
192
|
-
function createAccessibleLazyRouter(lazied) {
|
|
193
|
-
const recursive = new Proxy(lazied, {
|
|
194
|
-
get(target, key) {
|
|
195
|
-
if (typeof key !== "string") {
|
|
196
|
-
return Reflect.get(target, key);
|
|
197
|
-
}
|
|
198
|
-
const next = getRouter(lazied, [key]);
|
|
199
|
-
return createAccessibleLazyRouter(next);
|
|
200
|
-
}
|
|
201
|
-
});
|
|
202
|
-
return recursive;
|
|
203
|
-
}
|
|
204
|
-
function enhanceRouter(router, options) {
|
|
205
|
-
if (isLazy(router)) {
|
|
206
|
-
const laziedMeta = getLazyMeta(router);
|
|
207
|
-
const enhancedPrefix = laziedMeta?.prefix ? mergePrefix(options.prefix, laziedMeta?.prefix) : options.prefix;
|
|
208
|
-
const enhanced2 = lazy(
|
|
209
|
-
async () => {
|
|
210
|
-
const { default: unlaziedRouter } = await unlazy(router);
|
|
211
|
-
const enhanced3 = enhanceRouter(unlaziedRouter, options);
|
|
212
|
-
return unlazy(enhanced3);
|
|
213
|
-
},
|
|
214
|
-
{
|
|
215
|
-
...laziedMeta,
|
|
216
|
-
prefix: enhancedPrefix
|
|
217
|
-
}
|
|
218
|
-
);
|
|
219
|
-
const accessible = createAccessibleLazyRouter(enhanced2);
|
|
220
|
-
return accessible;
|
|
221
|
-
}
|
|
222
|
-
if (isProcedure(router)) {
|
|
223
|
-
const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares, {
|
|
224
|
-
dedupeLeading: options.dedupeLeadingMiddlewares
|
|
225
|
-
});
|
|
226
|
-
const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
|
|
227
|
-
const enhanced2 = new Procedure({
|
|
228
|
-
...router["~orpc"],
|
|
229
|
-
route: enhanceRoute(router["~orpc"].route, options),
|
|
230
|
-
errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
|
|
231
|
-
middlewares: newMiddlewares,
|
|
232
|
-
inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
|
|
233
|
-
outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
|
|
234
|
-
});
|
|
235
|
-
return enhanced2;
|
|
236
|
-
}
|
|
237
|
-
const enhanced = {};
|
|
238
|
-
for (const key in router) {
|
|
239
|
-
enhanced[key] = enhanceRouter(router[key], options);
|
|
240
|
-
}
|
|
241
|
-
return enhanced;
|
|
242
|
-
}
|
|
243
|
-
function traverseContractProcedures(options, callback, lazyOptions = []) {
|
|
244
|
-
let currentRouter = options.router;
|
|
245
|
-
const hiddenContract = getHiddenRouterContract(options.router);
|
|
246
|
-
if (hiddenContract !== void 0) {
|
|
247
|
-
currentRouter = hiddenContract;
|
|
248
|
-
}
|
|
249
|
-
if (isLazy(currentRouter)) {
|
|
250
|
-
lazyOptions.push({
|
|
251
|
-
router: currentRouter,
|
|
252
|
-
path: options.path
|
|
253
|
-
});
|
|
254
|
-
} else if (isContractProcedure(currentRouter)) {
|
|
255
|
-
callback({
|
|
256
|
-
contract: currentRouter,
|
|
257
|
-
path: options.path
|
|
258
|
-
});
|
|
259
|
-
} else {
|
|
260
|
-
for (const key in currentRouter) {
|
|
261
|
-
traverseContractProcedures(
|
|
262
|
-
{
|
|
263
|
-
router: currentRouter[key],
|
|
264
|
-
path: [...options.path, key]
|
|
265
|
-
},
|
|
266
|
-
callback,
|
|
267
|
-
lazyOptions
|
|
268
|
-
);
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
return lazyOptions;
|
|
272
|
-
}
|
|
273
|
-
async function resolveContractProcedures(options, callback) {
|
|
274
|
-
const pending = [options];
|
|
275
|
-
for (const options2 of pending) {
|
|
276
|
-
const lazyOptions = traverseContractProcedures(options2, callback);
|
|
277
|
-
for (const options3 of lazyOptions) {
|
|
278
|
-
const { default: router } = await unlazy(options3.router);
|
|
279
|
-
pending.push({
|
|
280
|
-
router,
|
|
281
|
-
path: options3.path
|
|
282
|
-
});
|
|
283
|
-
}
|
|
284
|
-
}
|
|
285
|
-
}
|
|
286
|
-
async function unlazyRouter(router) {
|
|
287
|
-
if (isProcedure(router)) {
|
|
288
|
-
return router;
|
|
289
|
-
}
|
|
290
|
-
const unlazied = {};
|
|
291
|
-
for (const key in router) {
|
|
292
|
-
const item = router[key];
|
|
293
|
-
const { default: unlaziedRouter } = await unlazy(item);
|
|
294
|
-
unlazied[key] = await unlazyRouter(unlaziedRouter);
|
|
295
|
-
}
|
|
296
|
-
return unlazied;
|
|
297
|
-
}
|
|
88
|
+
const initialSchemas = {
|
|
89
|
+
pathSchema: z.strictObject({}),
|
|
90
|
+
querySchema: z.strictObject({}),
|
|
91
|
+
bodySchema: z.strictObject({}),
|
|
92
|
+
outputSchema: z.unknown()
|
|
93
|
+
};
|
|
298
94
|
|
|
299
|
-
class
|
|
300
|
-
|
|
301
|
-
* This property holds the defined options.
|
|
302
|
-
*/
|
|
303
|
-
"~orpc";
|
|
95
|
+
class ProcedureBuilder extends Contract {
|
|
96
|
+
z;
|
|
304
97
|
constructor(def) {
|
|
305
|
-
|
|
98
|
+
super(def);
|
|
99
|
+
this.z = new SchemaClass();
|
|
306
100
|
}
|
|
307
101
|
/**
|
|
308
|
-
*
|
|
309
|
-
*
|
|
310
|
-
* @see {@link https://orpc.unnoq.com/docs/context Context Docs}
|
|
311
|
-
*/
|
|
312
|
-
$context() {
|
|
313
|
-
return this;
|
|
314
|
-
}
|
|
315
|
-
/**
|
|
316
|
-
* Creates a middleware.
|
|
317
|
-
*
|
|
318
|
-
* @see {@link https://orpc.unnoq.com/docs/middleware Middleware Docs}
|
|
319
|
-
*/
|
|
320
|
-
middleware(middleware) {
|
|
321
|
-
return decorateMiddleware(middleware);
|
|
322
|
-
}
|
|
323
|
-
/**
|
|
324
|
-
* Adds type-safe custom errors.
|
|
325
|
-
* The provided errors are spared-merged with any existing errors.
|
|
326
|
-
*
|
|
327
|
-
* @see {@link https://orpc.unnoq.com/docs/error-handling#type%E2%80%90safe-error-handling Type-Safe Error Handling Docs}
|
|
328
|
-
*/
|
|
329
|
-
errors(errors) {
|
|
330
|
-
return new Builder({
|
|
331
|
-
...this["~orpc"],
|
|
332
|
-
errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
|
|
333
|
-
});
|
|
334
|
-
}
|
|
335
|
-
/**
|
|
336
|
-
* Uses a middleware to modify the context or improve the pipeline.
|
|
337
|
-
*
|
|
338
|
-
* @info Supports both normal middleware and inline middleware implementations.
|
|
339
|
-
* @note The current context must be satisfy middleware dependent-context
|
|
340
|
-
* @see {@link https://orpc.unnoq.com/docs/middleware Middleware Docs}
|
|
341
|
-
*/
|
|
342
|
-
use(middleware) {
|
|
343
|
-
return new Builder({
|
|
344
|
-
...this["~orpc"],
|
|
345
|
-
middlewares: addMiddleware(this["~orpc"].middlewares, middleware)
|
|
346
|
-
});
|
|
347
|
-
}
|
|
348
|
-
useGating(gates, isGateEnabled) {
|
|
349
|
-
return this.use(({ next, context }) => {
|
|
350
|
-
return gatingContext.run(
|
|
351
|
-
(gate) => isGateEnabled(gate, context),
|
|
352
|
-
() => next({ context: { isGateEnabled } })
|
|
353
|
-
);
|
|
354
|
-
});
|
|
355
|
-
}
|
|
356
|
-
/**
|
|
357
|
-
* Sets or updates the metadata.
|
|
358
|
-
* The provided metadata is spared-merged with any existing metadata.
|
|
102
|
+
* Sets or updates the metadata for the contract.
|
|
103
|
+
* The provided metadata is spared-merged with any existing metadata in the contract.
|
|
359
104
|
*
|
|
360
105
|
* @see {@link https://orpc.unnoq.com/docs/metadata Metadata Docs}
|
|
361
106
|
*/
|
|
362
107
|
meta(meta) {
|
|
363
|
-
return new
|
|
108
|
+
return new ProcedureBuilder({
|
|
364
109
|
...this["~orpc"],
|
|
365
110
|
meta: mergeMeta(this["~orpc"].meta, meta)
|
|
366
111
|
});
|
|
367
112
|
}
|
|
113
|
+
endpoint(stringsOrEndpoint, ...values) {
|
|
114
|
+
const { method, path, pathSchema } = parseEndpointDefinition(stringsOrEndpoint, values);
|
|
115
|
+
const { prefix } = this["~orpc"];
|
|
116
|
+
const route = { method, path };
|
|
117
|
+
return new ProcedureBuilder({
|
|
118
|
+
...this["~orpc"],
|
|
119
|
+
route: mergeRoute(this["~orpc"].route, prefix ? prefixRoute(route, prefix) : route),
|
|
120
|
+
schemas: {
|
|
121
|
+
...this["~orpc"].schemas,
|
|
122
|
+
pathSchema
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}
|
|
368
126
|
/**
|
|
369
|
-
* Sets or updates the route definition.
|
|
370
|
-
* The provided route is spared-merged with any existing route.
|
|
127
|
+
* Sets or updates the route definition for the contract.
|
|
128
|
+
* The provided route is spared-merged with any existing route in the contract.
|
|
371
129
|
* This option is typically relevant when integrating with OpenAPI.
|
|
372
130
|
*
|
|
373
131
|
* @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
|
|
374
132
|
* @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
|
|
375
133
|
*/
|
|
376
|
-
|
|
377
|
-
|
|
134
|
+
endpointOpts(route) {
|
|
135
|
+
const { prefix } = this["~orpc"];
|
|
136
|
+
return new ProcedureBuilder({
|
|
378
137
|
...this["~orpc"],
|
|
379
|
-
route: mergeRoute(this["~orpc"].route, route)
|
|
138
|
+
route: mergeRoute(this["~orpc"].route, prefix ? prefixRoute(route, prefix) : route)
|
|
380
139
|
});
|
|
381
140
|
}
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
141
|
+
query(schema) {
|
|
142
|
+
return new ProcedureBuilder({
|
|
143
|
+
...this["~orpc"],
|
|
144
|
+
schemas: {
|
|
145
|
+
...this["~orpc"].schemas,
|
|
146
|
+
querySchema: schema instanceof core.$ZodType ? schema : object(schema)
|
|
147
|
+
},
|
|
148
|
+
inputValidationIndex: this["~orpc"].middlewares.length
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
body(schema) {
|
|
152
|
+
return new ProcedureBuilder({
|
|
389
153
|
...this["~orpc"],
|
|
390
|
-
|
|
154
|
+
schemas: {
|
|
155
|
+
...this["~orpc"].schemas,
|
|
156
|
+
bodySchema: schema instanceof core.$ZodType ? schema : object(schema)
|
|
157
|
+
},
|
|
391
158
|
inputValidationIndex: this["~orpc"].middlewares.length
|
|
392
159
|
});
|
|
393
160
|
}
|
|
@@ -397,23 +164,75 @@ class Builder {
|
|
|
397
164
|
* @see {@link https://orpc.unnoq.com/docs/procedure#input-output-validation Output Validation Docs}
|
|
398
165
|
*/
|
|
399
166
|
output(schema) {
|
|
400
|
-
return new
|
|
167
|
+
return new ProcedureBuilder({
|
|
401
168
|
...this["~orpc"],
|
|
402
|
-
|
|
169
|
+
schemas: {
|
|
170
|
+
...this["~orpc"].schemas,
|
|
171
|
+
outputSchema: schema instanceof core.$ZodType ? schema : object(schema)
|
|
172
|
+
},
|
|
403
173
|
outputValidationIndex: this["~orpc"].middlewares.length
|
|
404
174
|
});
|
|
405
175
|
}
|
|
176
|
+
use(middleware) {
|
|
177
|
+
return new this.constructor({
|
|
178
|
+
...this["~orpc"],
|
|
179
|
+
middlewares: addMiddleware(this["~orpc"].middlewares, middleware)
|
|
180
|
+
});
|
|
181
|
+
}
|
|
182
|
+
// `& {}` is so AuthType will be expanded in parameter info tooltips.
|
|
183
|
+
// The default of false for ValidatedAuthContext is used when you pass in the type 'none'. We use false
|
|
184
|
+
// because we can't use null or undefined (see ValidatedAuthContext) but we still want it to be falsy.
|
|
185
|
+
useAuth(type, ...rest) {
|
|
186
|
+
const config = { type, ...rest[0] };
|
|
187
|
+
const middleware = os.$context().middleware(async (options) => {
|
|
188
|
+
const { next, context } = options;
|
|
189
|
+
if (context.auth) return next();
|
|
190
|
+
const auth = await authByType(config, options);
|
|
191
|
+
if (auth === void 0) {
|
|
192
|
+
const { authConfigs } = options.procedure["~orpc"];
|
|
193
|
+
if (context.auth !== false && config === authConfigs.at(-1)) {
|
|
194
|
+
let authDescriptions = authConfigs.map(authDescription).join(", ");
|
|
195
|
+
if (authConfigs.length > 1) {
|
|
196
|
+
authDescriptions = `one of: ${authDescriptions}`;
|
|
197
|
+
}
|
|
198
|
+
throw new ORPCError("UNAUTHORIZED", {
|
|
199
|
+
message: `Authentication required. You must provide ${authDescriptions}`
|
|
200
|
+
});
|
|
201
|
+
}
|
|
202
|
+
return next();
|
|
203
|
+
}
|
|
204
|
+
return next({ context: { auth } });
|
|
205
|
+
});
|
|
206
|
+
return new this.constructor({
|
|
207
|
+
...this["~orpc"],
|
|
208
|
+
authConfigs: [...this["~orpc"].authConfigs, config],
|
|
209
|
+
middlewares: addMiddleware(this["~orpc"].middlewares, middleware)
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
useGating(gates, isGateEnabled) {
|
|
213
|
+
return this.use(({ next, context }) => {
|
|
214
|
+
return gatingContext.run(
|
|
215
|
+
(gate) => isGateEnabled(gate, context),
|
|
216
|
+
() => next({ context: { isGateEnabled } })
|
|
217
|
+
);
|
|
218
|
+
});
|
|
219
|
+
}
|
|
406
220
|
/**
|
|
407
221
|
* Defines the handler of the procedure.
|
|
408
222
|
*
|
|
409
223
|
* @see {@link https://orpc.unnoq.com/docs/procedure Procedure Docs}
|
|
410
224
|
*/
|
|
411
225
|
handler(handler) {
|
|
226
|
+
if (this["~orpc"].schemas.outputSchema === initialSchemas.outputSchema) {
|
|
227
|
+
throw new Error("You must call .output() before calling .handler()");
|
|
228
|
+
}
|
|
412
229
|
return new DecoratedProcedure({
|
|
413
230
|
...this["~orpc"],
|
|
414
231
|
handler
|
|
415
232
|
});
|
|
416
233
|
}
|
|
234
|
+
}
|
|
235
|
+
class BuilderWithMiddlewares extends ProcedureBuilder {
|
|
417
236
|
/**
|
|
418
237
|
* Prefixes all procedures in the router.
|
|
419
238
|
* The provided prefix is post-appended to any existing router prefix.
|
|
@@ -423,7 +242,7 @@ class Builder {
|
|
|
423
242
|
* @see {@link https://orpc.unnoq.com/docs/openapi/routing#route-prefixes OpenAPI Route Prefixes Docs}
|
|
424
243
|
*/
|
|
425
244
|
prefix(prefix) {
|
|
426
|
-
return new
|
|
245
|
+
return new this.constructor({
|
|
427
246
|
...this["~orpc"],
|
|
428
247
|
prefix: mergePrefix(this["~orpc"].prefix, prefix)
|
|
429
248
|
});
|
|
@@ -435,7 +254,7 @@ class Builder {
|
|
|
435
254
|
* @see {@link https://orpc.unnoq.com/docs/openapi/openapi-specification#operation-metadata OpenAPI Operation Metadata Docs}
|
|
436
255
|
*/
|
|
437
256
|
tag(...tags) {
|
|
438
|
-
return new
|
|
257
|
+
return new this.constructor({
|
|
439
258
|
...this["~orpc"],
|
|
440
259
|
tags: mergeTags(this["~orpc"].tags, tags)
|
|
441
260
|
});
|
|
@@ -448,123 +267,59 @@ class Builder {
|
|
|
448
267
|
router(router) {
|
|
449
268
|
return enhanceRouter(router, this["~orpc"]);
|
|
450
269
|
}
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function decorateMiddleware(middleware) {
|
|
273
|
+
const decorated = ((...args) => middleware(...args));
|
|
274
|
+
decorated.mapInput = (mapInput) => {
|
|
275
|
+
const mapped = decorateMiddleware(
|
|
276
|
+
(options, input, ...rest) => middleware(options, mapInput(input), ...rest)
|
|
277
|
+
);
|
|
278
|
+
return mapped;
|
|
279
|
+
};
|
|
280
|
+
return decorated;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
class Builder extends BuilderWithMiddlewares {
|
|
451
284
|
/**
|
|
452
|
-
*
|
|
453
|
-
* And applies all of the previously defined options to the specified router.
|
|
285
|
+
* Set or override the initial context.
|
|
454
286
|
*
|
|
455
|
-
* @see {@link https://orpc.unnoq.com/docs/
|
|
287
|
+
* @see {@link https://orpc.unnoq.com/docs/context Context Docs}
|
|
456
288
|
*/
|
|
457
|
-
|
|
458
|
-
return
|
|
289
|
+
$context() {
|
|
290
|
+
return this;
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* Creates a middleware.
|
|
294
|
+
*
|
|
295
|
+
* @see {@link https://orpc.unnoq.com/docs/middleware Middleware Docs}
|
|
296
|
+
*/
|
|
297
|
+
middleware(middleware) {
|
|
298
|
+
return decorateMiddleware(middleware);
|
|
459
299
|
}
|
|
460
300
|
}
|
|
461
301
|
function createApiBuilder(opts = {}) {
|
|
462
|
-
|
|
302
|
+
return new Builder({
|
|
463
303
|
route: {},
|
|
464
304
|
meta: opts.meta ?? {},
|
|
465
|
-
errorMap: {},
|
|
466
305
|
inputValidationIndex: 0,
|
|
467
306
|
outputValidationIndex: 0,
|
|
468
307
|
middlewares: [
|
|
469
|
-
onError((error,
|
|
308
|
+
onError((error, _options) => {
|
|
470
309
|
console.dir(error, { depth: null });
|
|
471
310
|
})
|
|
472
311
|
],
|
|
312
|
+
schemas: initialSchemas,
|
|
473
313
|
// NB: this is a relic from orpc -- I'm not convinced there's a need for this (or if there is, that it's
|
|
474
314
|
// the best solution). For now I've removed the interface to configure it externally.
|
|
475
|
-
dedupeLeadingMiddlewares: true
|
|
315
|
+
dedupeLeadingMiddlewares: true,
|
|
316
|
+
authConfigs: []
|
|
476
317
|
});
|
|
477
|
-
return base;
|
|
478
318
|
}
|
|
479
319
|
const os = createApiBuilder();
|
|
480
320
|
|
|
481
|
-
function implementerInternal(contract, middlewares) {
|
|
482
|
-
if (isContractProcedure(contract)) {
|
|
483
|
-
const impl2 = new Builder({
|
|
484
|
-
...contract["~orpc"],
|
|
485
|
-
middlewares,
|
|
486
|
-
inputValidationIndex: middlewares.length,
|
|
487
|
-
outputValidationIndex: middlewares.length,
|
|
488
|
-
dedupeLeadingMiddlewares: true
|
|
489
|
-
});
|
|
490
|
-
return impl2;
|
|
491
|
-
}
|
|
492
|
-
const impl = new Proxy(contract, {
|
|
493
|
-
get: (target, key) => {
|
|
494
|
-
if (typeof key !== "string") {
|
|
495
|
-
return Reflect.get(target, key);
|
|
496
|
-
}
|
|
497
|
-
let method;
|
|
498
|
-
if (key === "middleware") {
|
|
499
|
-
method = (mid) => decorateMiddleware(mid);
|
|
500
|
-
} else if (key === "use") {
|
|
501
|
-
method = (mid) => {
|
|
502
|
-
return implementerInternal(contract, addMiddleware(middlewares, mid));
|
|
503
|
-
};
|
|
504
|
-
} else if (key === "router") {
|
|
505
|
-
method = (router) => {
|
|
506
|
-
const adapted = enhanceRouter(router, {
|
|
507
|
-
middlewares,
|
|
508
|
-
errorMap: {},
|
|
509
|
-
prefix: void 0,
|
|
510
|
-
tags: void 0,
|
|
511
|
-
dedupeLeadingMiddlewares: true
|
|
512
|
-
});
|
|
513
|
-
return setHiddenRouterContract(adapted, contract);
|
|
514
|
-
};
|
|
515
|
-
} else if (key === "lazyRoute") {
|
|
516
|
-
method = (loader) => {
|
|
517
|
-
const adapted = enhanceRouter(lazy(loader), {
|
|
518
|
-
middlewares,
|
|
519
|
-
errorMap: {},
|
|
520
|
-
prefix: void 0,
|
|
521
|
-
tags: void 0,
|
|
522
|
-
dedupeLeadingMiddlewares: true
|
|
523
|
-
});
|
|
524
|
-
return setHiddenRouterContract(adapted, contract);
|
|
525
|
-
};
|
|
526
|
-
}
|
|
527
|
-
const next = getContractRouter(target, [key]);
|
|
528
|
-
if (!next) {
|
|
529
|
-
return method ?? next;
|
|
530
|
-
}
|
|
531
|
-
const nextImpl = implementerInternal(next, middlewares);
|
|
532
|
-
if (method) {
|
|
533
|
-
return new Proxy(method, {
|
|
534
|
-
get(_, key2) {
|
|
535
|
-
return Reflect.get(nextImpl, key2);
|
|
536
|
-
}
|
|
537
|
-
});
|
|
538
|
-
}
|
|
539
|
-
return nextImpl;
|
|
540
|
-
}
|
|
541
|
-
});
|
|
542
|
-
return impl;
|
|
543
|
-
}
|
|
544
|
-
function implement(contract) {
|
|
545
|
-
const implInternal = implementerInternal(contract, []);
|
|
546
|
-
const impl = new Proxy(implInternal, {
|
|
547
|
-
get: (target, key) => {
|
|
548
|
-
let method;
|
|
549
|
-
if (key === "$context") {
|
|
550
|
-
method = () => impl;
|
|
551
|
-
}
|
|
552
|
-
const next = Reflect.get(target, key);
|
|
553
|
-
if (!method || !next || typeof next !== "function" && typeof next !== "object") {
|
|
554
|
-
return method || next;
|
|
555
|
-
}
|
|
556
|
-
return new Proxy(method, {
|
|
557
|
-
get(_, key2) {
|
|
558
|
-
return Reflect.get(next, key2);
|
|
559
|
-
}
|
|
560
|
-
});
|
|
561
|
-
}
|
|
562
|
-
});
|
|
563
|
-
return impl;
|
|
564
|
-
}
|
|
565
|
-
|
|
566
321
|
function createAssertedLazyProcedure(lazied) {
|
|
567
|
-
const lazyProcedure =
|
|
322
|
+
const lazyProcedure = lazyInternal(async () => {
|
|
568
323
|
const { default: maybeProcedure } = await unlazy(lazied);
|
|
569
324
|
if (!isProcedure(maybeProcedure)) {
|
|
570
325
|
throw new Error(`
|
|
@@ -577,14 +332,6 @@ function createAssertedLazyProcedure(lazied) {
|
|
|
577
332
|
}, getLazyMeta(lazied));
|
|
578
333
|
return lazyProcedure;
|
|
579
334
|
}
|
|
580
|
-
function createContractedProcedure(procedure, contract) {
|
|
581
|
-
return new Procedure({
|
|
582
|
-
...procedure["~orpc"],
|
|
583
|
-
errorMap: contract["~orpc"].errorMap,
|
|
584
|
-
route: contract["~orpc"].route,
|
|
585
|
-
meta: contract["~orpc"].meta
|
|
586
|
-
});
|
|
587
|
-
}
|
|
588
335
|
function call(procedure, input, ...rest) {
|
|
589
336
|
const options = resolveMaybeOptionalOptions(rest);
|
|
590
337
|
return createProcedureClient(procedure, options)(input, options);
|
|
@@ -615,4 +362,21 @@ function createRouterClient(router, ...rest) {
|
|
|
615
362
|
return recursive;
|
|
616
363
|
}
|
|
617
364
|
|
|
618
|
-
|
|
365
|
+
function isSchemaIssue(issue) {
|
|
366
|
+
if (!isTypescriptObject(issue) || typeof issue.message !== "string") {
|
|
367
|
+
return false;
|
|
368
|
+
}
|
|
369
|
+
if (issue.path !== void 0) {
|
|
370
|
+
if (!Array.isArray(issue.path)) {
|
|
371
|
+
return false;
|
|
372
|
+
}
|
|
373
|
+
if (!issue.path.every(
|
|
374
|
+
(segment) => isPropertyKey(segment) || isTypescriptObject(segment) && isPropertyKey(segment.key)
|
|
375
|
+
)) {
|
|
376
|
+
return false;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
return true;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
export { Builder, BuilderWithMiddlewares, Contract, DecoratedProcedure, Procedure, ProcedureBuilder, addMiddleware, call, createApiBuilder, createAssertedLazyProcedure, createProcedureClient, createRouterClient, decorateMiddleware, enhanceRouter, getLazyMeta, getRouter, initialSchemas, isLazy, isProcedure, isSchemaIssue, lazyInternal, mergeMeta, os, parseEndpointDefinition, unlazy };
|