mirror of
https://github.com/vercel/commerce.git
synced 2025-07-26 03:31:23 +00:00
Merge branch 'feature/m2-featured-product' of github.com:KieIO/grocery-vercel-commerce into feature/m2-add-product-to-cart
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import { GetAllFacetsOperation } from './../types/facet';
|
||||
import type { ServerResponse } from 'http'
|
||||
import type { LoginOperation } from '../types/login'
|
||||
import type { GetAllPagesOperation, GetPageOperation } from '../types/page'
|
||||
@@ -23,6 +24,8 @@ export const OPERATIONS = [
|
||||
'getAllProductPaths',
|
||||
'getAllProducts',
|
||||
'getProduct',
|
||||
'getAllFacets',
|
||||
|
||||
] as const
|
||||
|
||||
export const defaultOperations = OPERATIONS.reduce((ops, k) => {
|
||||
@@ -154,8 +157,27 @@ export type Operations<P extends APIProvider> = {
|
||||
} & OperationOptions
|
||||
): Promise<T['data']>
|
||||
}
|
||||
|
||||
getAllFacets: {
|
||||
<T extends GetAllFacetsOperation>(opts: {
|
||||
variables?: T['variables']
|
||||
config?: P['config']
|
||||
preview?: boolean
|
||||
}): Promise<T['data']>
|
||||
|
||||
<T extends GetAllFacetsOperation>(
|
||||
opts: {
|
||||
variables?: T['variables']
|
||||
config?: P['config']
|
||||
preview?: boolean
|
||||
} & OperationOptions
|
||||
): Promise<T['data']>
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
export type APIOperations<P extends APIProvider> = {
|
||||
[K in keyof Operations<P>]?: (ctx: OperationContext<P>) => Operations<P>[K]
|
||||
}
|
||||
|
@@ -15,6 +15,7 @@ Adding a commerce provider means adding a new folder in `framework` with a folde
|
||||
- useSearch
|
||||
- getProduct
|
||||
- getAllProducts
|
||||
- getAllFacets
|
||||
- `wishlist`
|
||||
- useWishlist
|
||||
- useAddItem
|
||||
|
52
framework/commerce/types/facet.ts
Normal file
52
framework/commerce/types/facet.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { FacetValue } from './../../vendure/schema.d';
|
||||
|
||||
export type Facet = {
|
||||
id: string
|
||||
name: string
|
||||
code: string
|
||||
values: FacetValue[]
|
||||
}
|
||||
|
||||
export type SearchFacetsBody = {
|
||||
search?: string
|
||||
sort?: string
|
||||
locale?: string
|
||||
}
|
||||
|
||||
export type FacetTypes = {
|
||||
facet: Facet
|
||||
searchBody: SearchFacetsBody
|
||||
}
|
||||
|
||||
export type SearchFacetsHook<T extends FacetTypes = FacetTypes> = {
|
||||
data: {
|
||||
facets: T['facet'][]
|
||||
found: boolean
|
||||
}
|
||||
body: T['searchBody']
|
||||
input: T['searchBody']
|
||||
fetcherInput: T['searchBody']
|
||||
}
|
||||
|
||||
export type FacetsSchema<T extends FacetTypes = FacetTypes> = {
|
||||
endpoint: {
|
||||
options: {}
|
||||
handlers: {
|
||||
getFacets: SearchFacetsHook<T>
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
export type GetAllFacetsOperation<T extends FacetTypes = FacetTypes> = {
|
||||
data: { facets: T['facet'][] }
|
||||
variables: {
|
||||
ids?: string[]
|
||||
first?: number
|
||||
}
|
||||
}
|
||||
|
||||
export type GetFacetOperation<T extends FacetTypes = FacetTypes> = {
|
||||
data: { facet?: T['facet'] }
|
||||
variables: { code: string; } | { code?: never; }
|
||||
}
|
@@ -1,3 +1,5 @@
|
||||
import { FacetValueFilterInput, LogicalOperator, SearchResultSortParameter } from "@framework/schema"
|
||||
|
||||
export type ProductImage = {
|
||||
url: string
|
||||
alt?: string
|
||||
@@ -40,7 +42,6 @@ export type Product = {
|
||||
slug?: string
|
||||
path?: string
|
||||
images: ProductImage[]
|
||||
variants: ProductVariant[]
|
||||
price: ProductPrice
|
||||
options: ProductOption[]
|
||||
}
|
||||
@@ -79,17 +80,24 @@ export type ProductsSchema<T extends ProductTypes = ProductTypes> = {
|
||||
|
||||
export type GetAllProductPathsOperation<
|
||||
T extends ProductTypes = ProductTypes
|
||||
> = {
|
||||
data: { products: Pick<T['product'], 'path'>[] }
|
||||
variables: { first?: number }
|
||||
}
|
||||
> = {
|
||||
data: { products: Pick<T['product'], 'path'>[] }
|
||||
variables: { first?: number }
|
||||
}
|
||||
|
||||
export type GetAllProductsOperation<T extends ProductTypes = ProductTypes> = {
|
||||
data: { products: T['product'][] }
|
||||
variables: {
|
||||
relevance?: 'featured' | 'best_selling' | 'newest'
|
||||
ids?: string[]
|
||||
first?: number
|
||||
term?: String
|
||||
facetValueIds?: string[]
|
||||
facetValueOperator?: LogicalOperator
|
||||
facetValueFilters?: FacetValueFilterInput[]
|
||||
collectionId?: string
|
||||
collectionSlug?: string
|
||||
groupByProduct?: Boolean
|
||||
take?: number
|
||||
skip?: number
|
||||
sort?: SearchResultSortParameter
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -1,15 +1,16 @@
|
||||
import type { APIProvider, CommerceAPIConfig } from '@commerce/api'
|
||||
import type { CommerceAPIConfig } from '@commerce/api'
|
||||
import { CommerceAPI, getCommerceApi as commerceApi } from '@commerce/api'
|
||||
import fetchGraphqlApi from './utils/fetch-graphql-api'
|
||||
|
||||
import login from './operations/login'
|
||||
import getAllFacets from './operations/get-all-facets'
|
||||
import getAllPages from './operations/get-all-pages'
|
||||
import getPage from './operations/get-page'
|
||||
import getSiteInfo from './operations/get-site-info'
|
||||
import getCustomerWishlist from './operations/get-customer-wishlist'
|
||||
import getAllProductPaths from './operations/get-all-product-paths'
|
||||
import getAllProducts from './operations/get-all-products'
|
||||
import getCustomerWishlist from './operations/get-customer-wishlist'
|
||||
import getPage from './operations/get-page'
|
||||
import getProduct from './operations/get-product'
|
||||
import getSiteInfo from './operations/get-site-info'
|
||||
import login from './operations/login'
|
||||
import fetchGraphqlApi from './utils/fetch-graphql-api'
|
||||
|
||||
|
||||
export interface VendureConfig extends CommerceAPIConfig {}
|
||||
|
||||
@@ -40,6 +41,7 @@ const operations = {
|
||||
getAllProductPaths,
|
||||
getAllProducts,
|
||||
getProduct,
|
||||
getAllFacets,
|
||||
}
|
||||
|
||||
export const provider = { config, operations }
|
||||
|
45
framework/vendure/api/operations/get-all-facets.ts
Normal file
45
framework/vendure/api/operations/get-all-facets.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
import { OperationContext } from '@commerce/api/operations'
|
||||
import { Facet } from '@commerce/types/facet'
|
||||
import { Provider, VendureConfig } from '../'
|
||||
import { GetAllFacetsQuery } from '../../schema'
|
||||
import { getAllFacetsQuery } from '../../utils/queries/get-all-facets-query'
|
||||
|
||||
export type FacetVariables = { first?: number }
|
||||
|
||||
export default function getAllFacetsOperation({
|
||||
commerce,
|
||||
}: OperationContext<Provider>) {
|
||||
async function getAllFacets(opts?: {
|
||||
variables?: FacetVariables
|
||||
config?: Partial<VendureConfig>
|
||||
preview?: boolean
|
||||
}): Promise<{ facets: Facet[] }>
|
||||
|
||||
async function getAllFacets({
|
||||
query = getAllFacetsQuery,
|
||||
variables: { ...vars } = {},
|
||||
config: cfg,
|
||||
}: {
|
||||
query?: string
|
||||
variables?: FacetVariables
|
||||
config?: Partial<VendureConfig>
|
||||
preview?: boolean
|
||||
} = {}): Promise<{ facets: Facet[] | any[] }> {
|
||||
const config = commerce.getConfig(cfg)
|
||||
const variables = {
|
||||
input: {
|
||||
take: vars.first,
|
||||
groupByFacet: true,
|
||||
},
|
||||
}
|
||||
const { data } = await config.fetch<GetAllFacetsQuery>(query, {
|
||||
variables,
|
||||
})
|
||||
|
||||
return {
|
||||
facets: data.facets.items,
|
||||
}
|
||||
}
|
||||
|
||||
return getAllFacets
|
||||
}
|
@@ -5,7 +5,7 @@ import { normalizeSearchResult } from '../../utils/normalize'
|
||||
import { getAllProductsQuery } from '../../utils/queries/get-all-products-query'
|
||||
import { OperationContext } from '@commerce/api/operations'
|
||||
|
||||
export type ProductVariables = { first?: number }
|
||||
export type ProductVariables = { first?: number, facetValueIds?: string[] }
|
||||
|
||||
export default function getAllProductsOperation({
|
||||
commerce,
|
||||
@@ -30,6 +30,7 @@ export default function getAllProductsOperation({
|
||||
const variables = {
|
||||
input: {
|
||||
take: vars.first,
|
||||
facetValueIds: vars.facetValueIds,
|
||||
groupByProduct: true,
|
||||
},
|
||||
}
|
||||
|
49
framework/vendure/schema.d.ts
vendored
49
framework/vendure/schema.d.ts
vendored
@@ -93,6 +93,10 @@ export type QueryProductsArgs = {
|
||||
options?: Maybe<ProductListOptions>
|
||||
}
|
||||
|
||||
export type QueryFacetsArgs = {
|
||||
options?: Maybe<FacetListOptions>
|
||||
}
|
||||
|
||||
export type QuerySearchArgs = {
|
||||
input: SearchInput
|
||||
}
|
||||
@@ -2727,6 +2731,13 @@ export type ProductListOptions = {
|
||||
filter?: Maybe<ProductFilterParameter>
|
||||
}
|
||||
|
||||
export type FacetListOptions = {
|
||||
skip?: Maybe<Scalars['Int']>
|
||||
take?: Maybe<Scalars['Int']>
|
||||
sort?: Maybe<FacetSortParameter>
|
||||
filter?: Maybe<FacetFilterParameter>
|
||||
}
|
||||
|
||||
export type UpdateOrderItemsResult =
|
||||
| Order
|
||||
| OrderModificationError
|
||||
@@ -2884,6 +2895,23 @@ export type ProductVariantSortParameter = {
|
||||
discountPrice?: Maybe<SortOrder>
|
||||
}
|
||||
|
||||
|
||||
export type FacetFilterParameter = {
|
||||
createdAt?: Maybe<DateOperators>
|
||||
updatedAt?: Maybe<DateOperators>
|
||||
languageCode?: Maybe<StringOperators>
|
||||
name?: Maybe<StringOperators>
|
||||
code?: Maybe<StringOperators>
|
||||
}
|
||||
|
||||
export type FacetSortParameter = {
|
||||
id?: Maybe<SortOrder>
|
||||
createdAt?: Maybe<SortOrder>
|
||||
updatedAt?: Maybe<SortOrder>
|
||||
name?: Maybe<SortOrder>
|
||||
code?: Maybe<SortOrder>
|
||||
}
|
||||
|
||||
export type CustomerFilterParameter = {
|
||||
createdAt?: Maybe<DateOperators>
|
||||
updatedAt?: Maybe<DateOperators>
|
||||
@@ -3008,7 +3036,9 @@ export type CartFragment = { __typename?: 'Order' } & Pick<
|
||||
|
||||
export type SearchResultFragment = { __typename?: 'SearchResult' } & Pick<
|
||||
SearchResult,
|
||||
'productId' | 'productName' | 'description' | 'slug' | 'sku' | 'currencyCode'
|
||||
'productId' | 'sku' | 'productName' | 'description' | 'slug' | 'sku' | 'currencyCode'
|
||||
| 'productAsset' | 'price' | 'priceWithTax' | 'currencyCode'
|
||||
| 'collectionIds'
|
||||
> & {
|
||||
productAsset?: Maybe<
|
||||
{ __typename?: 'SearchResultAsset' } & Pick<
|
||||
@@ -3192,6 +3222,23 @@ export type GetAllProductsQuery = { __typename?: 'Query' } & {
|
||||
}
|
||||
}
|
||||
|
||||
export type GetAllFacetsQuery = { __typename?: 'Query' } & {
|
||||
facets: { __typename?: 'FacetList' } & {
|
||||
items: Array<
|
||||
{ __typename?: 'Facet' } & Pick<
|
||||
Facet,
|
||||
'id' | 'name' | 'code'
|
||||
> & {
|
||||
parent?: Maybe<{ __typename?: 'Facet' } & Pick<Facet, 'id'>>
|
||||
children?: Maybe<
|
||||
Array<{ __typename?: 'Facet' } & Pick<Facet, 'id'>>
|
||||
>
|
||||
}
|
||||
>,
|
||||
'totalItems'
|
||||
}
|
||||
}
|
||||
|
||||
export type ActiveOrderQueryVariables = Exact<{ [key: string]: never }>
|
||||
|
||||
export type ActiveOrderQuery = { __typename?: 'Query' } & {
|
||||
|
@@ -3,18 +3,20 @@ import { Cart } from '@commerce/types/cart'
|
||||
import { CartFragment, SearchResultFragment } from '../schema'
|
||||
|
||||
export function normalizeSearchResult(item: SearchResultFragment): Product {
|
||||
const imageUrl = item.productAsset?.preview ? item.productAsset?.preview + '?w=800&mode=crop' : ''
|
||||
return {
|
||||
id: item.productId,
|
||||
name: item.productName,
|
||||
description: item.description,
|
||||
slug: item.slug,
|
||||
path: item.slug,
|
||||
images: [{ url: item.productAsset?.preview + '?w=800&mode=crop' || '' }],
|
||||
variants: [],
|
||||
images: imageUrl ? [{ url: imageUrl }] : [],
|
||||
price: {
|
||||
// TODO: check price
|
||||
value: (item.priceWithTax as any).min / 100,
|
||||
currencyCode: item.currencyCode,
|
||||
},
|
||||
// TODO: check product option
|
||||
options: [],
|
||||
sku: item.sku,
|
||||
}
|
||||
|
17
framework/vendure/utils/queries/get-all-facets-query.ts
Normal file
17
framework/vendure/utils/queries/get-all-facets-query.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
export const getAllFacetsQuery = /* GraphQL */ `
|
||||
query facets ($options: FacetListOptions) {
|
||||
facets (options: $options){
|
||||
totalItems,
|
||||
items {
|
||||
id
|
||||
name
|
||||
code
|
||||
values {
|
||||
id
|
||||
name
|
||||
code
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
`
|
Reference in New Issue
Block a user