Code Monkey home page Code Monkey logo

acejax's Introduction

acejax

CircleCI XO code style code style: prettier install size Downloads

Zero dependency, Promise based HTTP request library for Node.js

Install

$ npm install --save acejax

Usage

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();

API

acejax(url, acejaxOptions?)

Returns: Promise

Both resolved and rejected Promise will have the same format: Response

url

Type: string

The request URL.

acejaxOptions

Type: object

The https.request() options.

The method is GET by default.

json

Type: boolean

Default: false

If this is set to true, the acejax library will use the JSON.parse() function to parse the response body.

body

Type: string | object | Buffer

The request body.

form

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().

acejax.get(url, acejaxOptions?)

acejax.delete(url, acejaxOptions?)

acejax.post(url, acejaxOptions?)

acejax.put(url, acejaxOptions?)

acejax.patch(url, acejaxOptions?)

Response

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.

statusCode

Type: number

The response status code.

statusMessage

Type: string

The response status message.

headers

Type: object

The response headers.

rawHeaders

Type: Array<string>

The response rawHeaders

httpVersion

Type: string

The response http version.

acejaxOptions

Type: object

It will return back the acejaxOptions.

url

Type: string

It will return back the request url.

body

Type: string | object

The body of the response.

LICENSE

MIT

acejax's People

Contributors

acestojanoski avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.