@octetstream/object-to-form-data
Advanced tools
Comparing version 2.0.0 to 3.0.0
{ | ||
"type": "module", | ||
"name": "@octetstream/object-to-form-data", | ||
"version": "2.0.0", | ||
"version": "3.0.0", | ||
"description": "Serialize given object/collection to a FormData.", | ||
"repository": "octet-stream/object-to-form-data", | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/octet-stream/object-to-form-data.git" | ||
}, | ||
"author": "Nick K. <nick.kruchinin@gmail.com>", | ||
"license": "MIT", | ||
"main": "serialize.js", | ||
"types": "serialize.d.ts", | ||
"main": "./lib/object-to-form-data.js", | ||
"types": "./lib/object-to-form-data.ts", | ||
"exports": { | ||
"import": { | ||
"types": "./lib/object-to-form-data.ts", | ||
"default": "./lib/object-to-form-data.js" | ||
}, | ||
"require": { | ||
"types": "./lib/object-to-form-data.cts", | ||
"default": "./lib/object-to-form-data.cjs" | ||
} | ||
}, | ||
"files": [ | ||
"lib" | ||
], | ||
"engines": { | ||
"node": ">= 12" | ||
"node": ">= 18" | ||
}, | ||
"browser": { | ||
"./form-data": "./form-data-browser" | ||
"devDependencies": { | ||
"@changesets/changelog-github": "0.5.0", | ||
"@changesets/cli": "2.27.1", | ||
"@octetstream/eslint-config": "8.0.2", | ||
"@size-limit/preset-small-lib": "11.0.2", | ||
"@types/node": "20.11.17", | ||
"@types/sinon": "17.0.3", | ||
"ava": "6.1.1", | ||
"c8": "9.1.0", | ||
"eslint": "8.56.0", | ||
"formdata-node": "6.0.3", | ||
"husky": "9.0.11", | ||
"lint-staged": "15.2.2", | ||
"sinon": "17.0.1", | ||
"size-limit": "11.0.2", | ||
"tsup": "8.0.2", | ||
"tsx": "4.7.1", | ||
"typescript": "5.3.3" | ||
}, | ||
"scripts": { | ||
"limit": "size-limit", | ||
"staged": "lint-staged", | ||
"eslint": "eslint *.js", | ||
"test": "ava", | ||
"coverage": "c8 npm test", | ||
"ci": "c8 npm test && c8 report --reporter=json", | ||
"_postinstall": "husky install", | ||
"prepublishOnly": "pinst --disable", | ||
"postpublish": "pinst --enable" | ||
}, | ||
"devDependencies": { | ||
"@octetstream/eslint-config": "5.0.0", | ||
"@size-limit/preset-small-lib": "4.10.2", | ||
"ava": "3.15.0", | ||
"c8": "7.7.2", | ||
"eslint": "7.26.0", | ||
"eslint-plugin-ava": "12.0.0", | ||
"formdata-node": "3.3.2", | ||
"husky": "6.0.0", | ||
"lint-staged": "11.0.0", | ||
"pinst": "^2.1.6", | ||
"proxyquire": "2.1.3", | ||
"size-limit": "4.10.2", | ||
"typescript": "4.2.4" | ||
}, | ||
"peerDependencies": { | ||
"formdata-node": ">= 3.x" | ||
}, | ||
"peerDependenciesMeta": { | ||
"formdata-node": { | ||
"optional": true | ||
} | ||
"build": "pnpm run lint:types && tsup", | ||
"eslint": "eslint src/**/*.ts", | ||
"lint:types": "tsc --noEmit", | ||
"test": "NODE_OPTIONS=\"--no-warnings --import tsx\" ava", | ||
"coverage": "c8 pnpm test", | ||
"coverage:report": "c8 pnpm test && c8 report --reporter=html", | ||
"ci": "c8 pnpm test && c8 report --reporter=json", | ||
"release": "pnpm run build && pnpm exec changeset publish", | ||
"preinstall": "npx only-allow pnpm" | ||
} | ||
} | ||
} |
@@ -10,17 +10,19 @@ # object-to-form-data | ||
## API | ||
## Installation | ||
`serialize(object[, options]) -> {FormData}` | ||
pnpm: | ||
* **{object}** object – Object to transform | ||
* **{object | boolean}** options – Serialization options. | ||
This argument might be an object with "root" and "strict" parameters. | ||
Or you can pass one of them as the second argument: | ||
+ **{boolean}** [strict = false] – if set to `true`, all `false` boolean | ||
values will be omitted. | ||
```sh | ||
pnpm add @octetstream/object-to-form-data | ||
``` | ||
npm: | ||
```sh | ||
npm i @octetstream/object-to-form-data | ||
``` | ||
## Usage | ||
```js | ||
import serialize from "@octetstream/object-to-form-data" | ||
import {objectToFormData} from "@octetstream/object-to-form-data" | ||
@@ -40,7 +42,8 @@ const object = { | ||
// You will receive a FormData instance with all fields of given object | ||
const form = objectToFormData(object) | ||
const options = { | ||
method: "post", | ||
// You will receive a FormData instance with all fields of given object | ||
body: serialize(object) | ||
body: form | ||
} | ||
@@ -51,2 +54,38 @@ | ||
**Important!** If you're using this library in Node.js, you also need the [formdata-node](https://github.com/octet-stream/form-data) package to serialize your objects/collections. See documentation of this implementation to learn how to send queries with that implementation. | ||
## API | ||
### `objectToFormData(input[, options]): FormData` | ||
Indicates whether or not to omit every `false` values. Applied enabled. Does not affect boolean array values | ||
This function takes following arguments: | ||
| Name | Type | Required | Default | Description | | ||
|---------|:---------------------------------------------------------------:|:---------:|:-----------:|----------------------------------| | ||
| input | `unknown[] \| Record<sting \| number, unknown>` | true | – | An object to transform | | ||
| options | [`ObjectToFormDataOptions`](#interface-objecttoformdataoptions) | false | `undefined` | Additional serialization options | | ||
### `interface ObjectToFormDataOptions` | ||
Serialization options | ||
| Name | Type | Required | Default | Description | | ||
|----------------|:----------------------------------------:|:--------:|:---------------------:|---------------------------------------------------------------------------------------------------------------| | ||
| strict | `boolean` | false | `false` | Indicates whether or not to omit every `false` values. Applied enabled. Does not affect boolean array values | | ||
| FormData | `FormData` | false | `globalThis.FormData` | Custom spec-compliant [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData) implementation | | ||
| notation | `"dot" \| "bracket"` | false | `"bracket"` | Type of the path notation. Can be either `"dot"` or `"bracket"` | | ||
| normalizeValue | [`NormalizeValue`](#type-normalizevalue) | false | `undefined` | Value normalizer. This function will be called on each *scalar* value, before it's added to FormData instance | | ||
### `type NormalizeValue` | ||
Value normalizer. | ||
This function will be called on each *scalar* value, before it's added to FormData instance. It **must** return either `Blob` or `string` | ||
This function will be called with the following arguments: | ||
| Name | Type | Description | | ||
|---------|:-------------------------:|-------------------------------------| | ||
| value | `unknown` | Current entry value | | ||
| name | `string` | The name of the entry | | ||
| path | `Array<string \| number>` | Entry's path within original object | |
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
Install scripts
Supply chain riskInstall scripts are run when the package is installed. The majority of malware in npm is hidden in install scripts.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
No repository
Supply chain riskPackage does not have a linked source code repository. Without this field, a package will have no reference to the location of the source code use to generate the package.
Found 1 instance in 1 package
13987
0
206
89
0
Yes
17
5
1
1