mirror of
https://github.com/hay-kot/mealie.git
synced 2025-07-12 16:14:02 -07:00
reorganize all frontend items
This commit is contained in:
parent
d67240d449
commit
00a8fdda41
147 changed files with 3845 additions and 743 deletions
64
frontend/composables/use-api.ts
Normal file
64
frontend/composables/use-api.ts
Normal file
|
@ -0,0 +1,64 @@
|
|||
import { AxiosResponse } from "axios";
|
||||
import { useContext } from "@nuxtjs/composition-api";
|
||||
import { NuxtAxiosInstance } from "@nuxtjs/axios";
|
||||
import { Api } from "~/api";
|
||||
import { ApiRequestInstance } from "~/types/api";
|
||||
|
||||
interface RequestResponse<T> {
|
||||
response: AxiosResponse<T> | null;
|
||||
data: T | null;
|
||||
error: any;
|
||||
}
|
||||
|
||||
const request = {
|
||||
async safe<T>(funcCall: any, url: string, data: object = {}): Promise<RequestResponse<T>> {
|
||||
const response = await funcCall(url, data).catch(function (error: object) {
|
||||
console.log(error);
|
||||
// Insert Generic Error Handling Here
|
||||
return { response: null, error, data: null };
|
||||
});
|
||||
return { response, error: null, data: response.data };
|
||||
},
|
||||
};
|
||||
|
||||
function getRequests(axoisInstance: NuxtAxiosInstance): ApiRequestInstance {
|
||||
const requests = {
|
||||
async get<T>(url: string, queryParams = {}): Promise<RequestResponse<T>> {
|
||||
let error = null;
|
||||
const response = await axoisInstance.get<T>(url, { params: { queryParams } }).catch((e) => {
|
||||
error = e;
|
||||
});
|
||||
if (response != null) {
|
||||
return { response, error, data: response?.data };
|
||||
}
|
||||
return { response: null, error, data: null };
|
||||
},
|
||||
|
||||
async post<T>(url: string, data: object) {
|
||||
return await request.safe<T>(axoisInstance.post, url, data);
|
||||
},
|
||||
|
||||
async put<T>(url: string, data: object) {
|
||||
return await request.safe<T>(axoisInstance.put, url, data);
|
||||
},
|
||||
|
||||
async patch<T>(url: string, data: object) {
|
||||
return await request.safe<T>(axoisInstance.patch, url, data);
|
||||
},
|
||||
|
||||
async delete<T>(url: string) {
|
||||
return await request.safe<T>(axoisInstance.delete, url);
|
||||
},
|
||||
};
|
||||
return requests;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
export const useApi = function (): Api {
|
||||
const { $axios } = useContext();
|
||||
const requests = getRequests($axios);
|
||||
|
||||
return new Api(requests)
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue