@hashrytech/quick-components-kit 0.15.1 → 0.15.3

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/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @hashrytech/quick-components-kit
2
2
 
3
+ ## 0.15.3
4
+
5
+ ### Patch Changes
6
+
7
+ - update: Adding objectToFormData function
8
+
9
+ ## 0.15.2
10
+
11
+ ### Patch Changes
12
+
13
+ - chore: more log updates
14
+
3
15
  ## 0.15.1
4
16
 
5
17
  ### Patch Changes
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Converts a JavaScript object to a FormData object.
3
+ * This is useful for sending data in a format suitable for form submissions, especially when dealing with file uploads.
4
+ *
5
+ * @param {Record<string, any>} obj - The object to convert to FormData.
6
+ * @returns {FormData} - The resulting FormData object.
7
+ */
8
+ export declare function objectToFormData(obj: Record<string, unknown>): FormData;
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Converts a JavaScript object to a FormData object.
3
+ * This is useful for sending data in a format suitable for form submissions, especially when dealing with file uploads.
4
+ *
5
+ * @param {Record<string, any>} obj - The object to convert to FormData.
6
+ * @returns {FormData} - The resulting FormData object.
7
+ */
8
+ export function objectToFormData(obj) {
9
+ const formData = new FormData();
10
+ const replacer = (key, value) => {
11
+ if (key === 'object')
12
+ return undefined;
13
+ return value;
14
+ };
15
+ for (const key in obj) {
16
+ if (!Object.prototype.hasOwnProperty.call(obj, key))
17
+ continue;
18
+ const value = obj[key];
19
+ if (value === undefined)
20
+ continue;
21
+ if (value instanceof Blob || value instanceof File) {
22
+ formData.append(key, value);
23
+ }
24
+ else if (typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean') {
25
+ formData.append(key, value.toString());
26
+ }
27
+ else if (Array.isArray(value)) {
28
+ value.forEach((item, index) => {
29
+ if (item === undefined)
30
+ return;
31
+ if (typeof item === 'string' || typeof item === 'number' || typeof item === 'boolean') {
32
+ formData.append(`${key}[${index}]`, item.toString());
33
+ }
34
+ else if (typeof item === 'object' && item !== null) {
35
+ formData.append(`${key}[${index}]`, JSON.stringify(item, replacer));
36
+ }
37
+ else {
38
+ console.warn(`Skipped unsupported array item at ${key}[${index}]:`, item);
39
+ }
40
+ });
41
+ }
42
+ else if (typeof value === 'object' && value !== null) {
43
+ formData.append(key, JSON.stringify(value, replacer));
44
+ }
45
+ else {
46
+ console.warn(`Skipped unsupported value for key "${key}":`, value);
47
+ }
48
+ }
49
+ return formData;
50
+ }
package/dist/index.d.ts CHANGED
@@ -16,3 +16,4 @@ export * from './modules/fetch-client.js';
16
16
  export * from './modules/api-proxy.js';
17
17
  export * from './modules/crypto.js';
18
18
  export * from './modules/problem-details.js';
19
+ export * from './functions/object-to-form-data.js';
package/dist/index.js CHANGED
@@ -18,4 +18,5 @@ export * from './modules/fetch-client.js';
18
18
  export * from './modules/api-proxy.js';
19
19
  export * from './modules/crypto.js';
20
20
  export * from './modules/problem-details.js';
21
+ export * from './functions/object-to-form-data.js';
21
22
  // Add more components here...
@@ -119,6 +119,8 @@ export class FetchClient {
119
119
  if (body instanceof FormData) {
120
120
  requestHeaders.delete('Content-Type'); // Important: Let browser set Content-Type for FormData
121
121
  processedBody = body;
122
+ if (this.debug)
123
+ console.debug(`Fetch Client: Removed Content-Type header since FormData is being used for body in request to ${url}`);
122
124
  }
123
125
  else if (body !== undefined && body !== null) {
124
126
  const contentType = requestHeaders.get('Content-Type');
package/package.json CHANGED
@@ -5,7 +5,7 @@
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/hashrytech/quick-components-kit.git"
7
7
  },
8
- "version": "0.15.1",
8
+ "version": "0.15.3",
9
9
  "license": "MIT",
10
10
  "author": "Hashry Tech",
11
11
  "files": [