GithubHelp home page GithubHelp logo

axios-core-api's Introduction

Axios Core Api

https://img.shields.io/github/license/brybrophy/axios-core-api?color=blue https://img.shields.io/npm/v/axios-core-api https://img.shields.io/bundlephobia/minzip/axios-core-api https://img.shields.io/librariesio/release/npm/axios-core-api https://img.shields.io/badge/tests-passing-brightgreen.svg

Generate an Axios instance with business logic for all HTTP request methods.

This package can be used to create a core api class to route requests between a client and an api.

It is written in TypeScript, and typings are included directly in the package.

Getting Started

yarn add axios-core-api or npm install axios-core-api

Usage

You can read an in depth article on why and how to uses this package on medium.

import ApiCore from "axios-core-api";

const apiConfig = {
  headers: {
    Accept: "application/json",
    Authorization: `Bearer 123abc`,
    "Content-Type": "application/json",
  },
  timeout: 15000,
};

export default class CrudApi {
  constructor() {
    this._apiCore = new ApiCore(apiConfig);
    this._basePath = "https://www.crud.org/api";
  }
}

Usage With TypeScript

import { AxiosRequestConfig } from "axios";
import ApiCore from "axios-core-api";

const apiConfig: AxiosRequestConfig = {
  headers: {
    Accept: "application/json",
    Authorization: `Bearer 123abc`,
    "Content-Type": "application/json",
  },
  timeout: 15000,
};

export default class CrudApi {
  _apiCore: ApiCore;
  _basePath: string;

  constructor() {
    this._apiCore = new ApiCore(apiConfig);
    this._basePath = "https://www.crud.org/api";
  }
}

Methods

Get

getAll() {
    return this._apiCore.get(`${this._basePath}`);
}

getOne(id) {
    return this._apiCore.get(`${this._basePath}/${id}`);
}

Post

create(newExample) {
    return this._apiCore.post(`${this._basePath}`, newExample);
}

Post Form Data

createForm(newExample) {
    return this._apiCore.postFormData(`${this._basePath}`, newExample);
}

Put

updatePut(id, nextExample) {
    return this._apiCore.put(`${this._basePath}/${id}`, nextExample);
}

Patch

updatePatch(id, nextExample) {
    return this._apiCore.patch(`${this._basePath}/${id}`, nextExample);
}

Delete

destroy(id) {
    return this._apiCore.delete(`${this._basePath}/$id`);
}

refreshApiInstance

refreshApiInstance(newAccessToken) {
    const newConfig = apiConfig;

    newConfig.headers.Authorization = `Bearer ${newAccessToken}`;

    this._apiCore.refreshApiInstance(newConfig);
}

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.