Last active
December 7, 2022 20:14
-
-
Save EnetoJara/331960fbb8744abca3d7dd600a28e829 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }), | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Axios } from "./axios"; | |
import { | |
AxiosRequestConfig, | |
AxiosResponse, | |
AxiosError, | |
} from "axios"; | |
/** | |
* @class Api Class is a fancy es6 wrapper class for axios. | |
* | |
* @param {import("axios").AxiosRequestConfig} config - axios Request Config. | |
* @link [AxiosRequestConfig](https://github.com/axios/axios#request-config) | |
*/ | |
export class Api extends Axios { | |
private token: string; | |
/** | |
* Creates an instance of api. | |
* @param {import("axios").AxiosRequestConfig} conf | |
*/ | |
public constructor (conf: AxiosRequestConfig) { | |
super(conf); | |
this.token=""; | |
this.getToken = this.getToken.bind(this); | |
this.setToken = this.setToken.bind(this); | |
this.getUri = this.getUri.bind(this); | |
this.request = this.request.bind(this); | |
this.get = this.get.bind(this); | |
this.options = this.options.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); | |
this.success = this.success.bind(this); | |
this.error = this.error.bind(this); | |
this.interceptors.request.use((param) => { | |
return { | |
...param, | |
defaults: { | |
headers: { | |
...param.headers, | |
"Authorization": `Bearer ${this.getToken()}` | |
}, | |
} | |
} | |
}, (error) => { | |
// handling error | |
}); | |
// this middleware is been called right before the response is get it by the method that triggers the request | |
this.interceptors.response.use((param) => ({ | |
...param | |
}, (error) => { | |
// handling error | |
})); | |
} | |
/** | |
* Gets Token. | |
* | |
* @returns {string} token. | |
* @memberof Api | |
*/ | |
public getToken (): string { | |
return `Bearer ${this.token}`; | |
} | |
/** | |
* Sets Token. | |
* | |
* @param {string} token - token. | |
* @memberof Api | |
*/ | |
public setToken (token: string): void { | |
this.token = token; | |
} | |
/** | |
* Get Uri | |
* | |
* @param {import("axios").AxiosRequestConfig} [config] | |
* @returns {string} | |
* @memberof Api | |
*/ | |
public getUri (config?: AxiosRequestConfig): string { | |
return this.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.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.get (url, config); | |
} | |
/** | |
* HTTP OPTIONS 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 options<T, R = AxiosResponse<T>>( | |
url: string, | |
config?: AxiosRequestConfig | |
): Promise<R> { | |
return this.options (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.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.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.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.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.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; | |
} | |
/** | |
* | |
* | |
* @template T type. | |
* @param {AxiosError<T>} error | |
* @memberof Api | |
*/ | |
public error<T> (error: AxiosError<T>): void { | |
throw error; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import axios, { AxiosInterceptorManager, AxiosRequestConfig, AxiosResponse } from "axios"; | |
export class Axios { | |
public interceptors: { | |
/** | |
* The **Request** interceptor will be call rigth before the `http request` | |
* @summary | |
* This a useful method especially if you need to send a token on each request. | |
*/ | |
request: AxiosInterceptorManager<AxiosRequestConfig>; | |
/** | |
* The **Response** interceptor will be call rigth before the `http request` is received. | |
* @summary | |
* This a useful method especially if you need to send a token on each request. | |
*/ | |
response: AxiosInterceptorManager<AxiosResponse>; | |
}; | |
constructor(config: AxiosRequestConfig) { | |
return axios.create(config); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks man for the Axios file !