2022-09-04 06:19:13 +00:00
|
|
|
import { PublicApi } from '~~/lib/api/public';
|
|
|
|
import { UserApi } from '~~/lib/api/user';
|
|
|
|
import { Requests } from '~~/lib/requests';
|
|
|
|
import { useAuthStore } from '~~/stores/auth';
|
2022-09-01 22:32:03 +00:00
|
|
|
|
2022-09-04 06:19:19 +00:00
|
|
|
function logger(r: Response) {
|
|
|
|
console.log(`${r.status} ${r.url} ${r.statusText}`);
|
2022-09-01 22:32:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function usePublicApi(): PublicApi {
|
2022-09-04 06:19:13 +00:00
|
|
|
const requests = new Requests('', '', {});
|
2022-09-01 22:32:03 +00:00
|
|
|
return new PublicApi(requests);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function useUserApi(): UserApi {
|
|
|
|
const authStore = useAuthStore();
|
2022-09-04 06:19:13 +00:00
|
|
|
|
|
|
|
const requests = new Requests('', () => authStore.token, {});
|
2022-09-04 06:19:19 +00:00
|
|
|
requests.addResponseInterceptor(logger);
|
2022-09-04 06:19:13 +00:00
|
|
|
requests.addResponseInterceptor(r => {
|
|
|
|
if (r.status === 401) {
|
|
|
|
authStore.clearSession();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-09-01 22:32:03 +00:00
|
|
|
return new UserApi(requests);
|
|
|
|
}
|