Skip to content

Instantly share code, notes, and snippets.

@bitIO
Last active September 10, 2020 03:40
Show Gist options
  • Save bitIO/f1aeb0a45a90b41ab6fb354f000a81ff to your computer and use it in GitHub Desktop.
Save bitIO/f1aeb0a45a90b41ab6fb354f000a81ff to your computer and use it in GitHub Desktop.
Axios Typescript API
import axios, { AxiosError, AxiosRequestConfig, AxiosResponse } from "axios";
import { API_BASE_URL } from './apiConfig';
/**
* ES6 Axios Class.
*
* @class Api
* @extends {Axios}
* @example
* class UserApi extends Api {
* public constructor (config) {
* super(config);
*
* this.login=this.login.bind(this);
* }
*
* public login (user: User) {
* return this.api.post<string, User, AxiosResponse<User>>("https://www.domain/login", {name: user.name, pass: user.pass})
* .then((res: AxiosResponse<string>) => res.data);
* }
* }
*/
export class Api {
[x:string]: any;
/**
* Creates an instance of Api.
*
* @param {import("axios").AxiosRequestConfig} [config] - axios configuration.
* @memberof Api
*/
public constructor (config?: AxiosRequestConfig) {
this.api = axios.create(config);
this.api.interceptors.request.use((param: AxiosRequestConfig) => ({
baseUrl: process.env.API_BASE_URL,
...param
}));
this.getUri = this.getUri.bind(this);
this.request = this.request.bind(this);
this.get = this.get.bind(this);
this.delete = this.delete.bind(this);
this.head = this.head.bind(this);
this.post = this.post.bind(this);
this.put = this.put.bind(this);
this.patch = this.patch.bind(this);
}
/**
* Get Uri
*
* @param {import("axios").AxiosRequestConfig} [config]
* @returns {string}
* @memberof Api
*/
public getUri (config?: AxiosRequestConfig): string {
return this.api.api.getUri(config);
}
/**
* Generic request.
*
* @access public
* @template T - `TYPE`: expected object.
* @template R - `RESPONSE`: expected object inside a axios response format.
* @param {import("axios").AxiosRequestConfig} [config] - axios request configuration.
* @returns {Promise<R>} - HTTP axios response payload.
* @memberof Api
*
* @example
* api.request({
* method: "GET|POST|DELETE|PUT|PATCH"
* baseUrl: "http://www.domain.com",
* url: "/api/v1/users",
* headers: {
* "Content-Type": "application/json"
* }
* }).then((response: AxiosResponse<User>) => response.data)
*
*/
public request<T, R = AxiosResponse<T>> (config: AxiosRequestConfig): Promise<R> {
return this.api.api.request(config);
}
/**
* HTTP GET method, used to fetch data `statusCode`: 200.
*
* @access public
* @template T - `TYPE`: expected object.
* @template R - `RESPONSE`: expected object inside a axios response format.
* @param {string} url - endpoint you want to reach.
* @param {import("axios").AxiosRequestConfig} [config] - axios request configuration.
* @returns {Promise<R>} HTTP `axios` response payload.
* @memberof Api
*/
public get<T, R = AxiosResponse<T>> (url: string, config?: AxiosRequestConfig): Promise<R> {
return this.api.get(url, config);
}
/**
* HTTP DELETE method, `statusCode`: 204 No Content.
*
* @access public
* @template T - `TYPE`: expected object.
* @template R - `RESPONSE`: expected object inside a axios response format.
* @param {string} url - endpoint you want to reach.
* @param {import("axios").AxiosRequestConfig} [config] - axios request configuration.
* @returns {Promise<R>} - HTTP [axios] response payload.
* @memberof Api
*/
public delete<T, R = AxiosResponse<T>> (url: string, config?: AxiosRequestConfig): Promise<R> {
return this.api.delete(url, config);
}
/**
* HTTP HEAD method.
*
* @access public
* @template T - `TYPE`: expected object.
* @template R - `RESPONSE`: expected object inside a axios response format.
* @param {string} url - endpoint you want to reach.
* @param {import("axios").AxiosRequestConfig} [config] - axios request configuration.
* @returns {Promise<R>} - HTTP [axios] response payload.
* @memberof Api
*/
public head<T, R = AxiosResponse<T>> (url: string, config?: AxiosRequestConfig): Promise<R> {
return this.api.head(url, config);
}
/**
* HTTP POST method `statusCode`: 201 Created.
*
* @access public
* @template T - `TYPE`: expected object.
* @template B - `BODY`: body request object.
* @template R - `RESPONSE`: expected object inside a axios response format.
* @param {string} url - endpoint you want to reach.
* @param {B} data - payload to be send as the `request body`,
* @param {import("axios").AxiosRequestConfig} [config] - axios request configuration.
* @returns {Promise<R>} - HTTP [axios] response payload.
* @memberof Api
*/
public post<T, B, R = AxiosResponse<T>> (url: string, data?: B, config?: AxiosRequestConfig): Promise<R> {
return this.api.post(url, data, config);
}
/**
* HTTP PUT method.
*
* @access public
* @template T - `TYPE`: expected object.
* @template B - `BODY`: body request object.
* @template R - `RESPONSE`: expected object inside a axios response format.
* @param {string} url - endpoint you want to reach.
* @param {B} data - payload to be send as the `request body`,
* @param {import("axios").AxiosRequestConfig} [config] - axios request configuration.
* @returns {Promise<R>} - HTTP [axios] response payload.
* @memberof Api
*/
public put<T, B, R = AxiosResponse<T>> (url: string, data?: B, config?: AxiosRequestConfig): Promise<R> {
return this.api.put(url, data, config);
}
/**
* HTTP PATCH method.
*
* @access public
* @template T - `TYPE`: expected object.
* @template B - `BODY`: body request object.
* @template R - `RESPONSE`: expected object inside a axios response format.
* @param {string} url - endpoint you want to reach.
* @param {B} data - payload to be send as the `request body`,
* @param {import("axios").AxiosRequestConfig} [config] - axios request configuration.
* @returns {Promise<R>} - HTTP [axios] response payload.
* @memberof Api
*/
public patch<T, B, R = AxiosResponse<T>> (url: string, data?: B, config?: AxiosRequestConfig): Promise<R> {
return this.api.patch(url, data, config);
}
/**
*
* @template T - type.
* @param {import("axios").AxiosResponse<T>} response - axios response.
* @returns {T} - expected object.
* @memberof Api
*/
public success<T> (response: AxiosResponse<T>): T {
return response.data;
}
public error (error: AxiosError<Error>) {
throw error;
}
}
import * as qs from "qs";
import { PathLike } from "fs";
export const apiConfig = {
returnRejectedPromiseOnError: true,
withCredentials: true,
timeout: 30000,
baseURL: "https://jsonplaceholder.typicode.com/",
headers: {
common: {
"Cache-Control": "no-cache, no-store, must-revalidate",
Pragma: "no-cache",
"Content-Type": "application/json",
Accept: "application/json",
},
},
paramsSerializer: (params: PathLike) => qs.stringify(params, { indices: false }),
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment