mealie/frontend/composables/api/api-client.ts
Hayden fcc5d99d40
chore: frontend testing setup (#1739)
* add vitest

* initialize lib w/ tests

* move to dev dep

* run tests in CI

* update file names

* move api folder to lib

* move api and api types to same folder

* update generator outpath

* rm husky

* i guess i _did_ need those types

* reorg types

* extract validators into testable components

* (WIP) start composable testing

* fix import type

* fix linter complaint

* simplify icon type def

* fix linter errors (maybe?)

* rename client file for sorting
2022-10-22 11:51:07 -08:00

83 lines
2.6 KiB
TypeScript

import { AxiosResponse } from "axios";
import { useContext } from "@nuxtjs/composition-api";
import type { NuxtAxiosInstance } from "@nuxtjs/axios";
import { ApiRequestInstance, RequestResponse } from "~/lib/api/types/non-generated";
import { AdminAPI, PublicApi, UserApi } from "~/lib/api";
const request = {
async safe<T, U>(
funcCall: (url: string, data: U) => Promise<AxiosResponse<T>>,
url: string,
data: U
): Promise<RequestResponse<T>> {
let error = null;
const response = await funcCall(url, data).catch(function (e) {
console.log(e);
// Insert Generic Error Handling Here
error = e;
return null;
});
return { response, error, data: response?.data ?? null };
},
};
function getRequests(axiosInstance: NuxtAxiosInstance): ApiRequestInstance {
return {
async get<T>(url: string, params = {}): Promise<RequestResponse<T>> {
let error = null;
const response = await axiosInstance.get<T>(url, params).catch((e) => {
error = e;
});
if (response != null) {
return { response, error, data: response?.data };
}
return { response: null, error, data: null };
},
async post<T, U>(url: string, data: U) {
// eslint-disable-next-line @typescript-eslint/unbound-method
return await request.safe<T, U>(axiosInstance.post, url, data);
},
async put<T, U = T>(url: string, data: U) {
// eslint-disable-next-line @typescript-eslint/unbound-method
return await request.safe<T, U>(axiosInstance.put, url, data);
},
async patch<T, U = Partial<T>>(url: string, data: U) {
// eslint-disable-next-line @typescript-eslint/unbound-method
return await request.safe<T, U>(axiosInstance.patch, url, data);
},
async delete<T>(url: string) {
// eslint-disable-next-line @typescript-eslint/unbound-method
return await request.safe<T, undefined>(axiosInstance.delete, url, undefined);
},
};
}
export const useAdminApi = function (): AdminAPI {
const { $axios, i18n } = useContext();
$axios.setHeader("Accept-Language", i18n.locale);
const requests = getRequests($axios);
return new AdminAPI(requests);
};
export const useUserApi = function (): UserApi {
const { $axios, i18n } = useContext();
$axios.setHeader("Accept-Language", i18n.locale);
const requests = getRequests($axios);
return new UserApi(requests);
};
export const usePublicApi = function (): PublicApi {
const { $axios, i18n } = useContext();
$axios.setHeader("Accept-Language", i18n.locale);
const requests = getRequests($axios);
return new PublicApi(requests);
};