All checks were successful
deploy to server / build-and-deploy (push) Successful in 3m15s
- 将逻辑转移到Server端后,简化前端逻辑
56 lines
1.3 KiB
TypeScript
56 lines
1.3 KiB
TypeScript
import {
|
|
toProductView,
|
|
toProductListView,
|
|
} from '~~/server/mappers/productMapper';
|
|
|
|
export const productService = {
|
|
async getProductList(locale: string) {
|
|
const query = await loadAssetAsString(
|
|
'assets/server',
|
|
'graphql/productList.graphql'
|
|
);
|
|
if (!query) {
|
|
throw createError({
|
|
statusCode: 500,
|
|
message: 'GraphQL query not found',
|
|
});
|
|
}
|
|
|
|
const data = await directus.query<{ products: Product[] }>(query, {
|
|
locale: locale,
|
|
});
|
|
|
|
const products = data?.products || [];
|
|
|
|
return products.map((product) => toProductListView(product));
|
|
},
|
|
async getProductById(id: string, locale: string) {
|
|
const query = await loadAssetAsString(
|
|
'assets/server',
|
|
'graphql/product.graphql'
|
|
);
|
|
if (!query) {
|
|
throw createError({
|
|
statusCode: 500,
|
|
message: 'GraphQL query not found',
|
|
});
|
|
}
|
|
|
|
const data = await directus.query<{ products_by_id: Product }>(query, {
|
|
id: id,
|
|
locale: locale,
|
|
});
|
|
|
|
const product = data?.products_by_id;
|
|
|
|
if (!product || product.status === 'archived') {
|
|
throw createError({
|
|
statusCode: 404,
|
|
statusMessage: 'Product not found',
|
|
});
|
|
}
|
|
|
|
return toProductView(product);
|
|
},
|
|
};
|