@temporary-name/server 1.9.3-alpha.afd18ec2afa743b08cf1b5c2eb6252ded18a1f43 → 1.9.3-alpha.bb3867758271e7fcf8c191f9693365d655697e7f
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 +5 -7
- package/dist/adapters/fetch/index.d.mts +3 -4
- package/dist/adapters/fetch/index.d.ts +3 -4
- package/dist/adapters/fetch/index.mjs +5 -7
- package/dist/adapters/node/index.d.mts +3 -4
- package/dist/adapters/node/index.d.ts +3 -4
- package/dist/adapters/node/index.mjs +5 -7
- package/dist/adapters/standard/index.d.mts +20 -78
- package/dist/adapters/standard/index.d.ts +20 -78
- package/dist/adapters/standard/index.mjs +5 -7
- package/dist/index.d.mts +69 -339
- package/dist/index.d.ts +69 -339
- package/dist/index.mjs +132 -357
- package/dist/openapi/index.d.mts +1 -2
- package/dist/openapi/index.d.ts +1 -2
- package/dist/openapi/index.mjs +63 -88
- 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.BeuTpcmO.d.mts → server.7aL9gcoU.d.mts} +2 -2
- package/dist/shared/server.BL2R5jcp.d.mts +228 -0
- package/dist/shared/server.BL2R5jcp.d.ts +228 -0
- package/dist/shared/server.C61o1Zch.mjs +413 -0
- package/dist/shared/{server.CQyYNJ1H.d.ts → server.D6Qs_UcF.d.mts} +2 -4
- package/dist/shared/{server.SLLuK6_v.d.ts → server.DFptr1Nz.d.ts} +2 -2
- package/dist/shared/{server.C1fnTLq0.d.mts → server.DpoO_ER_.d.ts} +2 -4
- package/dist/shared/{server._YqJjI50.mjs → server.DwbIdsnK.mjs} +58 -101
- package/dist/shared/server.JtIZ8YG7.mjs +237 -0
- package/package.json +10 -9
- package/dist/shared/server.BKSOrA6h.d.mts +0 -192
- package/dist/shared/server.BKSOrA6h.d.ts +0 -192
- package/dist/shared/server.BKh8I1Ny.mjs +0 -239
- package/dist/shared/server.DhdDYN-Z.mjs +0 -261
package/dist/openapi/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { isProcedure, resolveContractProcedures, ORPCError, createRouterClient } from '@temporary-name/server';
|
|
2
|
-
import { fallbackContractConfig, getEventIteratorSchemaDetails } from '@temporary-name/contract';
|
|
2
|
+
import { standardizeHTTPPath, fallbackContractConfig, getDynamicParams, getEventIteratorSchemaDetails } from '@temporary-name/contract';
|
|
3
3
|
import { isObject, stringifyJSON, findDeepMatches, toArray, clone, value, toHttpPath, isORPCErrorStatus, fallbackORPCErrorStatus, fallbackORPCErrorMessage, resolveMaybeOptionalOptions, createORPCErrorFromJson } from '@temporary-name/shared';
|
|
4
|
-
import {
|
|
4
|
+
import { j as jsonSerialize, d as deserialize, s as serialize } from '../shared/server.JtIZ8YG7.mjs';
|
|
5
5
|
import '@temporary-name/standard-server';
|
|
6
6
|
import { TypeName } from '@temporary-name/interop/json-schema-typed/draft-2020-12';
|
|
7
7
|
export { ContentEncoding as JSONSchemaContentEncoding, Format as JSONSchemaFormat, TypeName as JSONSchemaTypeName } from '@temporary-name/interop/json-schema-typed/draft-2020-12';
|
|
@@ -380,10 +380,8 @@ class CompositeSchemaConverter {
|
|
|
380
380
|
class OpenAPIGeneratorError extends Error {
|
|
381
381
|
}
|
|
382
382
|
class OpenAPIGenerator {
|
|
383
|
-
serializer;
|
|
384
383
|
converter;
|
|
385
384
|
constructor(options = {}) {
|
|
386
|
-
this.serializer = new StandardOpenAPIJsonSerializer();
|
|
387
385
|
this.converter = new CompositeSchemaConverter(toArray(options.schemaConverters));
|
|
388
386
|
}
|
|
389
387
|
/**
|
|
@@ -464,7 +462,7 @@ ${e.message}`
|
|
|
464
462
|
${errors.join("\n\n")}`
|
|
465
463
|
);
|
|
466
464
|
}
|
|
467
|
-
return
|
|
465
|
+
return jsonSerialize(doc)[0];
|
|
468
466
|
}
|
|
469
467
|
async #resolveCommonSchemas(doc, commonSchemas) {
|
|
470
468
|
let undefinedErrorJsonSchema = {
|
|
@@ -532,94 +530,77 @@ ${errors.join("\n\n")}`
|
|
|
532
530
|
}
|
|
533
531
|
async #request(doc, ref, def, baseSchemaConvertOptions) {
|
|
534
532
|
const method = fallbackContractConfig("defaultMethod", def.route.method);
|
|
535
|
-
const details = getEventIteratorSchemaDetails(def.inputSchema);
|
|
536
|
-
if (details) {
|
|
537
|
-
ref.requestBody = {
|
|
538
|
-
required: true,
|
|
539
|
-
content: toOpenAPIEventIteratorContent(
|
|
540
|
-
await this.converter.convert(details.yields, { ...baseSchemaConvertOptions, strategy: "input" }),
|
|
541
|
-
await this.converter.convert(details.returns, { ...baseSchemaConvertOptions, strategy: "input" })
|
|
542
|
-
)
|
|
543
|
-
};
|
|
544
|
-
return;
|
|
545
|
-
}
|
|
546
533
|
const dynamicParams = getDynamicParams(def.route.path)?.map((v) => v.name);
|
|
547
|
-
const
|
|
548
|
-
let [required, schema] = await this.converter.convert(def.inputSchema, {
|
|
534
|
+
const [_pathRequired, pathSchema] = await this.converter.convert(def.schemas.pathSchema, {
|
|
549
535
|
...baseSchemaConvertOptions,
|
|
550
536
|
strategy: "input",
|
|
551
|
-
minStructureDepthForRef:
|
|
537
|
+
minStructureDepthForRef: 1
|
|
552
538
|
});
|
|
553
|
-
if (
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
);
|
|
561
|
-
if (!isObjectSchema(schema)) {
|
|
562
|
-
throw error2;
|
|
563
|
-
}
|
|
564
|
-
const [paramsSchema, rest] = separateObjectSchema(schema, dynamicParams);
|
|
565
|
-
schema = rest;
|
|
566
|
-
required = rest.required ? rest.required.length !== 0 : false;
|
|
567
|
-
if (!checkParamsSchema(paramsSchema, dynamicParams)) {
|
|
568
|
-
throw error2;
|
|
569
|
-
}
|
|
570
|
-
ref.parameters ??= [];
|
|
571
|
-
ref.parameters.push(...toOpenAPIParameters(paramsSchema, "path"));
|
|
539
|
+
if (dynamicParams?.length) {
|
|
540
|
+
const error = new OpenAPIGeneratorError(
|
|
541
|
+
// TODO: fix this error
|
|
542
|
+
'When input structure is "compact", and path has dynamic params, input schema must be an object with all dynamic params as required.'
|
|
543
|
+
);
|
|
544
|
+
if (!isObjectSchema(pathSchema)) {
|
|
545
|
+
throw error;
|
|
572
546
|
}
|
|
573
|
-
if (
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
547
|
+
if (!checkParamsSchema(pathSchema, dynamicParams)) {
|
|
548
|
+
throw error;
|
|
549
|
+
}
|
|
550
|
+
ref.parameters ??= [];
|
|
551
|
+
ref.parameters.push(...toOpenAPIParameters(pathSchema, "path"));
|
|
552
|
+
} else {
|
|
553
|
+
const error = new OpenAPIGeneratorError("Params set via path do not match those on the route");
|
|
554
|
+
if (!isObjectSchema(pathSchema)) {
|
|
555
|
+
console.log("FOO", pathSchema);
|
|
556
|
+
throw error;
|
|
557
|
+
}
|
|
558
|
+
if (!checkParamsSchema(pathSchema, [])) {
|
|
559
|
+
console.log("BAR", pathSchema);
|
|
560
|
+
throw error;
|
|
587
561
|
}
|
|
588
|
-
return;
|
|
589
|
-
}
|
|
590
|
-
const error = new OpenAPIGeneratorError(
|
|
591
|
-
'When input structure is "detailed", input schema must satisfy: { params?: Record<string, unknown>, query?: Record<string, unknown>, headers?: Record<string, unknown>, body?: unknown }'
|
|
592
|
-
);
|
|
593
|
-
if (!isObjectSchema(schema)) {
|
|
594
|
-
throw error;
|
|
595
562
|
}
|
|
596
|
-
const
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
563
|
+
const [_queryRequired, querySchema] = await this.converter.convert(def.schemas.querySchema, {
|
|
564
|
+
...baseSchemaConvertOptions,
|
|
565
|
+
strategy: "input",
|
|
566
|
+
minStructureDepthForRef: 0
|
|
567
|
+
});
|
|
568
|
+
if (!isAnySchema(querySchema)) {
|
|
569
|
+
const resolvedSchema = resolveOpenAPIJsonSchemaRef(doc, querySchema);
|
|
570
|
+
if (!isObjectSchema(resolvedSchema)) {
|
|
571
|
+
throw new OpenAPIGeneratorError("Query param schema must satisfy: object | any | unknown");
|
|
572
|
+
}
|
|
573
|
+
ref.parameters ??= [];
|
|
574
|
+
ref.parameters.push(...toOpenAPIParameters(resolvedSchema, "query"));
|
|
601
575
|
}
|
|
602
|
-
|
|
603
|
-
const
|
|
604
|
-
if (
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
576
|
+
if (method !== "GET") {
|
|
577
|
+
const details = getEventIteratorSchemaDetails(def.schemas.bodySchema);
|
|
578
|
+
if (details) {
|
|
579
|
+
ref.requestBody = {
|
|
580
|
+
required: true,
|
|
581
|
+
content: toOpenAPIEventIteratorContent(
|
|
582
|
+
await this.converter.convert(details.yields, { ...baseSchemaConvertOptions, strategy: "input" }),
|
|
583
|
+
await this.converter.convert(details.returns, { ...baseSchemaConvertOptions, strategy: "input" })
|
|
584
|
+
)
|
|
585
|
+
};
|
|
586
|
+
} else {
|
|
587
|
+
const [bodyRequired, bodySchema] = await this.converter.convert(def.schemas.bodySchema, {
|
|
588
|
+
...baseSchemaConvertOptions,
|
|
589
|
+
strategy: "input",
|
|
590
|
+
minStructureDepthForRef: 0
|
|
591
|
+
});
|
|
592
|
+
if (isAnySchema(bodySchema)) {
|
|
593
|
+
return;
|
|
608
594
|
}
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
595
|
+
ref.requestBody = {
|
|
596
|
+
required: bodyRequired,
|
|
597
|
+
content: toOpenAPIContent(bodySchema)
|
|
598
|
+
};
|
|
612
599
|
}
|
|
613
600
|
}
|
|
614
|
-
if (schema.properties?.body !== void 0) {
|
|
615
|
-
ref.requestBody = {
|
|
616
|
-
required: schema.required?.includes("body"),
|
|
617
|
-
content: toOpenAPIContent(schema.properties.body)
|
|
618
|
-
};
|
|
619
|
-
}
|
|
620
601
|
}
|
|
621
602
|
async #successResponse(doc, ref, def, baseSchemaConvertOptions) {
|
|
622
|
-
const outputSchema = def.outputSchema;
|
|
603
|
+
const outputSchema = def.schemas.outputSchema;
|
|
623
604
|
const status = fallbackContractConfig("defaultSuccessStatus", def.route.successStatus);
|
|
624
605
|
const description = fallbackContractConfig("defaultSuccessDescription", def.route?.successDescription);
|
|
625
606
|
const eventIteratorSchemaDetails = getEventIteratorSchemaDetails(outputSchema);
|
|
@@ -757,19 +738,13 @@ ${errors.join("\n\n")}`
|
|
|
757
738
|
|
|
758
739
|
function createJsonifiedRouterClient(router, ...rest) {
|
|
759
740
|
const options = resolveMaybeOptionalOptions(rest);
|
|
760
|
-
const serializer = new StandardOpenAPISerializer(
|
|
761
|
-
new StandardOpenAPIJsonSerializer(),
|
|
762
|
-
new StandardBracketNotationSerializer()
|
|
763
|
-
);
|
|
764
741
|
options.interceptors ??= [];
|
|
765
742
|
options.interceptors.unshift(async (options2) => {
|
|
766
743
|
try {
|
|
767
|
-
return
|
|
744
|
+
return deserialize(serialize(await options2.next()));
|
|
768
745
|
} catch (e) {
|
|
769
746
|
if (e instanceof ORPCError) {
|
|
770
|
-
throw createORPCErrorFromJson(
|
|
771
|
-
serializer.deserialize(serializer.serialize(e.toJSON(), { outputFormat: "plain" }))
|
|
772
|
-
);
|
|
747
|
+
throw createORPCErrorFromJson(deserialize(serialize(e.toJSON(), { outputFormat: "plain" })));
|
|
773
748
|
}
|
|
774
749
|
throw e;
|
|
775
750
|
}
|
package/dist/plugins/index.d.mts
CHANGED
|
@@ -1,58 +1,8 @@
|
|
|
1
1
|
import { Value, Promisable, ORPCError } from '@temporary-name/shared';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { S as StandardHandlerInterceptorOptions, a as StandardHandlerPlugin, b as StandardHandlerOptions } from '../shared/server.C1fnTLq0.mjs';
|
|
5
|
-
import { C as Context, E as ProcedureClientInterceptorOptions } from '../shared/server.BKSOrA6h.mjs';
|
|
2
|
+
import { S as StandardHandlerInterceptorOptions, a as StandardHandlerPlugin, b as StandardHandlerOptions } from '../shared/server.D6Qs_UcF.mjs';
|
|
3
|
+
import { C as Context, G as ProcedureClientInterceptorOptions } from '../shared/server.BL2R5jcp.mjs';
|
|
6
4
|
import { Meta } from '@temporary-name/contract';
|
|
7
5
|
|
|
8
|
-
interface BatchHandlerOptions<T extends Context> {
|
|
9
|
-
/**
|
|
10
|
-
* The max size of the batch allowed.
|
|
11
|
-
*
|
|
12
|
-
* @default 10
|
|
13
|
-
*/
|
|
14
|
-
maxSize?: Value<Promisable<number>, [StandardHandlerInterceptorOptions<T>]>;
|
|
15
|
-
/**
|
|
16
|
-
* Map the request before processing it.
|
|
17
|
-
*
|
|
18
|
-
* @default merged back batch request headers into the request
|
|
19
|
-
*/
|
|
20
|
-
mapRequestItem?(request: StandardRequest, batchOptions: StandardHandlerInterceptorOptions<T>): StandardRequest;
|
|
21
|
-
/**
|
|
22
|
-
* Success batch response status code.
|
|
23
|
-
*
|
|
24
|
-
* @default 207
|
|
25
|
-
*/
|
|
26
|
-
successStatus?: Value<Promisable<number>, [
|
|
27
|
-
responses: Promise<BatchResponseBodyItem>[],
|
|
28
|
-
batchOptions: StandardHandlerInterceptorOptions<T>
|
|
29
|
-
]>;
|
|
30
|
-
/**
|
|
31
|
-
* success batch response headers.
|
|
32
|
-
*
|
|
33
|
-
* @default {}
|
|
34
|
-
*/
|
|
35
|
-
headers?: Value<Promisable<StandardHeaders>, [
|
|
36
|
-
responses: Promise<BatchResponseBodyItem>[],
|
|
37
|
-
batchOptions: StandardHandlerInterceptorOptions<T>
|
|
38
|
-
]>;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* The Batch Requests Plugin allows you to combine multiple requests and responses into a single batch,
|
|
42
|
-
* reducing the overhead of sending each one separately.
|
|
43
|
-
*
|
|
44
|
-
* @see {@link https://orpc.unnoq.com/docs/plugins/batch-requests Batch Requests Plugin Docs}
|
|
45
|
-
*/
|
|
46
|
-
declare class BatchHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
47
|
-
private readonly maxSize;
|
|
48
|
-
private readonly mapRequestItem;
|
|
49
|
-
private readonly successStatus;
|
|
50
|
-
private readonly headers;
|
|
51
|
-
order: number;
|
|
52
|
-
constructor(options?: BatchHandlerOptions<T>);
|
|
53
|
-
init(options: StandardHandlerOptions<T>): void;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
6
|
interface CORSOptions<T extends Context> {
|
|
57
7
|
origin?: Value<Promisable<string | readonly string[] | null | undefined>, [
|
|
58
8
|
origin: string,
|
|
@@ -80,32 +30,6 @@ declare class CORSPlugin<T extends Context> implements StandardHandlerPlugin<T>
|
|
|
80
30
|
init(options: StandardHandlerOptions<T>): void;
|
|
81
31
|
}
|
|
82
32
|
|
|
83
|
-
interface RequestHeadersPluginContext {
|
|
84
|
-
reqHeaders?: Headers;
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* The Request Headers Plugin injects a `reqHeaders` instance into the context,
|
|
88
|
-
* allowing access to request headers in oRPC.
|
|
89
|
-
*
|
|
90
|
-
* @see {@link https://orpc.unnoq.com/docs/plugins/request-headers Request Headers Plugin Docs}
|
|
91
|
-
*/
|
|
92
|
-
declare class RequestHeadersPlugin<T extends RequestHeadersPluginContext> implements StandardHandlerPlugin<T> {
|
|
93
|
-
init(options: StandardHandlerOptions<T>): void;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
interface ResponseHeadersPluginContext {
|
|
97
|
-
resHeaders?: Headers;
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* The Response Headers Plugin allows you to set response headers in oRPC.
|
|
101
|
-
* It injects a resHeaders instance into the context, enabling you to modify response headers easily.
|
|
102
|
-
*
|
|
103
|
-
* @see {@link https://orpc.unnoq.com/docs/plugins/response-headers Response Headers Plugin Docs}
|
|
104
|
-
*/
|
|
105
|
-
declare class ResponseHeadersPlugin<T extends ResponseHeadersPluginContext> implements StandardHandlerPlugin<T> {
|
|
106
|
-
init(options: StandardHandlerOptions<T>): void;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
33
|
interface SimpleCsrfProtectionHandlerPluginOptions<T extends Context> {
|
|
110
34
|
/**
|
|
111
35
|
* The name of the header to check.
|
|
@@ -156,5 +80,5 @@ declare class SimpleCsrfProtectionHandlerPlugin<T extends Context> implements St
|
|
|
156
80
|
init(options: StandardHandlerOptions<T>): void;
|
|
157
81
|
}
|
|
158
82
|
|
|
159
|
-
export {
|
|
160
|
-
export type {
|
|
83
|
+
export { CORSPlugin, SimpleCsrfProtectionHandlerPlugin };
|
|
84
|
+
export type { CORSOptions, SimpleCsrfProtectionHandlerPluginOptions };
|
package/dist/plugins/index.d.ts
CHANGED
|
@@ -1,58 +1,8 @@
|
|
|
1
1
|
import { Value, Promisable, ORPCError } from '@temporary-name/shared';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { S as StandardHandlerInterceptorOptions, a as StandardHandlerPlugin, b as StandardHandlerOptions } from '../shared/server.CQyYNJ1H.js';
|
|
5
|
-
import { C as Context, E as ProcedureClientInterceptorOptions } from '../shared/server.BKSOrA6h.js';
|
|
2
|
+
import { S as StandardHandlerInterceptorOptions, a as StandardHandlerPlugin, b as StandardHandlerOptions } from '../shared/server.DpoO_ER_.js';
|
|
3
|
+
import { C as Context, G as ProcedureClientInterceptorOptions } from '../shared/server.BL2R5jcp.js';
|
|
6
4
|
import { Meta } from '@temporary-name/contract';
|
|
7
5
|
|
|
8
|
-
interface BatchHandlerOptions<T extends Context> {
|
|
9
|
-
/**
|
|
10
|
-
* The max size of the batch allowed.
|
|
11
|
-
*
|
|
12
|
-
* @default 10
|
|
13
|
-
*/
|
|
14
|
-
maxSize?: Value<Promisable<number>, [StandardHandlerInterceptorOptions<T>]>;
|
|
15
|
-
/**
|
|
16
|
-
* Map the request before processing it.
|
|
17
|
-
*
|
|
18
|
-
* @default merged back batch request headers into the request
|
|
19
|
-
*/
|
|
20
|
-
mapRequestItem?(request: StandardRequest, batchOptions: StandardHandlerInterceptorOptions<T>): StandardRequest;
|
|
21
|
-
/**
|
|
22
|
-
* Success batch response status code.
|
|
23
|
-
*
|
|
24
|
-
* @default 207
|
|
25
|
-
*/
|
|
26
|
-
successStatus?: Value<Promisable<number>, [
|
|
27
|
-
responses: Promise<BatchResponseBodyItem>[],
|
|
28
|
-
batchOptions: StandardHandlerInterceptorOptions<T>
|
|
29
|
-
]>;
|
|
30
|
-
/**
|
|
31
|
-
* success batch response headers.
|
|
32
|
-
*
|
|
33
|
-
* @default {}
|
|
34
|
-
*/
|
|
35
|
-
headers?: Value<Promisable<StandardHeaders>, [
|
|
36
|
-
responses: Promise<BatchResponseBodyItem>[],
|
|
37
|
-
batchOptions: StandardHandlerInterceptorOptions<T>
|
|
38
|
-
]>;
|
|
39
|
-
}
|
|
40
|
-
/**
|
|
41
|
-
* The Batch Requests Plugin allows you to combine multiple requests and responses into a single batch,
|
|
42
|
-
* reducing the overhead of sending each one separately.
|
|
43
|
-
*
|
|
44
|
-
* @see {@link https://orpc.unnoq.com/docs/plugins/batch-requests Batch Requests Plugin Docs}
|
|
45
|
-
*/
|
|
46
|
-
declare class BatchHandlerPlugin<T extends Context> implements StandardHandlerPlugin<T> {
|
|
47
|
-
private readonly maxSize;
|
|
48
|
-
private readonly mapRequestItem;
|
|
49
|
-
private readonly successStatus;
|
|
50
|
-
private readonly headers;
|
|
51
|
-
order: number;
|
|
52
|
-
constructor(options?: BatchHandlerOptions<T>);
|
|
53
|
-
init(options: StandardHandlerOptions<T>): void;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
6
|
interface CORSOptions<T extends Context> {
|
|
57
7
|
origin?: Value<Promisable<string | readonly string[] | null | undefined>, [
|
|
58
8
|
origin: string,
|
|
@@ -80,32 +30,6 @@ declare class CORSPlugin<T extends Context> implements StandardHandlerPlugin<T>
|
|
|
80
30
|
init(options: StandardHandlerOptions<T>): void;
|
|
81
31
|
}
|
|
82
32
|
|
|
83
|
-
interface RequestHeadersPluginContext {
|
|
84
|
-
reqHeaders?: Headers;
|
|
85
|
-
}
|
|
86
|
-
/**
|
|
87
|
-
* The Request Headers Plugin injects a `reqHeaders` instance into the context,
|
|
88
|
-
* allowing access to request headers in oRPC.
|
|
89
|
-
*
|
|
90
|
-
* @see {@link https://orpc.unnoq.com/docs/plugins/request-headers Request Headers Plugin Docs}
|
|
91
|
-
*/
|
|
92
|
-
declare class RequestHeadersPlugin<T extends RequestHeadersPluginContext> implements StandardHandlerPlugin<T> {
|
|
93
|
-
init(options: StandardHandlerOptions<T>): void;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
interface ResponseHeadersPluginContext {
|
|
97
|
-
resHeaders?: Headers;
|
|
98
|
-
}
|
|
99
|
-
/**
|
|
100
|
-
* The Response Headers Plugin allows you to set response headers in oRPC.
|
|
101
|
-
* It injects a resHeaders instance into the context, enabling you to modify response headers easily.
|
|
102
|
-
*
|
|
103
|
-
* @see {@link https://orpc.unnoq.com/docs/plugins/response-headers Response Headers Plugin Docs}
|
|
104
|
-
*/
|
|
105
|
-
declare class ResponseHeadersPlugin<T extends ResponseHeadersPluginContext> implements StandardHandlerPlugin<T> {
|
|
106
|
-
init(options: StandardHandlerOptions<T>): void;
|
|
107
|
-
}
|
|
108
|
-
|
|
109
33
|
interface SimpleCsrfProtectionHandlerPluginOptions<T extends Context> {
|
|
110
34
|
/**
|
|
111
35
|
* The name of the header to check.
|
|
@@ -156,5 +80,5 @@ declare class SimpleCsrfProtectionHandlerPlugin<T extends Context> implements St
|
|
|
156
80
|
init(options: StandardHandlerOptions<T>): void;
|
|
157
81
|
}
|
|
158
82
|
|
|
159
|
-
export {
|
|
160
|
-
export type {
|
|
83
|
+
export { CORSPlugin, SimpleCsrfProtectionHandlerPlugin };
|
|
84
|
+
export type { CORSOptions, SimpleCsrfProtectionHandlerPluginOptions };
|
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,6 +1,6 @@
|
|
|
1
1
|
import { HTTPPath } from '@temporary-name/shared';
|
|
2
|
-
import { C as Context } from './server.
|
|
3
|
-
import { c as StandardHandleOptions } from './server.
|
|
2
|
+
import { C as Context } from './server.BL2R5jcp.mjs';
|
|
3
|
+
import { c as StandardHandleOptions } from './server.D6Qs_UcF.mjs';
|
|
4
4
|
|
|
5
5
|
type FriendlyStandardHandleOptions<T extends Context> = Omit<StandardHandleOptions<T>, 'context'> & (Record<never, never> extends T ? {
|
|
6
6
|
context?: T;
|