http-client
Advanced tools
Comparing version 4.0.0-0 to 4.0.0
@@ -1,15 +0,17 @@ | ||
## [HEAD] | ||
## [4.0.0-0] | ||
> Apr 27, 2016 | ||
- `handleResponse` runs response handlers top to bottom, just like Express | ||
- Added `options.responseHandlers` array to specify an array of transforms to | ||
run on the response after it is received | ||
- Added `enhanceFetch` to top-level exports for adding `options.responseHandlers` | ||
support to an arbitrary `fetch` function | ||
- Added `options.fetch` to fetch functions created using `createFetch` so users | ||
can swap out usage of the "global" fetch function, which makes testing easier | ||
- Added `enhanceFetch` to top-level exports for adding `options.responseHandlers` | ||
support to an arbitrary `fetch` function | ||
- Added `options.responseHandlers` array to specify an array of transforms to run | ||
on the response after it is received. `handleResponse` uses this to run handlers | ||
from top to bottom, just like Express | ||
- Removed callback API to preserve `fetch` method signature | ||
[HEAD]: https://github.com/mjackson/http-client/compare/v3.1.0...HEAD | ||
[4.0.0-0]: https://github.com/mjackson/http-client/compare/v3.1.0...v4.0.0-0 | ||
## [3.1.0] | ||
> Mar 28, 2016 | ||
@@ -21,2 +23,3 @@ - Added the `init` middleware | ||
## [3.0.0] | ||
> Mar 22, 2016 | ||
@@ -28,2 +31,3 @@ - Require consumers to provide their own global `fetch` function | ||
## [2.4.2] | ||
> Mar 22, 2016 | ||
@@ -30,0 +34,0 @@ - Fix bundling with Browserify |
{ | ||
"name": "http-client", | ||
"version": "4.0.0-0", | ||
"version": "4.0.0", | ||
"description": "Compose HTTP clients using JavaScript's fetch API", | ||
@@ -5,0 +5,0 @@ "author": "Michael Jackson", |
108
README.md
@@ -64,9 +64,5 @@ # http-client [![Travis][build-badge]][build] [![npm package][npm-badge]][npm] | ||
#### `fetch([input], [options])` | ||
An enhanced `fetch` function. Use this directly if you don't need any middleware. | ||
#### `createFetch(...middleware)` | ||
Creates a `fetch` function that is fronted by some middleware. This function has the same signature as the enhanced `fetch` function in the top-level API. | ||
Creates an [enhanced](#enhancefetchfetch) `fetch` function that is fronted by some middleware. | ||
@@ -77,5 +73,13 @@ #### `createStack(...middleware)` | ||
#### `enhanceFetch(fetch)` | ||
Returns an "enhanced" version of the given `fetch` function that uses an array of transforms in `options.responseHandlers` to modify the response after it is received. | ||
#### `fetch([input], [options])` | ||
An [enhanced](#enhancefetchfetch) `fetch` function. Use this directly if you don't need any middleware. | ||
#### `handleResponse(handler)` | ||
A helper for creating middleware that enhances the `response` object in some way. The `handler` function should return the new response value, or a promise for it. This function is used internally to create the `parseText` and `parseJSON` middleware. | ||
A helper for creating middleware that enhances the `response` object in some way. The `handler` function should return the new response value, or a promise for it. Response handlers run in the order they are defined. | ||
@@ -86,96 +90,92 @@ ## Middleware | ||
#### `init(propertyName, value)` | ||
#### `accept(contentType)` | ||
Sets the value of an arbitrary property in the options object. | ||
Adds an `Accept` header to the request. | ||
```js | ||
import { createFetch, init } from 'http-client' | ||
import { createFetch, accept } from 'http-client' | ||
const fetch = createFetch( | ||
init('credentials', 'include') | ||
accept('application/json') | ||
) | ||
``` | ||
#### `method(verb)` | ||
#### `auth(value)` | ||
Sets the request method. | ||
Adds an `Authorization` header to the request. | ||
```js | ||
import { createFetch, method } from 'http-client' | ||
import { createFetch, auth } from 'http-client' | ||
const fetch = createFetch( | ||
method('POST') | ||
auth('Bearer ' + oauth2Token) | ||
) | ||
``` | ||
#### `header(name, value)` | ||
#### `base(baseURL)` | ||
Adds a header to the request. | ||
Adds the given `baseURL` to the beginning of the request URL. | ||
```js | ||
import { createFetch, header } from 'http-client' | ||
import { createFetch, base } from 'http-client' | ||
const fetch = createFetch( | ||
header('Content-Type', 'application/json') | ||
base('https://api.stripe.com/v1') | ||
) | ||
fetch('/customers/5') // GET https://api.stripe.com/v1/customers/5 | ||
``` | ||
#### `auth(value)` | ||
#### `body(content, contentType)` | ||
Adds an `Authorization` header to the request. | ||
Sets the given `content` string as the request body. | ||
```js | ||
import { createFetch, auth } from 'http-client' | ||
import { createFetch, body } from 'http-client' | ||
const fetch = createFetch( | ||
auth('Bearer ' + oauth2Token) | ||
body(JSON.stringify(data), 'application/json') | ||
) | ||
``` | ||
#### `accept(contentType)` | ||
#### `header(name, value)` | ||
Adds an `Accept` header to the request. | ||
Adds a header to the request. | ||
```js | ||
import { createFetch, accept } from 'http-client' | ||
import { createFetch, header } from 'http-client' | ||
const fetch = createFetch( | ||
accept('application/json') | ||
header('Content-Type', 'application/json') | ||
) | ||
``` | ||
#### `base(baseURL)` | ||
#### `init(propertyName, value)` | ||
Adds the given `baseURL` to the beginning of the request URL. | ||
Sets the value of an arbitrary property in the options object. | ||
```js | ||
import { createFetch, base } from 'http-client' | ||
import { createFetch, init } from 'http-client' | ||
const fetch = createFetch( | ||
base('https://api.stripe.com/v1') | ||
init('credentials', 'include') | ||
) | ||
fetch('/customers/5') // GET https://api.stripe.com/v1/customers/5 | ||
``` | ||
#### `query(object)` | ||
#### `json(object)` | ||
Adds the data in the given object (or string) to the query string of the request URL. | ||
Adds the data in the given object as JSON to the request body. | ||
#### `body(content, contentType)` | ||
#### `method(verb)` | ||
Sets the given `content` string as the request body. | ||
Sets the request method. | ||
```js | ||
import { createFetch, body } from 'http-client' | ||
import { createFetch, method } from 'http-client' | ||
const fetch = createFetch( | ||
body(JSON.stringify(data), 'application/json') | ||
method('POST') | ||
) | ||
``` | ||
#### `json(object)` | ||
Adds the data in the given object as JSON to the request body. | ||
#### `params(object)` | ||
@@ -200,34 +200,38 @@ | ||
#### `parseText(propertyName = 'textString')` | ||
#### `parseJSON(propertyName = 'jsonData')` | ||
Reads the response body as text and puts it on `response.textString`. | ||
Reads the response body as JSON and puts it on `response.jsonData`. | ||
```js | ||
import { createFetch, parseText } from 'http-client' | ||
import { createFetch, parseJSON } from 'http-client' | ||
const fetch = createFetch( | ||
parseText() | ||
parseJSON() | ||
) | ||
fetch(input).then(response => { | ||
console.log(response.textString) | ||
console.log(response.jsonData) | ||
}) | ||
``` | ||
#### `parseJSON(propertyName = 'jsonData')` | ||
#### `parseText(propertyName = 'textString')` | ||
Reads the response body as JSON and puts it on `response.jsonData`. | ||
Reads the response body as text and puts it on `response.textString`. | ||
```js | ||
import { createFetch, parseJSON } from 'http-client' | ||
import { createFetch, parseText } from 'http-client' | ||
const fetch = createFetch( | ||
parseJSON() | ||
parseText() | ||
) | ||
fetch(input).then(response => { | ||
console.log(response.jsonData) | ||
console.log(response.textString) | ||
}) | ||
``` | ||
#### `query(object)` | ||
Adds the data in the given object (or string) to the query string of the request URL. | ||
#### `requestInfo()` | ||
@@ -234,0 +238,0 @@ |
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
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
34164
1
275