Skip to content

Commit

Permalink
Replace form-data with formdata-node
Browse files Browse the repository at this point in the history
  • Loading branch information
exacs committed Feb 11, 2021
1 parent a361dfd commit 3e4d45a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 44 deletions.
58 changes: 20 additions & 38 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
},
"license": "MIT",
"dependencies": {
"form-data": "^2.5.0",
"formdata-node": "^2.4.0",
"got": "^11.8.1",
"query-string": "^6.13.1"
},
Expand Down
11 changes: 6 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const got = require("got");
const queryString = require("query-string");
const FormData = require("form-data");
const FormData = require("formdata-node").default;
const fs = require("fs");
const { augmentGenerator } = require("./utils");

Expand Down Expand Up @@ -114,17 +114,18 @@ module.exports = class CanvasAPI {
}

async sendSis(endpoint, attachment, body = {}) {
const form = new FormData();
const fd = new FormData();

for (const key in body) {
form.append(key, body[key]);
fd.set(key, body[key]);
}

form.append("attachment", fs.createReadStream(attachment));
fd.set("attachment", fs.createReadStream(attachment));

return this.gotClient
.post(endpoint, {
body: form,
body: fd.stream,
headers: fd.headers,
})
.then((response) => {
return response;
Expand Down
20 changes: 20 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,26 @@ test("sendSis returns a parsed JSON object upon success", async (t) => {
t.deepEqual(response.body, { key: "value" });
});

test("sendSis throws an error if timeout is over", async (t) => {
const server = await createTestServer();

server.post("/file", (req, res) => {
setTimeout(() => {
res.send({ key: "value" });
}, 2000);
});

const canvas = new Canvas(server.url, "", { timeout: 1 });
const tmp = tempy.file();
fs.writeFileSync(tmp, "hello world");

try {
await canvas.sendSis("file", tmp);
} catch (err) {
t.pass();
}
});

test("List throws an error if the endpoint response is not an array", async (t) => {
const server = await createTestServer();

Expand Down

0 comments on commit 3e4d45a

Please sign in to comment.