-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Adding case interface, failure interface and the beggining …
…of the api
- Loading branch information
Showing
8 changed files
with
136 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export interface Case { | ||
case_code: string | ||
age: number | ||
sex: string | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface Failure<FailureType extends string> { | ||
type: FailureType | ||
reason: string | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Api } from "./axios/Api"; | ||
import { Case } from "../interfaces/case"; | ||
import { AxiosError, AxiosRequestConfig, AxiosResponse } from "axios"; | ||
|
||
export class CaseApi extends Api { | ||
constructor (config: AxiosRequestConfig) { | ||
super(config); | ||
|
||
this.getAllCases = this.getAllCases.bind(this); | ||
} | ||
|
||
public async getAllCases (): Promise<Case[]> { | ||
try { | ||
const res: AxiosResponse<Case[]> = await this.get<Case, AxiosResponse<Case[]>>("http://13.250.29.32:5000/cases"); | ||
console.log(res); | ||
return this.success(res); | ||
} catch (error) { | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { Axios } from "./Axios"; | ||
|
||
import { | ||
AxiosRequestConfig, | ||
AxiosResponse, | ||
AxiosError, | ||
} from "axios"; | ||
|
||
export class Api extends Axios { | ||
private token: string; | ||
|
||
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.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()}` | ||
}, | ||
} | ||
} | ||
}); | ||
|
||
this.interceptors.response.use(param => ({ | ||
...param | ||
})); | ||
} | ||
|
||
public getToken (): string { | ||
return `Bearer ${this.token}`; | ||
} | ||
|
||
public setToken (token: string): void { | ||
this.token = token; | ||
} | ||
|
||
public getUri (config?: AxiosRequestConfig): string { | ||
return this.getUri(config); | ||
} | ||
|
||
public request<T, R = AxiosResponse<T>>( | ||
config: AxiosRequestConfig | ||
): Promise<R> { | ||
return this.request(config); | ||
} | ||
|
||
public get<T, R = AxiosResponse<T>>( | ||
url: string, | ||
config?: AxiosRequestConfig | ||
): Promise<R> { | ||
return this.get (url, config); | ||
} | ||
|
||
public success<T>(response: AxiosResponse<T>): T { | ||
return response.data; | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import axios, { AxiosInterceptorManager, AxiosRequestConfig, AxiosResponse } from "axios"; | ||
|
||
export class Axios { | ||
public interceptors: { | ||
request: AxiosInterceptorManager<AxiosRequestConfig>, | ||
response: AxiosInterceptorManager<AxiosResponse> | ||
}; | ||
constructor(config: AxiosRequestConfig) { | ||
return axios.create(config); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
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