-
-
Save EnetoJara/331960fbb8744abca3d7dd600a28e829 to your computer and use it in GitHub Desktop.
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 }), | |
} |
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; | |
} | |
} |
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); | |
} | |
} |
I think you have to remove the part from line 45. its duplicated with syntax error
https://gist.github.com/EnetoJara/331960fbb8744abca3d7dd600a28e829#file-api-ts-L45-L81
I think you have to remove the part from line 45. its duplicated with syntax error
https://gist.github.com/EnetoJara/331960fbb8744abca3d7dd600a28e829#file-api-ts-L45-L81
Sorry for the delay. Thank you for letting me know. If there is anything el please feel free to add/modify it
Hi, why not add the response interceptor?
Hi, why not add the response interceptor?
Sorry for not responding b4, I just add them
I'm getting this error: Type 'AxiosStatic' is not a constructor function type.ts(2507)
while trying to extends from axios export class Api extends Axios {
I read on your post that you're importing axios like this: import { Axios } from "./axios";
. What's the content of this file?
Thanks man for the Axios file !
I think you have to remove the part from line 45. its duplicated with syntax error
https://gist.github.com/EnetoJara/331960fbb8744abca3d7dd600a28e829#file-api-ts-L45-L81