Last active
September 13, 2018 11:41
-
-
Save DanielRamosAcosta/2f773d815f5434f185c59aec1bab418c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| /** | |
| * These typings are Axios's repo | |
| * https://raw.githubusercontent.com/axios/axios/master/index.d.ts | |
| */ | |
| export interface AxiosTransformer { | |
| (data: any, headers?: any): any; | |
| } | |
| export interface AxiosAdapter { | |
| (config: AxiosRequestConfig): AxiosPromise<any>; | |
| } | |
| export interface AxiosBasicCredentials { | |
| username: string; | |
| password: string; | |
| } | |
| export interface AxiosProxyConfig { | |
| host: string; | |
| port: number; | |
| auth?: { | |
| username: string; | |
| password: string; | |
| }; | |
| protocol?: string; | |
| } | |
| export type Method = | |
| | "get" | |
| | "delete" | |
| | "head" | |
| | "options" | |
| | "post" | |
| | "put" | |
| | "patch"; | |
| export type ResponseType = | |
| | "arraybuffer" | |
| | "blob" | |
| | "document" | |
| | "json" | |
| | "text" | |
| | "stream"; | |
| export interface AxiosRequestConfig { | |
| url?: string; | |
| method?: Method; | |
| baseURL?: string; | |
| transformRequest?: AxiosTransformer | AxiosTransformer[]; | |
| transformResponse?: AxiosTransformer | AxiosTransformer[]; | |
| headers?: any; | |
| params?: any; | |
| paramsSerializer?: (params: any) => string; | |
| data?: any; | |
| timeout?: number; | |
| withCredentials?: boolean; | |
| adapter?: AxiosAdapter; | |
| auth?: AxiosBasicCredentials; | |
| responseType?: ResponseType; | |
| xsrfCookieName?: string; | |
| xsrfHeaderName?: string; | |
| onUploadProgress?: (progressEvent: any) => void; | |
| onDownloadProgress?: (progressEvent: any) => void; | |
| maxContentLength?: number; | |
| validateStatus?: (status: number) => boolean; | |
| maxRedirects?: number; | |
| socketPath?: string | null; | |
| httpAgent?: any; | |
| httpsAgent?: any; | |
| proxy?: AxiosProxyConfig | false; | |
| cancelToken?: CancelToken; | |
| } | |
| export interface AxiosResponse<T = any> { | |
| data: T; | |
| status: number; | |
| statusText: string; | |
| headers: any; | |
| config: AxiosRequestConfig; | |
| request?: any; | |
| } | |
| export interface AxiosError extends Error { | |
| config: AxiosRequestConfig; | |
| code?: string; | |
| request?: any; | |
| response?: AxiosResponse; | |
| isAxiosError: boolean; | |
| } | |
| export interface AxiosPromise<T = any> extends Promise<AxiosResponse<T>> {} | |
| export interface CancelStatic { | |
| new (message?: string): Cancel; | |
| } | |
| export interface Cancel { | |
| message: string; | |
| } | |
| export interface Canceler { | |
| (message?: string): void; | |
| } | |
| export interface CancelTokenStatic { | |
| new (executor: (cancel: Canceler) => void): CancelToken; | |
| source(): CancelTokenSource; | |
| } | |
| export interface CancelToken { | |
| promise: Promise<Cancel>; | |
| reason?: Cancel; | |
| throwIfRequested(): void; | |
| } | |
| export interface CancelTokenSource { | |
| token: CancelToken; | |
| cancel: Canceler; | |
| } | |
| export interface AxiosInterceptorManager<V> { | |
| use( | |
| onFulfilled?: (value: V) => V | Promise<V>, | |
| onRejected?: (error: any) => any | |
| ): number; | |
| eject(id: number): void; | |
| } | |
| export interface AxiosInstance { | |
| (config: AxiosRequestConfig): AxiosPromise; | |
| (url: string, config?: AxiosRequestConfig): AxiosPromise; | |
| defaults: AxiosRequestConfig; | |
| interceptors: { | |
| request: AxiosInterceptorManager<AxiosRequestConfig>; | |
| response: AxiosInterceptorManager<AxiosResponse>; | |
| }; | |
| getUri(config?: AxiosRequestConfig): string; | |
| request<T = any, R = AxiosResponse<T>>( | |
| config: AxiosRequestConfig | |
| ): Promise<R>; | |
| get<T = any, R = AxiosResponse<T>>( | |
| url: string, | |
| config?: AxiosRequestConfig | |
| ): Promise<R>; | |
| delete<T = any, R = AxiosResponse<T>>( | |
| url: string, | |
| config?: AxiosRequestConfig | |
| ): Promise<R>; | |
| head<T = any, R = AxiosResponse<T>>( | |
| url: string, | |
| config?: AxiosRequestConfig | |
| ): Promise<R>; | |
| post<T = any, R = AxiosResponse<T>>( | |
| url: string, | |
| data?: any, | |
| config?: AxiosRequestConfig | |
| ): Promise<R>; | |
| put<T = any, R = AxiosResponse<T>>( | |
| url: string, | |
| data?: any, | |
| config?: AxiosRequestConfig | |
| ): Promise<R>; | |
| patch<T = any, R = AxiosResponse<T>>( | |
| url: string, | |
| data?: any, | |
| config?: AxiosRequestConfig | |
| ): Promise<R>; | |
| } | |
| class Axios { | |
| private config: AxiosRequestConfig | |
| constructor(config?: AxiosRequestConfig) { | |
| this.config = config || { | |
| // TODO :) | |
| } | |
| } | |
| async get<T = any>( | |
| url: string, | |
| config?: AxiosRequestConfig | |
| ): Promise<AxiosResponse<T>> { | |
| const response = await fetch(url) | |
| const responseData: T = await response.json() | |
| const axiosResponse: AxiosResponse<T> = { | |
| data: responseData, | |
| status: response.status, | |
| statusText: response.statusText, | |
| headers: {}, | |
| config, | |
| } | |
| return axiosResponse | |
| } | |
| async post<T = any>( | |
| url: string, | |
| data?: any, | |
| config?: AxiosRequestConfig | |
| ): Promise<AxiosResponse<T>> { | |
| const response = await fetch(url, { method: "POST", body: JSON.stringify(data) }) | |
| const responseData: T = await response.json() | |
| const axiosResponse: AxiosResponse<T> = { | |
| data: responseData, | |
| status: response.status, | |
| statusText: response.statusText, | |
| headers: {}, | |
| config, | |
| } | |
| return axiosResponse | |
| } | |
| } | |
| class AxiosStatic extends Axios { | |
| create(config?: AxiosRequestConfig): Axios { | |
| return new Axios(config) | |
| } | |
| } | |
| const axios = new AxiosStatic() | |
| export default axios |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment