@orpc/openapi-client 0.0.0-next.a4ecb29 → 0.0.0-next.a55d49f

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.
@@ -0,0 +1,146 @@
1
+ import { isObject, NullProtoObj } from '@orpc/shared';
2
+
3
+ class StandardBracketNotationSerializer {
4
+ maxArrayIndex;
5
+ constructor(options = {}) {
6
+ this.maxArrayIndex = options.maxBracketNotationArrayIndex ?? 9999;
7
+ }
8
+ serialize(data, segments = [], result = []) {
9
+ if (Array.isArray(data)) {
10
+ data.forEach((item, i) => {
11
+ this.serialize(item, [...segments, i], result);
12
+ });
13
+ } else if (isObject(data)) {
14
+ for (const key in data) {
15
+ this.serialize(data[key], [...segments, key], result);
16
+ }
17
+ } else {
18
+ result.push([this.stringifyPath(segments), data]);
19
+ }
20
+ return result;
21
+ }
22
+ deserialize(serialized) {
23
+ if (serialized.length === 0) {
24
+ return {};
25
+ }
26
+ const arrayPushStyles = /* @__PURE__ */ new WeakSet();
27
+ const ref = { value: [] };
28
+ for (const [path, value] of serialized) {
29
+ const segments = this.parsePath(path);
30
+ let currentRef = ref;
31
+ let nextSegment = "value";
32
+ segments.forEach((segment, i) => {
33
+ if (!Array.isArray(currentRef[nextSegment]) && !isObject(currentRef[nextSegment])) {
34
+ currentRef[nextSegment] = [];
35
+ }
36
+ if (i !== segments.length - 1) {
37
+ if (Array.isArray(currentRef[nextSegment]) && !isValidArrayIndex(segment, this.maxArrayIndex)) {
38
+ if (arrayPushStyles.has(currentRef[nextSegment])) {
39
+ arrayPushStyles.delete(currentRef[nextSegment]);
40
+ currentRef[nextSegment] = pushStyleArrayToObject(currentRef[nextSegment]);
41
+ } else {
42
+ currentRef[nextSegment] = arrayToObject(currentRef[nextSegment]);
43
+ }
44
+ }
45
+ } else {
46
+ if (Array.isArray(currentRef[nextSegment])) {
47
+ if (segment === "") {
48
+ if (currentRef[nextSegment].length && !arrayPushStyles.has(currentRef[nextSegment])) {
49
+ currentRef[nextSegment] = arrayToObject(currentRef[nextSegment]);
50
+ }
51
+ } else {
52
+ if (arrayPushStyles.has(currentRef[nextSegment])) {
53
+ arrayPushStyles.delete(currentRef[nextSegment]);
54
+ currentRef[nextSegment] = pushStyleArrayToObject(currentRef[nextSegment]);
55
+ } else if (!isValidArrayIndex(segment, this.maxArrayIndex)) {
56
+ currentRef[nextSegment] = arrayToObject(currentRef[nextSegment]);
57
+ }
58
+ }
59
+ }
60
+ }
61
+ currentRef = currentRef[nextSegment];
62
+ nextSegment = segment;
63
+ });
64
+ if (Array.isArray(currentRef) && nextSegment === "") {
65
+ arrayPushStyles.add(currentRef);
66
+ currentRef.push(value);
67
+ } else if (nextSegment in currentRef) {
68
+ if (Array.isArray(currentRef[nextSegment])) {
69
+ currentRef[nextSegment].push(value);
70
+ } else {
71
+ currentRef[nextSegment] = [currentRef[nextSegment], value];
72
+ }
73
+ } else {
74
+ currentRef[nextSegment] = value;
75
+ }
76
+ }
77
+ return ref.value;
78
+ }
79
+ stringifyPath(segments) {
80
+ return segments.map((segment) => {
81
+ return segment.toString().replace(/[\\[\]]/g, (match) => {
82
+ switch (match) {
83
+ case "\\":
84
+ return "\\\\";
85
+ case "[":
86
+ return "\\[";
87
+ case "]":
88
+ return "\\]";
89
+ /* v8 ignore next 2 */
90
+ default:
91
+ return match;
92
+ }
93
+ });
94
+ }).reduce((result, segment, i) => {
95
+ if (i === 0) {
96
+ return segment;
97
+ }
98
+ return `${result}[${segment}]`;
99
+ }, "");
100
+ }
101
+ parsePath(path) {
102
+ const segments = [];
103
+ let inBrackets = false;
104
+ let currentSegment = "";
105
+ let backslashCount = 0;
106
+ for (let i = 0; i < path.length; i++) {
107
+ const char = path[i];
108
+ const nextChar = path[i + 1];
109
+ if (inBrackets && char === "]" && (nextChar === void 0 || nextChar === "[") && backslashCount % 2 === 0) {
110
+ if (nextChar === void 0) {
111
+ inBrackets = false;
112
+ }
113
+ segments.push(currentSegment);
114
+ currentSegment = "";
115
+ i++;
116
+ } else if (segments.length === 0 && char === "[" && backslashCount % 2 === 0) {
117
+ inBrackets = true;
118
+ segments.push(currentSegment);
119
+ currentSegment = "";
120
+ } else if (char === "\\") {
121
+ backslashCount++;
122
+ } else {
123
+ currentSegment += "\\".repeat(backslashCount / 2) + char;
124
+ backslashCount = 0;
125
+ }
126
+ }
127
+ return inBrackets || segments.length === 0 ? [path] : segments;
128
+ }
129
+ }
130
+ function isValidArrayIndex(value, maxIndex) {
131
+ return /^0$|^[1-9]\d*$/.test(value) && Number(value) <= maxIndex;
132
+ }
133
+ function arrayToObject(array) {
134
+ const obj = new NullProtoObj();
135
+ array.forEach((item, i) => {
136
+ obj[i] = item;
137
+ });
138
+ return obj;
139
+ }
140
+ function pushStyleArrayToObject(array) {
141
+ const obj = new NullProtoObj();
142
+ obj[""] = array.length === 1 ? array[0] : array;
143
+ return obj;
144
+ }
145
+
146
+ export { StandardBracketNotationSerializer as S };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@orpc/openapi-client",
3
3
  "type": "module",
4
- "version": "0.0.0-next.a4ecb29",
4
+ "version": "0.0.0-next.a55d49f",
5
5
  "license": "MIT",
6
6
  "homepage": "https://orpc.unnoq.com",
7
7
  "repository": {
@@ -19,19 +19,33 @@
19
19
  "import": "./dist/index.mjs",
20
20
  "default": "./dist/index.mjs"
21
21
  },
22
+ "./helpers": {
23
+ "types": "./dist/helpers/index.d.mts",
24
+ "import": "./dist/helpers/index.mjs",
25
+ "default": "./dist/helpers/index.mjs"
26
+ },
22
27
  "./standard": {
23
28
  "types": "./dist/adapters/standard/index.d.mts",
24
29
  "import": "./dist/adapters/standard/index.mjs",
25
30
  "default": "./dist/adapters/standard/index.mjs"
31
+ },
32
+ "./fetch": {
33
+ "types": "./dist/adapters/fetch/index.d.mts",
34
+ "import": "./dist/adapters/fetch/index.mjs",
35
+ "default": "./dist/adapters/fetch/index.mjs"
26
36
  }
27
37
  },
28
38
  "files": [
29
39
  "dist"
30
40
  ],
31
41
  "dependencies": {
32
- "@orpc/shared": "0.0.0-next.a4ecb29",
33
- "@orpc/client": "0.0.0-next.a4ecb29",
34
- "@orpc/standard-server": "0.0.0-next.a4ecb29"
42
+ "@orpc/client": "0.0.0-next.a55d49f",
43
+ "@orpc/contract": "0.0.0-next.a55d49f",
44
+ "@orpc/standard-server": "0.0.0-next.a55d49f",
45
+ "@orpc/shared": "0.0.0-next.a55d49f"
46
+ },
47
+ "devDependencies": {
48
+ "@orpc/server": "0.0.0-next.a55d49f"
35
49
  },
36
50
  "scripts": {
37
51
  "build": "unbuild",