|
|
@@ -1,10 +1,32 @@
|
|
|
import axios from 'axios';
|
|
|
|
|
|
// Partial function that accepts a generic params type and returns an api method
|
|
|
-export const baseApi = <T extends {}>(requestType: string, endpoint: string) => {
|
|
|
- if (requestType === 'POST') {
|
|
|
- return (token: string, params?: T, callback?: (err: any, res: any) => void) => {
|
|
|
- axios.post(`https://${process.env.API_SERVER + endpoint}`, params, {
|
|
|
+export const baseApi = <T extends {}, S = {}>(requestType: string, endpoint: ((pathParams?: S) => string) | string) => {
|
|
|
+ return (token: string, params: T, callback?: (err: any, res: any) => void) => {
|
|
|
+
|
|
|
+ // Generate endpoint literal
|
|
|
+ let endpointString: ((pathParams: S) => string) | string;
|
|
|
+ if (typeof endpoint === 'string') {
|
|
|
+ endpointString = endpoint;
|
|
|
+ } else {
|
|
|
+ endpointString = 'fuck'
|
|
|
+ }
|
|
|
+
|
|
|
+ // Handle request type (can refactor)
|
|
|
+ if (requestType === 'POST') {
|
|
|
+ axios.post(`https://${(process as any).env.API_SERVER + endpointString}`, params, {
|
|
|
+ headers: {
|
|
|
+ Authorization: `Bearer ${token}`
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .then(res => {
|
|
|
+ callback && callback(null, res.data);
|
|
|
+ })
|
|
|
+ .catch(err => {
|
|
|
+ callback && callback(err, null);
|
|
|
+ });
|
|
|
+ } else if (requestType === 'PUT') {
|
|
|
+ axios.put(`https://${(process as any).env.API_SERVER + endpointString}`, params, {
|
|
|
headers: {
|
|
|
Authorization: `Bearer ${token}`
|
|
|
}
|
|
|
@@ -15,21 +37,19 @@ export const baseApi = <T extends {}>(requestType: string, endpoint: string) =>
|
|
|
.catch(err => {
|
|
|
callback && callback(err, null);
|
|
|
});
|
|
|
- };
|
|
|
+ } else {
|
|
|
+ axios.get(`https://${(process as any).env.API_SERVER + endpoint}`, {
|
|
|
+ headers: {
|
|
|
+ Authorization: `Bearer ${token}`
|
|
|
+ },
|
|
|
+ params
|
|
|
+ })
|
|
|
+ .then(res => {
|
|
|
+ callback && callback(null, res.data);
|
|
|
+ })
|
|
|
+ .catch(err => {
|
|
|
+ callback && callback(err, null);
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|
|
|
-
|
|
|
- return (token: string, params?: T, callback?: (err: any, res: any) => void) => {
|
|
|
- axios.get(`https://${process.env.API_SERVER + endpoint}`, {
|
|
|
- headers: {
|
|
|
- Authorization: `Bearer ${token}`
|
|
|
- },
|
|
|
- params
|
|
|
- })
|
|
|
- .then(res => {
|
|
|
- callback && callback(null, res.data);
|
|
|
- })
|
|
|
- .catch(err => {
|
|
|
- callback && callback(err, null);
|
|
|
- });
|
|
|
- };
|
|
|
}
|