valyrian.js 7.2.12 → 8.0.1

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 (168) 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 +267 -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 +246 -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 +35 -51
  12. package/dist/hooks/index.js.map +3 -3
  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 +36 -52
  16. package/dist/hooks/index.mjs.map +3 -3
  17. package/dist/index.d.ts +18 -14
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +107 -88
  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 +107 -88
  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 +1 -1
  34. package/dist/node/index.d.ts.map +1 -1
  35. package/dist/node/index.js +125 -10
  36. package/dist/node/index.js.map +4 -4
  37. package/dist/node/index.mjs +125 -10
  38. package/dist/node/index.mjs.map +4 -4
  39. package/dist/node/node.sw.js +152 -0
  40. package/dist/node/utils/icons.d.ts +4 -5
  41. package/dist/node/utils/icons.d.ts.map +1 -1
  42. package/dist/node/utils/inline.d.ts +1 -1
  43. package/dist/node/utils/inline.d.ts.map +1 -1
  44. package/dist/node/utils/node.sw.js +152 -0
  45. package/dist/node/utils/session-storage.d.ts +22 -0
  46. package/dist/node/utils/session-storage.d.ts.map +1 -0
  47. package/dist/node/utils/sw.d.ts.map +1 -1
  48. package/dist/node/utils/tree-adapter.d.ts +5 -1
  49. package/dist/node/utils/tree-adapter.d.ts.map +1 -1
  50. package/dist/pulse-store/index.d.ts +16 -0
  51. package/dist/pulse-store/index.d.ts.map +1 -0
  52. package/dist/pulse-store/index.js +143 -0
  53. package/dist/pulse-store/index.js.map +7 -0
  54. package/dist/pulse-store/index.min.js +1 -0
  55. package/dist/pulse-store/index.min.js.map +1 -0
  56. package/dist/pulse-store/index.mjs +122 -0
  57. package/dist/pulse-store/index.mjs.map +7 -0
  58. package/dist/request/index.d.ts +11 -11
  59. package/dist/request/index.d.ts.map +1 -1
  60. package/dist/request/index.js +63 -84
  61. package/dist/request/index.js.map +2 -2
  62. package/dist/request/index.min.js +1 -0
  63. package/dist/request/index.min.js.map +1 -0
  64. package/dist/request/index.mjs +63 -84
  65. package/dist/request/index.mjs.map +2 -2
  66. package/dist/router/index.d.ts +36 -33
  67. package/dist/router/index.d.ts.map +1 -1
  68. package/dist/router/index.js +247 -96
  69. package/dist/router/index.js.map +3 -3
  70. package/dist/router/index.min.js +1 -0
  71. package/dist/router/index.min.js.map +1 -0
  72. package/dist/router/index.mjs +247 -96
  73. package/dist/router/index.mjs.map +3 -3
  74. package/dist/signals/index.d.ts +6 -0
  75. package/dist/signals/index.d.ts.map +1 -0
  76. package/dist/signals/index.js +92 -0
  77. package/dist/signals/index.js.map +7 -0
  78. package/dist/signals/index.min.js +1 -0
  79. package/dist/signals/index.min.js.map +1 -0
  80. package/dist/signals/index.mjs +71 -0
  81. package/dist/signals/index.mjs.map +7 -0
  82. package/dist/suspense/index.d.ts +6 -0
  83. package/dist/suspense/index.d.ts.map +1 -0
  84. package/dist/suspense/index.js +67 -0
  85. package/dist/suspense/index.js.map +7 -0
  86. package/dist/suspense/index.min.js +1 -0
  87. package/dist/suspense/index.min.js.map +1 -0
  88. package/dist/suspense/index.mjs +46 -0
  89. package/dist/suspense/index.mjs.map +7 -0
  90. package/dist/sw/index.min.js +1 -0
  91. package/dist/sw/index.min.js.map +1 -0
  92. package/dist/translate/index.d.ts +19 -0
  93. package/dist/translate/index.d.ts.map +1 -0
  94. package/dist/translate/index.js +150 -0
  95. package/dist/translate/index.js.map +7 -0
  96. package/dist/translate/index.min.js +1 -0
  97. package/dist/translate/index.min.js.map +1 -0
  98. package/dist/translate/index.mjs +129 -0
  99. package/dist/translate/index.mjs.map +7 -0
  100. package/dist/tsconfig.tsbuildinfo +1 -1
  101. package/dist/utils/deep-freeze.d.ts +3 -0
  102. package/dist/utils/deep-freeze.d.ts.map +1 -0
  103. package/dist/utils/getter-setter.d.ts +3 -0
  104. package/dist/utils/getter-setter.d.ts.map +1 -0
  105. package/dist/utils/has-changed.d.ts +2 -0
  106. package/dist/utils/has-changed.d.ts.map +1 -0
  107. package/dist/utils/index.d.ts +4 -0
  108. package/dist/utils/index.d.ts.map +1 -0
  109. package/dist/utils/index.js +138 -0
  110. package/dist/utils/index.js.map +7 -0
  111. package/dist/utils/index.min.js +1 -0
  112. package/dist/utils/index.min.js.map +1 -0
  113. package/dist/utils/index.mjs +115 -0
  114. package/dist/utils/index.mjs.map +7 -0
  115. package/lib/flux-store/index.ts +312 -0
  116. package/lib/hooks/index.ts +39 -57
  117. package/lib/index.ts +135 -118
  118. package/lib/native-store/index.ts +106 -0
  119. package/lib/node/index.ts +3 -1
  120. package/lib/node/utils/icons.ts +4 -4
  121. package/lib/node/utils/inline.ts +2 -0
  122. package/lib/node/utils/node.sw.js +152 -0
  123. package/lib/node/utils/session-storage.ts +117 -0
  124. package/lib/node/utils/sw.ts +34 -10
  125. package/lib/node/utils/tree-adapter.ts +19 -1
  126. package/lib/pulse-store/index.ts +188 -0
  127. package/lib/request/index.ts +92 -122
  128. package/lib/router/index.ts +353 -164
  129. package/lib/signals/index.ts +98 -0
  130. package/lib/suspense/index.ts +57 -0
  131. package/lib/translate/index.ts +156 -0
  132. package/lib/utils/deep-freeze.ts +54 -0
  133. package/lib/utils/getter-setter.ts +40 -0
  134. package/lib/utils/has-changed.ts +43 -0
  135. package/lib/utils/index.ts +3 -0
  136. package/package.json +40 -57
  137. package/tsconfig.json +5 -4
  138. package/dist/dataset/index.d.ts +0 -24
  139. package/dist/dataset/index.d.ts.map +0 -1
  140. package/dist/dataset/index.js +0 -178
  141. package/dist/dataset/index.js.map +0 -7
  142. package/dist/dataset/index.mjs +0 -157
  143. package/dist/dataset/index.mjs.map +0 -7
  144. package/dist/node/node.sw.tpl +0 -133
  145. package/dist/node/utils/node.sw.tpl +0 -133
  146. package/dist/proxy-signal/index.d.ts +0 -23
  147. package/dist/proxy-signal/index.d.ts.map +0 -1
  148. package/dist/proxy-signal/index.js +0 -138
  149. package/dist/proxy-signal/index.js.map +0 -7
  150. package/dist/proxy-signal/index.mjs +0 -117
  151. package/dist/proxy-signal/index.mjs.map +0 -7
  152. package/dist/signal/index.d.ts +0 -9
  153. package/dist/signal/index.d.ts.map +0 -1
  154. package/dist/signal/index.js +0 -76
  155. package/dist/signal/index.js.map +0 -7
  156. package/dist/signal/index.mjs +0 -55
  157. package/dist/signal/index.mjs.map +0 -7
  158. package/dist/store/index.d.ts +0 -16
  159. package/dist/store/index.d.ts.map +0 -1
  160. package/dist/store/index.js +0 -93
  161. package/dist/store/index.js.map +0 -7
  162. package/dist/store/index.mjs +0 -72
  163. package/dist/store/index.mjs.map +0 -7
  164. package/lib/dataset/index.ts +0 -193
  165. package/lib/node/utils/node.sw.tpl +0 -133
  166. package/lib/proxy-signal/index.ts +0 -187
  167. package/lib/signal/index.ts +0 -86
  168. package/lib/store/index.ts +0 -101
@@ -1,9 +1,10 @@
1
1
  import { isNodeJs } from "valyrian.js";
2
+ import { get, set } from "valyrian.js/utils";
2
3
 
3
4
  interface UrlOptions {
4
- base: string; // Used to prefix the url for scoped requests.
5
- node: string | null; // Used to redirect local requests to node server for server side rendering.
6
- api: string | null; // Used to redirect api requests to node server for server side rendering.
5
+ base?: string; // Used to prefix the url for scoped requests.
6
+ node?: string | null; // Used to redirect local requests to node server for server side rendering.
7
+ api?: string | null; // Used to redirect api requests to node server for server side rendering.
7
8
  }
8
9
 
9
10
  interface RequestOptions {
@@ -26,7 +27,7 @@ interface SendOptions extends RequestOptionsWithUrls, RequestInit {
26
27
 
27
28
  export interface RequestInterface {
28
29
  // eslint-disable-next-line no-unused-vars
29
- (method: string, url: string, data?: Record<string, any>, options?: Partial<SendOptions>): any | Response;
30
+ (method: string, url: string, data?: Record<string, any> | null, options?: Partial<SendOptions>): any | Response;
30
31
  // eslint-disable-next-line no-unused-vars
31
32
  new: (baseUrl: string, options?: RequestOptions) => RequestInterface;
32
33
  // eslint-disable-next-line no-unused-vars
@@ -34,58 +35,64 @@ export interface RequestInterface {
34
35
  // eslint-disable-next-line no-unused-vars
35
36
  getOptions: (key?: string) => RequestOptions | void;
36
37
  // eslint-disable-next-line no-unused-vars
37
- get: (url: string, data?: Record<string, any>, options?: Record<string, any>) => any | Response;
38
+ get: (url: string, data?: Record<string, any> | null, options?: Record<string, any>) => any | Response;
38
39
  // eslint-disable-next-line no-unused-vars
39
- post: (url: string, data?: Record<string, any>, options?: Record<string, any>) => any | Response;
40
+ post: (url: string, data?: Record<string, any> | null, options?: Record<string, any>) => any | Response;
40
41
  // eslint-disable-next-line no-unused-vars
41
- put: (url: string, data?: Record<string, any>, options?: Record<string, any>) => any | Response;
42
+ put: (url: string, data?: Record<string, any> | null, options?: Record<string, any>) => any | Response;
42
43
  // eslint-disable-next-line no-unused-vars
43
- patch: (url: string, data?: Record<string, any>, options?: Record<string, any>) => any | Response;
44
+ patch: (url: string, data?: Record<string, any> | null, options?: Record<string, any>) => any | Response;
44
45
  // eslint-disable-next-line no-unused-vars
45
- delete: (url: string, data?: Record<string, any>, options?: Record<string, any>) => any | Response;
46
+ delete: (url: string, data?: Record<string, any> | null, options?: Record<string, any>) => any | Response;
46
47
  // eslint-disable-next-line no-unused-vars
47
- head: (url: string, data?: Record<string, any>, options?: Record<string, any>) => any | Response;
48
+ head: (url: string, data?: Record<string, any> | null, options?: Record<string, any>) => any | Response;
48
49
  // eslint-disable-next-line no-unused-vars
49
- options: (url: string, data?: Record<string, any>, options?: Record<string, any>) => any | Response;
50
+ options: (url: string, data?: Record<string, any> | null, options?: Record<string, any>) => any | Response;
50
51
  [key: string | number | symbol]: any;
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
- const 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
- const 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"] };
@@ -114,7 +121,13 @@ 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 = {}) {
124
+ // eslint-disable-next-line complexity
125
+ const request = async function request(
126
+ method: string,
127
+ url: string,
128
+ data?: Record<string, any> | null,
129
+ options: Record<string, any> = {}
130
+ ) {
118
131
  const innerOptions: SendOptions = {
119
132
  method: method.toUpperCase(),
120
133
  headers: {},
@@ -123,6 +136,8 @@ 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
  }
@@ -130,37 +145,31 @@ function Requester(baseUrl = "", options: RequestOptions = defaultOptions) {
130
145
  const acceptType = innerOptions.headers.Accept;
131
146
  const contentType = innerOptions.headers["Content-Type"] || innerOptions.headers["content-type"] || "";
132
147
 
133
- if (innerOptions.allowedMethods.indexOf(method) === -1) {
134
- throw new Error("Method not allowed");
148
+ if (!innerOptions.allowedMethods.includes(method)) {
149
+ throw new Error(`Method ${method} not allowed. Allowed methods: ${innerOptions.allowedMethods.join(", ")}`);
150
+ }
151
+
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 (const 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
- const 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
- const 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
- const parsed = key.split(".");
207
- let next;
208
-
209
- while (parsed.length) {
210
- next = parsed.shift() as string;
211
-
212
- const nextIsArray = next.indexOf("[") > -1;
213
- if (nextIsArray) {
214
- const 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
- const parsed = key.split(".");
240
- let next;
241
-
242
- while (parsed.length) {
243
- next = parsed.shift() as string;
244
-
245
- const nextIsArray = next.indexOf("[") > -1;
246
- if (nextIsArray) {
247
- const 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> | null, options?: Record<string, any>) =>
235
+ request(method, url, data, options);
236
+ return acc;
237
+ }, {})
268
238
  );
269
239
 
270
240
  return request;