Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
That should wrap this issue; need to move on
- Loading branch information
Showing
24 changed files
with
481 additions
and
410 deletions.
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 |
---|---|---|
|
@@ -33,7 +33,7 @@ | |
"eslint docs --fix" | ||
], | ||
"**/pyproject.toml": [ | ||
"poetry update" | ||
"poetry update && git add poetry.lock" | ||
] | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,6 +1,92 @@ | ||
import axios from 'axios'; | ||
import axios, { Method } from 'axios'; | ||
import { API_BASE } from 'react-native-dotenv'; | ||
import { CreateData, FetchData, GenericModel, UpdateData } from '../types/api'; | ||
|
||
type UnrequiredGM = Partial<GenericModel>; | ||
type Methods = Extract<Uppercase<Method>, 'GET' | 'POST' | 'PATCH' | 'DELETE'>; | ||
type ReturnAPI< | ||
T extends UnrequiredGM = GenericModel, | ||
M extends Methods = 'GET' | ||
> = M extends 'GET' ? T[] : M extends 'POST' | 'PATCH' ? T : never; | ||
type RequiredAPI< | ||
T extends GenericModel = GenericModel, | ||
M extends Methods = 'GET' | ||
> = M extends 'GET' | ||
? undefined | ||
: M extends 'PATCH' | 'DELETE' | ||
? UpdateData<T> | ||
: CreateData<T>; | ||
|
||
export const axiosInstance = axios.create({ | ||
baseURL: API_BASE, | ||
}); | ||
|
||
export async function getData<T extends GenericModel = GenericModel>( | ||
path: string | ||
) { | ||
const response = await axiosInstance.get<FetchData<T>>(path); | ||
return 'results' in response.data ? response.data.results : response.data; | ||
} | ||
|
||
export async function createData<T extends GenericModel = GenericModel>( | ||
path: string, | ||
data: CreateData<T> | ||
) { | ||
const response = await axiosInstance.post<T>(path, data); | ||
return response.data; | ||
} | ||
|
||
export async function updateData<T extends GenericModel = GenericModel>( | ||
path: string, | ||
method: Extract<Methods, 'PATCH' | 'DELETE'>, | ||
data: UpdateData<T> | ||
) { | ||
const { id } = data; | ||
const url = `${path}${id}/`; | ||
const response = await axiosInstance.request<T>({ | ||
method, | ||
url, | ||
data, | ||
}); | ||
return response.data; | ||
} | ||
|
||
// export async function makeRequest<T extends GenericModel = GenericModel>( | ||
// path: string, | ||
// method: 'GET' | ||
// ): Promise<T[]>; | ||
// export async function makeRequest<T extends GenericModel = GenericModel>( | ||
// path: string, | ||
// method: 'POST', | ||
// data: CreateData<T> | ||
// ): Promise<T>; | ||
// export async function makeRequest<T extends GenericModel = GenericModel>( | ||
// path: string, | ||
// method: 'PATCH' | 'DELETE', | ||
// data: UpdateData<T> | ||
// ): Promise<T>; | ||
export async function makeRequest< | ||
T extends GenericModel = GenericModel, | ||
M extends Methods = 'GET' | ||
>( | ||
path: string, | ||
method: M = <M>'GET', | ||
data: RequiredAPI<T, M> | any = undefined | ||
): Promise<ReturnAPI<T, M>> { | ||
try { | ||
if (method === 'PATCH' || method === 'DELETE') { | ||
return (await updateData<T>( | ||
path, | ||
method, | ||
data as UpdateData<T> | ||
)) as ReturnAPI<T, M>; | ||
} else if (method === 'POST') { | ||
// eslint-disable-next-line prettier/prettier | ||
return (await createData<T>(path, data as CreateData<T>)) as ReturnAPI<T, M>; | ||
} else { | ||
return (await getData<T>(path)) as ReturnAPI<T, M>; | ||
} | ||
} catch (e) { | ||
throw axios.isAxiosError(e) ? e.response?.data : e; | ||
} | ||
} |
Oops, something went wrong.