@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
|
@@ -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
package/dist/index.js
CHANGED
|
@@ -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');
|