76 lines
2.1 KiB
TypeScript
76 lines
2.1 KiB
TypeScript
import axios from 'axios';
|
|
import type { AxiosInstance } from 'axios';
|
|
|
|
type ApiResponse = [string | null, any | null];
|
|
|
|
declare module 'axios' {
|
|
export interface AxiosInstance {
|
|
request(config: AxiosRequestConfig): Promise<ApiResponse>;
|
|
get(url: string, config?: AxiosRequestConfig): Promise<ApiResponse>;
|
|
delete(url: string, config?: AxiosRequestConfig): Promise<ApiResponse>;
|
|
head(url: string, config?: AxiosRequestConfig): Promise<ApiResponse>;
|
|
post(url: string, data?: any, config?: AxiosRequestConfig): Promise<ApiResponse>;
|
|
put(url: string, data?: any, config?: AxiosRequestConfig): Promise<ApiResponse>;
|
|
patch(url: string, data?: any, config?: AxiosRequestConfig): Promise<ApiResponse>;
|
|
}
|
|
}
|
|
|
|
const api = axios.create({
|
|
baseURL: 'http://localhost:41337',
|
|
withCredentials: true
|
|
});
|
|
|
|
// axios.interceptors.request.use(function onReq(config) {
|
|
// return config;
|
|
// }, function onError(error) {
|
|
// // console.error(error);
|
|
// Promise.resolve(['Network Error', null]);
|
|
// });
|
|
|
|
api.interceptors.response.use(function onSuccess(response) {
|
|
return [null, response.data];
|
|
}, function onError(error) {
|
|
// console.error(error);
|
|
if (error.response) return [error.response.data.error, null];
|
|
if (error.request) return ['Network Error', null];
|
|
});
|
|
|
|
const actions = {
|
|
getBundles: async function getBundles() {
|
|
return api.get('/bundles');
|
|
},
|
|
|
|
getUserOffers: async function getUserOffers(state, userId) {
|
|
return api.get(`/user/${userId}/offers`);
|
|
},
|
|
|
|
getOffers: async function getOffers(userId) {
|
|
return api.get(`/user/${userId}/offers`);
|
|
},
|
|
|
|
getWants: async function getWants() {
|
|
return api.get('/wants');
|
|
},
|
|
|
|
getLogin: async function getLogin() {
|
|
return api.get('/auth/login')
|
|
},
|
|
|
|
postLogin: async function postLogin(email, password) {
|
|
return api.post('/auth/login', {
|
|
email,
|
|
password,
|
|
// discogs,
|
|
});
|
|
},
|
|
|
|
postRegister: async function postRegister(email, password) {
|
|
return api.post('/auth/register', {
|
|
email,
|
|
password,
|
|
});
|
|
},
|
|
};
|
|
|
|
export default actions;
|