diff --git a/packages/arcgis-rest-request/src/utils/encode-form-data.ts b/packages/arcgis-rest-request/src/utils/encode-form-data.ts index 598a7d4415..cfa5900a9c 100644 --- a/packages/arcgis-rest-request/src/utils/encode-form-data.ts +++ b/packages/arcgis-rest-request/src/utils/encode-form-data.ts @@ -15,9 +15,9 @@ export function encodeFormData(params: any): FormData | string { if (useFormData) { const formData = new FormData(); Object.keys(newParams).forEach((key: any) => { - if (key === "file" && newParams[key].name) { - // Pass on the file's name if provided to override defaults such as "blob" - formData.append(key, newParams[key], newParams[key].name); + if (typeof Blob !== "undefined" && newParams[key] instanceof Blob) { + // Pass on the explicit file name to override default name such as "blob" + formData.append(key, newParams[key], newParams[key].name || key); } else { formData.append(key, newParams[key]); } diff --git a/packages/arcgis-rest-request/test/utils/encode-form-data.test.ts b/packages/arcgis-rest-request/test/utils/encode-form-data.test.ts index 622d10c1f2..7d3c8d609c 100644 --- a/packages/arcgis-rest-request/test/utils/encode-form-data.test.ts +++ b/packages/arcgis-rest-request/test/utils/encode-form-data.test.ts @@ -6,17 +6,35 @@ import { import { attachmentFile } from "../../../arcgis-rest-feature-service/test/attachments.test"; describe("encodeFormData", () => { - it("should encode in form data for multipart requests", () => { + it("should encode in form data for multipart file requests", () => { + const binaryObj = attachmentFile(); + + const formData = encodeFormData({ binary: binaryObj }); + expect(formData instanceof FormData).toBeTruthy(); + + const data = formData as FormData; + if (data.get) { + expect(data.get("binary") instanceof File).toBeTruthy(); + expect((data.get("binary") as File).name).toBe("foo.txt"); + } + }); + + it("should encode in form data for multipart blob requests", () => { const binaryObj = - typeof File !== "undefined" - ? new File(["foo"], "foo.txt", { + typeof Blob !== "undefined" + ? new Blob([], { type: "text/plain" }) - : new Buffer(""); + : Buffer.from(""); const formData = encodeFormData({ binary: binaryObj }); - expect(formData instanceof FormData).toBeTruthy(); + + const data = formData as FormData; + if (data.get) { + expect(data.get("binary") instanceof File).toBeTruthy(); + expect((data.get("binary") as File).name).toBe("binary"); + } }); it("should encode as query string for basic types", () => { diff --git a/packages/arcgis-rest-request/test/utils/process-params.test.ts b/packages/arcgis-rest-request/test/utils/process-params.test.ts index 57ff7b15a8..48aae612c5 100644 --- a/packages/arcgis-rest-request/test/utils/process-params.test.ts +++ b/packages/arcgis-rest-request/test/utils/process-params.test.ts @@ -163,7 +163,7 @@ describe("processParams", () => { ? new File(["foo"], "foo.txt", { type: "text/plain" }) - : new Buffer(""); + : Buffer.from(""); expect( requiresFormData({ @@ -178,7 +178,7 @@ describe("processParams", () => { ? new File(["foo"], "foo.txt", { type: "text/plain" }) - : new Buffer(""); + : Buffer.from(""); expect( requiresFormData({