@orpc/openapi-client 0.0.0-next.98e1b3d → 0.0.0-next.99d5d75
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 +144 -29
- package/dist/adapters/fetch/index.d.mts +22 -0
- package/dist/adapters/fetch/index.d.ts +22 -0
- package/dist/adapters/fetch/index.mjs +17 -0
- package/dist/adapters/standard/index.d.mts +19 -34
- package/dist/adapters/standard/index.d.ts +19 -34
- package/dist/adapters/standard/index.mjs +8 -226
- package/dist/helpers/index.d.mts +54 -0
- package/dist/helpers/index.d.ts +54 -0
- package/dist/helpers/index.mjs +7 -0
- package/dist/index.d.mts +15 -1
- package/dist/index.d.ts +15 -1
- package/dist/shared/openapi-client.B2Q9qU5m.mjs +302 -0
- package/dist/shared/openapi-client.Dgl8z2tb.mjs +39 -0
- package/dist/shared/openapi-client.Dx4REA6z.d.mts +107 -0
- package/dist/shared/openapi-client.Dx4REA6z.d.ts +107 -0
- package/dist/shared/openapi-client.t9fCAe3x.mjs +146 -0
- package/package.json +20 -7
|
@@ -1,226 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
this.serialize(item, [...segments, i], result);
|
|
10
|
-
});
|
|
11
|
-
} else if (isObject(data)) {
|
|
12
|
-
for (const key in data) {
|
|
13
|
-
this.serialize(data[key], [...segments, key], result);
|
|
14
|
-
}
|
|
15
|
-
} else {
|
|
16
|
-
result.push([this.stringifyPath(segments), data]);
|
|
17
|
-
}
|
|
18
|
-
return result;
|
|
19
|
-
}
|
|
20
|
-
deserialize(serialized) {
|
|
21
|
-
if (serialized.length === 0) {
|
|
22
|
-
return {};
|
|
23
|
-
}
|
|
24
|
-
const arrayPushStyles = /* @__PURE__ */ new WeakSet();
|
|
25
|
-
const ref = { value: [] };
|
|
26
|
-
for (const [path, value] of serialized) {
|
|
27
|
-
const segments = this.parsePath(path);
|
|
28
|
-
let currentRef = ref;
|
|
29
|
-
let nextSegment = "value";
|
|
30
|
-
segments.forEach((segment, i) => {
|
|
31
|
-
if (!Array.isArray(currentRef[nextSegment]) && !isObject(currentRef[nextSegment])) {
|
|
32
|
-
currentRef[nextSegment] = [];
|
|
33
|
-
}
|
|
34
|
-
if (i !== segments.length - 1) {
|
|
35
|
-
if (Array.isArray(currentRef[nextSegment]) && !isValidArrayIndex(segment)) {
|
|
36
|
-
currentRef[nextSegment] = { ...currentRef[nextSegment] };
|
|
37
|
-
}
|
|
38
|
-
} else {
|
|
39
|
-
if (Array.isArray(currentRef[nextSegment])) {
|
|
40
|
-
if (segment === "") {
|
|
41
|
-
if (currentRef[nextSegment].length && !arrayPushStyles.has(currentRef[nextSegment])) {
|
|
42
|
-
currentRef[nextSegment] = { ...currentRef[nextSegment] };
|
|
43
|
-
}
|
|
44
|
-
} else {
|
|
45
|
-
if (arrayPushStyles.has(currentRef[nextSegment])) {
|
|
46
|
-
currentRef[nextSegment] = { "": currentRef[nextSegment].at(-1) };
|
|
47
|
-
} else if (!isValidArrayIndex(segment)) {
|
|
48
|
-
currentRef[nextSegment] = { ...currentRef[nextSegment] };
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
currentRef = currentRef[nextSegment];
|
|
54
|
-
nextSegment = segment;
|
|
55
|
-
});
|
|
56
|
-
if (Array.isArray(currentRef)) {
|
|
57
|
-
if (nextSegment === "") {
|
|
58
|
-
arrayPushStyles.add(currentRef);
|
|
59
|
-
currentRef.push(value);
|
|
60
|
-
} else {
|
|
61
|
-
currentRef[Number(nextSegment)] = value;
|
|
62
|
-
}
|
|
63
|
-
} else {
|
|
64
|
-
currentRef[nextSegment] = value;
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
return ref.value;
|
|
68
|
-
}
|
|
69
|
-
stringifyPath(segments) {
|
|
70
|
-
return segments.map((segment) => {
|
|
71
|
-
return segment.toString().replace(/[\\[\]]/g, (match) => {
|
|
72
|
-
switch (match) {
|
|
73
|
-
case "\\":
|
|
74
|
-
return "\\\\";
|
|
75
|
-
case "[":
|
|
76
|
-
return "\\[";
|
|
77
|
-
case "]":
|
|
78
|
-
return "\\]";
|
|
79
|
-
/* v8 ignore next 2 */
|
|
80
|
-
default:
|
|
81
|
-
return match;
|
|
82
|
-
}
|
|
83
|
-
});
|
|
84
|
-
}).reduce((result, segment, i) => {
|
|
85
|
-
if (i === 0) {
|
|
86
|
-
return segment;
|
|
87
|
-
}
|
|
88
|
-
return `${result}[${segment}]`;
|
|
89
|
-
}, "");
|
|
90
|
-
}
|
|
91
|
-
parsePath(path) {
|
|
92
|
-
const segments = [];
|
|
93
|
-
let inBrackets = false;
|
|
94
|
-
let currentSegment = "";
|
|
95
|
-
let backslashCount = 0;
|
|
96
|
-
for (let i = 0; i < path.length; i++) {
|
|
97
|
-
const char = path[i];
|
|
98
|
-
const nextChar = path[i + 1];
|
|
99
|
-
if (inBrackets && char === "]" && (nextChar === void 0 || nextChar === "[") && backslashCount % 2 === 0) {
|
|
100
|
-
if (nextChar === void 0) {
|
|
101
|
-
inBrackets = false;
|
|
102
|
-
}
|
|
103
|
-
segments.push(currentSegment);
|
|
104
|
-
currentSegment = "";
|
|
105
|
-
i++;
|
|
106
|
-
} else if (segments.length === 0 && char === "[" && backslashCount % 2 === 0) {
|
|
107
|
-
inBrackets = true;
|
|
108
|
-
segments.push(currentSegment);
|
|
109
|
-
currentSegment = "";
|
|
110
|
-
} else if (char === "\\") {
|
|
111
|
-
backslashCount++;
|
|
112
|
-
} else {
|
|
113
|
-
currentSegment += "\\".repeat(backslashCount / 2) + char;
|
|
114
|
-
backslashCount = 0;
|
|
115
|
-
}
|
|
116
|
-
}
|
|
117
|
-
return inBrackets || segments.length === 0 ? [path] : segments;
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
function isValidArrayIndex(value) {
|
|
121
|
-
return /^0$|^[1-9]\d*$/.test(value);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
class StandardOpenAPIJsonSerializer {
|
|
125
|
-
customSerializers;
|
|
126
|
-
constructor(options = {}) {
|
|
127
|
-
this.customSerializers = options.customJsonSerializers ?? [];
|
|
128
|
-
}
|
|
129
|
-
serialize(data, hasBlobRef = { value: false }) {
|
|
130
|
-
for (const custom of this.customSerializers) {
|
|
131
|
-
if (custom.condition(data)) {
|
|
132
|
-
const result = this.serialize(custom.serialize(data), hasBlobRef);
|
|
133
|
-
return result;
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
if (data instanceof Blob) {
|
|
137
|
-
hasBlobRef.value = true;
|
|
138
|
-
return [data, hasBlobRef.value];
|
|
139
|
-
}
|
|
140
|
-
if (data instanceof Set) {
|
|
141
|
-
return this.serialize(Array.from(data), hasBlobRef);
|
|
142
|
-
}
|
|
143
|
-
if (data instanceof Map) {
|
|
144
|
-
return this.serialize(Array.from(data.entries()), hasBlobRef);
|
|
145
|
-
}
|
|
146
|
-
if (Array.isArray(data)) {
|
|
147
|
-
const json = data.map((v) => v === void 0 ? null : this.serialize(v, hasBlobRef)[0]);
|
|
148
|
-
return [json, hasBlobRef.value];
|
|
149
|
-
}
|
|
150
|
-
if (isObject(data)) {
|
|
151
|
-
const json = {};
|
|
152
|
-
for (const k in data) {
|
|
153
|
-
json[k] = this.serialize(data[k], hasBlobRef)[0];
|
|
154
|
-
}
|
|
155
|
-
return [json, hasBlobRef.value];
|
|
156
|
-
}
|
|
157
|
-
if (typeof data === "bigint" || data instanceof RegExp || data instanceof URL) {
|
|
158
|
-
return [data.toString(), hasBlobRef.value];
|
|
159
|
-
}
|
|
160
|
-
if (data instanceof Date) {
|
|
161
|
-
return [Number.isNaN(data.getTime()) ? null : data.toISOString(), hasBlobRef.value];
|
|
162
|
-
}
|
|
163
|
-
if (Number.isNaN(data)) {
|
|
164
|
-
return [null, hasBlobRef.value];
|
|
165
|
-
}
|
|
166
|
-
return [data, hasBlobRef.value];
|
|
167
|
-
}
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
class StandardOpenAPISerializer {
|
|
171
|
-
constructor(jsonSerializer, bracketNotation) {
|
|
172
|
-
this.jsonSerializer = jsonSerializer;
|
|
173
|
-
this.bracketNotation = bracketNotation;
|
|
174
|
-
}
|
|
175
|
-
serialize(data) {
|
|
176
|
-
if (isAsyncIteratorObject(data)) {
|
|
177
|
-
return mapEventIterator(data, {
|
|
178
|
-
value: async (value) => this.#serialize(value, false),
|
|
179
|
-
error: async (e) => {
|
|
180
|
-
return new ErrorEvent({
|
|
181
|
-
data: this.#serialize(toORPCError(e).toJSON(), false),
|
|
182
|
-
cause: e
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
});
|
|
186
|
-
}
|
|
187
|
-
return this.#serialize(data, true);
|
|
188
|
-
}
|
|
189
|
-
#serialize(data, enableFormData) {
|
|
190
|
-
if (data instanceof Blob || data === void 0) {
|
|
191
|
-
return data;
|
|
192
|
-
}
|
|
193
|
-
const [json, hasBlob] = this.jsonSerializer.serialize(data);
|
|
194
|
-
if (!enableFormData || !hasBlob) {
|
|
195
|
-
return json;
|
|
196
|
-
}
|
|
197
|
-
const form = new FormData();
|
|
198
|
-
for (const [path, value] of this.bracketNotation.serialize(json)) {
|
|
199
|
-
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
200
|
-
form.append(path, value.toString());
|
|
201
|
-
} else if (value instanceof Blob) {
|
|
202
|
-
form.append(path, value);
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
return form;
|
|
206
|
-
}
|
|
207
|
-
deserialize(data) {
|
|
208
|
-
if (data instanceof URLSearchParams || data instanceof FormData) {
|
|
209
|
-
return this.bracketNotation.deserialize(Array.from(data.entries()));
|
|
210
|
-
}
|
|
211
|
-
if (isAsyncIteratorObject(data)) {
|
|
212
|
-
return mapEventIterator(data, {
|
|
213
|
-
value: async (value) => value,
|
|
214
|
-
error: async (e) => {
|
|
215
|
-
if (e instanceof ErrorEvent && ORPCError.isValidJSON(e.data)) {
|
|
216
|
-
return ORPCError.fromJSON(e.data, { cause: e });
|
|
217
|
-
}
|
|
218
|
-
return e;
|
|
219
|
-
}
|
|
220
|
-
});
|
|
221
|
-
}
|
|
222
|
-
return data;
|
|
223
|
-
}
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
export { StandardBracketNotationSerializer, StandardOpenAPIJsonSerializer, StandardOpenAPISerializer };
|
|
1
|
+
export { S as StandardBracketNotationSerializer } from '../../shared/openapi-client.t9fCAe3x.mjs';
|
|
2
|
+
export { g as getIssueMessage, p as parseFormData } from '../../shared/openapi-client.Dgl8z2tb.mjs';
|
|
3
|
+
export { S as StandardOpenAPIJsonSerializer, a as StandardOpenAPILink, c as StandardOpenAPISerializer, b as StandardOpenapiLinkCodec, g as getDynamicParams, s as standardizeHTTPPath } from '../../shared/openapi-client.B2Q9qU5m.mjs';
|
|
4
|
+
import '@orpc/shared';
|
|
5
|
+
import '@orpc/contract';
|
|
6
|
+
import '@orpc/client/standard';
|
|
7
|
+
import '@orpc/client';
|
|
8
|
+
import '@orpc/standard-server';
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* parse a form data with bracket notation
|
|
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/openapi/bracket-notation Bracket Notation Docs}
|
|
28
|
+
*/
|
|
29
|
+
declare function parseFormData(form: FormData): any;
|
|
30
|
+
/**
|
|
31
|
+
* Get the issue message from the error.
|
|
32
|
+
*
|
|
33
|
+
* @param error - The error (can be anything) can contain `data.issues` (standard schema issues)
|
|
34
|
+
* @param path - The path of the field that has the issue follow [bracket notation](https://orpc.dev/docs/openapi/bracket-notation)
|
|
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
|
+
declare function getIssueMessage(error: unknown, path: string): string | undefined;
|
|
53
|
+
|
|
54
|
+
export { getIssueMessage, parseFormData };
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* parse a form data with bracket notation
|
|
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/openapi/bracket-notation Bracket Notation Docs}
|
|
28
|
+
*/
|
|
29
|
+
declare function parseFormData(form: FormData): any;
|
|
30
|
+
/**
|
|
31
|
+
* Get the issue message from the error.
|
|
32
|
+
*
|
|
33
|
+
* @param error - The error (can be anything) can contain `data.issues` (standard schema issues)
|
|
34
|
+
* @param path - The path of the field that has the issue follow [bracket notation](https://orpc.dev/docs/openapi/bracket-notation)
|
|
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
|
+
declare function getIssueMessage(error: unknown, path: string): string | undefined;
|
|
53
|
+
|
|
54
|
+
export { getIssueMessage, parseFormData };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import '@orpc/shared';
|
|
2
|
+
export { g as getIssueMessage, p as parseFormData } from '../shared/openapi-client.Dgl8z2tb.mjs';
|
|
3
|
+
import '@orpc/client/standard';
|
|
4
|
+
import '@orpc/client';
|
|
5
|
+
import '@orpc/contract';
|
|
6
|
+
import '@orpc/standard-server';
|
|
7
|
+
import '../shared/openapi-client.t9fCAe3x.mjs';
|
package/dist/index.d.mts
CHANGED
|
@@ -1,2 +1,16 @@
|
|
|
1
|
+
import { NestedClient, Client, ORPCError } from '@orpc/client';
|
|
1
2
|
|
|
2
|
-
|
|
3
|
+
type JsonifiedValue<T> = T extends string ? T : T extends number ? T : T extends boolean ? T : T extends null ? T : T extends undefined ? T : T extends Array<unknown> ? JsonifiedArray<T> : T extends Record<string, unknown> ? {
|
|
4
|
+
[K in keyof T]: JsonifiedValue<T[K]>;
|
|
5
|
+
} : T extends Date ? string : T extends bigint ? string : T extends File ? File : T extends Blob ? Blob : T extends RegExp ? string : T extends URL ? string : T extends Map<infer K, infer V> ? JsonifiedArray<[K, V][]> : T extends Set<infer U> ? JsonifiedArray<U[]> : T extends AsyncIteratorObject<infer U, infer V> ? AsyncIteratorObject<JsonifiedValue<U>, JsonifiedValue<V>> : unknown;
|
|
6
|
+
type JsonifiedArray<T extends Array<unknown>> = T extends readonly [] ? [] : T extends readonly [infer U, ...infer V] ? [U extends undefined ? null : JsonifiedValue<U>, ...JsonifiedArray<V>] : T extends Array<infer U> ? Array<JsonifiedValue<U>> : unknown;
|
|
7
|
+
/**
|
|
8
|
+
* Convert types that JSON not support to corresponding json types
|
|
9
|
+
*
|
|
10
|
+
* @see {@link https://orpc.dev/docs/openapi/client/openapi-link OpenAPI Link Docs}
|
|
11
|
+
*/
|
|
12
|
+
type JsonifiedClient<T extends NestedClient<any>> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? Client<UClientContext, UInput, JsonifiedValue<UOutput>, UError extends ORPCError<infer UCode, infer UData> ? ORPCError<UCode, JsonifiedValue<UData>> : UError> : {
|
|
13
|
+
[K in keyof T]: T[K] extends NestedClient<any> ? JsonifiedClient<T[K]> : T[K];
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type { JsonifiedArray, JsonifiedClient, JsonifiedValue };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,16 @@
|
|
|
1
|
+
import { NestedClient, Client, ORPCError } from '@orpc/client';
|
|
1
2
|
|
|
2
|
-
|
|
3
|
+
type JsonifiedValue<T> = T extends string ? T : T extends number ? T : T extends boolean ? T : T extends null ? T : T extends undefined ? T : T extends Array<unknown> ? JsonifiedArray<T> : T extends Record<string, unknown> ? {
|
|
4
|
+
[K in keyof T]: JsonifiedValue<T[K]>;
|
|
5
|
+
} : T extends Date ? string : T extends bigint ? string : T extends File ? File : T extends Blob ? Blob : T extends RegExp ? string : T extends URL ? string : T extends Map<infer K, infer V> ? JsonifiedArray<[K, V][]> : T extends Set<infer U> ? JsonifiedArray<U[]> : T extends AsyncIteratorObject<infer U, infer V> ? AsyncIteratorObject<JsonifiedValue<U>, JsonifiedValue<V>> : unknown;
|
|
6
|
+
type JsonifiedArray<T extends Array<unknown>> = T extends readonly [] ? [] : T extends readonly [infer U, ...infer V] ? [U extends undefined ? null : JsonifiedValue<U>, ...JsonifiedArray<V>] : T extends Array<infer U> ? Array<JsonifiedValue<U>> : unknown;
|
|
7
|
+
/**
|
|
8
|
+
* Convert types that JSON not support to corresponding json types
|
|
9
|
+
*
|
|
10
|
+
* @see {@link https://orpc.dev/docs/openapi/client/openapi-link OpenAPI Link Docs}
|
|
11
|
+
*/
|
|
12
|
+
type JsonifiedClient<T extends NestedClient<any>> = T extends Client<infer UClientContext, infer UInput, infer UOutput, infer UError> ? Client<UClientContext, UInput, JsonifiedValue<UOutput>, UError extends ORPCError<infer UCode, infer UData> ? ORPCError<UCode, JsonifiedValue<UData>> : UError> : {
|
|
13
|
+
[K in keyof T]: T[K] extends NestedClient<any> ? JsonifiedClient<T[K]> : T[K];
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type { JsonifiedArray, JsonifiedClient, JsonifiedValue };
|
|
@@ -0,0 +1,302 @@
|
|
|
1
|
+
import { toStandardHeaders, toHttpPath, getMalformedResponseErrorCode, StandardLink } from '@orpc/client/standard';
|
|
2
|
+
import { S as StandardBracketNotationSerializer } from './openapi-client.t9fCAe3x.mjs';
|
|
3
|
+
import { isObject, value, get, isAsyncIteratorObject } from '@orpc/shared';
|
|
4
|
+
import { isORPCErrorStatus, isORPCErrorJson, createORPCErrorFromJson, mapEventIterator, toORPCError } from '@orpc/client';
|
|
5
|
+
import { isContractProcedure, fallbackContractConfig, ORPCError } from '@orpc/contract';
|
|
6
|
+
import { mergeStandardHeaders, ErrorEvent } from '@orpc/standard-server';
|
|
7
|
+
|
|
8
|
+
class StandardOpenAPIJsonSerializer {
|
|
9
|
+
customSerializers;
|
|
10
|
+
constructor(options = {}) {
|
|
11
|
+
this.customSerializers = options.customJsonSerializers ?? [];
|
|
12
|
+
}
|
|
13
|
+
serialize(data, hasBlobRef = { value: false }) {
|
|
14
|
+
for (const custom of this.customSerializers) {
|
|
15
|
+
if (custom.condition(data)) {
|
|
16
|
+
const result = this.serialize(custom.serialize(data), hasBlobRef);
|
|
17
|
+
return result;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
if (data instanceof Blob) {
|
|
21
|
+
hasBlobRef.value = true;
|
|
22
|
+
return [data, hasBlobRef.value];
|
|
23
|
+
}
|
|
24
|
+
if (data instanceof Set) {
|
|
25
|
+
return this.serialize(Array.from(data), hasBlobRef);
|
|
26
|
+
}
|
|
27
|
+
if (data instanceof Map) {
|
|
28
|
+
return this.serialize(Array.from(data.entries()), hasBlobRef);
|
|
29
|
+
}
|
|
30
|
+
if (Array.isArray(data)) {
|
|
31
|
+
const json = data.map((v) => v === void 0 ? null : this.serialize(v, hasBlobRef)[0]);
|
|
32
|
+
return [json, hasBlobRef.value];
|
|
33
|
+
}
|
|
34
|
+
if (isObject(data)) {
|
|
35
|
+
const json = {};
|
|
36
|
+
for (const k in data) {
|
|
37
|
+
if (k === "toJSON" && typeof data[k] === "function") {
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
json[k] = this.serialize(data[k], hasBlobRef)[0];
|
|
41
|
+
}
|
|
42
|
+
return [json, hasBlobRef.value];
|
|
43
|
+
}
|
|
44
|
+
if (typeof data === "bigint" || data instanceof RegExp || data instanceof URL) {
|
|
45
|
+
return [data.toString(), hasBlobRef.value];
|
|
46
|
+
}
|
|
47
|
+
if (data instanceof Date) {
|
|
48
|
+
return [Number.isNaN(data.getTime()) ? null : data.toISOString(), hasBlobRef.value];
|
|
49
|
+
}
|
|
50
|
+
if (Number.isNaN(data)) {
|
|
51
|
+
return [null, hasBlobRef.value];
|
|
52
|
+
}
|
|
53
|
+
return [data, hasBlobRef.value];
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function standardizeHTTPPath(path) {
|
|
58
|
+
return `/${path.replace(/\/{2,}/g, "/").replace(/^\/|\/$/g, "")}`;
|
|
59
|
+
}
|
|
60
|
+
function getDynamicParams(path) {
|
|
61
|
+
return path ? standardizeHTTPPath(path).match(/\/\{[^}]+\}/g)?.map((v) => ({
|
|
62
|
+
raw: v,
|
|
63
|
+
name: v.match(/\{\+?([^}]+)\}/)[1]
|
|
64
|
+
})) : void 0;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
class StandardOpenapiLinkCodec {
|
|
68
|
+
constructor(contract, serializer, options) {
|
|
69
|
+
this.contract = contract;
|
|
70
|
+
this.serializer = serializer;
|
|
71
|
+
this.baseUrl = options.url;
|
|
72
|
+
this.headers = options.headers ?? {};
|
|
73
|
+
this.customErrorResponseBodyDecoder = options.customErrorResponseBodyDecoder;
|
|
74
|
+
}
|
|
75
|
+
baseUrl;
|
|
76
|
+
headers;
|
|
77
|
+
customErrorResponseBodyDecoder;
|
|
78
|
+
async encode(path, input, options) {
|
|
79
|
+
let headers = toStandardHeaders(await value(this.headers, options, path, input));
|
|
80
|
+
if (options.lastEventId !== void 0) {
|
|
81
|
+
headers = mergeStandardHeaders(headers, { "last-event-id": options.lastEventId });
|
|
82
|
+
}
|
|
83
|
+
const baseUrl = await value(this.baseUrl, options, path, input);
|
|
84
|
+
const procedure = get(this.contract, path);
|
|
85
|
+
if (!isContractProcedure(procedure)) {
|
|
86
|
+
throw new Error(`[StandardOpenapiLinkCodec] expect a contract procedure at ${path.join(".")}`);
|
|
87
|
+
}
|
|
88
|
+
const inputStructure = fallbackContractConfig("defaultInputStructure", procedure["~orpc"].route.inputStructure);
|
|
89
|
+
return inputStructure === "compact" ? this.#encodeCompact(procedure, path, input, options, baseUrl, headers) : this.#encodeDetailed(procedure, path, input, options, baseUrl, headers);
|
|
90
|
+
}
|
|
91
|
+
#encodeCompact(procedure, path, input, options, baseUrl, headers) {
|
|
92
|
+
let httpPath = standardizeHTTPPath(procedure["~orpc"].route.path ?? toHttpPath(path));
|
|
93
|
+
let httpBody = input;
|
|
94
|
+
const dynamicParams = getDynamicParams(httpPath);
|
|
95
|
+
if (dynamicParams?.length) {
|
|
96
|
+
if (!isObject(input)) {
|
|
97
|
+
throw new TypeError(`[StandardOpenapiLinkCodec] Invalid input shape for "compact" structure when has dynamic params at ${path.join(".")}.`);
|
|
98
|
+
}
|
|
99
|
+
const body = { ...input };
|
|
100
|
+
for (const param of dynamicParams) {
|
|
101
|
+
const value2 = input[param.name];
|
|
102
|
+
httpPath = httpPath.replace(param.raw, `/${encodeURIComponent(`${this.serializer.serialize(value2)}`)}`);
|
|
103
|
+
delete body[param.name];
|
|
104
|
+
}
|
|
105
|
+
httpBody = Object.keys(body).length ? body : void 0;
|
|
106
|
+
}
|
|
107
|
+
const method = fallbackContractConfig("defaultMethod", procedure["~orpc"].route.method);
|
|
108
|
+
const url = new URL(baseUrl);
|
|
109
|
+
url.pathname = `${url.pathname.replace(/\/$/, "")}${httpPath}`;
|
|
110
|
+
if (method === "GET") {
|
|
111
|
+
const serialized = this.serializer.serialize(httpBody, { outputFormat: "URLSearchParams" });
|
|
112
|
+
for (const [key, value2] of serialized) {
|
|
113
|
+
url.searchParams.append(key, value2);
|
|
114
|
+
}
|
|
115
|
+
return {
|
|
116
|
+
url,
|
|
117
|
+
method,
|
|
118
|
+
headers,
|
|
119
|
+
body: void 0,
|
|
120
|
+
signal: options.signal
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
return {
|
|
124
|
+
url,
|
|
125
|
+
method,
|
|
126
|
+
headers,
|
|
127
|
+
body: this.serializer.serialize(httpBody),
|
|
128
|
+
signal: options.signal
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
#encodeDetailed(procedure, path, input, options, baseUrl, headers) {
|
|
132
|
+
let httpPath = standardizeHTTPPath(procedure["~orpc"].route.path ?? toHttpPath(path));
|
|
133
|
+
const dynamicParams = getDynamicParams(httpPath);
|
|
134
|
+
if (!isObject(input) && input !== void 0) {
|
|
135
|
+
throw new TypeError(`[StandardOpenapiLinkCodec] Invalid input shape for "detailed" structure at ${path.join(".")}.`);
|
|
136
|
+
}
|
|
137
|
+
if (dynamicParams?.length) {
|
|
138
|
+
if (!isObject(input?.params)) {
|
|
139
|
+
throw new TypeError(`[StandardOpenapiLinkCodec] Invalid input.params shape for "detailed" structure when has dynamic params at ${path.join(".")}.`);
|
|
140
|
+
}
|
|
141
|
+
for (const param of dynamicParams) {
|
|
142
|
+
const value2 = input.params[param.name];
|
|
143
|
+
httpPath = httpPath.replace(param.raw, `/${encodeURIComponent(`${this.serializer.serialize(value2)}`)}`);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
let mergedHeaders = headers;
|
|
147
|
+
if (input?.headers !== void 0) {
|
|
148
|
+
if (!isObject(input.headers)) {
|
|
149
|
+
throw new TypeError(`[StandardOpenapiLinkCodec] Invalid input.headers shape for "detailed" structure at ${path.join(".")}.`);
|
|
150
|
+
}
|
|
151
|
+
mergedHeaders = mergeStandardHeaders(input.headers, headers);
|
|
152
|
+
}
|
|
153
|
+
const method = fallbackContractConfig("defaultMethod", procedure["~orpc"].route.method);
|
|
154
|
+
const url = new URL(baseUrl);
|
|
155
|
+
url.pathname = `${url.pathname.replace(/\/$/, "")}${httpPath}`;
|
|
156
|
+
if (input?.query !== void 0) {
|
|
157
|
+
const query = this.serializer.serialize(input.query, { outputFormat: "URLSearchParams" });
|
|
158
|
+
for (const [key, value2] of query) {
|
|
159
|
+
url.searchParams.append(key, value2);
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
if (method === "GET") {
|
|
163
|
+
return {
|
|
164
|
+
url,
|
|
165
|
+
method,
|
|
166
|
+
headers: mergedHeaders,
|
|
167
|
+
body: void 0,
|
|
168
|
+
signal: options.signal
|
|
169
|
+
};
|
|
170
|
+
}
|
|
171
|
+
return {
|
|
172
|
+
url,
|
|
173
|
+
method,
|
|
174
|
+
headers: mergedHeaders,
|
|
175
|
+
body: this.serializer.serialize(input?.body),
|
|
176
|
+
signal: options.signal
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
async decode(response, _options, path) {
|
|
180
|
+
const isOk = !isORPCErrorStatus(response.status);
|
|
181
|
+
const deserialized = await (async () => {
|
|
182
|
+
let isBodyOk = false;
|
|
183
|
+
try {
|
|
184
|
+
const body = await response.body();
|
|
185
|
+
isBodyOk = true;
|
|
186
|
+
return this.serializer.deserialize(body);
|
|
187
|
+
} catch (error) {
|
|
188
|
+
if (!isBodyOk) {
|
|
189
|
+
throw new Error("Cannot parse response body, please check the response body and content-type.", {
|
|
190
|
+
cause: error
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
throw new Error("Invalid OpenAPI response format.", {
|
|
194
|
+
cause: error
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
})();
|
|
198
|
+
if (!isOk) {
|
|
199
|
+
const error = this.customErrorResponseBodyDecoder?.(deserialized, response);
|
|
200
|
+
if (error !== null && error !== void 0) {
|
|
201
|
+
throw error;
|
|
202
|
+
}
|
|
203
|
+
if (isORPCErrorJson(deserialized)) {
|
|
204
|
+
throw createORPCErrorFromJson(deserialized);
|
|
205
|
+
}
|
|
206
|
+
throw new ORPCError(getMalformedResponseErrorCode(response.status), {
|
|
207
|
+
status: response.status,
|
|
208
|
+
data: { ...response, body: deserialized }
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
const procedure = get(this.contract, path);
|
|
212
|
+
if (!isContractProcedure(procedure)) {
|
|
213
|
+
throw new Error(`[StandardOpenapiLinkCodec] expect a contract procedure at ${path.join(".")}`);
|
|
214
|
+
}
|
|
215
|
+
const outputStructure = fallbackContractConfig("defaultOutputStructure", procedure["~orpc"].route.outputStructure);
|
|
216
|
+
if (outputStructure === "compact") {
|
|
217
|
+
return deserialized;
|
|
218
|
+
}
|
|
219
|
+
return {
|
|
220
|
+
status: response.status,
|
|
221
|
+
headers: response.headers,
|
|
222
|
+
body: deserialized
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
class StandardOpenAPISerializer {
|
|
228
|
+
constructor(jsonSerializer, bracketNotation) {
|
|
229
|
+
this.jsonSerializer = jsonSerializer;
|
|
230
|
+
this.bracketNotation = bracketNotation;
|
|
231
|
+
}
|
|
232
|
+
serialize(data, options = {}) {
|
|
233
|
+
if (isAsyncIteratorObject(data) && !options.outputFormat) {
|
|
234
|
+
return mapEventIterator(data, {
|
|
235
|
+
value: async (value) => this.#serialize(value, { outputFormat: "plain" }),
|
|
236
|
+
error: async (e) => {
|
|
237
|
+
return new ErrorEvent({
|
|
238
|
+
data: this.#serialize(toORPCError(e).toJSON(), { outputFormat: "plain" }),
|
|
239
|
+
cause: e
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
return this.#serialize(data, options);
|
|
245
|
+
}
|
|
246
|
+
#serialize(data, options) {
|
|
247
|
+
const [json, hasBlob] = this.jsonSerializer.serialize(data);
|
|
248
|
+
if (options.outputFormat === "plain") {
|
|
249
|
+
return json;
|
|
250
|
+
}
|
|
251
|
+
if (options.outputFormat === "URLSearchParams") {
|
|
252
|
+
const params = new URLSearchParams();
|
|
253
|
+
for (const [path, value] of this.bracketNotation.serialize(json)) {
|
|
254
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
255
|
+
params.append(path, value.toString());
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
return params;
|
|
259
|
+
}
|
|
260
|
+
if (json instanceof Blob || json === void 0 || !hasBlob) {
|
|
261
|
+
return json;
|
|
262
|
+
}
|
|
263
|
+
const form = new FormData();
|
|
264
|
+
for (const [path, value] of this.bracketNotation.serialize(json)) {
|
|
265
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
266
|
+
form.append(path, value.toString());
|
|
267
|
+
} else if (value instanceof Blob) {
|
|
268
|
+
form.append(path, value);
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return form;
|
|
272
|
+
}
|
|
273
|
+
deserialize(data) {
|
|
274
|
+
if (data instanceof URLSearchParams || data instanceof FormData) {
|
|
275
|
+
return this.bracketNotation.deserialize(Array.from(data.entries()));
|
|
276
|
+
}
|
|
277
|
+
if (isAsyncIteratorObject(data)) {
|
|
278
|
+
return mapEventIterator(data, {
|
|
279
|
+
value: async (value) => value,
|
|
280
|
+
error: async (e) => {
|
|
281
|
+
if (e instanceof ErrorEvent && isORPCErrorJson(e.data)) {
|
|
282
|
+
return createORPCErrorFromJson(e.data, { cause: e });
|
|
283
|
+
}
|
|
284
|
+
return e;
|
|
285
|
+
}
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
return data;
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
class StandardOpenAPILink extends StandardLink {
|
|
293
|
+
constructor(contract, linkClient, options) {
|
|
294
|
+
const jsonSerializer = new StandardOpenAPIJsonSerializer(options);
|
|
295
|
+
const bracketNotationSerializer = new StandardBracketNotationSerializer({ maxBracketNotationArrayIndex: 4294967294 });
|
|
296
|
+
const serializer = new StandardOpenAPISerializer(jsonSerializer, bracketNotationSerializer);
|
|
297
|
+
const linkCodec = new StandardOpenapiLinkCodec(contract, serializer, options);
|
|
298
|
+
super(linkCodec, linkClient, options);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
export { StandardOpenAPIJsonSerializer as S, StandardOpenAPILink as a, StandardOpenapiLinkCodec as b, StandardOpenAPISerializer as c, getDynamicParams as g, standardizeHTTPPath as s };
|