@temporary-name/server 1.9.3-alpha.e2d8d164da72fb570c2b14a4fa956c80f9e33cdc → 1.9.3-alpha.ec3bfb9dce56198911349c322c970208b21b50db
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 +4 -6
- package/dist/adapters/aws-lambda/index.d.ts +4 -6
- package/dist/adapters/aws-lambda/index.mjs +4 -4
- package/dist/adapters/fetch/index.d.mts +8 -86
- package/dist/adapters/fetch/index.d.ts +8 -86
- package/dist/adapters/fetch/index.mjs +16 -155
- package/dist/adapters/node/index.d.mts +8 -63
- package/dist/adapters/node/index.d.ts +8 -63
- package/dist/adapters/node/index.mjs +14 -120
- package/dist/adapters/standard/index.d.mts +10 -7
- package/dist/adapters/standard/index.d.ts +10 -7
- package/dist/adapters/standard/index.mjs +4 -4
- package/dist/helpers/index.mjs +3 -29
- package/dist/index.d.mts +121 -239
- package/dist/index.d.ts +121 -239
- package/dist/index.mjs +167 -352
- package/dist/openapi/index.d.mts +12 -28
- package/dist/openapi/index.d.ts +12 -28
- package/dist/openapi/index.mjs +59 -112
- package/dist/shared/server.C1RJffw4.mjs +30 -0
- package/dist/shared/server.CQIFwyhc.mjs +40 -0
- package/dist/shared/server.CVhIyQ4x.d.mts +41 -0
- package/dist/shared/server.CYa9puL2.mjs +403 -0
- package/dist/shared/server.ChOv1yG3.mjs +319 -0
- package/dist/shared/server.Cj3_Lp61.d.mts +373 -0
- package/dist/shared/server.Cj3_Lp61.d.ts +373 -0
- package/dist/shared/server.Cza0RB3u.mjs +160 -0
- package/dist/shared/server.D8RAzJ_p.d.ts +41 -0
- package/dist/shared/server.YUvuxHty.mjs +48 -0
- package/package.json +10 -28
- 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.B93y_8tj.d.mts +0 -23
- package/dist/shared/server.BYYf0Wn6.mjs +0 -202
- package/dist/shared/server.C3RuMHWl.d.mts +0 -192
- package/dist/shared/server.C3RuMHWl.d.ts +0 -192
- package/dist/shared/server.CT1xhSmE.d.mts +0 -56
- package/dist/shared/server.CqTex_jI.mjs +0 -265
- package/dist/shared/server.D_fags8X.d.ts +0 -23
- package/dist/shared/server.Kxw442A9.mjs +0 -247
- package/dist/shared/server.cjcgLdr1.d.ts +0 -56
package/dist/index.mjs
CHANGED
|
@@ -1,50 +1,69 @@
|
|
|
1
|
-
import { onError, resolveMaybeOptionalOptions } from '@temporary-name/shared';
|
|
2
|
-
export { AsyncIteratorClass, EventPublisher, ORPCError, asyncIteratorToStream as eventIteratorToStream, isDefinedError, onError, onFinish, onStart, onSuccess,
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
import {
|
|
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';
|
|
12
|
+
import 'cookie';
|
|
9
13
|
|
|
10
|
-
function
|
|
11
|
-
if (
|
|
12
|
-
|
|
13
|
-
}
|
|
14
|
-
for (let i = 0; i < middlewares.length; i++) {
|
|
15
|
-
if (compare[i] === void 0) {
|
|
16
|
-
return true;
|
|
17
|
-
}
|
|
18
|
-
if (middlewares[i] !== compare[i]) {
|
|
19
|
-
return false;
|
|
20
|
-
}
|
|
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}"` });
|
|
21
17
|
}
|
|
22
|
-
return true;
|
|
23
18
|
}
|
|
24
|
-
function
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return [...first, ...second];
|
|
19
|
+
function validateTokenAuth(config, token, options) {
|
|
20
|
+
const { tokenPrefix, validate } = config;
|
|
21
|
+
validateTokenPrefix(tokenPrefix, token);
|
|
22
|
+
return validate(token, options);
|
|
29
23
|
}
|
|
30
|
-
function
|
|
31
|
-
|
|
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);
|
|
32
31
|
}
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
* This property holds the defined options.
|
|
37
|
-
*/
|
|
38
|
-
"~orpc";
|
|
39
|
-
constructor(def) {
|
|
40
|
-
this["~orpc"] = def;
|
|
41
|
-
}
|
|
32
|
+
function authByHeader(config, options) {
|
|
33
|
+
const authHeader = options.request.headers.get(config.name);
|
|
34
|
+
return authHeader ? validateTokenAuth(config, authHeader, options) : void 0;
|
|
42
35
|
}
|
|
43
|
-
function
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
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 };
|
|
48
67
|
}
|
|
49
68
|
|
|
50
69
|
class DecoratedProcedure extends Procedure {
|
|
@@ -66,170 +85,19 @@ class DecoratedProcedure extends Procedure {
|
|
|
66
85
|
}
|
|
67
86
|
}
|
|
68
87
|
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
return Reflect.get(target, key);
|
|
77
|
-
}
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
function getHiddenRouterContract(router) {
|
|
81
|
-
return router[HIDDEN_ROUTER_CONTRACT_SYMBOL];
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
function getRouter(router, path) {
|
|
85
|
-
let current = router;
|
|
86
|
-
for (let i = 0; i < path.length; i++) {
|
|
87
|
-
const segment = path[i];
|
|
88
|
-
if (!current) {
|
|
89
|
-
return void 0;
|
|
90
|
-
}
|
|
91
|
-
if (isProcedure(current)) {
|
|
92
|
-
return void 0;
|
|
93
|
-
}
|
|
94
|
-
if (!isLazy(current)) {
|
|
95
|
-
current = current[segment];
|
|
96
|
-
continue;
|
|
97
|
-
}
|
|
98
|
-
const lazied = current;
|
|
99
|
-
const rest = path.slice(i);
|
|
100
|
-
return lazy(async () => {
|
|
101
|
-
const unwrapped = await unlazy(lazied);
|
|
102
|
-
const next = getRouter(unwrapped.default, rest);
|
|
103
|
-
return unlazy(next);
|
|
104
|
-
}, getLazyMeta(lazied));
|
|
105
|
-
}
|
|
106
|
-
return current;
|
|
107
|
-
}
|
|
108
|
-
function createAccessibleLazyRouter(lazied) {
|
|
109
|
-
const recursive = new Proxy(lazied, {
|
|
110
|
-
get(target, key) {
|
|
111
|
-
if (typeof key !== "string") {
|
|
112
|
-
return Reflect.get(target, key);
|
|
113
|
-
}
|
|
114
|
-
const next = getRouter(lazied, [key]);
|
|
115
|
-
return createAccessibleLazyRouter(next);
|
|
116
|
-
}
|
|
117
|
-
});
|
|
118
|
-
return recursive;
|
|
119
|
-
}
|
|
120
|
-
function enhanceRouter(router, options) {
|
|
121
|
-
if (isLazy(router)) {
|
|
122
|
-
const laziedMeta = getLazyMeta(router);
|
|
123
|
-
const enhancedPrefix = laziedMeta?.prefix ? mergePrefix(options.prefix, laziedMeta?.prefix) : options.prefix;
|
|
124
|
-
const enhanced2 = lazy(
|
|
125
|
-
async () => {
|
|
126
|
-
const { default: unlaziedRouter } = await unlazy(router);
|
|
127
|
-
const enhanced3 = enhanceRouter(unlaziedRouter, options);
|
|
128
|
-
return unlazy(enhanced3);
|
|
129
|
-
},
|
|
130
|
-
{
|
|
131
|
-
...laziedMeta,
|
|
132
|
-
prefix: enhancedPrefix
|
|
133
|
-
}
|
|
134
|
-
);
|
|
135
|
-
const accessible = createAccessibleLazyRouter(enhanced2);
|
|
136
|
-
return accessible;
|
|
137
|
-
}
|
|
138
|
-
if (isProcedure(router)) {
|
|
139
|
-
const newMiddlewares = mergeMiddlewares(options.middlewares, router["~orpc"].middlewares, {
|
|
140
|
-
dedupeLeading: options.dedupeLeadingMiddlewares
|
|
141
|
-
});
|
|
142
|
-
const newMiddlewareAdded = newMiddlewares.length - router["~orpc"].middlewares.length;
|
|
143
|
-
const enhanced2 = new Procedure({
|
|
144
|
-
...router["~orpc"],
|
|
145
|
-
route: enhanceRoute(router["~orpc"].route, options),
|
|
146
|
-
errorMap: mergeErrorMap(options.errorMap, router["~orpc"].errorMap),
|
|
147
|
-
middlewares: newMiddlewares,
|
|
148
|
-
inputValidationIndex: router["~orpc"].inputValidationIndex + newMiddlewareAdded,
|
|
149
|
-
outputValidationIndex: router["~orpc"].outputValidationIndex + newMiddlewareAdded
|
|
150
|
-
});
|
|
151
|
-
return enhanced2;
|
|
152
|
-
}
|
|
153
|
-
const enhanced = {};
|
|
154
|
-
for (const key in router) {
|
|
155
|
-
enhanced[key] = enhanceRouter(router[key], options);
|
|
156
|
-
}
|
|
157
|
-
return enhanced;
|
|
158
|
-
}
|
|
159
|
-
function traverseContractProcedures(options, callback, lazyOptions = []) {
|
|
160
|
-
let currentRouter = options.router;
|
|
161
|
-
const hiddenContract = getHiddenRouterContract(options.router);
|
|
162
|
-
if (hiddenContract !== void 0) {
|
|
163
|
-
currentRouter = hiddenContract;
|
|
164
|
-
}
|
|
165
|
-
if (isLazy(currentRouter)) {
|
|
166
|
-
lazyOptions.push({
|
|
167
|
-
router: currentRouter,
|
|
168
|
-
path: options.path
|
|
169
|
-
});
|
|
170
|
-
} else if (isContractProcedure(currentRouter)) {
|
|
171
|
-
callback({
|
|
172
|
-
contract: currentRouter,
|
|
173
|
-
path: options.path
|
|
174
|
-
});
|
|
175
|
-
} else {
|
|
176
|
-
for (const key in currentRouter) {
|
|
177
|
-
traverseContractProcedures(
|
|
178
|
-
{
|
|
179
|
-
router: currentRouter[key],
|
|
180
|
-
path: [...options.path, key]
|
|
181
|
-
},
|
|
182
|
-
callback,
|
|
183
|
-
lazyOptions
|
|
184
|
-
);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
return lazyOptions;
|
|
188
|
-
}
|
|
189
|
-
async function resolveContractProcedures(options, callback) {
|
|
190
|
-
const pending = [options];
|
|
191
|
-
for (const options2 of pending) {
|
|
192
|
-
const lazyOptions = traverseContractProcedures(options2, callback);
|
|
193
|
-
for (const options3 of lazyOptions) {
|
|
194
|
-
const { default: router } = await unlazy(options3.router);
|
|
195
|
-
pending.push({
|
|
196
|
-
router,
|
|
197
|
-
path: options3.path
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
async function unlazyRouter(router) {
|
|
203
|
-
if (isProcedure(router)) {
|
|
204
|
-
return router;
|
|
205
|
-
}
|
|
206
|
-
const unlazied = {};
|
|
207
|
-
for (const key in router) {
|
|
208
|
-
const item = router[key];
|
|
209
|
-
const { default: unlaziedRouter } = await unlazy(item);
|
|
210
|
-
unlazied[key] = await unlazyRouter(unlaziedRouter);
|
|
211
|
-
}
|
|
212
|
-
return unlazied;
|
|
213
|
-
}
|
|
88
|
+
const initialSchemas = {
|
|
89
|
+
pathSchema: z.strictObject({}),
|
|
90
|
+
querySchema: z.strictObject({}),
|
|
91
|
+
bodySchema: z.strictObject({}),
|
|
92
|
+
outputSchema: z.unknown()
|
|
93
|
+
};
|
|
214
94
|
|
|
215
|
-
class ProcedureBuilder extends
|
|
95
|
+
class ProcedureBuilder extends Contract {
|
|
216
96
|
z;
|
|
217
97
|
constructor(def) {
|
|
218
98
|
super(def);
|
|
219
99
|
this.z = new SchemaClass();
|
|
220
100
|
}
|
|
221
|
-
/**
|
|
222
|
-
* Adds type-safe custom errors to the contract.
|
|
223
|
-
* The provided errors are spared-merged with any existing errors in the contract.
|
|
224
|
-
*
|
|
225
|
-
* @see {@link https://orpc.unnoq.com/docs/error-handling#type%E2%80%90safe-error-handling Type-Safe Error Handling Docs}
|
|
226
|
-
*/
|
|
227
|
-
errors(errors) {
|
|
228
|
-
return new ProcedureBuilder({
|
|
229
|
-
...this["~orpc"],
|
|
230
|
-
errorMap: mergeErrorMap(this["~orpc"].errorMap, errors)
|
|
231
|
-
});
|
|
232
|
-
}
|
|
233
101
|
/**
|
|
234
102
|
* Sets or updates the metadata for the contract.
|
|
235
103
|
* The provided metadata is spared-merged with any existing metadata in the contract.
|
|
@@ -242,6 +110,19 @@ class ProcedureBuilder extends ContractProcedure {
|
|
|
242
110
|
meta: mergeMeta(this["~orpc"].meta, meta)
|
|
243
111
|
});
|
|
244
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
|
+
}
|
|
245
126
|
/**
|
|
246
127
|
* Sets or updates the route definition for the contract.
|
|
247
128
|
* The provided route is spared-merged with any existing route in the contract.
|
|
@@ -250,22 +131,30 @@ class ProcedureBuilder extends ContractProcedure {
|
|
|
250
131
|
* @see {@link https://orpc.unnoq.com/docs/openapi/routing OpenAPI Routing Docs}
|
|
251
132
|
* @see {@link https://orpc.unnoq.com/docs/openapi/input-output-structure OpenAPI Input/Output Structure Docs}
|
|
252
133
|
*/
|
|
253
|
-
|
|
134
|
+
endpointOpts(route) {
|
|
254
135
|
const { prefix } = this["~orpc"];
|
|
255
136
|
return new ProcedureBuilder({
|
|
256
137
|
...this["~orpc"],
|
|
257
138
|
route: mergeRoute(this["~orpc"].route, prefix ? prefixRoute(route, prefix) : route)
|
|
258
139
|
});
|
|
259
140
|
}
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
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) {
|
|
266
152
|
return new ProcedureBuilder({
|
|
267
153
|
...this["~orpc"],
|
|
268
|
-
|
|
154
|
+
schemas: {
|
|
155
|
+
...this["~orpc"].schemas,
|
|
156
|
+
bodySchema: schema instanceof core.$ZodType ? schema : object(schema)
|
|
157
|
+
},
|
|
269
158
|
inputValidationIndex: this["~orpc"].middlewares.length
|
|
270
159
|
});
|
|
271
160
|
}
|
|
@@ -277,23 +166,49 @@ class ProcedureBuilder extends ContractProcedure {
|
|
|
277
166
|
output(schema) {
|
|
278
167
|
return new ProcedureBuilder({
|
|
279
168
|
...this["~orpc"],
|
|
280
|
-
|
|
169
|
+
schemas: {
|
|
170
|
+
...this["~orpc"].schemas,
|
|
171
|
+
outputSchema: schema instanceof core.$ZodType ? schema : object(schema)
|
|
172
|
+
},
|
|
281
173
|
outputValidationIndex: this["~orpc"].middlewares.length
|
|
282
174
|
});
|
|
283
175
|
}
|
|
284
|
-
/**
|
|
285
|
-
* Uses a middleware to modify the context or improve the pipeline.
|
|
286
|
-
*
|
|
287
|
-
* @info Supports both normal middleware and inline middleware implementations.
|
|
288
|
-
* @note The current context must be satisfy middleware dependent-context
|
|
289
|
-
* @see {@link https://orpc.unnoq.com/docs/middleware Middleware Docs}
|
|
290
|
-
*/
|
|
291
176
|
use(middleware) {
|
|
292
177
|
return new this.constructor({
|
|
293
178
|
...this["~orpc"],
|
|
294
179
|
middlewares: addMiddleware(this["~orpc"].middlewares, middleware)
|
|
295
180
|
});
|
|
296
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 = baseApi.$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
|
+
}
|
|
297
212
|
useGating(gates, isGateEnabled) {
|
|
298
213
|
return this.use(({ next, context }) => {
|
|
299
214
|
return gatingContext.run(
|
|
@@ -308,6 +223,9 @@ class ProcedureBuilder extends ContractProcedure {
|
|
|
308
223
|
* @see {@link https://orpc.unnoq.com/docs/procedure Procedure Docs}
|
|
309
224
|
*/
|
|
310
225
|
handler(handler) {
|
|
226
|
+
if (this["~orpc"].schemas.outputSchema === initialSchemas.outputSchema) {
|
|
227
|
+
throw new Error("You must call .output() before calling .handler()");
|
|
228
|
+
}
|
|
311
229
|
return new DecoratedProcedure({
|
|
312
230
|
...this["~orpc"],
|
|
313
231
|
handler
|
|
@@ -359,31 +277,6 @@ function decorateMiddleware(middleware) {
|
|
|
359
277
|
);
|
|
360
278
|
return mapped;
|
|
361
279
|
};
|
|
362
|
-
decorated.concat = (concatMiddleware, mapInput) => {
|
|
363
|
-
const mapped = mapInput ? decorateMiddleware(concatMiddleware).mapInput(mapInput) : concatMiddleware;
|
|
364
|
-
const concatted = decorateMiddleware((options, input, output, ...rest) => {
|
|
365
|
-
const merged = middleware(
|
|
366
|
-
{
|
|
367
|
-
...options,
|
|
368
|
-
next: (...[nextOptions1]) => mapped(
|
|
369
|
-
{
|
|
370
|
-
...options,
|
|
371
|
-
context: { ...options.context, ...nextOptions1?.context },
|
|
372
|
-
next: (...[nextOptions2]) => options.next({ context: { ...nextOptions1?.context, ...nextOptions2?.context } })
|
|
373
|
-
},
|
|
374
|
-
input,
|
|
375
|
-
output,
|
|
376
|
-
...rest
|
|
377
|
-
)
|
|
378
|
-
},
|
|
379
|
-
input,
|
|
380
|
-
output,
|
|
381
|
-
...rest
|
|
382
|
-
);
|
|
383
|
-
return merged;
|
|
384
|
-
});
|
|
385
|
-
return concatted;
|
|
386
|
-
};
|
|
387
280
|
return decorated;
|
|
388
281
|
}
|
|
389
282
|
|
|
@@ -405,112 +298,25 @@ class Builder extends BuilderWithMiddlewares {
|
|
|
405
298
|
return decorateMiddleware(middleware);
|
|
406
299
|
}
|
|
407
300
|
}
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
}
|
|
425
|
-
const os = createApiBuilder();
|
|
426
|
-
|
|
427
|
-
function implementerInternal(contract, middlewares) {
|
|
428
|
-
if (isContractProcedure(contract)) {
|
|
429
|
-
const impl2 = new Builder({
|
|
430
|
-
...contract["~orpc"],
|
|
431
|
-
middlewares,
|
|
432
|
-
inputValidationIndex: middlewares.length,
|
|
433
|
-
outputValidationIndex: middlewares.length,
|
|
434
|
-
dedupeLeadingMiddlewares: true
|
|
435
|
-
});
|
|
436
|
-
return impl2;
|
|
437
|
-
}
|
|
438
|
-
const impl = new Proxy(contract, {
|
|
439
|
-
get: (target, key) => {
|
|
440
|
-
if (typeof key !== "string") {
|
|
441
|
-
return Reflect.get(target, key);
|
|
442
|
-
}
|
|
443
|
-
let method;
|
|
444
|
-
if (key === "middleware") {
|
|
445
|
-
method = (mid) => decorateMiddleware(mid);
|
|
446
|
-
} else if (key === "use") {
|
|
447
|
-
method = (mid) => {
|
|
448
|
-
return implementerInternal(contract, addMiddleware(middlewares, mid));
|
|
449
|
-
};
|
|
450
|
-
} else if (key === "router") {
|
|
451
|
-
method = (router) => {
|
|
452
|
-
const adapted = enhanceRouter(router, {
|
|
453
|
-
middlewares,
|
|
454
|
-
errorMap: {},
|
|
455
|
-
prefix: void 0,
|
|
456
|
-
tags: void 0,
|
|
457
|
-
dedupeLeadingMiddlewares: true
|
|
458
|
-
});
|
|
459
|
-
return setHiddenRouterContract(adapted, contract);
|
|
460
|
-
};
|
|
461
|
-
} else if (key === "lazyRoute") {
|
|
462
|
-
method = (loader) => {
|
|
463
|
-
const adapted = enhanceRouter(lazy(loader), {
|
|
464
|
-
middlewares,
|
|
465
|
-
errorMap: {},
|
|
466
|
-
prefix: void 0,
|
|
467
|
-
tags: void 0,
|
|
468
|
-
dedupeLeadingMiddlewares: true
|
|
469
|
-
});
|
|
470
|
-
return setHiddenRouterContract(adapted, contract);
|
|
471
|
-
};
|
|
472
|
-
}
|
|
473
|
-
const next = getContractRouter(target, [key]);
|
|
474
|
-
if (!next) {
|
|
475
|
-
return method ?? next;
|
|
476
|
-
}
|
|
477
|
-
const nextImpl = implementerInternal(next, middlewares);
|
|
478
|
-
if (method) {
|
|
479
|
-
return new Proxy(method, {
|
|
480
|
-
get(_, key2) {
|
|
481
|
-
return Reflect.get(nextImpl, key2);
|
|
482
|
-
}
|
|
483
|
-
});
|
|
484
|
-
}
|
|
485
|
-
return nextImpl;
|
|
486
|
-
}
|
|
487
|
-
});
|
|
488
|
-
return impl;
|
|
489
|
-
}
|
|
490
|
-
function implement(contract) {
|
|
491
|
-
const implInternal = implementerInternal(contract, []);
|
|
492
|
-
const impl = new Proxy(implInternal, {
|
|
493
|
-
get: (target, key) => {
|
|
494
|
-
let method;
|
|
495
|
-
if (key === "$context") {
|
|
496
|
-
method = () => impl;
|
|
497
|
-
}
|
|
498
|
-
const next = Reflect.get(target, key);
|
|
499
|
-
if (!method || !next || typeof next !== "function" && typeof next !== "object") {
|
|
500
|
-
return method || next;
|
|
501
|
-
}
|
|
502
|
-
return new Proxy(method, {
|
|
503
|
-
get(_, key2) {
|
|
504
|
-
return Reflect.get(next, key2);
|
|
505
|
-
}
|
|
506
|
-
});
|
|
507
|
-
}
|
|
508
|
-
});
|
|
509
|
-
return impl;
|
|
510
|
-
}
|
|
301
|
+
const baseApi = new Builder({
|
|
302
|
+
route: {},
|
|
303
|
+
meta: {},
|
|
304
|
+
inputValidationIndex: 0,
|
|
305
|
+
outputValidationIndex: 0,
|
|
306
|
+
middlewares: [
|
|
307
|
+
onError((error, _options) => {
|
|
308
|
+
console.dir(error, { depth: null });
|
|
309
|
+
})
|
|
310
|
+
],
|
|
311
|
+
schemas: initialSchemas,
|
|
312
|
+
// NB: this is a relic from orpc -- I'm not convinced there's a need for this (or if there is, that it's
|
|
313
|
+
// the best solution). For now I've removed the interface to configure it externally.
|
|
314
|
+
dedupeLeadingMiddlewares: true,
|
|
315
|
+
authConfigs: []
|
|
316
|
+
});
|
|
511
317
|
|
|
512
318
|
function createAssertedLazyProcedure(lazied) {
|
|
513
|
-
const lazyProcedure =
|
|
319
|
+
const lazyProcedure = lazyInternal(async () => {
|
|
514
320
|
const { default: maybeProcedure } = await unlazy(lazied);
|
|
515
321
|
if (!isProcedure(maybeProcedure)) {
|
|
516
322
|
throw new Error(`
|
|
@@ -523,14 +329,6 @@ function createAssertedLazyProcedure(lazied) {
|
|
|
523
329
|
}, getLazyMeta(lazied));
|
|
524
330
|
return lazyProcedure;
|
|
525
331
|
}
|
|
526
|
-
function createContractedProcedure(procedure, contract) {
|
|
527
|
-
return new Procedure({
|
|
528
|
-
...procedure["~orpc"],
|
|
529
|
-
errorMap: contract["~orpc"].errorMap,
|
|
530
|
-
route: contract["~orpc"].route,
|
|
531
|
-
meta: contract["~orpc"].meta
|
|
532
|
-
});
|
|
533
|
-
}
|
|
534
332
|
function call(procedure, input, ...rest) {
|
|
535
333
|
const options = resolveMaybeOptionalOptions(rest);
|
|
536
334
|
return createProcedureClient(procedure, options)(input, options);
|
|
@@ -561,4 +359,21 @@ function createRouterClient(router, ...rest) {
|
|
|
561
359
|
return recursive;
|
|
562
360
|
}
|
|
563
361
|
|
|
564
|
-
|
|
362
|
+
function isSchemaIssue(issue) {
|
|
363
|
+
if (!isTypescriptObject(issue) || typeof issue.message !== "string") {
|
|
364
|
+
return false;
|
|
365
|
+
}
|
|
366
|
+
if (issue.path !== void 0) {
|
|
367
|
+
if (!Array.isArray(issue.path)) {
|
|
368
|
+
return false;
|
|
369
|
+
}
|
|
370
|
+
if (!issue.path.every(
|
|
371
|
+
(segment) => isPropertyKey(segment) || isTypescriptObject(segment) && isPropertyKey(segment.key)
|
|
372
|
+
)) {
|
|
373
|
+
return false;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
return true;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
export { Builder, BuilderWithMiddlewares, Contract, DecoratedProcedure, Procedure, ProcedureBuilder, addMiddleware, baseApi, call, createAssertedLazyProcedure, createProcedureClient, createRouterClient, decorateMiddleware, enhanceRouter, getLazyMeta, getRouter, initialSchemas, isLazy, isProcedure, isSchemaIssue, lazyInternal, mergeMeta, parseEndpointDefinition, unlazy };
|