Zero dependency, Promise based HTTP request library for Node.js
$ npm install --save acejax
const acejax = require('acejax');
const myFunction = async () => {
try {
const result = await acejax('https://github.com/acestojanoski/acejax');
console.log(result.body);
// output: <!doctype html>...
} catch(error) {
console.error(error);
// output: { AcejaxError: Unauthorized...
}
};
myFunction();
const acejax = require('acejax');
const myFunction = async () => {
try {
const result = await acejax('https://some-api-endpoint', { json: true });
console.log(result.body);
/* output (json parsed):
* {
* id: 1,
* name: 'some name', ...
* }
*/
} catch(error) {
console.error(error);
// output: { AcejaxError: Forbidden...
}
};
myFunction();
Returns: Promise
Both resolved and rejected Promise will have the same format: Response
Type: string
The request URL.
Type: object
The https.request() options.
The method is GET
by default.
Type: boolean
Default: false
If this is set to true
, the acejax library will use the JSON.parse() function to parse the response body.
Type: string | object | Buffer
The request body.
Type: object | string
If this option is provided the Content-Type
header will be set to application/x-www-form-urlencoded
.
If it is an object, the object will be converted to string using new URLSearchParams(object).toString()
.
Type: object
On promise rejection, it will be an instance of the custom error class AcejaxError
. The name
of the error will be set to AcejaxError
, and the error message will be the statusMessage
of the response. Additionally, it contains the standard response properties.
Type: number
The response status code.
Type: string
The response status message.
Type: object
The response headers.
Type: Array<string>
The response rawHeaders
Type: string
The response http version.
Type: object
It will return back the acejaxOptions.
Type: string
It will return back the request url.
Type: string | object
The body of the response.