form-data-encoder
Advanced tools
Comparing version 1.0.0 to 1.0.1
@@ -20,2 +20,3 @@ "use strict"; | ||
const createBoundary_1 = __importDefault(require("./util/createBoundary")); | ||
const escapeName_1 = __importDefault(require("./util/escapeName")); | ||
const isFormData_1 = __importDefault(require("./util/isFormData")); | ||
@@ -142,5 +143,5 @@ const isFile_1 = __importDefault(require("./util/isFile")); | ||
header += `${__classPrivateFieldGet(this, _Encoder_DASHES, "f")}${this.boundary}${__classPrivateFieldGet(this, _Encoder_CRLF, "f")}`; | ||
header += `Content-Disposition: form-data; name="${name}"`; | ||
header += `Content-Disposition: form-data; name="${escapeName_1.default(name)}"`; | ||
if (isFile_1.default(value)) { | ||
header += `; filename="${value.name}"${__classPrivateFieldGet(this, _Encoder_CRLF, "f")}`; | ||
header += `; filename="${escapeName_1.default(value.name)}"${__classPrivateFieldGet(this, _Encoder_CRLF, "f")}`; | ||
header += `Content-Type: ${value.type || "application/octet-stream"}`; | ||
@@ -147,0 +148,0 @@ } |
@@ -14,2 +14,3 @@ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) { | ||
import createBoundary from "./util/createBoundary.js"; | ||
import escape from "./util/escapeName.js"; | ||
import isFormData from "./util/isFormData.js"; | ||
@@ -136,5 +137,5 @@ import isFile from "./util/isFile.js"; | ||
header += `${__classPrivateFieldGet(this, _Encoder_DASHES, "f")}${this.boundary}${__classPrivateFieldGet(this, _Encoder_CRLF, "f")}`; | ||
header += `Content-Disposition: form-data; name="${name}"`; | ||
header += `Content-Disposition: form-data; name="${escape(name)}"`; | ||
if (isFile(value)) { | ||
header += `; filename="${value.name}"${__classPrivateFieldGet(this, _Encoder_CRLF, "f")}`; | ||
header += `; filename="${escape(value.name)}"${__classPrivateFieldGet(this, _Encoder_CRLF, "f")}`; | ||
header += `Content-Type: ${value.type || "application/octet-stream"}`; | ||
@@ -141,0 +142,0 @@ } |
{ | ||
"name": "form-data-encoder", | ||
"version": "1.0.0", | ||
"version": "1.0.1", | ||
"description": "Encode FormData content into the multipart/form-data format", | ||
@@ -14,4 +14,3 @@ "repository": "octet-stream/form-data-encoder", | ||
"spec-compatible", | ||
"form", | ||
"node" | ||
"form" | ||
], | ||
@@ -45,9 +44,9 @@ "main": "./lib/cjs/index.js", | ||
"@types/mime-types": "2.1.0", | ||
"@types/node": "15.12.2", | ||
"@typescript-eslint/eslint-plugin": "4.26.1", | ||
"@types/node": "15.12.4", | ||
"@typescript-eslint/eslint-plugin": "4.27.0", | ||
"@zoltu/typescript-transformer-append-js-extension": "1.0.1", | ||
"ava": "3.15.0", | ||
"c8": "7.7.2", | ||
"c8": "7.7.3", | ||
"dotenv": "10.0.0", | ||
"eslint": "7.28.0", | ||
"eslint": "7.29.0", | ||
"eslint-config-airbnb-typescript": "12.3.1", | ||
@@ -57,3 +56,3 @@ "eslint-plugin-ava": "12.0.0", | ||
"eslint-plugin-react": "7.24.0", | ||
"formdata-node": "3.5.3", | ||
"formdata-node": "3.5.4", | ||
"husky": "6.0.0", | ||
@@ -64,3 +63,3 @@ "lint-staged": "11.0.0", | ||
"ttypescript": "1.5.12", | ||
"typescript": "4.3.2" | ||
"typescript": "4.3.4" | ||
}, | ||
@@ -67,0 +66,0 @@ "dependencies": { |
@@ -104,5 +104,59 @@ # form-data-encoder | ||
4. In this example we will pull FormData content into the ReadableStream: | ||
4. Another way to convert FormData parts to blob using `form-data-encoder` is making a Blob-ish class: | ||
```js | ||
import {Readable} from "stream" | ||
import {FormData} from "formdata-polyfill/esm-min.js" | ||
import {blobFrom} from "fetch-blob/from.js" | ||
import {Encoder} from "form-data-encoder" | ||
import Blob from "fetch-blob" | ||
import fetch from "node-fetch" | ||
class BlobDataItem { | ||
constructor(encoder) { | ||
this.#encoder = encoder | ||
this.#size = encoder.headers["Content-Length"] | ||
this.#type = encoder.headers["Content-Type"] | ||
} | ||
get type() { | ||
return this.#type | ||
} | ||
get size() { | ||
return this.#size | ||
} | ||
stream() { | ||
return Readable.from(this.#encoder) | ||
} | ||
get [Symbol.toStringTag]() { | ||
return "Blob" | ||
} | ||
} | ||
const fd = new FormData() | ||
fd.set("name", "John Doe") | ||
fd.set("avatar", await blobFrom("path/to/an/avatar.png"), "avatar.png") | ||
const encoder = new Encoder(fd) | ||
// Note that node-fetch@2 performs more strictness tests for Blob objects, so you may need to do extra steps before you set up request body (like, maybe you'll need to instaniate a Blob with BlobDataItem as one of its blobPart) | ||
const blob = new BlobDataItem(enocoder) // or new Blob([new BlobDataItem(enocoder)], {type: encoder.contentType}) | ||
const options = { | ||
method: "post", | ||
body: blob | ||
} | ||
await fetch("https://httpbin.org/post", options) | ||
``` | ||
5. In this example we will pull FormData content into the ReadableStream: | ||
```js | ||
// This module is only necessary when you targeting Node.js or need web streams that implement Symbol.asyncIterator | ||
@@ -144,3 +198,3 @@ import {ReadableStream} from "web-streams-polyfill/ponyfill/es2018" | ||
5. Speaking of async iterables - if HTTP client supports them, you can use encoder like this: | ||
6. Speaking of async iterables - if HTTP client supports them, you can use encoder like this: | ||
@@ -168,3 +222,3 @@ ```js | ||
6. ...And for those client whose supporting form-data-encoder out of the box, the usage will be much, much more simpler: | ||
7. ...And for those client whose supporting form-data-encoder out of the box, the usage will be much, much more simpler: | ||
@@ -171,0 +225,0 @@ ```js |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
35772
32
575
306