UNPKG

@brightkyefoo/form-data

Version:

Extension de FormData pour ajouter JSON stringifié si nécessaire

43 lines (38 loc) 1.27 kB
export default class BrightFormData extends FormData { override append(name: string, value: any) { if (typeof value === 'string' || value instanceof Blob) { super.append(name, value) } else { super.append(name, JSON.stringify(value)) } } override get(name: string): FormDataEntryValue | null { const notComputedValue = super.get(name) if (!notComputedValue) return null if (notComputedValue instanceof File) return notComputedValue try { return JSON.parse(notComputedValue) } catch (_) { return notComputedValue } } override set(name: string, value: any) { if (typeof value === 'string' || value instanceof Blob) { super.set(name, value) } else { super.set(name, JSON.stringify(value)) } } /** * This method help appending multiple files as an array to the key name specified * @param name is the name of the key you want to append * @param files is an array of files */ addFiles(name: string, files: File[]) { for (let i = 0; i < files.length; i++) { this.append(`${name}[]`, files[i]); } } }