honoka
Just a fetch() API wrapper for both Browser and Node.js.
Features
- Same as fetch() API
- Timeout
- Interceptors before request and response
- Transform/convert request and response
Installing
Using npm:
$ npm install honoka
Using cdn:
Example
Performing a GET
request
// Make a request for a user with a given IDhonoka ; // Optionally the request above could also be done ashonoka ;
Performing a POST
request
honoka ;
honoka API
Requests can be made by passing the relevant config to honoka
.
honoka(options)
// Send a POST request;
honoka(url[, options])
// Send a GET request (default method);
Request method aliases
For convenience aliases have been provided for all supported request methods.
honoka.get(url[, options])
honoka.delete(url[, options])
honoka.head(url[, options])
honoka.options(url[, options])
honoka.post(url[, options])
honoka.put(url[, options])
honoka.patch(url[, options])
Request Config
These are the available config options for making requests. Same as fetch() API.
// `method` is the request method to be used when making the request method: 'get' // default // `headers` are custom headers to be sent headers: 'X-Requested-With': 'XMLHttpRequest' // `data` are the URL parameters or post body to be sent data: ID: 12345 // `baseURL` will be prepended to `url` unless `url` is absolute. baseURL: 'https://some-domain.com/api/' // `timeout` specifies the number of milliseconds before the request times out. // If the request takes longer than `timeout`, the request will be aborted. timeout: 1000 // `dataType` indicates the type of data that the server will respond with // options are 'arraybuffer', 'blob', 'buffer', 'json', 'text', 'auto' dataType: 'auto' // default // Authentication credentials mode // https://developer.mozilla.org/en-US/docs/Web/API/Request/credentials credentials: 'omit' // default // `expectedStatus` defines whether to resolve or reject the promise for a given // HTTP response status code. If `expectedStatus` returns `true` (or is set to `null` // or `undefined`), the promise will be resolved; otherwise, the promise will be // rejected. { return status >= 200 && status < 400; // default } // to ignore interceptors for one request ignoreInterceptors: false
Config Defaults
You can specify config defaults that will be applied to every request.
Global Defaults
honokadefaultsbaseURL = 'https://example.com/api';honokadefaultstimeout = 10e3;honokadefaultsmethod = 'get';honokadefaultsheaderspost'Content-Type' = 'application/json';
Interceptors
You can intercept requests or responses before they are handled by then
.
const unregister = honokainterceptors // Unregister your interceptor;
Abort Operation
You can cancel a pending request manually by using AbortController
.
let abortController = signal = abortControllersignal; ; ;
Promises
honoka depends on a native ES6 Promise implementation to be supported.
If your environment doesn't support ES6 Promises, you can polyfill.
TypeScript
honoka includes TypeScript definitions.
;honoka.get'/user?ID=12345';
Changelog
For changelogs, see Release Notes.
License
MIT