Skip to content

Commit

Permalink
fix(encodeFormData): append file name based on object type instead of…
Browse files Browse the repository at this point in the history
… key and name properties

Append a filename to parameters of type File that don't have the key "file". Also append a filename
to blobs (which don't have a "name" property).

AFFECTS PACKAGES:
@esri/arcgis-rest-request
  • Loading branch information
Noah Mulfinger authored and noahmulfinger committed Aug 10, 2018
1 parent 7ab02af commit 401c6dd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
6 changes: 3 additions & 3 deletions packages/arcgis-rest-request/src/utils/encode-form-data.ts
Expand Up @@ -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]);
}
Expand Down
28 changes: 23 additions & 5 deletions packages/arcgis-rest-request/test/utils/encode-form-data.test.ts
Expand Up @@ -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", () => {
Expand Down
Expand Up @@ -163,7 +163,7 @@ describe("processParams", () => {
? new File(["foo"], "foo.txt", {
type: "text/plain"
})
: new Buffer("");
: Buffer.from("");

expect(
requiresFormData({
Expand All @@ -178,7 +178,7 @@ describe("processParams", () => {
? new File(["foo"], "foo.txt", {
type: "text/plain"
})
: new Buffer("");
: Buffer.from("");

expect(
requiresFormData({
Expand Down

0 comments on commit 401c6dd

Please sign in to comment.