Comparing version 2.0.5 to 3.0.0
{ | ||
"name": "gofor", | ||
"version": "2.0.5", | ||
"description": "lean fetch decorator that reverse merges default options", | ||
"version": "3.0.0", | ||
"description": "Lean, isomorphic fetch decorator that reverse merges default options", | ||
"keywords": [ | ||
@@ -26,8 +26,5 @@ "fetch", | ||
}, | ||
"main": "src/index.js", | ||
"browser": "dist/index.js", | ||
"main": "./src/index.js", | ||
"scripts": { | ||
"start": "npm run build -- --watch", | ||
"build": "webpack --config configuration/webpack.config", | ||
"test": "mocha --opts configuration/mocha.opts **/test.js **/*.test.js", | ||
"test": "mocha --opts configuration/mocha.opts './{,!(node_modules)/**}/test.js' './{,!(node_modules)/**}/*.test.js'", | ||
"test:single": "mocha --opts configuration/mocha.opts ", | ||
@@ -41,18 +38,7 @@ "lint": "eslint src lib", | ||
"devDependencies": { | ||
"@babel/core": "^7.1.5", | ||
"@babel/plugin-proposal-class-properties": "^7.1.0", | ||
"@babel/plugin-proposal-json-strings": "^7.0.0", | ||
"@babel/plugin-syntax-dynamic-import": "^7.0.0", | ||
"@babel/plugin-syntax-import-meta": "^7.0.0", | ||
"@babel/plugin-transform-parameters": "^7.1.0", | ||
"@babel/plugin-transform-spread": "^7.0.0", | ||
"@babel/preset-env": "^7.1.5", | ||
"@fiverr/eslint-config-fiverr": "^2.0.0", | ||
"babel-loader": "^8.0.4", | ||
"chai": "^4.2.0", | ||
"eslint": "^5.9.0", | ||
"eslint-plugin-react": "^7.11.1", | ||
"mocha": "^5.2.0", | ||
"webpack": "^4.25.1", | ||
"webpack-cli": "^3.1.2" | ||
"mocha": "^5.2.0" | ||
}, | ||
@@ -59,0 +45,0 @@ "publishConfig": { |
# gofor [](https://www.npmjs.com/package/gofor) [](https://circleci.com/gh/fiverr/gofor) | ||
Each Gofor instance exposes a fetch method: a lean [fetch decorator](https://developer.mozilla.org/en/docs/Web/API/Fetch_API) that *deep reverse merges* default options. | ||
Each Gofor instance exposes a fetch method: a lean, isomorphic [fetch decorator](https://developer.mozilla.org/en/docs/Web/API/Fetch_API) that *deep reverse merges* default options. | ||
Options you pass through in for each request will take precedence, but will supplemented with the defaults. | ||
## Install | ||
- [Installation](#installation) | ||
- [Usage](#usage) | ||
- [Using the `Gofor` class](#using-the-gofor-class) | ||
- ["Out of the box" usability with instances](#out-of-the-box-usability-with-instances) | ||
- [Configuring an instance](#configuring-an-instance) | ||
- [Setting `Headers`](#setting-headers) | ||
- [Delayed configuration](#delayed-configuration) | ||
- [FAQ](#faq) | ||
- [How do I migrate from version 2 to version 3?](#how-do-I-migrate-from-version-2-to-version-3) | ||
## Installation | ||
``` | ||
@@ -12,6 +22,9 @@ npm i gofor | ||
## Using the constructor | ||
## Usage | ||
### Using the `Gofor` class | ||
```js | ||
const Gofor = require('gofor'); | ||
const myGofor = new Gofor({headers: {'X-Custom-Header': 'Custom-Value'}}); | ||
myGofor.fetch('/page') | ||
@@ -22,14 +35,25 @@ .then(...) | ||
### "out of the box" usability with instances | ||
### "Out of the box" usability with instances | ||
You can opt to import the `gofor` property to get a working instance with the default environment `Headers`: | ||
```js | ||
const {gofor} = require('gofor'); | ||
const { gofor } = require('gofor'); | ||
gofor('/page').then(...); // This is the fetch | ||
gofor.config({headers: {'X-Custom-Header': 'Custom-Value'}}); | ||
gofor('/page').then(...); // Now includes default settings | ||
``` | ||
Each call to `Gofor.gofor` generates a new instance: | ||
```js | ||
const gofor1 = require('gofor').gofor; | ||
const gofor2 = require('gofor').gofor; | ||
gofor1 === gofor2 // false | ||
``` | ||
### Configuring an instance | ||
```javascript | ||
const {gofor} = require('gofor'); | ||
```js | ||
const { gofor } = require('gofor'); | ||
const defaultHeaders = new Headers(); | ||
defaultHeaders.append('X-Requested-With', 'XMLHttpRequest'); | ||
@@ -55,3 +79,5 @@ defaultHeaders.append('Content-Type', 'application/json; charset=utf-8'); | ||
### Supports headers as object literals or Headers instances | ||
#### Setting `Headers` | ||
`Gofor` supports setting headers either as an object literal or as a `Headers` instance | ||
**Default header keys will be run over if matched by passed in header keys**. Other keys will be merged. This is made by design. | ||
@@ -61,2 +87,4 @@ | ||
```js | ||
// Configure using the "Headers" constructor | ||
gofor.config({ | ||
@@ -70,2 +98,12 @@ credentials: 'same-origin', | ||
// Or, using an object literal | ||
gofor.config({ | ||
credentials: 'same-origin', | ||
headers: { | ||
'Content-Type': 'application/json; charset=utf-8', | ||
'X-Custom-Header': 'Custom-Value' | ||
} | ||
}); | ||
gofor('/page', { | ||
@@ -83,15 +121,7 @@ headers: new Headers({ | ||
### Each gofor getter creates a new instance | ||
```js | ||
const gofor1 = require('gofor').gofor; | ||
const gofor2 = require('gofor').gofor; | ||
gofor1 === gofor2 // false | ||
``` | ||
### Delayed configuration | ||
#### Delayed configuration | ||
The function will be called once on first use, and its result will be memoised. useful for cases where you need to pull information from the document and don't want to create a race condition. | ||
```js | ||
const {gofor} = require('gofor'); | ||
const { gofor } = require('gofor'); | ||
@@ -109,13 +139,8 @@ gofor.config(() => ({ | ||
## Node Runtime | ||
Gofor brings a pre baked node compatible flavour using [node-fetch](https://www.npmjs.com/package/node-fetch). | ||
```js | ||
const {gofor} = require('gofor/server'); | ||
``` | ||
## FAQ | ||
## Bundled version | ||
Environments which exclude node_modules from the transpiling pipeline should include the "browser" entry instead of "main". This exposes a bundled ES5 commonjs module. | ||
### How do I migrate from version 2 to version 3? | ||
Also available for explicit import: | ||
const {gofor} = require('gofor/dist'); | ||
The two breaking changes in version 3 are: | ||
- Deprecation of the server entry (`gofor/server`) in favor of a truly isomorphic solution. | ||
- Deprecation of the `dist` folder. |
@@ -7,2 +7,3 @@ /** | ||
const fetch = require('node-fetch'); | ||
const iterate = require('../lib/iterate'); | ||
@@ -55,5 +56,2 @@ | ||
* @classProperty {Function} gofor a fresh fetcher instance | ||
* @property {Function} fetcher The function used to perform requests. | ||
* @property {Object} interfaces The request interface constructors. | ||
* @property {Boolean} supportsHeaders Whether the Headers constructor is available or not. | ||
* @property {Object} defaults The default options. | ||
@@ -74,3 +72,3 @@ */ | ||
return this.fetcher(...args); | ||
return fetch(...args); | ||
}; | ||
@@ -85,24 +83,2 @@ | ||
get fetcher() { | ||
return function(...args) { | ||
return fetch(...args); | ||
}; | ||
} | ||
get interfaces() { | ||
return { | ||
Headers, | ||
Request, | ||
Response | ||
}; | ||
} | ||
get supportsHeaders() { | ||
try { | ||
return typeof this.interfaces.Headers.prototype.entries === 'function'; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
get defaults() { | ||
@@ -162,4 +138,4 @@ return this[defaultsKey] = this[defaultsKey] || this[getDefaults](); | ||
toHeaders(headers) { | ||
const { Headers } = this.interfaces; | ||
if (headers && typeof headers === 'object' && this.supportsHeaders && Headers && !(headers instanceof Headers)) { | ||
const { Headers } = fetch; | ||
if (headers && typeof headers === 'object' && Headers && !(headers instanceof Headers)) { | ||
const result = new Headers(); | ||
@@ -184,8 +160,3 @@ | ||
const defaults = this.defaults.headers; | ||
if (!this.supportsHeaders) { | ||
return Object.assign({}, defaults, submitted); | ||
} | ||
const { Headers } = this.interfaces; | ||
const { Headers } = fetch; | ||
const headers = new Headers(); | ||
@@ -192,0 +163,0 @@ const keys = []; |
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
5
140
0
6
12399
9
238