Skip to content

Commit

Permalink
[FEATURE] Adding case interface, failure interface and the beggining …
Browse files Browse the repository at this point in the history
…of the api
  • Loading branch information
JustalK committed Nov 20, 2020
1 parent 1af099d commit 495d6b5
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 1 deletion.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,10 @@
"@testing-library/user-event": "^12.1.10",
"@types/jest": "^26.0.15",
"@types/node": "^12.19.6",
"@types/qs": "^6.9.5",
"@types/react": "^16.9.56",
"@types/react-dom": "^16.9.9",
"axios": "^0.21.0",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-scripts": "4.0.0",
Expand Down
5 changes: 5 additions & 0 deletions src/interfaces/case.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface Case {
case_code: string
age: number
sex: string
}
4 changes: 4 additions & 0 deletions src/interfaces/failure.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface Failure<FailureType extends string> {
type: FailureType
reason: string
}
21 changes: 21 additions & 0 deletions src/services/CaseApi.ts
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;
}
}
}
73 changes: 73 additions & 0 deletions src/services/axios/Api.ts
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;
}
}
11 changes: 11 additions & 0 deletions src/services/axios/Axios.ts
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);
}
}
18 changes: 18 additions & 0 deletions src/services/axios/api.config.ts
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 }),
}
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true
"noEmit": true,
"strictPropertyInitialization": false
},
"include": [
"src",
Expand Down

0 comments on commit 495d6b5

Please sign in to comment.