@orpc/openapi-client 0.0.0-next.b4fc1d9 → 0.0.0-next.b50e4fc

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.
@@ -1,229 +1,8 @@
1
- import { isObject, isAsyncIteratorObject } from '@orpc/shared';
2
- import { mapEventIterator, toORPCError, ORPCError } from '@orpc/client';
3
- import { ErrorEvent } from '@orpc/standard-server';
4
-
5
- class StandardBracketNotationSerializer {
6
- serialize(data, segments = [], result = []) {
7
- if (Array.isArray(data)) {
8
- data.forEach((item, i) => {
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
- if (k === "toJSON" && typeof data[k] === "function") {
154
- continue;
155
- }
156
- json[k] = this.serialize(data[k], hasBlobRef)[0];
157
- }
158
- return [json, hasBlobRef.value];
159
- }
160
- if (typeof data === "bigint" || data instanceof RegExp || data instanceof URL) {
161
- return [data.toString(), hasBlobRef.value];
162
- }
163
- if (data instanceof Date) {
164
- return [Number.isNaN(data.getTime()) ? null : data.toISOString(), hasBlobRef.value];
165
- }
166
- if (Number.isNaN(data)) {
167
- return [null, hasBlobRef.value];
168
- }
169
- return [data, hasBlobRef.value];
170
- }
171
- }
172
-
173
- class StandardOpenAPISerializer {
174
- constructor(jsonSerializer, bracketNotation) {
175
- this.jsonSerializer = jsonSerializer;
176
- this.bracketNotation = bracketNotation;
177
- }
178
- serialize(data) {
179
- if (isAsyncIteratorObject(data)) {
180
- return mapEventIterator(data, {
181
- value: async (value) => this.#serialize(value, false),
182
- error: async (e) => {
183
- return new ErrorEvent({
184
- data: this.#serialize(toORPCError(e).toJSON(), false),
185
- cause: e
186
- });
187
- }
188
- });
189
- }
190
- return this.#serialize(data, true);
191
- }
192
- #serialize(data, enableFormData) {
193
- if (data instanceof Blob || data === void 0) {
194
- return data;
195
- }
196
- const [json, hasBlob] = this.jsonSerializer.serialize(data);
197
- if (!enableFormData || !hasBlob) {
198
- return json;
199
- }
200
- const form = new FormData();
201
- for (const [path, value] of this.bracketNotation.serialize(json)) {
202
- if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
203
- form.append(path, value.toString());
204
- } else if (value instanceof Blob) {
205
- form.append(path, value);
206
- }
207
- }
208
- return form;
209
- }
210
- deserialize(data) {
211
- if (data instanceof URLSearchParams || data instanceof FormData) {
212
- return this.bracketNotation.deserialize(Array.from(data.entries()));
213
- }
214
- if (isAsyncIteratorObject(data)) {
215
- return mapEventIterator(data, {
216
- value: async (value) => value,
217
- error: async (e) => {
218
- if (e instanceof ErrorEvent && ORPCError.isValidJSON(e.data)) {
219
- return ORPCError.fromJSON(e.data, { cause: e });
220
- }
221
- return e;
222
- }
223
- });
224
- }
225
- return data;
226
- }
227
- }
228
-
229
- 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
- export { }
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
- export { }
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 };