mirror of
https://github.com/vercel/commerce.git
synced 2025-07-26 19:51:23 +00:00
feat: get-blog-list
This commit is contained in:
52
framework/vendure/api/operations/get-all-blogs.ts
Normal file
52
framework/vendure/api/operations/get-all-blogs.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { OperationContext } from '@commerce/api/operations'
|
||||
import { Provider, VendureConfig } from '..'
|
||||
import { GetAllBlogsQuery,BlogList } from '../../schema'
|
||||
import { getAllBlogsQuery } from '../../utils/queries/get-all-blog-query'
|
||||
|
||||
export type BlogVariables = { take?: number,skip?:number }
|
||||
|
||||
export default function getAllBlogsOperation({
|
||||
commerce,
|
||||
}: OperationContext<Provider>) {
|
||||
async function getAllBlogs(opts?: {
|
||||
variables?: BlogVariables
|
||||
config?: Partial<VendureConfig>
|
||||
preview?: boolean
|
||||
}): Promise<{ blogs: GetAllBlogsQuery,totalItems:number }>
|
||||
|
||||
async function getAllBlogs({
|
||||
query = getAllBlogsQuery,
|
||||
variables: { ...vars } = {},
|
||||
config: cfg,
|
||||
}: {
|
||||
query?: string
|
||||
variables?: BlogVariables
|
||||
config?: Partial<VendureConfig>
|
||||
preview?: boolean
|
||||
} = {}): Promise<{ blogs: GetAllBlogsQuery | any[] ,totalItems?:number }> {
|
||||
const config = commerce.getConfig(cfg)
|
||||
const variables = {
|
||||
options: {
|
||||
take: vars.take,
|
||||
skip: vars.skip,
|
||||
},
|
||||
}
|
||||
const { data } = await config.fetch<GetAllBlogsQuery>(query, {
|
||||
variables,
|
||||
})
|
||||
|
||||
return {
|
||||
blogs: data?.blogs?.items?.map((val:BlogList)=>({
|
||||
id: val.id,
|
||||
title: val.translations[0]?.title,
|
||||
imageSrc: val.featuredAsset?.preview ?? null,
|
||||
slug: val.translations[0]?.slug,
|
||||
description: val.translations[0]?.description,
|
||||
isHidden: val.isHidden
|
||||
})),
|
||||
totalItems: data?.blogs?.totalItems || null
|
||||
}
|
||||
}
|
||||
|
||||
return getAllBlogs
|
||||
}
|
Reference in New Issue
Block a user