@temporary-name/server 1.9.3-alpha.5c041722542bb790586629fc98d4c72d9f6ee65e → 1.9.3-alpha.5dc8b200530586870ac736830d4584e0333cfd05
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 +3 -4
- package/dist/adapters/aws-lambda/index.d.ts +3 -4
- package/dist/adapters/aws-lambda/index.mjs +3 -3
- package/dist/adapters/fetch/index.d.mts +3 -4
- package/dist/adapters/fetch/index.d.ts +3 -4
- package/dist/adapters/fetch/index.mjs +3 -3
- package/dist/adapters/node/index.d.mts +3 -4
- package/dist/adapters/node/index.d.ts +3 -4
- package/dist/adapters/node/index.mjs +3 -3
- package/dist/adapters/standard/index.d.mts +10 -6
- package/dist/adapters/standard/index.d.ts +10 -6
- package/dist/adapters/standard/index.mjs +4 -4
- package/dist/helpers/index.mjs +3 -29
- package/dist/index.d.mts +29 -23
- package/dist/index.d.ts +29 -23
- package/dist/index.mjs +121 -15
- package/dist/openapi/index.d.mts +1 -1
- package/dist/openapi/index.d.ts +1 -1
- package/dist/openapi/index.mjs +51 -34
- package/dist/plugins/index.d.mts +4 -80
- package/dist/plugins/index.d.ts +4 -80
- package/dist/plugins/index.mjs +17 -189
- package/dist/shared/{server.C4l2ZA3m.d.ts → server.B-meye9-.d.ts} +2 -3
- package/dist/shared/{server.SmOM3Fae.mjs → server.Ba0Z2fNc.mjs} +18 -26
- package/dist/shared/server.C1RJffw4.mjs +30 -0
- package/dist/shared/{server.B5czSQQh.d.mts → server.DkYpsO6W.d.mts} +71 -55
- package/dist/shared/{server.B5czSQQh.d.ts → server.DkYpsO6W.d.ts} +71 -55
- package/dist/shared/{server.D7UjcuIv.d.ts → server.DwNnHUZP.d.ts} +2 -2
- package/dist/shared/{server.Bk8Hmx7Z.d.mts → server.I-tTl_ce.d.mts} +2 -2
- package/dist/shared/{server.Kxw442A9.mjs → server.JtIZ8YG7.mjs} +1 -11
- package/dist/shared/{server.BdGbIyq8.mjs → server.miXh-9wo.mjs} +38 -18
- package/dist/shared/{server.TgmlNh8r.d.mts → server.vLcMd_kA.d.mts} +2 -3
- package/package.json +10 -22
package/dist/plugins/index.mjs
CHANGED
|
@@ -1,119 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { parseBatchRequest, toBatchResponse } from '@temporary-name/standard-server/batch';
|
|
4
|
-
import { toFetchHeaders } from '@temporary-name/standard-server-fetch';
|
|
5
|
-
|
|
6
|
-
class BatchHandlerPlugin {
|
|
7
|
-
maxSize;
|
|
8
|
-
mapRequestItem;
|
|
9
|
-
successStatus;
|
|
10
|
-
headers;
|
|
11
|
-
order = 5e6;
|
|
12
|
-
constructor(options = {}) {
|
|
13
|
-
this.maxSize = options.maxSize ?? 10;
|
|
14
|
-
this.mapRequestItem = options.mapRequestItem ?? ((request, { request: batchRequest }) => ({
|
|
15
|
-
...request,
|
|
16
|
-
headers: {
|
|
17
|
-
...batchRequest.headers,
|
|
18
|
-
...request.headers
|
|
19
|
-
}
|
|
20
|
-
}));
|
|
21
|
-
this.successStatus = options.successStatus ?? 207;
|
|
22
|
-
this.headers = options.headers ?? {};
|
|
23
|
-
}
|
|
24
|
-
init(options) {
|
|
25
|
-
options.rootInterceptors ??= [];
|
|
26
|
-
options.rootInterceptors.unshift(async (options2) => {
|
|
27
|
-
const xHeader = flattenHeader(options2.request.headers["x-orpc-batch"]);
|
|
28
|
-
if (xHeader === void 0) {
|
|
29
|
-
return options2.next();
|
|
30
|
-
}
|
|
31
|
-
let isParsing = false;
|
|
32
|
-
try {
|
|
33
|
-
return await runWithSpan({ name: "handle_batch_request" }, async (span) => {
|
|
34
|
-
const mode = xHeader === "buffered" ? "buffered" : "streaming";
|
|
35
|
-
isParsing = true;
|
|
36
|
-
const parsed = parseBatchRequest({ ...options2.request, body: await options2.request.body() });
|
|
37
|
-
isParsing = false;
|
|
38
|
-
span?.setAttribute("batch.mode", mode);
|
|
39
|
-
span?.setAttribute("batch.size", parsed.length);
|
|
40
|
-
const maxSize = await value(this.maxSize, options2);
|
|
41
|
-
if (parsed.length > maxSize) {
|
|
42
|
-
const message = "Batch request size exceeds the maximum allowed size";
|
|
43
|
-
setSpanError(span, message);
|
|
44
|
-
return {
|
|
45
|
-
matched: true,
|
|
46
|
-
response: {
|
|
47
|
-
status: 413,
|
|
48
|
-
headers: {},
|
|
49
|
-
body: message
|
|
50
|
-
}
|
|
51
|
-
};
|
|
52
|
-
}
|
|
53
|
-
const responses = parsed.map((request, index) => {
|
|
54
|
-
const mapped = this.mapRequestItem(request, options2);
|
|
55
|
-
return options2.next({ ...options2, request: { ...mapped, body: () => Promise.resolve(mapped.body) } }).then(({ response: response2, matched }) => {
|
|
56
|
-
span?.addEvent(`response.${index}.${matched ? "success" : "not_matched"}`);
|
|
57
|
-
if (matched) {
|
|
58
|
-
if (response2.body instanceof Blob || response2.body instanceof FormData || isAsyncIteratorObject(response2.body)) {
|
|
59
|
-
return {
|
|
60
|
-
index,
|
|
61
|
-
status: 500,
|
|
62
|
-
headers: {},
|
|
63
|
-
body: "Batch responses do not support file/blob, or event-iterator. Please call this procedure separately outside of the batch request."
|
|
64
|
-
};
|
|
65
|
-
}
|
|
66
|
-
return { ...response2, index };
|
|
67
|
-
}
|
|
68
|
-
return { index, status: 404, headers: {}, body: "No procedure matched" };
|
|
69
|
-
}).catch((err) => {
|
|
70
|
-
Promise.reject(err);
|
|
71
|
-
return { index, status: 500, headers: {}, body: "Internal server error" };
|
|
72
|
-
});
|
|
73
|
-
});
|
|
74
|
-
await Promise.race(responses);
|
|
75
|
-
const status = await value(this.successStatus, responses, options2);
|
|
76
|
-
const headers = await value(this.headers, responses, options2);
|
|
77
|
-
const promises = [...responses];
|
|
78
|
-
const response = await toBatchResponse({
|
|
79
|
-
status,
|
|
80
|
-
headers,
|
|
81
|
-
mode,
|
|
82
|
-
body: new AsyncIteratorClass(
|
|
83
|
-
async () => {
|
|
84
|
-
const handling = promises.filter((p) => p !== void 0);
|
|
85
|
-
if (handling.length <= 0) {
|
|
86
|
-
return { done: true, value: void 0 };
|
|
87
|
-
}
|
|
88
|
-
const value2 = await Promise.race(handling);
|
|
89
|
-
promises[value2.index] = void 0;
|
|
90
|
-
return { done: false, value: value2 };
|
|
91
|
-
},
|
|
92
|
-
async () => {
|
|
93
|
-
}
|
|
94
|
-
)
|
|
95
|
-
});
|
|
96
|
-
return {
|
|
97
|
-
matched: true,
|
|
98
|
-
response
|
|
99
|
-
};
|
|
100
|
-
});
|
|
101
|
-
} catch (cause) {
|
|
102
|
-
if (isParsing) {
|
|
103
|
-
return {
|
|
104
|
-
matched: true,
|
|
105
|
-
response: {
|
|
106
|
-
status: 400,
|
|
107
|
-
headers: {},
|
|
108
|
-
body: "Invalid batch request, this could be caused by a malformed request body or a missing header"
|
|
109
|
-
}
|
|
110
|
-
};
|
|
111
|
-
}
|
|
112
|
-
throw cause;
|
|
113
|
-
}
|
|
114
|
-
});
|
|
115
|
-
}
|
|
116
|
-
}
|
|
1
|
+
import { value, ORPCError } from '@temporary-name/shared';
|
|
2
|
+
import { maybeSetHeader } from '@temporary-name/standard-server';
|
|
117
3
|
|
|
118
4
|
class CORSPlugin {
|
|
119
5
|
options;
|
|
@@ -132,17 +18,13 @@ class CORSPlugin {
|
|
|
132
18
|
options.rootInterceptors ??= [];
|
|
133
19
|
options.rootInterceptors.unshift(async (interceptorOptions) => {
|
|
134
20
|
if (interceptorOptions.request.method === "OPTIONS") {
|
|
135
|
-
const resHeaders =
|
|
21
|
+
const resHeaders = new Headers();
|
|
136
22
|
if (this.options.maxAge !== void 0) {
|
|
137
|
-
resHeaders
|
|
138
|
-
}
|
|
139
|
-
if (this.options.allowMethods?.length) {
|
|
140
|
-
resHeaders["access-control-allow-methods"] = flattenHeader(this.options.allowMethods);
|
|
141
|
-
}
|
|
142
|
-
const allowHeaders = this.options.allowHeaders ?? interceptorOptions.request.headers["access-control-request-headers"];
|
|
143
|
-
if (typeof allowHeaders === "string" || allowHeaders?.length) {
|
|
144
|
-
resHeaders["access-control-allow-headers"] = flattenHeader(allowHeaders);
|
|
23
|
+
resHeaders.set("access-control-max-age", this.options.maxAge.toString());
|
|
145
24
|
}
|
|
25
|
+
maybeSetHeader(resHeaders, "access-control-allow-methods", this.options.allowMethods);
|
|
26
|
+
const allowHeaders = this.options.allowHeaders ?? interceptorOptions.request.headers.get("access-control-request-headers");
|
|
27
|
+
maybeSetHeader(resHeaders, "access-control-allow-headers", allowHeaders);
|
|
146
28
|
return {
|
|
147
29
|
matched: true,
|
|
148
30
|
response: {
|
|
@@ -159,87 +41,33 @@ class CORSPlugin {
|
|
|
159
41
|
if (!result.matched) {
|
|
160
42
|
return result;
|
|
161
43
|
}
|
|
162
|
-
const origin =
|
|
44
|
+
const origin = interceptorOptions.request.headers.get("origin") ?? "";
|
|
163
45
|
const allowedOrigin = await value(this.options.origin, origin, interceptorOptions);
|
|
164
46
|
const allowedOriginArr = Array.isArray(allowedOrigin) ? allowedOrigin : [allowedOrigin];
|
|
165
47
|
if (allowedOriginArr.includes("*")) {
|
|
166
|
-
result.response.headers
|
|
48
|
+
result.response.headers.set("access-control-allow-origin", "*");
|
|
167
49
|
} else {
|
|
168
50
|
if (allowedOriginArr.includes(origin)) {
|
|
169
|
-
result.response.headers
|
|
51
|
+
result.response.headers.set("access-control-allow-origin", origin);
|
|
170
52
|
}
|
|
171
|
-
result.response.headers.vary
|
|
53
|
+
result.response.headers.set("vary", interceptorOptions.request.headers.get("vary") ?? "origin");
|
|
172
54
|
}
|
|
173
55
|
const allowedTimingOrigin = await value(this.options.timingOrigin, origin, interceptorOptions);
|
|
174
56
|
const allowedTimingOriginArr = Array.isArray(allowedTimingOrigin) ? allowedTimingOrigin : [allowedTimingOrigin];
|
|
175
57
|
if (allowedTimingOriginArr.includes("*")) {
|
|
176
|
-
result.response.headers
|
|
58
|
+
result.response.headers.set("timing-allow-origin", "*");
|
|
177
59
|
} else if (allowedTimingOriginArr.includes(origin)) {
|
|
178
|
-
result.response.headers
|
|
60
|
+
result.response.headers.set("timing-allow-origin", origin);
|
|
179
61
|
}
|
|
180
62
|
if (this.options.credentials) {
|
|
181
|
-
result.response.headers
|
|
182
|
-
}
|
|
183
|
-
if (this.options.exposeHeaders?.length) {
|
|
184
|
-
result.response.headers["access-control-expose-headers"] = flattenHeader(this.options.exposeHeaders);
|
|
63
|
+
result.response.headers.set("access-control-allow-credentials", "true");
|
|
185
64
|
}
|
|
65
|
+
maybeSetHeader(result.response.headers, "access-control-expose-headers", this.options.exposeHeaders);
|
|
186
66
|
return result;
|
|
187
67
|
});
|
|
188
68
|
}
|
|
189
69
|
}
|
|
190
70
|
|
|
191
|
-
class RequestHeadersPlugin {
|
|
192
|
-
init(options) {
|
|
193
|
-
options.rootInterceptors ??= [];
|
|
194
|
-
options.rootInterceptors.push((interceptorOptions) => {
|
|
195
|
-
const reqHeaders = interceptorOptions.context.reqHeaders ?? toFetchHeaders(interceptorOptions.request.headers);
|
|
196
|
-
return interceptorOptions.next({
|
|
197
|
-
...interceptorOptions,
|
|
198
|
-
context: {
|
|
199
|
-
...interceptorOptions.context,
|
|
200
|
-
reqHeaders
|
|
201
|
-
}
|
|
202
|
-
});
|
|
203
|
-
});
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
class ResponseHeadersPlugin {
|
|
208
|
-
init(options) {
|
|
209
|
-
options.rootInterceptors ??= [];
|
|
210
|
-
options.rootInterceptors.push(async (interceptorOptions) => {
|
|
211
|
-
const resHeaders = interceptorOptions.context.resHeaders ?? new Headers();
|
|
212
|
-
const result = await interceptorOptions.next({
|
|
213
|
-
...interceptorOptions,
|
|
214
|
-
context: {
|
|
215
|
-
...interceptorOptions.context,
|
|
216
|
-
resHeaders
|
|
217
|
-
}
|
|
218
|
-
});
|
|
219
|
-
if (!result.matched) {
|
|
220
|
-
return result;
|
|
221
|
-
}
|
|
222
|
-
const responseHeaders = clone(result.response.headers);
|
|
223
|
-
for (const [key, value] of resHeaders) {
|
|
224
|
-
if (Array.isArray(responseHeaders[key])) {
|
|
225
|
-
responseHeaders[key].push(value);
|
|
226
|
-
} else if (responseHeaders[key] !== void 0) {
|
|
227
|
-
responseHeaders[key] = [responseHeaders[key], value];
|
|
228
|
-
} else {
|
|
229
|
-
responseHeaders[key] = value;
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
return {
|
|
233
|
-
...result,
|
|
234
|
-
response: {
|
|
235
|
-
...result.response,
|
|
236
|
-
headers: responseHeaders
|
|
237
|
-
}
|
|
238
|
-
};
|
|
239
|
-
});
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
|
|
243
71
|
const SIMPLE_CSRF_PROTECTION_CONTEXT_SYMBOL = Symbol("SIMPLE_CSRF_PROTECTION_CONTEXT");
|
|
244
72
|
class SimpleCsrfProtectionHandlerPlugin {
|
|
245
73
|
headerName;
|
|
@@ -266,7 +94,7 @@ class SimpleCsrfProtectionHandlerPlugin {
|
|
|
266
94
|
...options2,
|
|
267
95
|
context: {
|
|
268
96
|
...options2.context,
|
|
269
|
-
[SIMPLE_CSRF_PROTECTION_CONTEXT_SYMBOL]: options2.request.headers
|
|
97
|
+
[SIMPLE_CSRF_PROTECTION_CONTEXT_SYMBOL]: options2.request.headers.get(headerName) === headerValue
|
|
270
98
|
}
|
|
271
99
|
});
|
|
272
100
|
});
|
|
@@ -285,4 +113,4 @@ class SimpleCsrfProtectionHandlerPlugin {
|
|
|
285
113
|
}
|
|
286
114
|
}
|
|
287
115
|
|
|
288
|
-
export {
|
|
116
|
+
export { CORSPlugin, SimpleCsrfProtectionHandlerPlugin };
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { Meta } from '@temporary-name/contract';
|
|
2
|
-
import { HTTPPath, Interceptor } from '@temporary-name/shared';
|
|
3
|
-
import {
|
|
4
|
-
import { C as Context, R as Router, H as ProcedureClientInterceptorOptions } from './server.B5czSQQh.js';
|
|
2
|
+
import { HTTPPath, StandardLazyRequest, Interceptor, StandardResponse } from '@temporary-name/shared';
|
|
3
|
+
import { C as Context, R as Router, N as ProcedureClientInterceptorOptions } from './server.DkYpsO6W.js';
|
|
5
4
|
|
|
6
5
|
interface StandardHandlerPlugin<T extends Context> {
|
|
7
6
|
order?: number;
|
|
@@ -1,26 +1,17 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { d as deserialize, s as serialize, b as bracketNotationDeserialize, a as standardizeHTTPPath } from './server.Kxw442A9.mjs';
|
|
1
|
+
import { stringifyJSON, isObject, isORPCErrorStatus, tryDecodeURIComponent, toHttpPath, toArray, intercept, runWithSpan, ORPC_NAME, isAsyncIteratorObject, asyncIteratorWithSpan, setSpanError, ORPCError, toORPCError } from '@temporary-name/shared';
|
|
2
|
+
import { c as createProcedureClient } from './server.miXh-9wo.mjs';
|
|
3
|
+
import { fallbackContractConfig, standardizeHTTPPath } from '@temporary-name/contract';
|
|
4
|
+
import { d as deserialize, b as bracketNotationDeserialize, s as serialize } from './server.JtIZ8YG7.mjs';
|
|
6
5
|
import { traverseContractProcedures, isProcedure, getLazyMeta, unlazy, getRouter, createContractedProcedure } from '@temporary-name/server';
|
|
7
6
|
import { createRouter, addRoute, findRoute } from 'rou3';
|
|
8
7
|
|
|
9
8
|
async function decode(request, pathParams) {
|
|
10
|
-
|
|
11
|
-
|
|
9
|
+
return {
|
|
10
|
+
path: pathParams ?? {},
|
|
11
|
+
query: bracketNotationDeserialize(Array.from(request.url.searchParams.entries())),
|
|
12
|
+
headers: request.headers,
|
|
13
|
+
body: deserialize(await request.body()) ?? {}
|
|
12
14
|
};
|
|
13
|
-
const data = request.method === "GET" ? deserializeSearchParams() : deserialize(await request.body());
|
|
14
|
-
if (data === void 0) {
|
|
15
|
-
return pathParams;
|
|
16
|
-
}
|
|
17
|
-
if (isObject(data)) {
|
|
18
|
-
return {
|
|
19
|
-
...pathParams,
|
|
20
|
-
...data
|
|
21
|
-
};
|
|
22
|
-
}
|
|
23
|
-
return data;
|
|
24
15
|
}
|
|
25
16
|
function encode(output, procedure) {
|
|
26
17
|
const successStatus = fallbackContractConfig(
|
|
@@ -34,7 +25,7 @@ function encode(output, procedure) {
|
|
|
34
25
|
if (outputStructure === "compact") {
|
|
35
26
|
return {
|
|
36
27
|
status: successStatus,
|
|
37
|
-
headers:
|
|
28
|
+
headers: new Headers(),
|
|
38
29
|
body: serialize(output)
|
|
39
30
|
};
|
|
40
31
|
}
|
|
@@ -53,14 +44,14 @@ function encode(output, procedure) {
|
|
|
53
44
|
}
|
|
54
45
|
return {
|
|
55
46
|
status: output.status ?? successStatus,
|
|
56
|
-
headers: output.headers ??
|
|
47
|
+
headers: output.headers ?? new Headers(),
|
|
57
48
|
body: serialize(output.body)
|
|
58
49
|
};
|
|
59
50
|
}
|
|
60
51
|
function encodeError(error) {
|
|
61
52
|
return {
|
|
62
53
|
status: error.status,
|
|
63
|
-
headers:
|
|
54
|
+
headers: new Headers(),
|
|
64
55
|
body: serialize(error.toJSON(), { outputFormat: "plain" })
|
|
65
56
|
};
|
|
66
57
|
}
|
|
@@ -214,12 +205,12 @@ class StandardHandler {
|
|
|
214
205
|
span?.setAttribute("rpc.system", ORPC_NAME);
|
|
215
206
|
span?.setAttribute("rpc.method", match.path.join("."));
|
|
216
207
|
step = "decode_input";
|
|
217
|
-
|
|
208
|
+
const input = await runWithSpan({ name: "decode_input" }, () => decode(request2, match.params));
|
|
218
209
|
step = void 0;
|
|
219
|
-
if (isAsyncIteratorObject(input)) {
|
|
220
|
-
input = asyncIteratorWithSpan(
|
|
210
|
+
if (isAsyncIteratorObject(input.body)) {
|
|
211
|
+
input.body = asyncIteratorWithSpan(
|
|
221
212
|
{ name: "consume_event_iterator_input", signal: request2.signal },
|
|
222
|
-
input
|
|
213
|
+
input.body
|
|
223
214
|
);
|
|
224
215
|
}
|
|
225
216
|
const client = createProcedureClient(match.procedure, {
|
|
@@ -229,8 +220,9 @@ class StandardHandler {
|
|
|
229
220
|
});
|
|
230
221
|
step = "call_procedure";
|
|
231
222
|
const output = await client(input, {
|
|
223
|
+
request: request2,
|
|
232
224
|
signal: request2.signal,
|
|
233
|
-
lastEventId:
|
|
225
|
+
lastEventId: request2.headers.get("last-event-id") ?? void 0
|
|
234
226
|
});
|
|
235
227
|
step = void 0;
|
|
236
228
|
const response = encode(output, match.procedure);
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { parse, serialize } from 'cookie';
|
|
2
|
+
|
|
3
|
+
function setCookie(headers, name, value, options = {}) {
|
|
4
|
+
if (headers === void 0) {
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
const cookieString = serialize(name, value, {
|
|
8
|
+
path: "/",
|
|
9
|
+
...options
|
|
10
|
+
});
|
|
11
|
+
headers.append("Set-Cookie", cookieString);
|
|
12
|
+
}
|
|
13
|
+
function getCookie(headers, name, options = {}) {
|
|
14
|
+
if (headers === void 0) {
|
|
15
|
+
return void 0;
|
|
16
|
+
}
|
|
17
|
+
const cookieHeader = headers.get("cookie");
|
|
18
|
+
if (cookieHeader === null) {
|
|
19
|
+
return void 0;
|
|
20
|
+
}
|
|
21
|
+
return parse(cookieHeader, options)[name];
|
|
22
|
+
}
|
|
23
|
+
function deleteCookie(headers, name, options = {}) {
|
|
24
|
+
return setCookie(headers, name, "", {
|
|
25
|
+
...options,
|
|
26
|
+
maxAge: 0
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export { deleteCookie as d, getCookie as g, setCookie as s };
|
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
import { ErrorMap, ErrorMapItem, InferSchemaInput,
|
|
2
|
-
import { ORPCErrorCode, MaybeOptionalOptions, ORPCErrorOptions, ORPCError, Promisable, HTTPPath, ClientContext, Interceptor, PromiseWithError, Value, Client } from '@temporary-name/shared';
|
|
1
|
+
import { ErrorMap, ErrorMapItem, InferSchemaInput, Meta, Schemas, ContractProcedureDef, AnyContractRouter, ContractProcedure, InferProcedureClientInputs, InferSchemaOutput, EnhanceRouteOptions, MergedErrorMap, AnyContractProcedure, ContractRouter, AnySchema, ErrorFromErrorMap } from '@temporary-name/contract';
|
|
2
|
+
import { ORPCErrorCode, MaybeOptionalOptions, ORPCErrorOptions, ORPCError, Promisable, StandardLazyRequest, HTTPPath, ClientContext, Interceptor, PromiseWithError, Value, Client } from '@temporary-name/shared';
|
|
3
3
|
|
|
4
4
|
type Context = Record<PropertyKey, any>;
|
|
5
5
|
type MergedInitialContext<TInitial extends Context, TAdditional extends Context, TCurrent extends Context> = TInitial & Omit<TAdditional, keyof TCurrent>;
|
|
6
6
|
type MergedCurrentContext<T extends Context, U extends Context> = Omit<T, keyof U> & U;
|
|
7
|
+
type BuildContextWithAuth<TContext extends Context, TAuthContext> = MergedCurrentContext<TContext, {
|
|
8
|
+
auth: TAuthContext | ('auth' extends keyof TContext ? TContext['auth'] : never);
|
|
9
|
+
}>;
|
|
7
10
|
declare function mergeCurrentContext<T extends Context, U extends Context>(context: T, other: U): MergedCurrentContext<T, U>;
|
|
8
11
|
|
|
9
12
|
type ORPCErrorConstructorMapItemOptions<TData> = Omit<ORPCErrorOptions<TData>, 'defined' | 'status'>;
|
|
@@ -13,21 +16,56 @@ type ORPCErrorConstructorMap<T extends ErrorMap> = {
|
|
|
13
16
|
};
|
|
14
17
|
declare function createORPCErrorConstructorMap<T extends ErrorMap>(errors: T): ORPCErrorConstructorMap<T>;
|
|
15
18
|
|
|
16
|
-
|
|
19
|
+
type MiddlewareResult<TOutContext extends Context, TOutput> = Promisable<{
|
|
20
|
+
output: TOutput;
|
|
21
|
+
context: TOutContext;
|
|
22
|
+
}>;
|
|
23
|
+
interface MiddlewareNextFn<TOutput> {
|
|
24
|
+
<U extends Context = {}>(options?: {
|
|
25
|
+
context?: U;
|
|
26
|
+
}): MiddlewareResult<U, TOutput>;
|
|
27
|
+
}
|
|
28
|
+
interface MiddlewareOutputFn<TOutput> {
|
|
29
|
+
(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
|
|
30
|
+
}
|
|
31
|
+
interface MiddlewareOptions<TInContext extends Context, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> extends ProcedureHandlerOptions<TInContext, TErrorConstructorMap, TMeta> {
|
|
32
|
+
next: MiddlewareNextFn<TOutput>;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* A function that represents a middleware.
|
|
36
|
+
*
|
|
37
|
+
* @see {@link https://orpc.unnoq.com/docs/middleware Middleware Docs}
|
|
38
|
+
*/
|
|
39
|
+
interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
|
|
40
|
+
(options: MiddlewareOptions<TInContext, TOutput, TErrorConstructorMap, TMeta>, input: TInput, output: MiddlewareOutputFn<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
|
|
41
|
+
}
|
|
42
|
+
type AnyMiddleware = Middleware<any, any, any, any, any, any>;
|
|
43
|
+
interface MapInputMiddleware<TInput, TMappedInput> {
|
|
44
|
+
(input: TInput): TMappedInput;
|
|
45
|
+
}
|
|
46
|
+
declare function middlewareOutputFn<TOutput>(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
|
|
47
|
+
|
|
48
|
+
type DefaultProcedureHandlerOptions = ProcedureHandlerOptions<Context, ORPCErrorConstructorMap<ErrorMap>, Meta>;
|
|
49
|
+
interface ProcedureHandlerOptions<TCurrentContext extends Context, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
|
|
17
50
|
context: TCurrentContext;
|
|
18
|
-
input: TInput;
|
|
19
51
|
path: readonly string[];
|
|
20
52
|
procedure: Procedure<Context, Context, Schemas, ErrorMap, TMeta>;
|
|
21
53
|
signal?: AbortSignal;
|
|
54
|
+
request?: StandardLazyRequest;
|
|
22
55
|
lastEventId: string | undefined;
|
|
23
56
|
errors: TErrorConstructorMap;
|
|
24
57
|
}
|
|
25
|
-
interface ProcedureHandler<TCurrentContext extends Context, TInput
|
|
26
|
-
|
|
58
|
+
interface ProcedureHandler<TCurrentContext extends Context, TInput extends {
|
|
59
|
+
path: any;
|
|
60
|
+
query: any;
|
|
61
|
+
body: any;
|
|
62
|
+
}, THandlerOutput, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
|
63
|
+
(input: TInput, opt: ProcedureHandlerOptions<TCurrentContext, ORPCErrorConstructorMap<TErrorMap>, TMeta>): Promisable<THandlerOutput>;
|
|
27
64
|
}
|
|
28
65
|
interface ProcedureDef<TInitialContext extends Context, TCurrentContext extends Context, TSchemas extends Schemas, TErrorMap extends ErrorMap, TMeta extends Meta> extends ContractProcedureDef<TSchemas, TErrorMap, TMeta> {
|
|
29
66
|
__initialContext?: (type: TInitialContext) => unknown;
|
|
30
67
|
middlewares: readonly AnyMiddleware[];
|
|
68
|
+
authConfigs: TypedAuthConfig[];
|
|
31
69
|
inputValidationIndex: number;
|
|
32
70
|
outputValidationIndex: number;
|
|
33
71
|
handler: ProcedureHandler<TCurrentContext, any, any, any, any>;
|
|
@@ -47,43 +85,23 @@ declare class Procedure<TInitialContext extends Context, TCurrentContext extends
|
|
|
47
85
|
type AnyProcedure = Procedure<any, any, Schemas, any, any>;
|
|
48
86
|
declare function isProcedure(item: unknown): item is AnyProcedure;
|
|
49
87
|
|
|
50
|
-
type
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
}
|
|
59
|
-
interface
|
|
60
|
-
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
path: readonly string[];
|
|
68
|
-
procedure: Procedure<Context, Context, Schemas, ErrorMap, TMeta>;
|
|
69
|
-
signal?: AbortSignal;
|
|
70
|
-
lastEventId: string | undefined;
|
|
71
|
-
next: MiddlewareNextFn<TOutput>;
|
|
72
|
-
errors: TErrorConstructorMap;
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* A function that represents a middleware.
|
|
76
|
-
*
|
|
77
|
-
* @see {@link https://orpc.unnoq.com/docs/middleware Middleware Docs}
|
|
78
|
-
*/
|
|
79
|
-
interface Middleware<TInContext extends Context, TOutContext extends Context, TInput, TOutput, TErrorConstructorMap extends ORPCErrorConstructorMap<any>, TMeta extends Meta> {
|
|
80
|
-
(options: MiddlewareOptions<TInContext, TOutput, TErrorConstructorMap, TMeta>, input: TInput, output: MiddlewareOutputFn<TOutput>): Promisable<MiddlewareResult<TOutContext, TOutput>>;
|
|
81
|
-
}
|
|
82
|
-
type AnyMiddleware = Middleware<any, any, any, any, any, any>;
|
|
83
|
-
interface MapInputMiddleware<TInput, TMappedInput> {
|
|
84
|
-
(input: TInput): TMappedInput;
|
|
85
|
-
}
|
|
86
|
-
declare function middlewareOutputFn<TOutput>(output: TOutput): MiddlewareResult<Record<never, never>, TOutput>;
|
|
88
|
+
type ValidatedAuthContext = {};
|
|
89
|
+
interface BasicAuthConfig<TAuthContext extends ValidatedAuthContext, TCurrentContext extends Context, TMeta extends Meta> {
|
|
90
|
+
tokenPrefix?: string;
|
|
91
|
+
validate: (username: string, password: string, options: ProcedureHandlerOptions<TCurrentContext, ORPCErrorConstructorMap<any>, TMeta>) => Promisable<TAuthContext>;
|
|
92
|
+
}
|
|
93
|
+
interface TokenAuthConfig<TAuthContext extends ValidatedAuthContext, TCurrentContext extends Context, TMeta extends Meta> {
|
|
94
|
+
tokenPrefix?: string;
|
|
95
|
+
validate: (token: string, options: ProcedureHandlerOptions<TCurrentContext, ORPCErrorConstructorMap<any>, TMeta>) => Promisable<TAuthContext>;
|
|
96
|
+
}
|
|
97
|
+
interface NamedTokenAuthConfig<TAuthContext extends ValidatedAuthContext, TCurrentContext extends Context, TMeta extends Meta> extends TokenAuthConfig<TAuthContext, TCurrentContext, TMeta> {
|
|
98
|
+
name: string;
|
|
99
|
+
}
|
|
100
|
+
type AuthType = 'header' | 'query' | 'cookie' | 'bearer' | 'basic' | 'none';
|
|
101
|
+
type AuthConfig<TAuthType extends AuthType, TAuthContext extends ValidatedAuthContext = ValidatedAuthContext, TCurrentContext extends Context = Context, TMeta extends Meta = Meta> = TAuthType extends 'basic' ? BasicAuthConfig<TAuthContext, TCurrentContext, TMeta> : TAuthType extends 'bearer' ? TokenAuthConfig<TAuthContext, TCurrentContext, TMeta> : TAuthType extends 'header' ? NamedTokenAuthConfig<TAuthContext, TCurrentContext, TMeta> : TAuthType extends 'query' ? NamedTokenAuthConfig<TAuthContext, TCurrentContext, TMeta> : TAuthType extends 'cookie' ? NamedTokenAuthConfig<TAuthContext, TCurrentContext, TMeta> : TAuthType extends 'none' ? {} : never;
|
|
102
|
+
type TypedAuthConfig<TAuthType extends AuthType = AuthType> = {
|
|
103
|
+
type: TAuthType;
|
|
104
|
+
} & AuthConfig<TAuthType, object>;
|
|
87
105
|
|
|
88
106
|
/**
|
|
89
107
|
* Represents a router, which defines a hierarchical structure of procedures.
|
|
@@ -120,7 +138,7 @@ type InferRouterCurrentContexts<T extends AnyRouter> = T extends Procedure<any,
|
|
|
120
138
|
* @info A procedure is a router too.
|
|
121
139
|
* @see {@link https://orpc.unnoq.com/docs/router#utilities Router Utilities Docs}
|
|
122
140
|
*/
|
|
123
|
-
type InferRouterInputs<T extends AnyRouter> = T extends Procedure<any, any, infer USchemas, any, any> ?
|
|
141
|
+
type InferRouterInputs<T extends AnyRouter> = T extends Procedure<any, any, infer USchemas, any, any> ? InferProcedureClientInputs<USchemas> : {
|
|
124
142
|
[K in keyof T]: T[K] extends Lazyable<infer U extends AnyRouter> ? InferRouterInputs<U> : never;
|
|
125
143
|
};
|
|
126
144
|
/**
|
|
@@ -203,15 +221,13 @@ declare function unlazy<T extends Lazyable<any>>(lazied: T): Promise<{
|
|
|
203
221
|
default: T extends Lazy<infer U> ? U : T;
|
|
204
222
|
}>;
|
|
205
223
|
|
|
206
|
-
type ProcedureClient<TClientContext extends ClientContext, TSchemas extends Schemas, TErrorMap extends ErrorMap> = Client<TClientContext,
|
|
207
|
-
interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TErrorMap extends ErrorMap, TMeta extends Meta> {
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
signal?: AbortSignal;
|
|
214
|
-
lastEventId: string | undefined;
|
|
224
|
+
type ProcedureClient<TClientContext extends ClientContext, TSchemas extends Schemas, TErrorMap extends ErrorMap> = Client<TClientContext, InferProcedureClientInputs<TSchemas>, InferSchemaOutput<TSchemas['outputSchema']>, ErrorFromErrorMap<TErrorMap>>;
|
|
225
|
+
interface ProcedureClientInterceptorOptions<TInitialContext extends Context, TErrorMap extends ErrorMap, TMeta extends Meta> extends ProcedureHandlerOptions<TInitialContext, ORPCErrorConstructorMap<TErrorMap>, TMeta> {
|
|
226
|
+
input: {
|
|
227
|
+
path: unknown;
|
|
228
|
+
query: unknown;
|
|
229
|
+
body: unknown;
|
|
230
|
+
};
|
|
215
231
|
}
|
|
216
232
|
type CreateProcedureClientOptions<TInitialContext extends Context, TOutputSchema extends AnySchema, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext> = {
|
|
217
233
|
/**
|
|
@@ -231,5 +247,5 @@ type CreateProcedureClientOptions<TInitialContext extends Context, TOutputSchema
|
|
|
231
247
|
*/
|
|
232
248
|
declare function createProcedureClient<TInitialContext extends Context, TSchemas extends Schemas, TErrorMap extends ErrorMap, TMeta extends Meta, TClientContext extends ClientContext>(lazyableProcedure: Lazyable<Procedure<TInitialContext, any, TSchemas, TErrorMap, TMeta>>, ...rest: MaybeOptionalOptions<CreateProcedureClientOptions<TInitialContext, TSchemas['outputSchema'], TErrorMap, TMeta, TClientContext>>): ProcedureClient<TClientContext, TSchemas, TErrorMap>;
|
|
233
249
|
|
|
234
|
-
export {
|
|
235
|
-
export type {
|
|
250
|
+
export { enhanceRouter as $, middlewareOutputFn as F, isProcedure as K, Procedure as P, createProcedureClient as Q, getRouter as Y, createAccessibleLazyRouter as _, traverseContractProcedures as a4, resolveContractProcedures as a5, unlazyRouter as a7, mergeCurrentContext as m, createORPCErrorConstructorMap as p, LAZY_SYMBOL as q, lazyInternal as s, lazy as t, isLazy as u, getLazyMeta as v, unlazy as w };
|
|
251
|
+
export type { AuthType as A, BuildContextWithAuth as B, Context as C, MiddlewareOptions as D, EnhanceRouterOptions as E, DefaultProcedureHandlerOptions as G, ProcedureHandlerOptions as H, InferRouterInitialContext as I, ProcedureDef as J, Lazy as L, Middleware as M, ProcedureClientInterceptorOptions as N, ORPCErrorConstructorMap as O, Router as R, InferRouterInitialContexts as S, TypedAuthConfig as T, InferRouterCurrentContexts as U, ValidatedAuthContext as V, InferRouterInputs as W, InferRouterOutputs as X, AccessibleLazyRouter as Z, CreateProcedureClientOptions as a, TraverseContractProceduresOptions as a0, TraverseContractProcedureCallbackOptions as a1, ContractProcedureCallbackOptions as a2, LazyTraverseContractProceduresOptions as a3, UnlaziedRouter as a6, ProcedureClient as b, MergedInitialContext as c, MergedCurrentContext as d, AuthConfig as e, ProcedureHandler as f, EnhancedRouter as g, MapInputMiddleware as h, AnyMiddleware as i, AnyProcedure as j, Lazyable as k, AnyRouter as l, ORPCErrorConstructorMapItemOptions as n, ORPCErrorConstructorMapItem as o, LazyMeta as r, MiddlewareResult as x, MiddlewareNextFn as y, MiddlewareOutputFn as z };
|