A flexible promise based HTTP client for Node.js and browser.
Features
- Cloneable and combinable request config
- Support Node.js and browser
- Promise/A+ based
- Chainable API
- Cancelable
- Support timeout
Installing
Using npm:
$ npm install http-ask
Using yarn:
$ yarn add http-ask
Usage
Basic GET
request
// Fetch a user with query (eg: http://localhost/api/users?page=32)Ask ; // Optionally, you can use an `ask` instanceconst ask = 'http://localhost/api/users';ask ;
Combinable url
// Fetch a user by id. (eg: http://localhost/api/users/2333)const id = 2333; Ask ; // Above could also be done asAsk ;
Combinable query
// Fetch users with token and other query. (eg: http://localhost/api/users?token=asdf&page=23&count=10)const token = 'asdf'; Ask ; // Above could also be done asAsk ;
Clone ask
instance
const apiHost = 'http://localhost/api';const token = 'asdf'; // create a common api `ask` instanceconst askApiWithToken = apiHost; askApiWithToken ; askApiWithToken ;
Performing POST
, PUT
, DELETE
request
// create a common posts `ask` instanceconst askPosts = askApiWithToken; // postaskPosts body name: 'Chirs' ; // putaskPosts body name: 'Chirs' ; // deleteaskPosts method'delete' // Above two lines are equal with `.delete(id)` ;
API
Class: new Ask([url[, config]])
Create an ask instance.
Arguments
url
(String): Request URL. In fact, it could be a part (or prefix) of URL.config
(Object): Supportquery
,method
,url
,headers
,cancellation
,timeout
, and any other options from fetch api options
Return
(Object): ask
instance.
Example
// es6; // es5// var Ask = require('http-ask').default; const ask = 'url' method: 'post' body: ur: 'awesome' ;
Static Method: Ask.create([url[, config]])
The same with new Ask()
.
Return
(Object): ask
instance.
Static Method: Ask.request(url[, config])
Short hand for Ask.create(url, config).exec()
;
Return
(Promise): A promise to get response data.
Static Method: Ask.clone(ask)
The same with ask.clone()
.
Return
(Object): ask
instance.
Static Property: Ask.Cancellation()
See the follow Cancellation
section for detail.
Return
(Object): cancellation
instance, which has a cancel
method.
Method: ask#method(method)
Set HTTP request method
Arguments
method
(String): All HTTP methods are supported. Default toget
.
Return
(Object): ask
instance.
Method: ask#get([url])
Set GET
method and url.
Arguments
url
: Request URL.
Return
(Object): ask
instance.
Method: ask#post([url])
Set POST
method and url.
Arguments
url
: Request URL.
Return
(Object): ask
instance.
Method: ask#put([url])
Set PUT
method and url.
Arguments
url
: Request URL.
Return
(Object): ask
instance.
Method: ask#patch([url])
Set PATCH
method and url.
Arguments
url
: Request URL.
Return
(Object): ask
instance.
Method: ask#delete([url])
Set DELETE
method and url.
Arguments
url
: Request URL.
Return
(Object): ask
instance.
Method: ask#url(url)
Set or join URL.
Arguments
url
(String): Request URL.
Return
(Object): ask
instance.
Example
// `url` doesn't start with `/`Ask // the final url is: 'http://you.are/very/very/awesome'; // `url` starts with '/'Ask // start with `/` // the final url is: 'http://you.are/awesome';
Method: ask#query(query)
Set URL query.
Arguments
query
(Object): URL queryJSON
.
Return
(Object): ask
instance.
Example
Ask // the final url is: 'http://localhost/?a=1&b=3&c=5';
Method: ask#body(body)
Set HTTP request body.
Arguments
body
(Object): AJSON
or instance ofFormData
as usual.
Return
(Object): ask
instance.
Method: ask#set(headerKey, headerValue)
Set HTTP request header.
Arguments
headerKey
(String): Header key.headerValue
(String): Header value.
Return
(Object): ask
instance.
Method: ask#parser(parser)
Add a response parser.
A parser is a function that receives two arguments:
data
(Any): The response dataresponse
(Response): The Response instance
Parser should return a promise. The promise value will be passed to the next parser.
Arguments
parser
(Function): Response parser.
Return
(Object): ask
instance.
Example
Ask ;// will log: // Status: 200// Data: awesome!!!
Method: ask#timeout(ms)
Set HTTP request timeout.
Arguments
ms
(Number): Timeout(ms). Defaults to 30000.
Return
(Object): ask
instance.
Method: ask#cancellation(cancellation)
Set a cancellation token. See the follow example for detail.
Arguments
cancellation
(Cancellation).
Return
(Object): ask
instance.
Example
; const cancellation = ; ; return Ask // register a cancellation ;
Method: ask#clone()
Clone ask
with current config.
Return
(Object): ask
instance.
Method: ask#exec()
Execute request.
Return
(Promise): A promise to get response data.
Property: ask#response
Http Response instance. It is null
before .exec()
.
Example
const ask = 'http://localhost/';ask;
License
MIT