mirror of
https://github.com/vercel/commerce.git
synced 2025-07-26 19:51:23 +00:00
fix code
This commit is contained in:
@@ -10,7 +10,8 @@ import type {
|
||||
GetProductOperation,
|
||||
} from '../types/product'
|
||||
import type {
|
||||
GetAllBlogsOperation
|
||||
GetAllBlogsOperation,
|
||||
GetFeaturedOperation
|
||||
} from '../types/blogs'
|
||||
import type { APIProvider, CommerceAPI } from '.'
|
||||
import { GetAllCollectionsOperation } from '@commerce/types/collection';
|
||||
@@ -30,7 +31,8 @@ export const OPERATIONS = [
|
||||
'getProduct',
|
||||
'getAllFacets',
|
||||
'getAllCollections',
|
||||
'getAllBlogs'
|
||||
'getAllBlogs',
|
||||
'getFeaturedBlog'
|
||||
] as const
|
||||
|
||||
export const defaultOperations = OPERATIONS.reduce((ops, k) => {
|
||||
@@ -149,6 +151,22 @@ export type Operations<P extends APIProvider> = {
|
||||
|
||||
|
||||
getAllBlogs: {
|
||||
<T extends GetFeaturedOperation>(opts: {
|
||||
variables?: T['variables']
|
||||
config?: P['config']
|
||||
preview?: boolean
|
||||
}): Promise<T['data']>
|
||||
|
||||
<T extends GetFeaturedOperation>(
|
||||
opts: {
|
||||
variables?: T['variables']
|
||||
config?: P['config']
|
||||
preview?: boolean
|
||||
} & OperationOptions
|
||||
): Promise<T['data']>
|
||||
}
|
||||
|
||||
getFeaturedBlog: {
|
||||
<T extends GetAllBlogsOperation>(opts: {
|
||||
variables?: T['variables']
|
||||
config?: P['config']
|
||||
@@ -165,6 +183,7 @@ export type Operations<P extends APIProvider> = {
|
||||
}
|
||||
|
||||
|
||||
|
||||
getProduct: {
|
||||
<T extends GetProductOperation>(opts: {
|
||||
variables: T['variables']
|
||||
|
@@ -19,8 +19,14 @@ export type GetAllBlogsOperation<T extends BlogsType = BlogsType> = {
|
||||
variables: {
|
||||
take?: number
|
||||
skip?: number
|
||||
sort?: SearchResultSortParameter
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
export type GetFeaturedOperation<T extends BlogsType = BlogsType> = {
|
||||
data: { items: T['items'][] }
|
||||
variables: {
|
||||
take?: number
|
||||
skip?: number
|
||||
}
|
||||
}
|
@@ -12,6 +12,7 @@ import getSiteInfo from './operations/get-site-info'
|
||||
import login from './operations/login'
|
||||
import fetchGraphqlApi from './utils/fetch-graphql-api'
|
||||
import getAllBlogs from './operations/get-all-blogs'
|
||||
import getFeaturedBlog from './operations/get-featured-blog'
|
||||
|
||||
export interface VendureConfig extends CommerceAPIConfig {}
|
||||
|
||||
@@ -44,7 +45,8 @@ const operations = {
|
||||
getProduct,
|
||||
getAllFacets,
|
||||
getAllCollections,
|
||||
getAllBlogs
|
||||
getAllBlogs,
|
||||
getFeaturedBlog
|
||||
}
|
||||
|
||||
export const provider = { config, operations }
|
||||
|
@@ -3,7 +3,11 @@ 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 type BlogVariables = {
|
||||
excludeBlogIds?: string[],
|
||||
take?: number,
|
||||
skip?:number
|
||||
}
|
||||
|
||||
export default function getAllBlogsOperation({
|
||||
commerce,
|
||||
@@ -24,8 +28,10 @@ export default function getAllBlogsOperation({
|
||||
config?: Partial<VendureConfig>
|
||||
preview?: boolean
|
||||
} = {}): Promise<{ blogs: GetAllBlogsQuery | any[] ,totalItems?:number }> {
|
||||
console.log(vars.excludeBlogIds)
|
||||
const config = commerce.getConfig(cfg)
|
||||
const variables = {
|
||||
excludeBlogIds: vars.excludeBlogIds,
|
||||
options: {
|
||||
take: vars.take,
|
||||
skip: vars.skip,
|
||||
@@ -34,7 +40,6 @@ export default function getAllBlogsOperation({
|
||||
const { data } = await config.fetch<GetAllBlogsQuery>(query, {
|
||||
variables,
|
||||
})
|
||||
|
||||
return {
|
||||
blogs: data?.blogs?.items?.map((val:BlogList)=>({
|
||||
id: val.id,
|
||||
@@ -42,7 +47,11 @@ export default function getAllBlogsOperation({
|
||||
imageSrc: val.featuredAsset?.preview ?? null,
|
||||
slug: val.translations[0]?.slug,
|
||||
description: val.translations[0]?.description,
|
||||
isHidden: val.isHidden
|
||||
isPublish: val.isPublish,
|
||||
isFeatured: val.isFeatured,
|
||||
authorName: val.authorName,
|
||||
authorAvatarAsset : val.authorAvatarAsset?.preview,
|
||||
createdAt: val.createdAt
|
||||
})),
|
||||
totalItems: data?.blogs?.totalItems || null
|
||||
}
|
||||
|
56
framework/vendure/api/operations/get-featured-blog.ts
Normal file
56
framework/vendure/api/operations/get-featured-blog.ts
Normal file
@@ -0,0 +1,56 @@
|
||||
import { OperationContext } from '@commerce/api/operations'
|
||||
import { Provider, VendureConfig } from '..'
|
||||
import { GetFeaturedBlogQuery,BlogList } from '../../schema'
|
||||
import { getFeatuedBlogQuery } from '../../utils/queries/get-featued-query'
|
||||
|
||||
export type BlogVariables = {
|
||||
take?: number,
|
||||
skip?:number
|
||||
}
|
||||
|
||||
export default function getFeaturedBlogOperation({
|
||||
commerce,
|
||||
}: OperationContext<Provider>) {
|
||||
async function getFeaturedBlog(opts?: {
|
||||
variables?: BlogVariables
|
||||
config?: Partial<VendureConfig>
|
||||
preview?: boolean
|
||||
}): Promise<{ featuredBlogs: GetFeaturedBlogQuery,totalItems:number }>
|
||||
|
||||
async function getFeaturedBlog({
|
||||
query = getFeatuedBlogQuery,
|
||||
variables: { ...vars } = {},
|
||||
config: cfg,
|
||||
}: {
|
||||
query?: string
|
||||
variables?: BlogVariables
|
||||
config?: Partial<VendureConfig>
|
||||
preview?: boolean
|
||||
} = {}): Promise<{ featuredBlogs: GetFeaturedBlogQuery | any[] ,totalItems?:number }> {
|
||||
const config = commerce.getConfig(cfg)
|
||||
const variables = {
|
||||
options: {
|
||||
take: vars.take,
|
||||
},
|
||||
}
|
||||
const { data } = await config.fetch<GetFeaturedBlogQuery>(query, {
|
||||
variables,
|
||||
})
|
||||
return {
|
||||
featuredBlogs: data?.featuredBlogs?.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,
|
||||
isPublish: val.isPublish,
|
||||
isFeatured: val.isFeatured,
|
||||
authorName: val.authorName,
|
||||
authorAvatarAsset : val.authorAvatarAsset?.preview,
|
||||
createdAt: val.createdAt
|
||||
}))
|
||||
}
|
||||
}
|
||||
|
||||
return getFeaturedBlog
|
||||
}
|
12
framework/vendure/schema.d.ts
vendored
12
framework/vendure/schema.d.ts
vendored
@@ -2317,11 +2317,12 @@ export type BlogList = Node &{
|
||||
createdAt: DateTime!
|
||||
updatedAt: DateTime!
|
||||
featuredAsset?: Maybe<Asset>
|
||||
isHidden:Boolean
|
||||
isPublish:Boolean
|
||||
translations: Array<BlogTranslation>
|
||||
authorName: Scalars['String']
|
||||
authorAvatarAsset:Array<Asset>
|
||||
authorAvatarAsset:Asset
|
||||
relevantProducts: Product
|
||||
isFeatured: Boolean
|
||||
}
|
||||
|
||||
export type BlogTranslation = {
|
||||
@@ -2342,8 +2343,15 @@ export type GetAllBlogsQuery = PaginatedList & {
|
||||
'totalItems'
|
||||
}
|
||||
}
|
||||
export type GetFeaturedBlogQuery = PaginatedList & {
|
||||
featuredBlogs: { __typename?: 'BlogList' } & {
|
||||
items: Array<{ __typename?: 'Blog' } & BlogList!>,
|
||||
'totalItems'
|
||||
}
|
||||
}
|
||||
|
||||
export type QueryBlogs = {
|
||||
excludeBlogIds:Array,
|
||||
options: BlogListOptions
|
||||
}
|
||||
|
||||
|
@@ -1,20 +1,26 @@
|
||||
export const getAllBlogsQuery = /* GraphQL */ `
|
||||
query GetBlogs ($options: BlogListOptions){
|
||||
blogs(options: $options){
|
||||
totalItems
|
||||
items {
|
||||
id
|
||||
isHidden
|
||||
featuredAsset {
|
||||
preview
|
||||
}
|
||||
translations {
|
||||
title
|
||||
slug
|
||||
description
|
||||
content
|
||||
}
|
||||
}
|
||||
query GetBlogs($excludeBlogIds: [ID]!, $options: BlogListOptions) {
|
||||
blogs(excludeBlogIds: $excludeBlogIds, options: $options) {
|
||||
totalItems
|
||||
items {
|
||||
id
|
||||
isPublish
|
||||
isFeatured
|
||||
authorName
|
||||
createdAt
|
||||
authorAvatarAsset{
|
||||
preview
|
||||
}
|
||||
featuredAsset {
|
||||
preview
|
||||
}
|
||||
translations {
|
||||
title
|
||||
slug
|
||||
description
|
||||
content
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
||||
|
25
framework/vendure/utils/queries/get-featued-query.ts
Normal file
25
framework/vendure/utils/queries/get-featued-query.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
export const getFeatuedBlogQuery = /* GraphQL */ `
|
||||
query GetFeaturedBlog($options: BlogListOptions) {
|
||||
featuredBlogs( options: $options){
|
||||
items {
|
||||
id
|
||||
isPublish
|
||||
isFeatured
|
||||
authorName
|
||||
createdAt
|
||||
authorAvatarAsset{
|
||||
preview
|
||||
}
|
||||
featuredAsset {
|
||||
preview
|
||||
}
|
||||
translations {
|
||||
title
|
||||
slug
|
||||
description
|
||||
content
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
Reference in New Issue
Block a user