then-busboy
Advanced tools
Comparing version 2.0.0-beta.7 to 2.0.0-beta.8
@@ -19,2 +19,6 @@ "use strict"; | ||
var _isPlainObject = require("lodash/isPlainObject"); | ||
var _isPlainObject2 = _interopRequireDefault(_isPlainObject); | ||
var _invariant = require("@octetstream/invariant"); | ||
@@ -38,7 +42,7 @@ | ||
* | ||
* @param {object} file | ||
* @param {object} options | ||
* | ||
* @constructor | ||
*/ | ||
constructor(file) { | ||
constructor(options) { | ||
this.read = () => new Promise((resolve, reject) => { | ||
@@ -62,4 +66,6 @@ const data = []; | ||
const { contents, filename, enc, mime } = file; | ||
(0, _invariant2.default)(!(0, _isPlainObject2.default)(options), TypeError, "File options should be a plain object. Received", (0, _getType2.default)(options)); | ||
const { contents, filename, enc, mime } = options; | ||
(0, _invariant2.default)(!contents, "File contents required."); | ||
@@ -69,2 +75,14 @@ | ||
(0, _invariant2.default)(!filename, "Filename required."); | ||
(0, _invariant2.default)(!(0, _util.isString)(filename), TypeError, "Filename should be a string. Received %s", (0, _getType2.default)(filename)); | ||
(0, _invariant2.default)(!enc, "File encoding required."); | ||
(0, _invariant2.default)(!(0, _util.isString)(enc), TypeError, "File encoding should be a string. Received %s", (0, _getType2.default)(enc)); | ||
(0, _invariant2.default)(!mime, "File mime type required."); | ||
(0, _invariant2.default)(!(0, _util.isString)(mime), TypeError, "File mime type should be a string. Received %s", (0, _getType2.default)(mime)); | ||
const ext = (0, _path.extname)(filename); | ||
@@ -71,0 +89,0 @@ const base = (0, _path.basename)(filename, ext); |
{ | ||
"name": "then-busboy", | ||
"version": "2.0.0-beta.7", | ||
"description": "Promise-based wrapper around Busboy, inspired by async-busboy", | ||
"version": "2.0.0-beta.8", | ||
"description": "Promise-based wrapper around Busboy. Process multipart/form-data content and returns it as a single object.", | ||
"repository": "octet-stream/then-busboy", | ||
@@ -66,3 +66,2 @@ "main": "./main.js", | ||
"babel-plugin-transform-object-rest-spread": "6.26.0", | ||
"co": "4.6.0", | ||
"codecov": "2.3.0", | ||
@@ -69,0 +68,0 @@ "eslint": "^4.6.0", |
# then-busboy | ||
Promise-based wrapper around Busboy, inspired by [async-busboy](https://github.com/m4nuC/async-busboy) | ||
Promise-based wrapper around Busboy. Process multipart/form-data content and returns it as a single object. | ||
@@ -10,7 +10,7 @@ [![dependencies Status](https://david-dm.org/octet-stream/then-busboy/status.svg)](https://david-dm.org/octet-stream/then-busboy) | ||
## Installation | ||
Note: The current documentation is for 2.x versions of then-busboy. | ||
If you're looking for a previous version, check out the [1.x branch](https://github.com/octet-stream/then-busboy/tree/1.x). | ||
## Installation | ||
Use can install `then-busboy` from npm: | ||
@@ -94,2 +94,50 @@ | ||
## Fields format | ||
then-busboy can restore an object structure from form-data field names | ||
if you will follow the special naming format with bracket notation: | ||
``` | ||
# Note that the following example is just a pseudo code | ||
rootField[nestedField] = "I beat Twilight Sparkle and all I got was this lousy t-shirt" | ||
``` | ||
then-busboy will return the this object for an example from above: | ||
```js | ||
{ | ||
rootField: { | ||
nestedField: "I beat Twilight Sparkle and all I got was this lousy t-shirt" | ||
} | ||
} | ||
``` | ||
You can also send an arrays and collections using bracket format: | ||
``` | ||
message[sender] = "John Doe" | ||
message[text] = "Some whatever text message." | ||
message[attachments][0][file] = <here is the file content> | ||
message[attachments][0][description] = "Here is a description of the file" | ||
``` | ||
then-busboy returns the following object: | ||
```js | ||
{ | ||
message: { | ||
sender: "John Doe", | ||
text: "Some whatever text message.", | ||
attachments: [ | ||
{ | ||
file: File, // this field will be represended as a File instance | ||
description: "Here is a description of the file" | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
**Note that there is no an implementation for array as *root field* for now!** | ||
## Usage | ||
@@ -96,0 +144,0 @@ |
@@ -8,2 +8,3 @@ import {createWriteStream} from "fs" | ||
import isPlainObject from "lodash/isPlainObject" | ||
import invariant from "@octetstream/invariant" | ||
@@ -18,9 +19,14 @@ import nanoid from "nanoid" | ||
* | ||
* @param {object} file | ||
* @param {object} options | ||
* | ||
* @constructor | ||
*/ | ||
constructor(file) { | ||
const {contents, filename, enc, mime} = file | ||
constructor(options) { | ||
invariant( | ||
!isPlainObject(options), TypeError, | ||
"File options should be a plain object. Received", getType(options) | ||
) | ||
const {contents, filename, enc, mime} = options | ||
invariant(!contents, "File contents required.") | ||
@@ -33,2 +39,23 @@ | ||
invariant(!filename, "Filename required.") | ||
invariant( | ||
!isString(filename), TypeError, | ||
"Filename should be a string. Received %s", getType(filename) | ||
) | ||
invariant(!enc, "File encoding required.") | ||
invariant( | ||
!isString(enc), TypeError, | ||
"File encoding should be a string. Received %s", getType(enc) | ||
) | ||
invariant(!mime, "File mime type required.") | ||
invariant( | ||
!isString(mime), TypeError, | ||
"File mime type should be a string. Received %s", getType(mime) | ||
) | ||
const ext = extname(filename) | ||
@@ -35,0 +62,0 @@ const base = basename(filename, ext) |
@@ -219,1 +219,74 @@ import Stream, {Writable} from "stream" | ||
}) | ||
test("Should throw an error when no filename given", t => { | ||
t.plan(1) | ||
const contents = createReadStream(__filename) | ||
const trap = () => new File({contents}) | ||
t.throws(trap, "Filename required.") | ||
}) | ||
test("Should throw an error when filename is not a string", t => { | ||
t.plan(1) | ||
const contents = createReadStream(__filename) | ||
const trap = () => new File({contents, filename: 42}) | ||
t.throws(trap, "Filename should be a string. Received number") | ||
}) | ||
test("Should throw an error when no enc given", t => { | ||
t.plan(1) | ||
const contents = createReadStream(__filename) | ||
const trap = () => new File({contents, filename: basename(__filename)}) | ||
t.throws(trap, "File encoding required.") | ||
}) | ||
test("Should throw an error when enc is not a string", t => { | ||
t.plan(1) | ||
const contents = createReadStream(__filename) | ||
const trap = () => new File({ | ||
contents, | ||
filename: basename(__filename), | ||
enc: [] | ||
}) | ||
t.throws(trap, "File encoding should be a string. Received array") | ||
}) | ||
test("Should throw an error when no mime given", t => { | ||
t.plan(1) | ||
const contents = createReadStream(__filename) | ||
const trap = () => new File({ | ||
contents, | ||
filename: basename(__filename), | ||
enc: "utf-8" | ||
}) | ||
t.throws(trap, "File mime type required.") | ||
}) | ||
test("Should throw an error when mime is not a string", t => { | ||
t.plan(1) | ||
const contents = createReadStream(__filename) | ||
const trap = () => new File({ | ||
contents, | ||
filename: basename(__filename), | ||
enc: "utf-8", | ||
mime: /.*/ | ||
}) | ||
t.throws(trap, "File mime type should be a string. Received RegExp") | ||
}) |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
55778
19
1600
181