refactor: 将数据获取从app端移至server端
- 调整数据获取位置以提升安全性 - 对于后端状态为Archived的数据,通过server控制不进行获取
This commit is contained in:
@ -1,16 +1,17 @@
|
||||
import GetCompanyProfile from '@/graphql/companyProfile.graphql?raw';
|
||||
|
||||
export const useCompanyProfile = () => {
|
||||
const { $directus } = useNuxtApp();
|
||||
const { getDirectusLocale } = useLocalizations();
|
||||
const locale = getDirectusLocale();
|
||||
|
||||
return useAsyncData(`company-profile-${locale}`, async () => {
|
||||
return await $directus.query<{ company_profile: CompanyProfile }>(
|
||||
GetCompanyProfile,
|
||||
{
|
||||
locale: locale,
|
||||
}
|
||||
);
|
||||
try {
|
||||
const data = await $fetch(`/api/cms/companyProfile`, {
|
||||
headers: { 'x-locale': locale },
|
||||
});
|
||||
|
||||
return data;
|
||||
} catch (error) {
|
||||
logger.error('Error fetching company profile: ', error);
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@ -1,16 +1,17 @@
|
||||
import GetContactInfo from '@/graphql/contactInfo.graphql?raw';
|
||||
|
||||
export const useContactInfo = () => {
|
||||
const { $directus } = useNuxtApp();
|
||||
const { getDirectusLocale } = useLocalizations();
|
||||
const locale = getDirectusLocale();
|
||||
|
||||
return useAsyncData(`contact-info-${locale}`, async () => {
|
||||
return await $directus.query<{ contact_info: ContactInfo }>(
|
||||
GetContactInfo,
|
||||
{
|
||||
locale: locale,
|
||||
}
|
||||
);
|
||||
try {
|
||||
const data = await $fetch('/api/cms/contactInfo', {
|
||||
headers: { 'x-locale': locale },
|
||||
});
|
||||
|
||||
return data;
|
||||
} catch (error) {
|
||||
logger.error('Error fetching contact info: ', error);
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@ -1,16 +1,17 @@
|
||||
import GetDocumentList from '@/graphql/documentList.graphql?raw';
|
||||
|
||||
export const useDocumentList = () => {
|
||||
const { $directus } = useNuxtApp();
|
||||
const { getDirectusLocale } = useLocalizations();
|
||||
const locale = getDirectusLocale();
|
||||
|
||||
return useAsyncData(`document-list-${locale}`, async () => {
|
||||
return await $directus.query<{ product_documents: ProductDocument[] }>(
|
||||
GetDocumentList,
|
||||
{
|
||||
locale: locale,
|
||||
}
|
||||
);
|
||||
try {
|
||||
const data = $fetch(`/api/cms/documentList`, {
|
||||
headers: { 'x-locale': locale },
|
||||
});
|
||||
|
||||
return data;
|
||||
} catch (error) {
|
||||
logger.error('Error fetching document list:', error);
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@ -1,13 +1,17 @@
|
||||
import GetHomepage from '@/graphql/homepage.graphql?raw';
|
||||
|
||||
export const useHomepage = () => {
|
||||
const { $directus } = useNuxtApp();
|
||||
const { getDirectusLocale } = useLocalizations();
|
||||
const locale = getDirectusLocale();
|
||||
|
||||
return useAsyncData(`homepage-${locale}`, async () => {
|
||||
return await $directus.query<{ homepage: Homepage }>(GetHomepage, {
|
||||
locale: locale,
|
||||
});
|
||||
try {
|
||||
const data = $fetch(`/api/cms/homepage`, {
|
||||
headers: { 'x-locale': locale },
|
||||
});
|
||||
|
||||
return data;
|
||||
} catch (error) {
|
||||
logger.error('Error fetching homepage:', error);
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@ -1,15 +1,17 @@
|
||||
import GetProduct from '@/graphql/product.graphql?raw';
|
||||
|
||||
export const useProduct = (id: string) => {
|
||||
const { $directus } = useNuxtApp();
|
||||
|
||||
const { getDirectusLocale } = useLocalizations();
|
||||
const locale = getDirectusLocale();
|
||||
|
||||
return useAsyncData(`product-${id}-${locale}`, async () => {
|
||||
return await $directus.query<{ products_by_id: Product }>(GetProduct, {
|
||||
id: id,
|
||||
locale: locale,
|
||||
});
|
||||
try {
|
||||
const data = await $fetch(`/api/cms/product/${id}`, {
|
||||
headers: { 'x-locale': locale },
|
||||
});
|
||||
|
||||
return data;
|
||||
} catch (error) {
|
||||
logger.error('Error fetching product: ', error);
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@ -1,14 +1,17 @@
|
||||
import GetProductList from '@/graphql/productList.graphql?raw';
|
||||
|
||||
export const useProductList = () => {
|
||||
const { $directus } = useNuxtApp();
|
||||
|
||||
const { getDirectusLocale } = useLocalizations();
|
||||
const locale = getDirectusLocale();
|
||||
|
||||
return useAsyncData(`product-list-${locale}`, async () => {
|
||||
return await $directus.query<{ products: Product[] }>(GetProductList, {
|
||||
locale: locale,
|
||||
});
|
||||
try {
|
||||
const data = await $fetch(`/api/cms/productList`, {
|
||||
headers: { 'x-locale': locale },
|
||||
});
|
||||
|
||||
return data;
|
||||
} catch (error) {
|
||||
logger.error('Error fetching product list: ', error);
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@ -1,14 +1,17 @@
|
||||
import GetQuestionList from '@/graphql/questionList.graphql?raw';
|
||||
|
||||
export const useQuestionList = () => {
|
||||
const { $directus } = useNuxtApp();
|
||||
|
||||
const { getDirectusLocale } = useLocalizations();
|
||||
const locale = getDirectusLocale();
|
||||
|
||||
return useAsyncData(`question-list-${locale}`, async () => {
|
||||
return await $directus.query<{ questions: Question[] }>(GetQuestionList, {
|
||||
locale: locale,
|
||||
});
|
||||
try {
|
||||
const data = $fetch(`/api/cms/questionList`, {
|
||||
headers: { 'x-locale': locale },
|
||||
});
|
||||
|
||||
return data;
|
||||
} catch (error) {
|
||||
logger.error('Error fetching question list:', error);
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@ -1,14 +1,17 @@
|
||||
import GetSolution from '@/graphql/solution.graphql?raw';
|
||||
|
||||
export const useSolution = (id: string) => {
|
||||
const { $directus } = useNuxtApp();
|
||||
const { getDirectusLocale } = useLocalizations();
|
||||
const locale = getDirectusLocale();
|
||||
|
||||
return useAsyncData(`solution-${id}-${locale}`, async () => {
|
||||
return await $directus.query<{ solutions_by_id: Solution }>(GetSolution, {
|
||||
id: id,
|
||||
locale: locale,
|
||||
});
|
||||
try {
|
||||
const data = await $fetch(`/api/cms/solution/${id}`, {
|
||||
headers: { 'x-locale': locale },
|
||||
});
|
||||
|
||||
return data;
|
||||
} catch (error) {
|
||||
logger.error('Error fetching solution: ', error);
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
@ -1,13 +1,17 @@
|
||||
import GetSolutionList from '@/graphql/solutionList.graphql?raw';
|
||||
|
||||
export const useSolutionList = () => {
|
||||
const { $directus } = useNuxtApp();
|
||||
const { getDirectusLocale } = useLocalizations();
|
||||
const locale = getDirectusLocale();
|
||||
|
||||
return useAsyncData(`solution-list-${locale}`, async () => {
|
||||
return await $directus.query<{ solutions: Solution[] }>(GetSolutionList, {
|
||||
locale: locale,
|
||||
});
|
||||
try {
|
||||
const data = $fetch(`/api/cms/solutionList`, {
|
||||
headers: { 'x-locale': locale },
|
||||
});
|
||||
|
||||
return data;
|
||||
} catch (error) {
|
||||
logger.error('Error fetching solution list:', error);
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user