@orpc/openapi 2.0.0-beta.46 → 2.0.0-beta.48
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/README.md +6 -4
- package/dist/adapters/aws-lambda/index.d.mts +14 -26
- package/dist/adapters/aws-lambda/index.mjs +1 -21
- package/dist/adapters/aws-lambda/openapi-handler.d.mts +17 -0
- package/dist/adapters/aws-lambda/openapi-handler.mjs +11 -0
- package/dist/adapters/fastify/index.d.mts +1 -23
- package/dist/adapters/fastify/index.mjs +1 -21
- package/dist/adapters/fastify/openapi-handler.d.mts +14 -0
- package/dist/adapters/fastify/openapi-handler.mjs +11 -0
- package/dist/adapters/fetch/index.d.mts +2 -37
- package/dist/adapters/fetch/index.mjs +2 -34
- package/dist/adapters/fetch/openapi-handler.d.mts +14 -0
- package/dist/adapters/fetch/openapi-handler.mjs +11 -0
- package/dist/adapters/fetch/openapi-link.d.mts +15 -0
- package/dist/adapters/fetch/openapi-link.mjs +11 -0
- package/dist/adapters/node/index.d.mts +1 -23
- package/dist/adapters/node/index.mjs +1 -21
- package/dist/adapters/node/openapi-handler.d.mts +14 -0
- package/dist/adapters/node/openapi-handler.mjs +11 -0
- package/dist/adapters/standard/index.d.mts +3 -48
- package/dist/adapters/standard/index.mjs +3 -12
- package/dist/adapters/standard/openapi-handler-codec.d.mts +61 -0
- package/dist/adapters/standard/openapi-handler-codec.mjs +203 -0
- package/dist/adapters/standard/openapi-link-codec.d.mts +53 -0
- package/dist/adapters/standard/openapi-link-codec.mjs +356 -0
- package/dist/adapters/standard/openapi-matcher.d.mts +29 -0
- package/dist/adapters/standard/openapi-matcher.mjs +105 -0
- package/dist/adapters/standard/utils.d.mts +4 -0
- package/dist/adapters/standard/utils.mjs +36 -0
- package/dist/bracket-notation.d.mts +26 -0
- package/dist/bracket-notation.mjs +156 -0
- package/dist/client-factory.d.mts +24 -0
- package/dist/client-factory.mjs +5 -0
- package/dist/constants.d.mts +4 -0
- package/dist/constants.mjs +4 -0
- package/dist/extensions/route.d.mts +74 -39
- package/dist/extensions/route.mjs +8 -10
- package/dist/helpers/form-data.d.mts +57 -0
- package/dist/helpers/form-data.mjs +36 -0
- package/dist/helpers/index.d.mts +1 -59
- package/dist/helpers/index.mjs +1 -39
- package/dist/index.d.mts +13 -159
- package/dist/index.mjs +11 -778
- package/dist/meta.d.mts +345 -0
- package/dist/meta.mjs +56 -0
- package/dist/openapi-generator-components.d.mts +24 -0
- package/dist/openapi-generator-components.mjs +227 -0
- package/dist/openapi-generator-operation.d.mts +29 -0
- package/dist/openapi-generator-operation.mjs +447 -0
- package/dist/openapi-generator.d.mts +87 -0
- package/dist/openapi-generator.mjs +115 -0
- package/dist/openapi-json-serializer.d.mts +86 -0
- package/dist/openapi-json-serializer.mjs +229 -0
- package/dist/openapi-serializer.d.mts +43 -0
- package/dist/openapi-serializer.mjs +87 -0
- package/dist/plugins/index.d.mts +1 -121
- package/dist/plugins/index.mjs +1 -167
- package/dist/plugins/openapi-reference.d.mts +118 -0
- package/dist/plugins/openapi-reference.mjs +157 -0
- package/dist/router-utils.d.mts +18 -0
- package/dist/router-utils.mjs +29 -0
- package/dist/types.d.mts +27 -0
- package/dist/types.mjs +1 -0
- package/dist/utils.d.mts +14 -0
- package/dist/utils.mjs +40 -0
- package/package.json +15 -15
- package/dist/adapters/aws-lambda/index.d.ts +0 -27
- package/dist/adapters/fastify/index.d.ts +0 -23
- package/dist/adapters/fetch/index.d.ts +0 -37
- package/dist/adapters/node/index.d.ts +0 -23
- package/dist/adapters/standard/index.d.ts +0 -48
- package/dist/extensions/route.d.ts +0 -41
- package/dist/helpers/index.d.ts +0 -59
- package/dist/index.d.ts +0 -159
- package/dist/plugins/index.d.ts +0 -121
- package/dist/shared/openapi.B9PQzqBn.mjs +0 -49
- package/dist/shared/openapi.BCrrWRbv.d.mts +0 -87
- package/dist/shared/openapi.BNe1y1cD.mjs +0 -360
- package/dist/shared/openapi.B_SoJfYz.d.mts +0 -328
- package/dist/shared/openapi.B_SoJfYz.d.ts +0 -328
- package/dist/shared/openapi.BkjX_6hE.d.ts +0 -87
- package/dist/shared/openapi.Covi8zVE.d.mts +0 -33
- package/dist/shared/openapi.Covi8zVE.d.ts +0 -33
- package/dist/shared/openapi.DXqJL3gB.mjs +0 -408
- package/dist/shared/openapi.DtEVcxuW.mjs +0 -149
- package/dist/shared/openapi.E3ZBiweM.mjs +0 -353
- package/dist/shared/openapi.YMRD3b4R.d.mts +0 -157
- package/dist/shared/openapi.YMRD3b4R.d.ts +0 -157
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { createPrefixMatcher, Router } from "@openapi-spec/router";
|
|
2
|
+
import { Procedure, resolveContractProcedure, unlazy, walkProcedureContractsSync } from "@orpc/server";
|
|
3
|
+
import { mergeHttpPath, pathToHttpPath, value } from "@orpc/shared";
|
|
4
|
+
import { DEFAULT_OPENAPI_METHOD } from "../../constants.mjs";
|
|
5
|
+
import { getOpenAPIMeta } from "../../meta.mjs";
|
|
6
|
+
export class OpenAPIMatcher {
|
|
7
|
+
filter;
|
|
8
|
+
rootRouter;
|
|
9
|
+
tree = new Router();
|
|
10
|
+
pendingLazyRouters = new Set();
|
|
11
|
+
constructor(router, options = {}) {
|
|
12
|
+
this.filter = options.filter ?? true;
|
|
13
|
+
this.rootRouter = router;
|
|
14
|
+
this.index(router);
|
|
15
|
+
}
|
|
16
|
+
index(router, path = []) {
|
|
17
|
+
const lazyResults = walkProcedureContractsSync(router, (contract, path) => {
|
|
18
|
+
if (!value(this.filter, contract, path)) {
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
const meta = getOpenAPIMeta(contract);
|
|
22
|
+
const method = meta?.method ?? DEFAULT_OPENAPI_METHOD;
|
|
23
|
+
const postHttpPath = meta?.path ?? pathToHttpPath(path);
|
|
24
|
+
const httpPath = meta?.prefix ? mergeHttpPath(meta.prefix, postHttpPath) : postHttpPath;
|
|
25
|
+
this.tree.add(method, httpPath, {
|
|
26
|
+
path,
|
|
27
|
+
contract,
|
|
28
|
+
procedure: contract instanceof Procedure ? contract : undefined
|
|
29
|
+
});
|
|
30
|
+
}, path);
|
|
31
|
+
for (const result of lazyResults) {
|
|
32
|
+
const prefix = getOpenAPIMeta(result.router)?.prefix;
|
|
33
|
+
this.pendingLazyRouters.add({
|
|
34
|
+
...result,
|
|
35
|
+
matcher: prefix ? createPrefixMatcher(prefix) : undefined
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async match(method, pathname, prefix) {
|
|
40
|
+
if (prefix) {
|
|
41
|
+
if (!pathname.startsWith(prefix)) {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
const charAfterPrefix = pathname[prefix.length];
|
|
45
|
+
if (charAfterPrefix === "/") {
|
|
46
|
+
pathname = pathname.slice(prefix.length);
|
|
47
|
+
} else if (charAfterPrefix === undefined) {
|
|
48
|
+
pathname = "/";
|
|
49
|
+
} else if (prefix[prefix.length - 1] === "/") {
|
|
50
|
+
pathname = pathname.slice(prefix.length - 1);
|
|
51
|
+
} else {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const loading = this.resolvePendingLazyRouters(pathname);
|
|
57
|
+
if (loading !== undefined) {
|
|
58
|
+
await loading;
|
|
59
|
+
}
|
|
60
|
+
const match = this.tree.match(method, pathname);
|
|
61
|
+
if (match === undefined) {
|
|
62
|
+
return undefined;
|
|
63
|
+
}
|
|
64
|
+
const entry = match.data;
|
|
65
|
+
return {
|
|
66
|
+
path: entry.path,
|
|
67
|
+
procedure: entry.procedure ?? await this.resolveProcedure(entry),
|
|
68
|
+
params: match.params
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
resolvePendingLazyRouters(pathname) {
|
|
72
|
+
for (const pending of this.pendingLazyRouters) {
|
|
73
|
+
if (pending.matcher === undefined || pending.matcher(pathname)) {
|
|
74
|
+
return this.loadPendingLazyRouters(pathname);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
async loadPendingLazyRouters(pathname) {
|
|
79
|
+
for (const pending of this.pendingLazyRouters) {
|
|
80
|
+
if (pending.matcher === undefined || pending.matcher(pathname)) {
|
|
81
|
+
await this.loadPendingLazyRouter(pending);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
loadPendingLazyRouter(pending) {
|
|
86
|
+
if (pending.loading === undefined) {
|
|
87
|
+
pending.loading = this.indexPendingLazyRouter(pending).catch((error) => {
|
|
88
|
+
pending.loading = undefined;
|
|
89
|
+
throw error;
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
return pending.loading;
|
|
93
|
+
}
|
|
94
|
+
async indexPendingLazyRouter(pending) {
|
|
95
|
+
const { default: router } = await unlazy(pending.router);
|
|
96
|
+
this.index(router, pending.path);
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
this.pendingLazyRouters.delete(pending);
|
|
100
|
+
}
|
|
101
|
+
async resolveProcedure(entry) {
|
|
102
|
+
entry.procedure = await resolveContractProcedure(this.rootRouter, entry.contract, entry.path);
|
|
103
|
+
return entry.procedure;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { Public } from "@orpc/shared";
|
|
2
|
+
import type { StandardHeaders } from "@standard-server/core";
|
|
3
|
+
import type { OpenAPIJsonSerializer } from "../../openapi-json-serializer.mjs";
|
|
4
|
+
export declare function serializeHeaders(headers: object, serializer: Public<OpenAPIJsonSerializer>): StandardHeaders;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { isTypescriptObject, NullProtoObj } from "@orpc/shared";
|
|
2
|
+
export function serializeHeaders(headers, serializer) {
|
|
3
|
+
const result = new NullProtoObj();
|
|
4
|
+
for (const [key, value] of Object.entries(headers)) {
|
|
5
|
+
|
|
6
|
+
if (Array.isArray(value)) {
|
|
7
|
+
const lines = [];
|
|
8
|
+
for (const item of value) {
|
|
9
|
+
const line = serializeHeaderValue(item, serializer);
|
|
10
|
+
if (line !== undefined) {
|
|
11
|
+
lines.push(line);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
result[key] = lines;
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
const line = serializeHeaderValue(value, serializer);
|
|
18
|
+
if (line !== undefined) {
|
|
19
|
+
result[key] = line;
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
function serializeHeaderValue(value, serializer) {
|
|
25
|
+
const serialized = serializer.serializeJson(value).json;
|
|
26
|
+
if (Array.isArray(serialized)) {
|
|
27
|
+
return serialized.filter((item) => item !== undefined && item !== null).map(String).join(",");
|
|
28
|
+
}
|
|
29
|
+
if (isTypescriptObject(serialized)) {
|
|
30
|
+
return Object.entries(serialized).filter(([, val]) => val !== undefined && val !== null).map(([key, val]) => `${String(key)},${String(val)}`).join(",");
|
|
31
|
+
}
|
|
32
|
+
if (serialized !== undefined && serialized !== null) {
|
|
33
|
+
return String(serialized);
|
|
34
|
+
}
|
|
35
|
+
return undefined;
|
|
36
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Segment } from "@orpc/shared";
|
|
2
|
+
export type BracketNotationSerializeResult = [string, unknown][];
|
|
3
|
+
export interface BracketNotationSerializerOptions {
|
|
4
|
+
/**
|
|
5
|
+
* Maximum number of empty slots that explicit array indexes may leave in total during deserialization
|
|
6
|
+
* (e.g., `a[0]=x&a[3]=y` leaves 2). An index that would exceed it turns its array into an object
|
|
7
|
+
* instead (e.g., `?arr[5000]=x` becomes `{ arr: { 5000: 'x' } }`).
|
|
8
|
+
*
|
|
9
|
+
* Empty slots take memory (a lone `?arr[999]=x` takes ~12KB in V8), and code iterating an array walks
|
|
10
|
+
* every one of them, so this keeps both in line with the input.
|
|
11
|
+
*
|
|
12
|
+
* NOTE: Append-style notation (e.g., `arr[]`) never leaves empty slots.
|
|
13
|
+
*
|
|
14
|
+
* @default 1_000
|
|
15
|
+
*/
|
|
16
|
+
maxDeserializingEmptySlots?: number | undefined;
|
|
17
|
+
}
|
|
18
|
+
export declare class BracketNotationSerializer {
|
|
19
|
+
private readonly maxDeserializingEmptySlots;
|
|
20
|
+
constructor(options?: BracketNotationSerializerOptions);
|
|
21
|
+
serialize(data: unknown): BracketNotationSerializeResult;
|
|
22
|
+
private internalSerialize;
|
|
23
|
+
deserialize(serialized: BracketNotationSerializeResult): Record<string, unknown>;
|
|
24
|
+
stringifyPath(segments: readonly Segment[]): string;
|
|
25
|
+
parsePath(path: string): string[];
|
|
26
|
+
}
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { getOwn, isPlainObject, NullProtoObj, setOwn } from "@orpc/shared";
|
|
2
|
+
export class BracketNotationSerializer {
|
|
3
|
+
maxDeserializingEmptySlots;
|
|
4
|
+
constructor(options = {}) {
|
|
5
|
+
this.maxDeserializingEmptySlots = options.maxDeserializingEmptySlots ?? 1e3;
|
|
6
|
+
}
|
|
7
|
+
serialize(data) {
|
|
8
|
+
const result = [];
|
|
9
|
+
this.internalSerialize(data, "", true, result);
|
|
10
|
+
return result;
|
|
11
|
+
}
|
|
12
|
+
internalSerialize(data, path, isRoot, result) {
|
|
13
|
+
if (Array.isArray(data)) {
|
|
14
|
+
data.forEach((item, i) => {
|
|
15
|
+
this.internalSerialize(item, isRoot ? i.toString() : `${path}[${i}]`, false, result);
|
|
16
|
+
});
|
|
17
|
+
} else if (isPlainObject(data)) {
|
|
18
|
+
for (const key of Object.keys(data)) {
|
|
19
|
+
this.internalSerialize(data[key], isRoot ? key : `${path}[${key}]`, false, result);
|
|
20
|
+
}
|
|
21
|
+
} else {
|
|
22
|
+
result.push([path, data]);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
deserialize(serialized) {
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
let arrayPushStyles;
|
|
29
|
+
const root = new NullProtoObj();
|
|
30
|
+
let emptySlotsLeft = this.maxDeserializingEmptySlots;
|
|
31
|
+
for (const [path, value] of serialized) {
|
|
32
|
+
const segments = this.parsePath(path);
|
|
33
|
+
let currentRef = root;
|
|
34
|
+
|
|
35
|
+
let nextSegment = segments[0];
|
|
36
|
+
for (let i = 1; i < segments.length; i++) {
|
|
37
|
+
const segment = segments[i];
|
|
38
|
+
const isLast = i === segments.length - 1;
|
|
39
|
+
const existing = getOwn(currentRef, nextSegment);
|
|
40
|
+
let child = existing;
|
|
41
|
+
let key = segment;
|
|
42
|
+
|
|
43
|
+
if (Array.isArray(child) || !isPlainObject(child)) {
|
|
44
|
+
const isArray = Array.isArray(child);
|
|
45
|
+
const isPushStyle = isArray && !!arrayPushStyles?.has(child);
|
|
46
|
+
const length = isArray ? child.length : 0;
|
|
47
|
+
const index = internalToArrayIndex(segment);
|
|
48
|
+
const emptySlots = index === undefined ? 0 : Math.max(0, index - length);
|
|
49
|
+
const canBeArray = segment === "" ? isLast && (isPushStyle || length === 0) : index !== undefined && !(isLast && isPushStyle) && emptySlots <= emptySlotsLeft;
|
|
50
|
+
if (canBeArray) {
|
|
51
|
+
emptySlotsLeft -= emptySlots;
|
|
52
|
+
child = isArray ? child : [];
|
|
53
|
+
key = index ?? segment;
|
|
54
|
+
} else if (isArray) {
|
|
55
|
+
arrayPushStyles?.delete(child);
|
|
56
|
+
child = isPushStyle ? internalPushStyleArrayToObject(child) : internalArrayToObject(child);
|
|
57
|
+
} else {
|
|
58
|
+
child = new NullProtoObj();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
if (child !== existing) {
|
|
62
|
+
internalSetOwn(currentRef, nextSegment, child);
|
|
63
|
+
}
|
|
64
|
+
currentRef = child;
|
|
65
|
+
nextSegment = key;
|
|
66
|
+
}
|
|
67
|
+
if (Array.isArray(currentRef) && nextSegment === "") {
|
|
68
|
+
arrayPushStyles ??= new WeakSet();
|
|
69
|
+
arrayPushStyles.add(currentRef);
|
|
70
|
+
currentRef.push(value);
|
|
71
|
+
} else if (Object.hasOwn(currentRef, nextSegment)) {
|
|
72
|
+
const current = currentRef[nextSegment];
|
|
73
|
+
if (Array.isArray(current)) {
|
|
74
|
+
current.push(value);
|
|
75
|
+
} else {
|
|
76
|
+
internalSetOwn(currentRef, nextSegment, [current, value]);
|
|
77
|
+
}
|
|
78
|
+
} else {
|
|
79
|
+
internalSetOwn(currentRef, nextSegment, value);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return root;
|
|
83
|
+
}
|
|
84
|
+
stringifyPath(segments) {
|
|
85
|
+
if (segments.length === 0) {
|
|
86
|
+
return "";
|
|
87
|
+
}
|
|
88
|
+
let result = segments[0].toString();
|
|
89
|
+
for (let i = 1; i < segments.length; i++) {
|
|
90
|
+
result += `[${segments[i]}]`;
|
|
91
|
+
}
|
|
92
|
+
return result;
|
|
93
|
+
}
|
|
94
|
+
parsePath(path) {
|
|
95
|
+
const start = path.indexOf("[");
|
|
96
|
+
|
|
97
|
+
if (start === -1 || path[path.length - 1] !== "]") {
|
|
98
|
+
return [path];
|
|
99
|
+
}
|
|
100
|
+
const segments = [path.slice(0, start)];
|
|
101
|
+
let from = start + 1;
|
|
102
|
+
let to = path.indexOf("][", from);
|
|
103
|
+
while (to !== -1) {
|
|
104
|
+
segments.push(path.slice(from, to));
|
|
105
|
+
from = to + 2;
|
|
106
|
+
to = path.indexOf("][", from);
|
|
107
|
+
}
|
|
108
|
+
segments.push(path.slice(from, -1));
|
|
109
|
+
return segments;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const MAX_ARRAY_INDEX = 2 ** 32 - 2;
|
|
114
|
+
|
|
115
|
+
function internalToArrayIndex(key) {
|
|
116
|
+
|
|
117
|
+
if (key.length === 0 || key.length > 10) {
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
120
|
+
let index = key.charCodeAt(0) - 48;
|
|
121
|
+
if (index < 0 || index > 9 || index === 0 && key.length > 1) {
|
|
122
|
+
return undefined;
|
|
123
|
+
}
|
|
124
|
+
for (let i = 1; i < key.length; i++) {
|
|
125
|
+
const digit = key.charCodeAt(i) - 48;
|
|
126
|
+
if (digit < 0 || digit > 9) {
|
|
127
|
+
return undefined;
|
|
128
|
+
}
|
|
129
|
+
index = index * 10 + digit;
|
|
130
|
+
}
|
|
131
|
+
return index <= MAX_ARRAY_INDEX ? index : undefined;
|
|
132
|
+
}
|
|
133
|
+
function internalArrayToObject(array) {
|
|
134
|
+
const obj = new NullProtoObj();
|
|
135
|
+
array.forEach((item, i) => {
|
|
136
|
+
obj[i] = item;
|
|
137
|
+
});
|
|
138
|
+
return obj;
|
|
139
|
+
}
|
|
140
|
+
function internalPushStyleArrayToObject(array) {
|
|
141
|
+
const obj = new NullProtoObj();
|
|
142
|
+
obj[""] = array.length === 1 ? array[0] : array;
|
|
143
|
+
return obj;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
function internalSetOwn(container, key, value) {
|
|
147
|
+
if (typeof key === "string" && container instanceof NullProtoObj) {
|
|
148
|
+
const index = internalToArrayIndex(key);
|
|
149
|
+
|
|
150
|
+
if (index !== undefined && index !== 0 && !Object.hasOwn(container, index - 1) && !Object.hasOwn(container, MAX_ARRAY_INDEX)) {
|
|
151
|
+
container[MAX_ARRAY_INDEX] = undefined;
|
|
152
|
+
delete container[MAX_ARRAY_INDEX];
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
setOwn(container, key, value);
|
|
156
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import type { ClientContext, ClientLink, ORPCClientOptions } from "@orpc/client";
|
|
2
|
+
import type { RouterContract, RouterContractClient } from "@orpc/contract";
|
|
3
|
+
import type { JsonifiedClient } from "./types.mjs";
|
|
4
|
+
export interface ContractJsonifiedClientFactory<TClientContext extends ClientContext> {
|
|
5
|
+
<T extends RouterContract>(contract: T): JsonifiedClient<RouterContractClient<T, TClientContext>>;
|
|
6
|
+
}
|
|
7
|
+
export interface ContractJsonifiedClientFactoryOptions<TClientContext extends ClientContext> extends Pick<ORPCClientOptions<JsonifiedClient<RouterContractClient<RouterContract, TClientContext>>>, "interceptors" | "scoped"> {
|
|
8
|
+
/**
|
|
9
|
+
* An optional reference to the root router-contract.
|
|
10
|
+
* When provided, the client factory will automatically register the passed contract
|
|
11
|
+
* into the router at the path defined by `meta.path`.
|
|
12
|
+
*/
|
|
13
|
+
contractRef?: RouterContract | undefined;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Like `createContractClientFactory`, but the clients it builds are typed as `JsonifiedClient`,
|
|
17
|
+
* for links that require it, such as OpenAPI Link.
|
|
18
|
+
*
|
|
19
|
+
* @remarks
|
|
20
|
+
* **Warning**: Every procedure contract passed to the factory must define `meta.path` matching its location in the root contract.
|
|
21
|
+
*
|
|
22
|
+
* @see {@link https://orpc.dev/docs/contract/client-factory | Contract Client Factory}
|
|
23
|
+
*/
|
|
24
|
+
export declare function createContractJsonifiedClientFactory<TClientContext extends ClientContext>(link: ClientLink<TClientContext>, options?: ContractJsonifiedClientFactoryOptions<TClientContext>): ContractJsonifiedClientFactory<TClientContext>;
|
|
@@ -1,41 +1,76 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Context } from
|
|
3
|
-
import { OpenAPIMeta } from
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
1
|
+
import type { AnySchema, ErrorMap } from "@orpc/contract";
|
|
2
|
+
import type { Context } from "@orpc/server";
|
|
3
|
+
import type { OpenAPIMeta } from "../meta.mjs";
|
|
4
|
+
declare module "@orpc/contract" {
|
|
5
|
+
interface ContractBuilder<TErrorMap extends ErrorMap> {
|
|
6
|
+
route(meta: OpenAPIMeta): ContractBuilder<TErrorMap>;
|
|
7
|
+
}
|
|
8
|
+
interface ProcedureContractBuilderWithInput<
|
|
9
|
+
TInputSchema extends AnySchema,
|
|
10
|
+
TErrorMap extends ErrorMap
|
|
11
|
+
> {
|
|
12
|
+
route(meta: OpenAPIMeta): ProcedureContractBuilderWithInput<TInputSchema, TErrorMap>;
|
|
13
|
+
}
|
|
14
|
+
interface ProcedureContractBuilderWithOutput<
|
|
15
|
+
TOutputSchema extends AnySchema,
|
|
16
|
+
TErrorMap extends ErrorMap
|
|
17
|
+
> {
|
|
18
|
+
route(meta: OpenAPIMeta): ProcedureContractBuilderWithOutput<TOutputSchema, TErrorMap>;
|
|
19
|
+
}
|
|
20
|
+
interface ProcedureContractBuilderWithInputOutput<
|
|
21
|
+
TInputSchema extends AnySchema,
|
|
22
|
+
TOutputSchema extends AnySchema,
|
|
23
|
+
TErrorMap extends ErrorMap
|
|
24
|
+
> {
|
|
25
|
+
route(meta: OpenAPIMeta): ProcedureContractBuilderWithInputOutput<TInputSchema, TOutputSchema, TErrorMap>;
|
|
26
|
+
}
|
|
21
27
|
}
|
|
22
|
-
declare module
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
28
|
+
declare module "@orpc/server" {
|
|
29
|
+
interface Builder<
|
|
30
|
+
TInitialContext extends Context,
|
|
31
|
+
TErrorMap extends ErrorMap
|
|
32
|
+
> {
|
|
33
|
+
route(meta: OpenAPIMeta): Builder<TInitialContext, TErrorMap>;
|
|
34
|
+
}
|
|
35
|
+
interface BuilderWithMiddlewares<
|
|
36
|
+
TInitialContext extends Context,
|
|
37
|
+
TInjectedContext extends Context,
|
|
38
|
+
TErrorMap extends ErrorMap
|
|
39
|
+
> {
|
|
40
|
+
route(meta: OpenAPIMeta): BuilderWithMiddlewares<TInitialContext, TInjectedContext, TErrorMap>;
|
|
41
|
+
}
|
|
42
|
+
interface BuilderWithInput<
|
|
43
|
+
TInitialContext extends Context,
|
|
44
|
+
TInjectedContext extends Context,
|
|
45
|
+
TInputSchema extends AnySchema,
|
|
46
|
+
TErrorMap extends ErrorMap
|
|
47
|
+
> {
|
|
48
|
+
route(meta: OpenAPIMeta): BuilderWithInput<TInitialContext, TInjectedContext, TInputSchema, TErrorMap>;
|
|
49
|
+
}
|
|
50
|
+
interface BuilderWithOutput<
|
|
51
|
+
TInitialContext extends Context,
|
|
52
|
+
TInjectedContext extends Context,
|
|
53
|
+
TOutputSchema extends AnySchema,
|
|
54
|
+
TErrorMap extends ErrorMap
|
|
55
|
+
> {
|
|
56
|
+
route(meta: OpenAPIMeta): BuilderWithOutput<TInitialContext, TInjectedContext, TOutputSchema, TErrorMap>;
|
|
57
|
+
}
|
|
58
|
+
interface BuilderWithInputOutput<
|
|
59
|
+
TInitialContext extends Context,
|
|
60
|
+
TInjectedContext extends Context,
|
|
61
|
+
TInputSchema extends AnySchema,
|
|
62
|
+
TOutputSchema extends AnySchema,
|
|
63
|
+
TErrorMap extends ErrorMap
|
|
64
|
+
> {
|
|
65
|
+
route(meta: OpenAPIMeta): BuilderWithInputOutput<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, TErrorMap>;
|
|
66
|
+
}
|
|
67
|
+
interface DecoratedProcedure<
|
|
68
|
+
TInitialContext extends Context,
|
|
69
|
+
TInjectedContext extends Context,
|
|
70
|
+
TInputSchema extends AnySchema,
|
|
71
|
+
TOutputSchema extends AnySchema,
|
|
72
|
+
TErrorMap extends ErrorMap
|
|
73
|
+
> {
|
|
74
|
+
route(meta: OpenAPIMeta): DecoratedProcedure<TInitialContext, TInjectedContext, TInputSchema, TOutputSchema, TErrorMap>;
|
|
75
|
+
}
|
|
41
76
|
}
|
|
@@ -1,14 +1,12 @@
|
|
|
1
|
-
import { ContractBuilder } from
|
|
2
|
-
import { Builder, DecoratedProcedure } from
|
|
3
|
-
import {
|
|
4
|
-
import '@orpc/shared';
|
|
5
|
-
|
|
1
|
+
import { ContractBuilder } from "@orpc/contract";
|
|
2
|
+
import { Builder, DecoratedProcedure } from "@orpc/server";
|
|
3
|
+
import { openapi } from "../meta.mjs";
|
|
6
4
|
ContractBuilder.prototype.route = function route(meta) {
|
|
7
|
-
|
|
5
|
+
return this.meta(openapi(meta));
|
|
8
6
|
};
|
|
9
|
-
Builder.prototype.route = function
|
|
10
|
-
|
|
7
|
+
Builder.prototype.route = function route(meta) {
|
|
8
|
+
return this.meta(openapi(meta));
|
|
11
9
|
};
|
|
12
|
-
DecoratedProcedure.prototype.route = function
|
|
13
|
-
|
|
10
|
+
DecoratedProcedure.prototype.route = function route(meta) {
|
|
11
|
+
return this.meta(openapi(meta));
|
|
14
12
|
};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse a form data with [bracket notation](https://orpc.dev/docs/openapi/bracket-notation) syntax
|
|
3
|
+
*
|
|
4
|
+
* @example
|
|
5
|
+
* ```ts
|
|
6
|
+
* const form = new FormData()
|
|
7
|
+
* form.append('a', '1')
|
|
8
|
+
* form.append('user[name]', 'John')
|
|
9
|
+
* form.append('user[age]', '20')
|
|
10
|
+
* form.append('user[friends][]', 'Bob')
|
|
11
|
+
* form.append('user[friends][]', 'Alice')
|
|
12
|
+
* form.append('user[friends][]', 'Charlie')
|
|
13
|
+
* form.append('thumb', new Blob(['hello']), 'thumb.png')
|
|
14
|
+
*
|
|
15
|
+
* parseFormData(form)
|
|
16
|
+
* // {
|
|
17
|
+
* // a: '1',
|
|
18
|
+
* // user: {
|
|
19
|
+
* // name: 'John',
|
|
20
|
+
* // age: '20',
|
|
21
|
+
* // friends: ['Bob', 'Alice', 'Charlie'],
|
|
22
|
+
* // },
|
|
23
|
+
* // thumb: form.get('thumb'),
|
|
24
|
+
* // }
|
|
25
|
+
* ```
|
|
26
|
+
*
|
|
27
|
+
* @see {@link https://orpc.dev/docs/helpers/form-data#parseformdata | Form Data Helpers - parseFormData}
|
|
28
|
+
*/
|
|
29
|
+
export declare function parseFormData(form: FormData): any;
|
|
30
|
+
/**
|
|
31
|
+
* Gets the issue message from the error for a given field path.
|
|
32
|
+
*
|
|
33
|
+
* @remarks
|
|
34
|
+
* **Note**: Requires validation errors to follow the standard schema issue format, stored under `data.issues` — customized validation errors may not be found.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```tsx
|
|
38
|
+
* const { error, data, execute } = useServerAction(someAction)
|
|
39
|
+
*
|
|
40
|
+
* return <form action={(form) => execute(parseFormData(form))}>
|
|
41
|
+
* <input name="user[name]" type="text" />
|
|
42
|
+
* <p>{getIssueMessage(error, 'user[name]')}</p>
|
|
43
|
+
*
|
|
44
|
+
* <input name="user[age]" type="number" />
|
|
45
|
+
* <p>{getIssueMessage(error, 'user[age]')}</p>
|
|
46
|
+
*
|
|
47
|
+
* <input name="images[]" type="file" />
|
|
48
|
+
* <p>{getIssueMessage(error, 'images[]')}</p>
|
|
49
|
+
* </form>
|
|
50
|
+
* ```
|
|
51
|
+
*
|
|
52
|
+
* @param error - The error (can be anything) can contain `data.issues` (standard schema issues)
|
|
53
|
+
* @param path - The path of the field that has the issue follow [bracket notation](https://orpc.dev/docs/openapi/bracket-notation)
|
|
54
|
+
*
|
|
55
|
+
* @see {@link https://orpc.dev/docs/helpers/form-data#getissuemessage | Form Data Helpers - getIssueMessage}
|
|
56
|
+
*/
|
|
57
|
+
export declare function getIssueMessage(error: unknown, path: string): string | undefined;
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { isSchemaIssue } from "@orpc/contract";
|
|
2
|
+
import { isTypescriptObject } from "@orpc/shared";
|
|
3
|
+
import { BracketNotationSerializer } from "../bracket-notation.mjs";
|
|
4
|
+
|
|
5
|
+
export function parseFormData(form) {
|
|
6
|
+
const serializer = new BracketNotationSerializer();
|
|
7
|
+
return serializer.deserialize(Array.from(form.entries()));
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function getIssueMessage(error, path) {
|
|
11
|
+
if (!isTypescriptObject(error) || !isTypescriptObject(error.data) || !Array.isArray(error.data.issues)) {
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
const serializer = new BracketNotationSerializer();
|
|
15
|
+
for (const issue of error.data.issues) {
|
|
16
|
+
if (!isSchemaIssue(issue)) {
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
if (issue.path === undefined) {
|
|
20
|
+
if (path === "") {
|
|
21
|
+
return issue.message;
|
|
22
|
+
}
|
|
23
|
+
continue;
|
|
24
|
+
}
|
|
25
|
+
const issuePath = serializer.stringifyPath(issue.path.map((segment) => typeof segment === "object" ? segment.key.toString() : segment.toString()));
|
|
26
|
+
if (issuePath === path) {
|
|
27
|
+
return issue.message;
|
|
28
|
+
}
|
|
29
|
+
if (path.endsWith("[]") && issuePath.replace(/\[(?:0|[1-9]\d*)\]$/, "[]") === path) {
|
|
30
|
+
return issue.message;
|
|
31
|
+
}
|
|
32
|
+
if (path === "" && /^(?:0|[1-9]\d*)$/.test(issuePath)) {
|
|
33
|
+
return issue.message;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
}
|
package/dist/helpers/index.d.mts
CHANGED
|
@@ -1,59 +1 @@
|
|
|
1
|
-
|
|
2
|
-
* Parse a form data with [bracket notation](https://orpc.dev/docs/openapi/bracket-notation) syntax
|
|
3
|
-
*
|
|
4
|
-
* @example
|
|
5
|
-
* ```ts
|
|
6
|
-
* const form = new FormData()
|
|
7
|
-
* form.append('a', '1')
|
|
8
|
-
* form.append('user[name]', 'John')
|
|
9
|
-
* form.append('user[age]', '20')
|
|
10
|
-
* form.append('user[friends][]', 'Bob')
|
|
11
|
-
* form.append('user[friends][]', 'Alice')
|
|
12
|
-
* form.append('user[friends][]', 'Charlie')
|
|
13
|
-
* form.append('thumb', new Blob(['hello']), 'thumb.png')
|
|
14
|
-
*
|
|
15
|
-
* parseFormData(form)
|
|
16
|
-
* // {
|
|
17
|
-
* // a: '1',
|
|
18
|
-
* // user: {
|
|
19
|
-
* // name: 'John',
|
|
20
|
-
* // age: '20',
|
|
21
|
-
* // friends: ['Bob', 'Alice', 'Charlie'],
|
|
22
|
-
* // },
|
|
23
|
-
* // thumb: form.get('thumb'),
|
|
24
|
-
* // }
|
|
25
|
-
* ```
|
|
26
|
-
*
|
|
27
|
-
* @see {@link https://orpc.dev/docs/helpers/form-data#parseformdata | Form Data Helpers - parseFormData}
|
|
28
|
-
*/
|
|
29
|
-
declare function parseFormData(form: FormData): any;
|
|
30
|
-
/**
|
|
31
|
-
* Gets the issue message from the error for a given field path.
|
|
32
|
-
*
|
|
33
|
-
* @remarks
|
|
34
|
-
* **Note**: Requires validation errors to follow the standard schema issue format, stored under `data.issues` — customized validation errors may not be found.
|
|
35
|
-
*
|
|
36
|
-
* @example
|
|
37
|
-
* ```tsx
|
|
38
|
-
* const { error, data, execute } = useServerAction(someAction)
|
|
39
|
-
*
|
|
40
|
-
* return <form action={(form) => execute(parseFormData(form))}>
|
|
41
|
-
* <input name="user[name]" type="text" />
|
|
42
|
-
* <p>{getIssueMessage(error, 'user[name]')}</p>
|
|
43
|
-
*
|
|
44
|
-
* <input name="user[age]" type="number" />
|
|
45
|
-
* <p>{getIssueMessage(error, 'user[age]')}</p>
|
|
46
|
-
*
|
|
47
|
-
* <input name="images[]" type="file" />
|
|
48
|
-
* <p>{getIssueMessage(error, 'images[]')}</p>
|
|
49
|
-
* </form>
|
|
50
|
-
* ```
|
|
51
|
-
*
|
|
52
|
-
* @param error - The error (can be anything) can contain `data.issues` (standard schema issues)
|
|
53
|
-
* @param path - The path of the field that has the issue follow [bracket notation](https://orpc.dev/docs/openapi/bracket-notation)
|
|
54
|
-
*
|
|
55
|
-
* @see {@link https://orpc.dev/docs/helpers/form-data#getissuemessage | Form Data Helpers - getIssueMessage}
|
|
56
|
-
*/
|
|
57
|
-
declare function getIssueMessage(error: unknown, path: string): string | undefined;
|
|
58
|
-
|
|
59
|
-
export { getIssueMessage, parseFormData };
|
|
1
|
+
export * from "./form-data.mjs";
|