valyrian.js 7.2.11 → 8.0.0

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.
Files changed (165) hide show
  1. package/README.md +6 -6
  2. package/dist/flux-store/index.d.ts +32 -0
  3. package/dist/flux-store/index.d.ts.map +1 -0
  4. package/dist/flux-store/index.js +258 -0
  5. package/dist/flux-store/index.js.map +7 -0
  6. package/dist/flux-store/index.min.js +1 -0
  7. package/dist/flux-store/index.min.js.map +1 -0
  8. package/dist/flux-store/index.mjs +237 -0
  9. package/dist/flux-store/index.mjs.map +7 -0
  10. package/dist/hooks/index.d.ts.map +1 -1
  11. package/dist/hooks/index.js +42 -75
  12. package/dist/hooks/index.js.map +2 -2
  13. package/dist/hooks/index.min.js +1 -0
  14. package/dist/hooks/index.min.js.map +1 -0
  15. package/dist/hooks/index.mjs +43 -76
  16. package/dist/hooks/index.mjs.map +2 -2
  17. package/dist/index.d.ts +52 -54
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +397 -328
  20. package/dist/index.js.map +3 -3
  21. package/dist/index.min.js +1 -1
  22. package/dist/index.min.js.map +1 -1
  23. package/dist/index.mjs +397 -328
  24. package/dist/index.mjs.map +3 -3
  25. package/dist/native-store/index.d.ts +14 -0
  26. package/dist/native-store/index.d.ts.map +1 -0
  27. package/dist/native-store/index.js +103 -0
  28. package/dist/native-store/index.js.map +7 -0
  29. package/dist/native-store/index.min.js +1 -0
  30. package/dist/native-store/index.min.js.map +1 -0
  31. package/dist/native-store/index.mjs +82 -0
  32. package/dist/native-store/index.mjs.map +7 -0
  33. package/dist/node/index.d.ts.map +1 -1
  34. package/dist/node/index.js +223 -86
  35. package/dist/node/index.js.map +4 -4
  36. package/dist/node/index.mjs +223 -86
  37. package/dist/node/index.mjs.map +4 -4
  38. package/dist/node/node.sw.js +152 -0
  39. package/dist/node/utils/inline.d.ts.map +1 -1
  40. package/dist/node/utils/node.sw.js +152 -0
  41. package/dist/node/utils/session-storage.d.ts +22 -0
  42. package/dist/node/utils/session-storage.d.ts.map +1 -0
  43. package/dist/node/utils/sw.d.ts.map +1 -1
  44. package/dist/node/utils/tree-adapter.d.ts +9 -0
  45. package/dist/node/utils/tree-adapter.d.ts.map +1 -1
  46. package/dist/pulse-store/index.d.ts +13 -0
  47. package/dist/pulse-store/index.d.ts.map +1 -0
  48. package/dist/pulse-store/index.js +143 -0
  49. package/dist/pulse-store/index.js.map +7 -0
  50. package/dist/pulse-store/index.min.js +1 -0
  51. package/dist/pulse-store/index.min.js.map +1 -0
  52. package/dist/pulse-store/index.mjs +122 -0
  53. package/dist/pulse-store/index.mjs.map +7 -0
  54. package/dist/request/index.d.ts.map +1 -1
  55. package/dist/request/index.js +68 -89
  56. package/dist/request/index.js.map +2 -2
  57. package/dist/request/index.min.js +1 -0
  58. package/dist/request/index.min.js.map +1 -0
  59. package/dist/request/index.mjs +68 -89
  60. package/dist/request/index.mjs.map +2 -2
  61. package/dist/router/index.d.ts +32 -31
  62. package/dist/router/index.d.ts.map +1 -1
  63. package/dist/router/index.js +256 -104
  64. package/dist/router/index.js.map +3 -3
  65. package/dist/router/index.min.js +1 -0
  66. package/dist/router/index.min.js.map +1 -0
  67. package/dist/router/index.mjs +256 -104
  68. package/dist/router/index.mjs.map +3 -3
  69. package/dist/signals/index.d.ts +6 -0
  70. package/dist/signals/index.d.ts.map +1 -0
  71. package/dist/signals/index.js +92 -0
  72. package/dist/signals/index.js.map +7 -0
  73. package/dist/signals/index.min.js +1 -0
  74. package/dist/signals/index.min.js.map +1 -0
  75. package/dist/signals/index.mjs +71 -0
  76. package/dist/signals/index.mjs.map +7 -0
  77. package/dist/suspense/index.d.ts +6 -0
  78. package/dist/suspense/index.d.ts.map +1 -0
  79. package/dist/suspense/index.js +67 -0
  80. package/dist/suspense/index.js.map +7 -0
  81. package/dist/suspense/index.min.js +1 -0
  82. package/dist/suspense/index.min.js.map +1 -0
  83. package/dist/suspense/index.mjs +46 -0
  84. package/dist/suspense/index.mjs.map +7 -0
  85. package/dist/sw/index.min.js +1 -0
  86. package/dist/sw/index.min.js.map +1 -0
  87. package/dist/translate/index.d.ts +19 -0
  88. package/dist/translate/index.d.ts.map +1 -0
  89. package/dist/translate/index.js +150 -0
  90. package/dist/translate/index.js.map +7 -0
  91. package/dist/translate/index.min.js +1 -0
  92. package/dist/translate/index.min.js.map +1 -0
  93. package/dist/translate/index.mjs +129 -0
  94. package/dist/translate/index.mjs.map +7 -0
  95. package/dist/tsconfig.tsbuildinfo +1 -1
  96. package/dist/utils/deep-freeze.d.ts +3 -0
  97. package/dist/utils/deep-freeze.d.ts.map +1 -0
  98. package/dist/utils/getter-setter.d.ts +3 -0
  99. package/dist/utils/getter-setter.d.ts.map +1 -0
  100. package/dist/utils/has-changed.d.ts +2 -0
  101. package/dist/utils/has-changed.d.ts.map +1 -0
  102. package/dist/utils/index.d.ts +4 -0
  103. package/dist/utils/index.d.ts.map +1 -0
  104. package/dist/utils/index.js +138 -0
  105. package/dist/utils/index.js.map +7 -0
  106. package/dist/utils/index.min.js +1 -0
  107. package/dist/utils/index.min.js.map +1 -0
  108. package/dist/utils/index.mjs +115 -0
  109. package/dist/utils/index.mjs.map +7 -0
  110. package/lib/flux-store/index.ts +301 -0
  111. package/lib/hooks/index.ts +52 -101
  112. package/lib/index.ts +479 -719
  113. package/lib/native-store/index.ts +106 -0
  114. package/lib/node/index.ts +5 -3
  115. package/lib/node/utils/icons.ts +5 -5
  116. package/lib/node/utils/inline.ts +17 -17
  117. package/lib/node/utils/node.sw.js +152 -0
  118. package/lib/node/utils/session-storage.ts +117 -0
  119. package/lib/node/utils/sw.ts +35 -11
  120. package/lib/node/utils/tree-adapter.ts +99 -52
  121. package/lib/pulse-store/index.ts +181 -0
  122. package/lib/request/index.ts +86 -116
  123. package/lib/router/index.ts +358 -170
  124. package/lib/signals/index.ts +98 -0
  125. package/lib/suspense/index.ts +57 -0
  126. package/lib/translate/index.ts +156 -0
  127. package/lib/utils/deep-freeze.ts +54 -0
  128. package/lib/utils/getter-setter.ts +40 -0
  129. package/lib/utils/has-changed.ts +43 -0
  130. package/lib/utils/index.ts +3 -0
  131. package/package.json +38 -50
  132. package/tsconfig.json +1 -1
  133. package/dist/dataset/index.d.ts +0 -24
  134. package/dist/dataset/index.d.ts.map +0 -1
  135. package/dist/dataset/index.js +0 -178
  136. package/dist/dataset/index.js.map +0 -7
  137. package/dist/dataset/index.mjs +0 -157
  138. package/dist/dataset/index.mjs.map +0 -7
  139. package/dist/node/node.sw.tpl +0 -133
  140. package/dist/node/utils/node.sw.tpl +0 -133
  141. package/dist/proxy-signal/index.d.ts +0 -23
  142. package/dist/proxy-signal/index.d.ts.map +0 -1
  143. package/dist/proxy-signal/index.js +0 -138
  144. package/dist/proxy-signal/index.js.map +0 -7
  145. package/dist/proxy-signal/index.mjs +0 -117
  146. package/dist/proxy-signal/index.mjs.map +0 -7
  147. package/dist/signal/index.d.ts +0 -20
  148. package/dist/signal/index.d.ts.map +0 -1
  149. package/dist/signal/index.js +0 -95
  150. package/dist/signal/index.js.map +0 -7
  151. package/dist/signal/index.mjs +0 -74
  152. package/dist/signal/index.mjs.map +0 -7
  153. package/dist/store/index.d.ts +0 -16
  154. package/dist/store/index.d.ts.map +0 -1
  155. package/dist/store/index.js +0 -93
  156. package/dist/store/index.js.map +0 -7
  157. package/dist/store/index.mjs +0 -72
  158. package/dist/store/index.mjs.map +0 -7
  159. package/lib/dataset/index.ts +0 -193
  160. package/lib/index.d.ts +0 -0
  161. package/lib/interfaces.ts.bak +0 -141
  162. package/lib/node/utils/node.sw.tpl +0 -133
  163. package/lib/proxy-signal/index.ts +0 -187
  164. package/lib/signal/index.ts +0 -161
  165. package/lib/store/index.ts +0 -101
@@ -1,4 +1,5 @@
1
1
  import { isNodeJs } from "valyrian.js";
2
+ import { get, set } from "valyrian.js/utils";
2
3
 
3
4
  interface UrlOptions {
4
5
  base: string; // Used to prefix the url for scoped requests.
@@ -51,48 +52,54 @@ export interface RequestInterface {
51
52
  }
52
53
 
53
54
  // This method is used to serialize an object into a query string.
54
- function serialize(obj: Record<string, any>, prefix: string = ""): string {
55
- return Object.keys(obj)
56
- .map((prop: string) => {
57
- let k = prefix ? `${prefix}[${prop}]` : prop;
58
- return typeof obj[prop] === "object"
59
- ? serialize(obj[prop], k)
60
- : `${encodeURIComponent(k)}=${encodeURIComponent(obj[prop])}`;
61
- })
62
- .join("&");
63
- }
55
+ function serialize(obj: Record<string, any>, prefix: string = ""): URLSearchParams {
56
+ const params = new URLSearchParams();
57
+
58
+ Object.keys(obj).forEach((prop: string) => {
59
+ const key = prefix ? `${prefix}[${prop}]` : prop;
60
+ if (typeof obj[prop] === "object") {
61
+ params.append(key, serialize(obj[prop], key).toString());
62
+ } else {
63
+ params.append(key, obj[prop]);
64
+ }
65
+ });
64
66
 
65
- function parseUrl(url: string, options: RequestOptionsWithUrls) {
66
- let u = /^https?/gi.test(url) ? url : options.urls.base + url;
67
+ return params;
68
+ }
67
69
 
68
- let parts = u.split("?");
69
- u = parts[0].trim().replace(/^\/\//, "/").replace(/\/$/, "").trim();
70
+ function serializeFormData(data: Record<string, any>): FormData {
71
+ return Object.entries(data).reduce((fd, [key, value]) => {
72
+ fd.append(key, value);
73
+ return fd;
74
+ }, new FormData());
75
+ }
70
76
 
71
- if (parts[1]) {
72
- u += `?${parts[1]}`;
73
- }
77
+ function parseUrl(url: string, options: RequestOptionsWithUrls) {
78
+ const urlWithoutSlash = url.replace(/\/+$/, "").trim();
79
+ const u = /^https?/gi.test(urlWithoutSlash) ? urlWithoutSlash : `${options.urls.base}${urlWithoutSlash}`;
74
80
 
75
81
  if (isNodeJs && typeof options.urls.node === "string") {
76
- options.urls.node = options.urls.node;
77
-
78
82
  if (typeof options.urls.api === "string") {
79
- options.urls.api = options.urls.api.replace(/\/$/gi, "").trim();
80
- u = u.replace(options.urls.api, options.urls.node);
83
+ return new URL(u.replace(options.urls.api, options.urls.node));
81
84
  }
82
85
 
83
86
  if (!/^https?/gi.test(u)) {
84
- u = options.urls.node + u;
87
+ return new URL(u, options.urls.node);
85
88
  }
86
89
  }
87
90
 
88
- return u;
91
+ if (/^https?/gi.test(u)) {
92
+ return new URL(u);
93
+ }
94
+
95
+ return new URL(u, options.urls.base);
89
96
  }
90
97
 
91
98
  const defaultOptions: RequestOptions = { allowedMethods: ["get", "post", "put", "patch", "delete", "head", "options"] };
92
99
 
93
100
  // eslint-disable-next-line sonarjs/cognitive-complexity
94
101
  function Requester(baseUrl = "", options: RequestOptions = defaultOptions) {
95
- let url = baseUrl.replace(/\/$/gi, "").trim();
102
+ const url = baseUrl.replace(/\/$/gi, "").trim();
96
103
  if (!options.urls) {
97
104
  options.urls = {
98
105
  base: "",
@@ -105,7 +112,7 @@ function Requester(baseUrl = "", options: RequestOptions = defaultOptions) {
105
112
  options.allowedMethods = defaultOptions.allowedMethods;
106
113
  }
107
114
 
108
- let opts: RequestOptionsWithUrls = {
115
+ const opts: RequestOptionsWithUrls = {
109
116
  ...(options as RequestOptionsWithUrls),
110
117
  urls: {
111
118
  node: options.urls.node || null,
@@ -114,8 +121,14 @@ function Requester(baseUrl = "", options: RequestOptions = defaultOptions) {
114
121
  }
115
122
  };
116
123
 
117
- const request = async function request(method: string, url: string, data?: Record<string, any>, options = {}) {
118
- let innerOptions: SendOptions = {
124
+ // eslint-disable-next-line complexity
125
+ const request = async function request(
126
+ method: string,
127
+ url: string,
128
+ data?: Record<string, any>,
129
+ options: Record<string, any> = {}
130
+ ) {
131
+ const innerOptions: SendOptions = {
119
132
  method: method.toUpperCase(),
120
133
  headers: {},
121
134
  resolveWithFullResponse: false,
@@ -123,44 +136,40 @@ function Requester(baseUrl = "", options: RequestOptions = defaultOptions) {
123
136
  ...options
124
137
  } as SendOptions;
125
138
 
139
+ innerOptions.headers = { ...innerOptions.headers, ...opts.headers, ...options.headers };
140
+
126
141
  if (!innerOptions.headers.Accept) {
127
142
  innerOptions.headers.Accept = "application/json";
128
143
  }
129
144
 
130
- let acceptType = innerOptions.headers.Accept;
131
- let contentType = innerOptions.headers["Content-Type"] || innerOptions.headers["content-type"] || "";
145
+ const acceptType = innerOptions.headers.Accept;
146
+ const contentType = innerOptions.headers["Content-Type"] || innerOptions.headers["content-type"] || "";
147
+
148
+ if (!innerOptions.allowedMethods.includes(method)) {
149
+ throw new Error(`Method ${method} not allowed. Allowed methods: ${innerOptions.allowedMethods.join(", ")}`);
150
+ }
132
151
 
133
- if (innerOptions.allowedMethods.indexOf(method) === -1) {
134
- throw new Error("Method not allowed");
152
+ let finalUrl: URL;
153
+ try {
154
+ finalUrl = parseUrl(url, opts);
155
+ } catch (error) {
156
+ throw new Error(`Failed to parse URL: ${url}`);
135
157
  }
136
158
 
137
159
  if (data) {
138
- if (innerOptions.method === "GET" && typeof data === "object") {
139
- url += `?${serialize(data)}`;
140
- }
160
+ const isJson = /json/gi.test(contentType);
141
161
 
142
- if (innerOptions.method !== "GET") {
143
- if (/json/gi.test(contentType)) {
144
- innerOptions.body = JSON.stringify(data);
145
- } else {
146
- let formData;
147
- if (data instanceof FormData) {
148
- formData = data;
149
- } else {
150
- formData = new FormData();
151
- for (let i in data) {
152
- formData.append(i, data[i]);
153
- }
154
- }
155
- innerOptions.body = formData;
156
- }
162
+ if (innerOptions.method === "GET" && typeof data === "object") {
163
+ finalUrl.search = serialize(data).toString();
164
+ } else if (innerOptions.method !== "GET") {
165
+ innerOptions.body = isJson ? JSON.stringify(data) : serializeFormData(data);
157
166
  }
158
167
  }
159
168
 
160
- let response = await fetch(parseUrl(url, opts), innerOptions);
169
+ const response = await fetch(finalUrl.toString(), innerOptions);
161
170
  let body = null;
162
171
  if (!response.ok) {
163
- let err = new Error(response.statusText) as Error & { response?: any; body?: any };
172
+ const err = new Error(`${response.status}: ${response.statusText}`) as Error & { response?: any; body?: any };
164
173
  err.response = response;
165
174
  if (/text/gi.test(acceptType)) {
166
175
  err.body = await response.text();
@@ -170,7 +179,9 @@ function Requester(baseUrl = "", options: RequestOptions = defaultOptions) {
170
179
  try {
171
180
  err.body = await response.json();
172
181
  } catch (error) {
173
- // ignore
182
+ err.body = null;
183
+ // eslint-disable-next-line no-console
184
+ console.warn("Failed to parse JSON response:", error);
174
185
  }
175
186
  }
176
187
 
@@ -183,88 +194,47 @@ function Requester(baseUrl = "", options: RequestOptions = defaultOptions) {
183
194
 
184
195
  if (/text/gi.test(acceptType)) {
185
196
  body = await response.text();
186
- return body;
187
- }
188
-
189
- if (/json/gi.test(acceptType)) {
197
+ } else if (/json/gi.test(acceptType)) {
190
198
  try {
191
199
  body = await response.json();
192
- return body;
193
200
  } catch (error) {
194
- // ignore
201
+ // eslint-disable-next-line no-console
202
+ console.warn("Failed to parse JSON response:", error);
203
+ body = null;
195
204
  }
205
+ } else if (/blob/gi.test(acceptType)) {
206
+ body = await response.blob();
207
+ } else if (/arraybuffer/gi.test(acceptType)) {
208
+ body = await response.arrayBuffer();
209
+ } else {
210
+ body = response;
196
211
  }
197
212
 
198
- return response;
213
+ return body || response;
199
214
  } as unknown as RequestInterface;
200
215
 
201
216
  request.new = (baseUrl: string, options?: RequestOptions) => Requester(baseUrl, { ...opts, ...(options || {}) });
202
217
 
203
218
  request.setOption = (key: string, value: any) => {
204
- let result = opts;
205
-
206
- let parsed = key.split(".");
207
- let next;
208
-
209
- while (parsed.length) {
210
- next = parsed.shift() as string;
211
-
212
- let nextIsArray = next.indexOf("[") > -1;
213
- if (nextIsArray) {
214
- let idx = next.replace(/\D/gi, "");
215
- next = next.split("[")[0];
216
- parsed.unshift(idx);
217
- }
218
-
219
- if (parsed.length > 0 && typeof result[next] !== "object") {
220
- result[next] = nextIsArray ? [] : {};
221
- }
222
-
223
- if (parsed.length === 0 && typeof value !== "undefined") {
224
- result[next] = value;
225
- }
226
-
227
- result = result[next];
228
- }
229
-
230
- return result;
219
+ set(opts, key, value);
220
+ return opts;
231
221
  };
232
222
 
233
223
  request.getOptions = (key?: string) => {
234
- if (!key) {
235
- return opts;
224
+ if (key) {
225
+ return get(opts, key);
236
226
  }
237
227
 
238
- let result = opts;
239
- let parsed = key.split(".");
240
- let next;
241
-
242
- while (parsed.length) {
243
- next = parsed.shift() as string;
244
-
245
- let nextIsArray = next.indexOf("[") > -1;
246
- if (nextIsArray) {
247
- let idx = next.replace(/\D/gi, "");
248
- next = next.split("[")[0];
249
- parsed.unshift(idx);
250
- }
251
-
252
- if (parsed.length > 0 && typeof result[next] !== "object") {
253
- return null;
254
- }
255
-
256
- if (parsed.length === 0) {
257
- return result[next];
258
- }
259
-
260
- result = result[next];
261
- }
228
+ return opts;
262
229
  };
263
230
 
264
- opts.allowedMethods.forEach(
265
- (method) =>
266
- (request[method] = (url: string, data?: Record<string, any>, options?: Record<string, any>) =>
267
- request(method, url, data, options))
231
+ Object.assign(
232
+ request,
233
+ opts.allowedMethods.reduce((acc: Record<string, any>, method) => {
234
+ acc[method] = (url: string, data?: Record<string, any>, options?: Record<string, any>) =>
235
+ request(method, url, data, options);
236
+ return acc;
237
+ }, {})
268
238
  );
269
239
 
270
240
  return request;