1.0.18 • Published 4 years ago
axios-core-api v1.0.18
Axios Core Api
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);
}
1.0.18
4 years ago
1.0.17
5 years ago
1.0.16
5 years ago
1.0.15
7 years ago
1.0.14
7 years ago
1.0.13
7 years ago
1.0.12
7 years ago
1.0.11
7 years ago
1.0.10
7 years ago
1.0.9
8 years ago
1.0.8
8 years ago
1.0.7
8 years ago
1.0.6
8 years ago
1.0.5
8 years ago
1.0.4
8 years ago
1.0.3
8 years ago
1.0.2
8 years ago
1.0.1
8 years ago
1.0.0
8 years ago