fix conflict

This commit is contained in:
Quangnhankie
2021-10-15 13:38:47 +07:00
99 changed files with 2194 additions and 784 deletions

View File

@@ -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'
@@ -9,6 +10,7 @@ import type {
GetProductOperation,
} from '../types/product'
import type { APIProvider, CommerceAPI } from '.'
import { GetAllCollectionsOperation } from '@commerce/types/collection';
const noop = () => {
throw new Error('Not implemented')
@@ -23,6 +25,9 @@ export const OPERATIONS = [
'getAllProductPaths',
'getAllProducts',
'getProduct',
'getAllFacets',
'getAllCollections',
] as const
export const defaultOperations = OPERATIONS.reduce((ops, k) => {
@@ -154,8 +159,43 @@ 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']>
}
getAllCollections: {
<T extends GetAllCollectionsOperation>(opts: {
variables?: T['variables']
config?: P['config']
preview?: boolean
}): Promise<T['data']>
<T extends GetAllCollectionsOperation>(
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]
}

View File

@@ -15,6 +15,8 @@ Adding a commerce provider means adding a new folder in `framework` with a folde
- useSearch
- getProduct
- getAllProducts
- getAllFacets
- getAllCollections
- `wishlist`
- useWishlist
- useAddItem

View File

@@ -0,0 +1,54 @@
import { Asset } from '../../vendure/schema.d';
export type Collection = {
id: string
name: string
slug: string
description: string
featuredAsse: Asset
asset: Asset[]
}
export type SearchCollectionsBody = {
search?: string
sort?: string
locale?: string
}
export type CollectionTypes = {
collection: Collection
searchBody: SearchCollectionsBody
}
export type SearchCollectionsHook<T extends CollectionTypes = CollectionTypes> = {
data: {
collections: T['collection'][]
found: boolean
}
body: T['searchBody']
input: T['searchBody']
fetcherInput: T['searchBody']
}
export type CollectionsSchema<T extends CollectionTypes = CollectionTypes> = {
endpoint: {
options: {}
handlers: {
getCollections: SearchCollectionsHook<T>
}
}
}
export type GetAllCollectionsOperation<T extends CollectionTypes = CollectionTypes> = {
data: { collections: T['collection'][] }
variables: {
ids?: string[]
first?: number
}
}
export type GetCollectionOperation<T extends CollectionTypes = CollectionTypes> = {
data: { collection?: T['collection'] }
variables: { code: string; } | { code?: never; }
}

View 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; }
}

View File

@@ -1,3 +1,6 @@
import { CurrencyCode } from './../../vendure/schema.d';
import { FacetValueFilterInput, LogicalOperator, SearchResultSortParameter } from "@framework/schema"
export type ProductImage = {
url: string
alt?: string
@@ -40,9 +43,28 @@ export type Product = {
slug?: string
path?: string
images: ProductImage[]
variants: ProductVariant[]
price: ProductPrice
price: number
currencyCode: CurrencyCode
options: ProductOption[]
facetValueIds?: string[]
collectionIds?: string[]
collection?: string,
}
export type ProductCard = {
id: string
name: string
slug?: string
imageSrc: string
price: number
currencyCode: CurrencyCode
oldPrice?: number
discount?: number
weight?: number
facetValueIds?: string[],
collectionIds?: string[],
collection?: string,
isNotSell?: boolean
}
export type SearchProductsBody = {
@@ -79,17 +101,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
}
}

View File

@@ -1,15 +1,17 @@
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 getAllCollections from './operations/get-all-collection'
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 +42,8 @@ const operations = {
getAllProductPaths,
getAllProducts,
getProduct,
getAllFacets,
getAllCollections,
}
export const provider = { config, operations }

View File

@@ -0,0 +1,45 @@
import { OperationContext } from '@commerce/api/operations'
import { Collection } from '@commerce/types/collection'
import { Provider, VendureConfig } from '..'
import { GetAllCollectionsQuery } from '../../schema'
import { getAllCollectionsQuery } from '../../utils/queries/get-all-collections-query'
export type CollectionVariables = { first?: number }
export default function getAllCollectionsOperation({
commerce,
}: OperationContext<Provider>) {
async function getAllCollections(opts?: {
variables?: CollectionVariables
config?: Partial<VendureConfig>
preview?: boolean
}): Promise<{ collections: Collection[] }>
async function getAllCollections({
query = getAllCollectionsQuery,
variables: { ...vars } = {},
config: cfg,
}: {
query?: string
variables?: CollectionVariables
config?: Partial<VendureConfig>
preview?: boolean
} = {}): Promise<{ collections: Collection[] | any[] }> {
const config = commerce.getConfig(cfg)
const variables = {
input: {
take: vars.first,
groupByCollection: true,
},
}
const { data } = await config.fetch<GetAllCollectionsQuery>(query, {
variables,
})
return {
collections: data.collections.items,
}
}
return getAllCollections
}

View File

@@ -0,0 +1,46 @@
import { OperationContext } from '@commerce/api/operations'
import { Facet } from '@commerce/types/facet'
import { Provider, VendureConfig } from '../'
import { FacetFilterParameter, FacetSortParameter, GetAllFacetsQuery } from '../../schema'
import { getAllFacetsQuery } from '../../utils/queries/get-all-facets-query'
export type FacetVariables = { first?: number, filter?: FacetFilterParameter, sort?: FacetSortParameter }
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 = {
options: {
take: vars.first,
filter: vars.filter,
sort: vars.sort,
},
}
const { data } = await config.fetch<GetAllFacetsQuery>(query, {
variables,
})
return {
facets: data.facets.items,
}
}
return getAllFacets
}

View File

@@ -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,
@@ -14,7 +14,7 @@ export default function getAllProductsOperation({
variables?: ProductVariables
config?: Partial<VendureConfig>
preview?: boolean
}): Promise<{ products: Product[] }>
}): Promise<{ products: Product[], totalItems: number }>
async function getAllProducts({
query = getAllProductsQuery,
@@ -25,11 +25,12 @@ export default function getAllProductsOperation({
variables?: ProductVariables
config?: Partial<VendureConfig>
preview?: boolean
} = {}): Promise<{ products: Product[] | any[] }> {
} = {}): Promise<{ products: Product[] | any[], totalItems: number }> {
const config = commerce.getConfig(cfg)
const variables = {
input: {
take: vars.first,
facetValueIds: vars.facetValueIds,
groupByProduct: true,
},
}
@@ -39,6 +40,7 @@ export default function getAllProductsOperation({
return {
products: data.search.items.map((item) => normalizeSearchResult(item)),
totalItems: data.search.totalItems as number,
}
}

View File

@@ -2,7 +2,7 @@ import { Product } from '@commerce/types/product'
import { OperationContext } from '@commerce/api/operations'
import { Provider, VendureConfig } from '../'
import { GetProductQuery } from '../../schema'
import { getProductQuery } from '../../utils/queries/get-product-query'
import { getProductQuery, getProductDetailQuery } from '../../utils/queries/get-product-query'
export default function getProductOperation({
commerce,
@@ -16,10 +16,8 @@ export default function getProductOperation({
variables: { slug: string }
config?: Partial<VendureConfig>
preview?: boolean
}): Promise<Product | {} | any> {
}): Promise<Product | null> {
const config = commerce.getConfig(cfg)
const locale = config.locale
const { data } = await config.fetch<GetProductQuery>(query, { variables })
const product = data.product
@@ -28,7 +26,6 @@ export default function getProductOperation({
return product.optionGroups.find((og) => og.id === id)!.name
}
return {
product: {
id: product.id,
name: product.name,
description: product.description,
@@ -49,20 +46,19 @@ export default function getProductOperation({
values: [{ label: o.name }],
})),
})),
price: {
value: product.variants[0].priceWithTax / 100,
currencyCode: product.variants[0].currencyCode,
},
price: product.variants[0].priceWithTax / 100,
currencyCode: product.variants[0].currencyCode,
options: product.optionGroups.map((og) => ({
id: og.id,
displayName: og.name,
values: og.options.map((o) => ({ label: o.name })),
})),
} as Product,
}
facetValueIds: product.facetValues.map(item=> item.id),
collectionIds: product.collections.map(item => item.id)
} as Product
}
return {}
return null
}
return getProduct

View File

@@ -1,5 +1,6 @@
import { ResetPassword } from './schema.d';
import { requestPasswordReset } from '@framework/schema';
import { FacetValue } from './schema.d';
export type Maybe<T> = T | null
export type Exact<T extends { [key: string]: unknown }> = {
[K in keyof T]: T[K]
@@ -95,6 +96,10 @@ export type QueryProductsArgs = {
options?: Maybe<ProductListOptions>
}
export type QueryFacetsArgs = {
options?: Maybe<FacetListOptions>
}
export type QuerySearchArgs = {
input: SearchInput
}
@@ -2729,6 +2734,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
@@ -2886,6 +2898,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>
@@ -3010,7 +3039,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' | 'facetValueIds' | 'collectionIds'
> & {
productAsset?: Maybe<
{ __typename?: 'SearchResultAsset' } & Pick<
@@ -3223,7 +3254,43 @@ export type GetAllProductsQueryVariables = Exact<{
export type GetAllProductsQuery = { __typename?: 'Query' } & {
search: { __typename?: 'SearchResponse' } & {
items: Array<{ __typename?: 'SearchResult' } & SearchResultFragment>
items: Array<{ __typename?: 'SearchResult' } & SearchResultFragment>,
'totalItems'
}
}
export type GetAllFacetsQuery = { __typename?: 'Query' } & {
facets: { __typename?: 'FacetList' } & {
items: Array<
{ __typename?: 'Facet' } & Pick<
Facet,
'id' | 'name' | 'code' | 'values'
>
& {
parent?: Maybe<{ __typename?: 'Facet' } & Pick<Facet, 'id'>>
children?: Maybe<
Array<{ __typename?: 'Facet' } & Pick<Facet, 'id'>>
>
}
>,
'totalItems'
}
}
export type GetAllCollectionsQuery = { __typename?: 'Query' } & {
collections: { __typename?: 'CollectionList' } & {
items: Array<
{ __typename?: 'Collection' } & Pick<
Collection,
'id' | 'name' | 'slug'
> & {
parent?: Maybe<{ __typename?: 'Collection' } & Pick<Collection, 'id'>>
children?: Maybe<
Array<{ __typename?: 'Collection' } & Pick<Collection, 'id'>>
>
}
>,
'totalItems'
}
}
@@ -3271,7 +3338,7 @@ export type GetProductQuery = { __typename?: 'Query' } & {
variants: Array<
{ __typename?: 'ProductVariant' } & Pick<
ProductVariant,
'id' | 'priceWithTax' | 'currencyCode'
'id' | 'priceWithTax' | 'currencyCode' | 'price'
> & {
options: Array<
{ __typename?: 'ProductOption' } & Pick<
@@ -3306,6 +3373,18 @@ export type GetProductQuery = { __typename?: 'Query' } & {
>
}
>
facetValues: Array<
{ __typename?: 'FacetValue' } & Pick<
FacetValue,
'id'
>
>
collections: Array<
{ __typename?: 'Collection' } & Pick<
Collection,
'id'
>
>
}
>
}

View File

@@ -19,6 +19,8 @@ export const searchResultFragment = /* GraphQL */ `
min
max
}
}
},
facetValueIds,
collectionIds,
}
`

View File

@@ -1,22 +1,23 @@
import { Product } from '@commerce/types/product'
import { Cart } from '@commerce/types/cart'
import { ProductCard } from '@commerce/types/product'
import { CartFragment, SearchResultFragment } from '../schema'
export function normalizeSearchResult(item: SearchResultFragment): Product {
export function normalizeSearchResult(item: SearchResultFragment): ProductCard {
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: [],
price: {
value: (item.priceWithTax as any).min / 100,
currencyCode: item.currencyCode,
},
options: [],
sku: item.sku,
imageSrc: item.productAsset?.preview ? item.productAsset?.preview + '?w=800&mode=crop' : '',
price: (item.priceWithTax as any).min / 100,
currencyCode: item.currencyCode,
facetValueIds: item.facetValueIds,
collectionIds: item.collectionIds,
// TODO:
// oldPrice: item.price
// discount
// isNotSell
// weight
}
}

View File

@@ -0,0 +1,12 @@
export const getAllCollectionsQuery = /* GraphQL */ `
query collections ($options: CollectionListOptions) {
collections (options: $options){
totalItems,
items {
id
name
slug
}
}
}
`

View 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
}
}
}
}
`

View File

@@ -3,6 +3,7 @@ import { searchResultFragment } from '../fragments/search-result-fragment'
export const getAllProductsQuery = /* GraphQL */ `
query getAllProducts($input: SearchInput!) {
search(input: $input) {
totalItems
items {
...SearchResult
}

View File

@@ -24,7 +24,7 @@ export const getCollectionsNameQuery = /* GraphQL */ `
collections{
items{
name
link:slug
slug
}
}
}

View File

@@ -36,6 +36,28 @@ export const getProductQuery = /* GraphQL */ `
name
}
}
facetValues {
id
}
collections {
id
}
}
}
`
export const getProductDetailQuery = /* GraphQL */ `
query GetProductDetail($slug: String! = "hand-trowel") {
product(slug: $slug) {
name
description
variants {
price
priceWithTax
}
assets {
preview
name
}
}
}
`