mirror of
https://github.com/vercel/commerce.git
synced 2025-05-17 23:16:59 +00:00
Add get-site-info operator
Signed-off-by: Chloe <pinkcloudvnn@gmail.com>
This commit is contained in:
parent
31a283a832
commit
8eda86ae82
5
.vscode/settings.json
vendored
5
.vscode/settings.json
vendored
@ -1,4 +1,5 @@
|
|||||||
{
|
{
|
||||||
"editor.defaultFormatter": "esbenp.prettier-vscode",
|
"editor.defaultFormatter": "esbenp.prettier-vscode",
|
||||||
"editor.formatOnSave": true
|
"editor.formatOnSave": true,
|
||||||
}
|
"typescript.tsdk": "node_modules/typescript/lib"
|
||||||
|
}
|
33
packages/opencommerce/schema.d.ts
vendored
33
packages/opencommerce/schema.d.ts
vendored
@ -8195,3 +8195,36 @@ export type GetProductBySlugQuery = {
|
|||||||
} | null
|
} | null
|
||||||
} | null
|
} | null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type GetTagsQueryVariables = Exact<{
|
||||||
|
first: Scalars['ConnectionLimitInt']
|
||||||
|
shopId: Scalars['ID']
|
||||||
|
}>
|
||||||
|
|
||||||
|
export type GetTagsQuery = {
|
||||||
|
__typename?: 'Query'
|
||||||
|
tags: {
|
||||||
|
__typename?: 'TagConnection'
|
||||||
|
edges: Array<{
|
||||||
|
__typename?: 'TagEdge'
|
||||||
|
node: {
|
||||||
|
__typename?: 'Tag'
|
||||||
|
_id: string
|
||||||
|
displayTitle: string | null
|
||||||
|
slug: string | null
|
||||||
|
} | null
|
||||||
|
} | null> | null
|
||||||
|
} | null
|
||||||
|
}
|
||||||
|
|
||||||
|
export type GetAllProductVendorsQueryVariables = Exact<{
|
||||||
|
shopIds: Array<InputMaybe<Scalars['ID']>> | InputMaybe<Scalars['ID']>
|
||||||
|
}>
|
||||||
|
|
||||||
|
export type GetAllProductVendorsQuery = {
|
||||||
|
__typename?: 'Query'
|
||||||
|
vendors: {
|
||||||
|
__typename?: 'VendorConnection'
|
||||||
|
nodes: Array<{ __typename?: 'Vendor'; name: string | null } | null> | null
|
||||||
|
} | null
|
||||||
|
}
|
||||||
|
@ -1,42 +1,63 @@
|
|||||||
import { OperationContext } from '@vercel/commerce/api/operations'
|
import {
|
||||||
import { Category } from '@vercel/commerce/types/site'
|
OperationContext,
|
||||||
import type { OpenCommerceConfig } from '../index'
|
OperationOptions,
|
||||||
|
} from '@vercel/commerce/api/operations'
|
||||||
|
import {
|
||||||
|
GetTagsQuery,
|
||||||
|
GetAllProductVendorsQuery,
|
||||||
|
GetTagsQueryVariables,
|
||||||
|
GetAllProductVendorsQueryVariables,
|
||||||
|
} from '../../../schema'
|
||||||
|
import getTagsQuery from '../queries/get-tags-query'
|
||||||
|
import { GetSiteInfoOperation, OCCategory } from '../../types/site'
|
||||||
|
import { normalizeCategory, normalizeVendors } from '../../utils/normalize'
|
||||||
|
import type { OpenCommerceConfig, Provider } from '..'
|
||||||
|
import filterEdges from '../utils/filter-edges'
|
||||||
|
import getAllProductVendors from '../queries/get-vendors-query'
|
||||||
|
|
||||||
export type GetSiteInfoResult<
|
export default function getSiteInfoOperation({
|
||||||
T extends { categories: any[]; brands: any[] } = {
|
commerce,
|
||||||
categories: Category[]
|
}: OperationContext<Provider>) {
|
||||||
brands: any[]
|
async function getSiteInfo<T extends GetSiteInfoOperation>(opts?: {
|
||||||
}
|
config?: Partial<OpenCommerceConfig>
|
||||||
> = T
|
preview?: boolean
|
||||||
|
}): Promise<T['data']>
|
||||||
|
|
||||||
export default function getSiteInfoOperation({}: OperationContext<any>) {
|
async function getSiteInfo<T extends GetSiteInfoOperation>(
|
||||||
function getSiteInfo({
|
opts: {
|
||||||
query,
|
config?: Partial<OpenCommerceConfig>
|
||||||
variables,
|
preview?: boolean
|
||||||
|
} & OperationOptions
|
||||||
|
): Promise<T['data']>
|
||||||
|
|
||||||
|
async function getSiteInfo<T extends GetSiteInfoOperation>({
|
||||||
config: cfg,
|
config: cfg,
|
||||||
}: {
|
}: {
|
||||||
query?: string
|
query?: string
|
||||||
variables?: any
|
|
||||||
config?: Partial<OpenCommerceConfig>
|
config?: Partial<OpenCommerceConfig>
|
||||||
preview?: boolean
|
preview?: boolean
|
||||||
} = {}): Promise<GetSiteInfoResult> {
|
} = {}): Promise<T['data']> {
|
||||||
return Promise.resolve({
|
const { fetch, shopId } = commerce.getConfig(cfg)
|
||||||
categories: [
|
|
||||||
{
|
const [categoriesResponse, vendorsResponse] = await Promise.all([
|
||||||
id: 'new-arrivals',
|
await fetch<GetTagsQuery, GetTagsQueryVariables>(getTagsQuery, {
|
||||||
name: 'New Arrivals',
|
variables: { first: 250, shopId },
|
||||||
slug: 'new-arrivals',
|
}),
|
||||||
path: '/new-arrivals',
|
await fetch<
|
||||||
},
|
GetAllProductVendorsQuery,
|
||||||
{
|
GetAllProductVendorsQueryVariables
|
||||||
id: 'featured',
|
>(getAllProductVendors, { variables: { shopIds: [shopId] } }),
|
||||||
name: 'Featured',
|
])
|
||||||
slug: 'featured',
|
|
||||||
path: '/featured',
|
const categories = filterEdges(categoriesResponse.data.tags?.edges).map(
|
||||||
},
|
(edge) => normalizeCategory(edge.node! as OCCategory)
|
||||||
],
|
)
|
||||||
brands: [],
|
|
||||||
})
|
const brands = [
|
||||||
|
...new Set(filterEdges(vendorsResponse.data.vendors?.nodes)),
|
||||||
|
].map(normalizeVendors)
|
||||||
|
|
||||||
|
return { categories, brands }
|
||||||
}
|
}
|
||||||
|
|
||||||
return getSiteInfo
|
return getSiteInfo
|
||||||
|
14
packages/opencommerce/src/api/queries/get-tags-query.ts
Normal file
14
packages/opencommerce/src/api/queries/get-tags-query.ts
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
const getTagsQuery = /* GraphQL */ `
|
||||||
|
query getTags($first: ConnectionLimitInt!, $shopId: ID!) {
|
||||||
|
tags(first: $first, shopId: $shopId) {
|
||||||
|
edges {
|
||||||
|
node {
|
||||||
|
_id
|
||||||
|
displayTitle
|
||||||
|
slug
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
export default getTagsQuery
|
10
packages/opencommerce/src/api/queries/get-vendors-query.ts
Normal file
10
packages/opencommerce/src/api/queries/get-vendors-query.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
const getAllProductVendors = /* GraphQL */ `
|
||||||
|
query getAllProductVendors($shopIds: [ID]!) {
|
||||||
|
vendors(shopIds: $shopIds) {
|
||||||
|
nodes {
|
||||||
|
name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
`
|
||||||
|
export default getAllProductVendors
|
15
packages/opencommerce/src/types/site.ts
Normal file
15
packages/opencommerce/src/types/site.ts
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import { Vendor as QueryVender, TagEdge } from '../../schema'
|
||||||
|
|
||||||
|
export * from '@vercel/commerce/types/site'
|
||||||
|
|
||||||
|
export type OCCategory = NonNullable<NonNullable<TagEdge>['node']>
|
||||||
|
|
||||||
|
export type OCVendor = QueryVender
|
||||||
|
|
||||||
|
export type Vendor = {
|
||||||
|
node: {
|
||||||
|
entityId: string
|
||||||
|
name: string
|
||||||
|
path: string
|
||||||
|
}
|
||||||
|
}
|
@ -4,6 +4,7 @@ import type {
|
|||||||
ProductOptionValues,
|
ProductOptionValues,
|
||||||
ProductVariant,
|
ProductVariant,
|
||||||
} from '../types/product'
|
} from '../types/product'
|
||||||
|
import { OCCategory, Category, Vendor, OCVendor } from '../types/site'
|
||||||
import {
|
import {
|
||||||
CatalogItemProduct,
|
CatalogItemProduct,
|
||||||
CatalogProduct,
|
CatalogProduct,
|
||||||
@ -205,3 +206,22 @@ function colorizeProductOptionValue(
|
|||||||
}
|
}
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function normalizeCategory(category: OCCategory): Category {
|
||||||
|
return {
|
||||||
|
id: category._id,
|
||||||
|
name: category.displayTitle ?? '',
|
||||||
|
slug: category.slug ?? '',
|
||||||
|
path: category.slug ? `/${category.slug}` : '',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export function normalizeVendors({ name }: OCVendor): Vendor {
|
||||||
|
return {
|
||||||
|
node: {
|
||||||
|
entityId: name ?? '',
|
||||||
|
name: name ?? '',
|
||||||
|
path: `brands/${name}`,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user