Chained and declarative HTTP client based on the browser Fetch API
Features
- ✔︎ Chained and declarative api methods
- ✔︎ Based on the browser Fetch API
- ✔︎ Form support
- ✔︎ Download support
Browser Support
The latest version of Chrome, Firefox, and Safari.
Install
npm i fly-http
Example
async { const res = await http console}
API Preview
- PATH
- QUERY
- HEADER
- FORM
- HTTP METHODS
- CACHE
- cache(ttl: number)
- clearCache()
- OTHER
- beforeSend(callback: func)
- enrichResponse()
- download(fileName?: string)
- options(options: object)
API Docs
path(path: string | number)
Add path segment to request url including non-encoded path segment.
Examples
http
you can also make one path with the slash:
http
p(path: string | number)
Alias for path().
Examples
http
query(key: string, value: string | number | boolean | array)
Add query parameter to request url.
Examples
http
q(key: string, value: string | number | boolean | array)
Alias for query().
Examples
http
queryAll(params: object)
Accept object as params.
Examples
http
header(name: string, value: string | number | boolean)
Add header to request.
Examples
http ;
You can also define custom header by this method:
auth(value: string)
Convenience method for setting the "Authorization" header, same with:
Examples
content(contentType: string)
Set the content type of the body.
Examples
content'multipart/form-data'
withText()
Convenience method for sending data as plain text, same with:
content'text/plain'
withJson()
Convenience method for sending data as json, same with:
content'application/json'
accept(acceptType: string)
Set the Accept header.
Examples
accept'text/csv'
asText()
Convenience method for getting plain text response, same with:
accept'text/plain'
asXML()
Convenience method for getting XML response, same with:
accept'text/plain'
append(name: string, value: string | Blob, fileName?: string)
Append formData params and set the content-type header to multipart/form-data
automatically.
Examples
http accept'application/vnd.oasis.ubl+json' ;
formData(data: object)
Convert the javascript object to a FormData and set the content-type header to multipart/form-data
automatically.
Examples
const data = pony: true text: 'abc'http
formUrl(input: object | string)
Convert the input to an url encoded string and set the content-type header to application/x-www-form-urlencoded
automatically.
Examples
const input = pony: true tim: isFat: false const alreadyEncodedForm = 'pony=true&tim=%7B%22isFat%22%3Afalse%7D'httphttp
send(method: string, body?: any)
Send request.
supported methods: POST, GET, POST, PUT, DELETE, PATCH, etc.
get()
Send GET request, same with:
post(body?: any)
Send POST request, same with:
put(body?: any)
Send PUT request, same with:
patch(body?: any)
Send PATCH request, same with:
head()
Send HEAD request, same with:
delete()
Send DELETE request, same with:
cache(ttl: number)
Makes this request cache for ttl milliseconds.
clearCache()
Clear the request cache map.
Examples
beforeSend(callback: func)
Callback that gets invoked with string url before sending.
Note: It can only change headers and body of the request.
enrichResponse()
Return a promise for the response (including status code and headers), rather than for just the response data.
Examples
http
download(fileName?: string)
File will be named by following priority:
- High: download(fileName).
- Medium: response headers 'Content-Disposition' filename.
- Low: string after the last slash of path url.
Examples
// file will be downloaded and named 'prop-types.js'http // file will be downloaded and named 'types.js'http // you can also download file by chained path stylehttp
options(options: object)
Set Fetch API options, this options
method will replace other options you defined.
Examples
http optionscredentials: 'include'
Changelog
License
MIT © Kimi Gao