mirror of
https://github.com/mealie-recipes/mealie.git
synced 2026-04-08 22:15:34 -04:00
chore: Nuxt 4 upgrade (#7426)
This commit is contained in:
@@ -1,86 +0,0 @@
|
||||
import type { AxiosInstance, AxiosResponse, AxiosRequestConfig } from "axios";
|
||||
import type { Composer } from "vue-i18n";
|
||||
import type { ApiRequestInstance, RequestResponse } from "~/lib/api/types/non-generated";
|
||||
import { AdminAPI, PublicApi, UserApi } from "~/lib/api";
|
||||
import { PublicExploreApi } from "~/lib/api/client-public";
|
||||
import { useGlobalI18n } from "~/composables/use-global-i18n";
|
||||
|
||||
const request = {
|
||||
async safe<T, U>(
|
||||
funcCall: (url: string, data: U, config?: AxiosRequestConfig) => Promise<AxiosResponse<T>>,
|
||||
url: string,
|
||||
data: U,
|
||||
config?: AxiosRequestConfig,
|
||||
): Promise<RequestResponse<T>> {
|
||||
let error = null;
|
||||
const response = await funcCall(url, data, config).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: AxiosInstance): ApiRequestInstance {
|
||||
return {
|
||||
async get<T>(url: string, params = {}, config?: AxiosRequestConfig): Promise<RequestResponse<T>> {
|
||||
let error = null;
|
||||
const response = await axiosInstance.get<T>(url, { ...config, 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, config?: AxiosRequestConfig) {
|
||||
return await request.safe<T, U>(axiosInstance.post, url, data, config);
|
||||
},
|
||||
|
||||
async put<T, U = T>(url: string, data: U, config?: AxiosRequestConfig) {
|
||||
return await request.safe<T, U>(axiosInstance.put, url, data, config);
|
||||
},
|
||||
|
||||
async patch<T, U = Partial<T>>(url: string, data: U, config?: AxiosRequestConfig) {
|
||||
return await request.safe<T, U>(axiosInstance.patch, url, data, config);
|
||||
},
|
||||
|
||||
async delete<T>(url: string, config?: AxiosRequestConfig) {
|
||||
return await request.safe<T, undefined>(axiosInstance.delete, url, undefined, config);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export const useRequests = function (i18n?: Composer): ApiRequestInstance {
|
||||
const { $axios } = useNuxtApp();
|
||||
if (!i18n) {
|
||||
i18n = useGlobalI18n();
|
||||
}
|
||||
|
||||
$axios.defaults.headers.common["Accept-Language"] = i18n.locale.value;
|
||||
|
||||
return getRequests($axios);
|
||||
};
|
||||
|
||||
export const useAdminApi = function (i18n?: Composer): AdminAPI {
|
||||
const requests = useRequests(i18n);
|
||||
return new AdminAPI(requests);
|
||||
};
|
||||
|
||||
export const useUserApi = function (i18n?: Composer): UserApi {
|
||||
const requests = useRequests(i18n);
|
||||
return new UserApi(requests);
|
||||
};
|
||||
|
||||
export const usePublicApi = function (i18n?: Composer): PublicApi {
|
||||
const requests = useRequests(i18n);
|
||||
return new PublicApi(requests);
|
||||
};
|
||||
|
||||
export const usePublicExploreApi = function (groupSlug: string, i18n?: Composer): PublicExploreApi {
|
||||
const requests = useRequests(i18n);
|
||||
return new PublicExploreApi(requests, groupSlug);
|
||||
};
|
||||
@@ -1,2 +0,0 @@
|
||||
export { useStaticRoutes } from "./static-routes";
|
||||
export { useAdminApi, usePublicApi, usePublicExploreApi, useUserApi } from "./api-client";
|
||||
@@ -1,51 +0,0 @@
|
||||
function UnknownToString(ukn: string | unknown) {
|
||||
return typeof ukn === "string" ? ukn : "";
|
||||
}
|
||||
|
||||
export const useStaticRoutes = () => {
|
||||
const { $config } = useNuxtApp();
|
||||
const prefix = `${$config.public.SUB_PATH}/api`.replace("//", "/");
|
||||
|
||||
// Methods to Generate reference urls for assets/images *
|
||||
function recipeImage(recipeId: string, version: string | unknown = "", key: string | number = 1) {
|
||||
return `${prefix}/media/recipes/${recipeId}/images/original.webp?rnd=${key}&version=${UnknownToString(version)}`;
|
||||
}
|
||||
|
||||
function recipeSmallImage(recipeId: string, version: string | unknown = "", key: string | number = 1) {
|
||||
return `${prefix}/media/recipes/${recipeId}/images/min-original.webp?rnd=${key}&version=${UnknownToString(
|
||||
version,
|
||||
)}`;
|
||||
}
|
||||
|
||||
function recipeTinyImage(recipeId: string, version: string | unknown = "", key: string | number = 1) {
|
||||
return `${prefix}/media/recipes/${recipeId}/images/tiny-original.webp?rnd=${key}&version=${UnknownToString(
|
||||
version,
|
||||
)}`;
|
||||
}
|
||||
|
||||
function recipeTimelineEventImage(recipeId: string, timelineEventId: string) {
|
||||
return `${prefix}/media/recipes/${recipeId}/images/timeline/${timelineEventId}/original.webp`;
|
||||
}
|
||||
|
||||
function recipeTimelineEventSmallImage(recipeId: string, timelineEventId: string) {
|
||||
return `${prefix}/media/recipes/${recipeId}/images/timeline/${timelineEventId}/min-original.webp`;
|
||||
}
|
||||
|
||||
function recipeTimelineEventTinyImage(recipeId: string, timelineEventId: string) {
|
||||
return `${prefix}/media/recipes/${recipeId}/images/timeline/${timelineEventId}/tiny-original.webp`;
|
||||
}
|
||||
|
||||
function recipeAssetPath(recipeId: string, assetName: string) {
|
||||
return `${prefix}/media/recipes/${recipeId}/assets/${assetName}`;
|
||||
}
|
||||
|
||||
return {
|
||||
recipeImage,
|
||||
recipeSmallImage,
|
||||
recipeTinyImage,
|
||||
recipeTimelineEventImage,
|
||||
recipeTimelineEventSmallImage,
|
||||
recipeTimelineEventTinyImage,
|
||||
recipeAssetPath,
|
||||
};
|
||||
};
|
||||
@@ -1,28 +0,0 @@
|
||||
import { alert } from "~/composables/use-toast";
|
||||
import { useGlobalI18n } from "~/composables/use-global-i18n";
|
||||
|
||||
export function useDownloader() {
|
||||
function download(url: string, filename: string) {
|
||||
useFetch(url, {
|
||||
method: "GET",
|
||||
responseType: "blob",
|
||||
onResponse({ response }) {
|
||||
if (!response.ok) {
|
||||
console.error("Download failed", response);
|
||||
const i18n = useGlobalI18n();
|
||||
alert.error(i18n.t("events.something-went-wrong"));
|
||||
return;
|
||||
}
|
||||
|
||||
const url = window.URL.createObjectURL(new Blob([response._data]));
|
||||
const link = document.createElement("a");
|
||||
link.href = url;
|
||||
link.setAttribute("download", filename);
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
return download;
|
||||
}
|
||||
Reference in New Issue
Block a user