https://github.com/mouvedia/cb-fetch
š„ Cross-Browser HTTP client
https://github.com/mouvedia/cb-fetch
ajax cross-browser fetch fluent-interface forward-compatible umd xdr xhr
Last synced: 2 months ago
JSON representation
š„ Cross-Browser HTTP client
- Host: GitHub
- URL: https://github.com/mouvedia/cb-fetch
- Owner: Mouvedia
- License: mit
- Created: 2015-11-24T03:31:35.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2020-01-20T13:51:56.000Z (over 5 years ago)
- Last Synced: 2025-04-10T17:24:41.027Z (2 months ago)
- Topics: ajax, cross-browser, fetch, fluent-interface, forward-compatible, umd, xdr, xhr
- Language: JavaScript
- Homepage: https://npm.im/cb-fetch
- Size: 197 KB
- Stars: 13
- Watchers: 1
- Forks: 1
- Open Issues: 15
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: FUNDING.yml
- License: LICENSE.md
Awesome Lists containing this project
README




![]()
![]()
# cb-fetch
A truly **c**ross-**b**rowser and forward-compatible library to do asynchronous HTTP requests that follows the **c**all**b**ack pattern.
## Table of Contents
- [Usage](#usage)
- [Installation](#installation)
- [Importation](#importation)
- [Examples](#examples)
- [Support](#support)
- [Methods](#methods)
- [Properties](#properties)
- [Gotchas](#gotchas)
- [License](#license)## Usage
### Installation
#### npm
~~~sh
npm install --save cb-fetch
~~~#### yarn
~~~sh
yarn add cb-fetch
~~~#### jspm
~~~sh
jspm install cb-fetch
~~~#### bower
~~~sh
bower install --save cb-fetch
~~~#### jsDelivr
~~~xml
~~~
### Importation
AMD
define(function (require) {
var request = require('cb-fetch');
});
CJS
standard compliant
var request = require('cb-fetch')['default'];
Node.js compatible
var request = require('cb-fetch');
YUI
YUI({
modules: { 'is-string': 'path/to/@string/isstring.js' }
}).use('is-string', 'cb-fetch', function (Y) {
var request = Y['default'];
});
TS
import request = require('cb-fetch');
Global Namespace
If none of the previously listed module registration methods are supported, a global variable named
request
will be exposed.### Examples
~~~js
// here's your typical request
request('http://www.example.com?key1=value1&key2=value2')
.done(response => { /* ⦠*/ });// taking a comprehensive approach is encouraged though
request()
.get('http://www.example.com')
.query('key1=value1&key2=value2')
.done(onSuccessCallback, onErrorCallback);// passing an object offers options not available otherwise
let abort = request({
url: new URL('http://www.example.com'),
parameters: new URLSearchParams('_csrf=TOKEN'),
mode: 'cors',
credentials: 'include',
responseType: 'json'
}).get('/segment')
.query({ foo: ['bar', 'qux'] })
.pass('Content-Type', 'application/json')
.hook('download', e => { /* ⦠*/ })
.done({
success: onSuccessCallback,
error: onErrorCallback,
abort: onAbortCallback
});// forcefully aborts the request
abort();
~~~## Support
- [x] `fetch`
- [x] `XMLHttpRequest`
- [x] `XDomainRequest`
- [x] Universal Module Definition
- [x] WebDAV
- [x] TypeScript
- [x] Flow## Methods
### get / head / delete / post / patch / put
~~~
(Options.url?) => Object
~~~### query
~~~
(Options.parameters?) => Object
~~~### send
~~~
(Options.body?) => Object
~~~### hook
#### loadstart
~~~
('loadstart', () => Boolean | Void) => Object
~~~download
![]()
~~~
('download', Object => Any) => Object
~~~#### loadend
~~~
('loadend', () => Any) => Object
~~~### pass
~~~
{
(name: String, value: String),
(headers: Object | Headers)
} => Object
~~~~~~js
// assigns
.pass(new Headers({ key: 'value' }))// appends
.pass({ key: 'value' })// sets
.pass('key', 'value')
~~~### done
~~~
{
(onSuccess?: Function, onError?: Function),
({
success?: Function,
error?: Function,
timeout?: Function,
abort?: Function
})
} => () => Void,
throws: TypeError
~~~## Properties
### Request Options
Property | Default | Value(s)
-------- | ------- | --------
body | null | BufferSource, Blob, Document², FormData, String, URLSearchParams, ReadableStream
credentials | 'sameāorigin' | 'include', 'omit'ā¶, 'same-origin'
headers | {} | Object, Headers³
method | 'GET' | String
mode | 'sameāorigin' | 'cors', 'no-cors'¹, 'same-origin'
password | null | String
parameters | | URLSearchParams, Object, String
responseMediaType² | | String
responseType | | 'text', 'json', 'blob', 'document', 'arraybuffer', 'formdata'¹, 'moz-blob', 'moz-chunked-arraybuffer', 'moz-chunked-text', 'msxml-document'
timeout | 0 | ā
username | null | String
url | location.href | String, URL
multipartā· | false | Boolean
tunnelingāµ | false | Boolean
XSLPatternā“ | false | Boolean### Progress Event
Property | Type
-------- | ----
chunk | String, ArrayBuffer, Blob, Uint8Array, null
aggregate | String, ArrayBuffer, Blob, Uint8Array, null
loaded | ā
total | ā
lengthComputable | Boolean### Response
Property | Type
-------- | ----
body | Object, String, Document, ArrayBuffer, Blob, FormData¹, ReadableStream¹, null
headers | Object
instance | XMLHttpRequest, XDomainRequest, Response, AnonXMLHttpRequest
statusCode | ā
statusText | String
url | String¹ fetch only
² XHR only
³ except Gecko 34ā43
ā“ MSXML 3.0 only
āµ method override
ā¶ fetch, Gecko 16+, Presto/2.10.232ā2.12.423
ā· Gecko 1.7βā22## Gotchas
### `delete` reserved keyword
In pre-ES5 environments, the delete method requires the use of the bracket notation.
### Gecko
For the browsers powered by Gecko 1.9.1ā20 to have the exposed response headers
populated into the `headers` property, the following conditions must be met:- `Access-Control-Expose-Headers` response header exposes itself
- `Access-Control-Expose-Headers` field value is not `*`
- `mode` set to `cors`### Trident
#### `XDomainRequest` intrinsic limitations
- only support GET and POST methods
- cannot set request headers
- no credentials
- same scheme restriction
- the informational and redirection status code classes are considered errors
- the response's status code and status text are not supplied
- same-origin requests _also_ require the server to respond with an `Access-Control-Allow-Origin` header of either `*`
or the exact URL of the requesting document#### Platform for Privacy Preferences
Internet Explorerās default settings restrict the use of 3rd party cookies unless a P3P compact policy
declaration has been included through a custom HTTP response header; hence, the `"include"` credentials mode cannot be
fully honored if a cookie has been deemed unsatisfactory.## License
[](https://app.fossa.io/projects/git%2Bgithub.com%2FMouvedia%2Fcb-fetch?ref=badge_large)