GithubHelp home page GithubHelp logo

ad-http's Introduction

#ad-http Build Status

#Install :

yarn add ad-http

#Simple Usage

import ADHttp, {Request} from 'ad-http'

 ADHttp.process('https://jsonplaceholder.typicode.com/todos/1')
        .then(response => response.body())
        .then(body => body.json())
        .then(json => {
            console.log(json)
        })
        .catch(error=>{
            console.error(error)
        })

#Advanced Usage import

import {
    PrepareRequestInterceptor,
    StatusCheckerHttpInterceptor,
    BaseUrlHttpInterceptor,
    HttpError,
    StatusError,
    Rest,
    Request,
    NormalClient
} from 'ad-http'

create rest instance

type BaseResponse = { status: boolean, message: string }
let rest = new Rest<BaseResponse>();

or use custom client

type BaseResponse = { status: boolean, message: string }
let rest = new Rest<BaseResponse>(new NormalClient());

change request or response if need

rest.interceptors.push(new PrepareRequestInterceptor());
rest.interceptors.push(new StatusCheckerHttpInterceptor());
rest.interceptors.push(new BaseUrlHttpInterceptor('https://httpstat.us/'));

write custom interceptors

// make new request
rest.interceptors.push({
    name: 'MakeNewRequest',
    intercept: async (chain) => {
        console.log('check status to make new request ' + chain.request.url);
        let response = await chain.proceed();
        if (response.status === 404) {
            let url = 'https://jsonplaceholder.typicode.com/todos';
            console.log('try make new request to' + url);
            return (
                chain.reset()
                    .proceed(
                        chain.request
                            .edit()
                            .url(url)
                            .build()
                    )
            );
        }
        return response;
    }
});

create request

let request = new Request.Builder()
        .url('/404')
        .build();

process request

rest.process(request)
    .then(async (response) => {
       console.log('Status : ',response.status);
       console.log('Headers : ',response.headers);
       let body=await response.body();
       let model=await body.json();
       console.log(model);
    })
    .catch((e) => {
       console.error(e);
       console.error(e instanceof StatusError);
       console.error(e instanceof HttpError);
       console.error(e instanceof Error);
    });

ad-http's People

Contributors

ahmaddehnavi avatar

Watchers

 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.