refactor: adjust the types

This commit is contained in:
Zaiste 2021-04-19 15:36:35 +02:00
parent b5e21a54b3
commit 1e946c8b47
No known key found for this signature in database
GPG Key ID: 15DF7EBC7F2FFE35
6 changed files with 15 additions and 13 deletions

View File

@ -1,4 +1,4 @@
import { ProductEdge } from '../../schema' import { ProductCountableEdge } from '../../schema'
import { SaleorConfig } from '..' import { SaleorConfig } from '..'
const fetchAllProducts = async ({ const fetchAllProducts = async ({
@ -10,15 +10,15 @@ const fetchAllProducts = async ({
}: { }: {
config: SaleorConfig config: SaleorConfig
query: string query: string
acc?: ProductEdge[] acc?: ProductCountableEdge[]
variables?: any variables?: any
cursor?: string cursor?: string
}): Promise<ProductEdge[]> => { }): Promise<ProductCountableEdge[]> => {
const { data } = await config.fetch(query, { const { data } = await config.fetch(query, {
variables: { ...variables, cursor }, variables: { ...variables, cursor },
}) })
const edges: ProductEdge[] = data.products?.edges ?? [] const edges: ProductCountableEdge[] = data.products?.edges ?? []
const hasNextPage = data.products?.pageInfo?.hasNextPage const hasNextPage = data.products?.pageInfo?.hasNextPage
acc = acc.concat(edges) acc = acc.concat(edges)

View File

@ -1,5 +1,5 @@
import { getConfig, SaleorConfig } from '../api' import { getConfig, SaleorConfig } from '../api'
import { PageEdge } from '../schema' import { PageCountableEdge } from '../schema'
import { getAllPagesQuery } from '../utils/queries' import { getAllPagesQuery } from '../utils/queries'
type Variables = { type Variables = {

View File

@ -1,4 +1,4 @@
import { CollectionEdge } from '../schema' import { CollectionCountableEdge } from '../schema'
import { getConfig, SaleorConfig } from '../api' import { getConfig, SaleorConfig } from '../api'
import getAllCollectionsQuery from '../utils/queries/get-all-collections-query' import getAllCollectionsQuery from '../utils/queries/get-all-collections-query'
@ -14,7 +14,7 @@ const getAllCollections = async (options?: {
const edges = data.collections?.edges ?? [] const edges = data.collections?.edges ?? []
const categories = edges.map( const categories = edges.map(
({ node: { id: entityId, name, slug } }: CollectionEdge) => ({ ({ node: { id: entityId, name, slug } }: CollectionCountableEdge) => ({
entityId, entityId,
name, name,
path: `/${slug}`, path: `/${slug}`,

View File

@ -1,7 +1,7 @@
import { Product } from '@commerce/types' import { Product } from '@commerce/types'
import { getConfig, SaleorConfig } from '../api' import { getConfig, SaleorConfig } from '../api'
import fetchAllProducts from '../api/utils/fetch-all-products' import fetchAllProducts from '../api/utils/fetch-all-products'
import { ProductEdge } from '../schema' import { ProductCountableEdge } from '../schema'
import getAllProductsPathsQuery from '../utils/queries/get-all-products-paths-query' import getAllProductsPathsQuery from '../utils/queries/get-all-products-paths-query'
type ProductPath = { type ProductPath = {
@ -31,7 +31,7 @@ const getAllProductPaths = async (options?: {
}) })
return { return {
products: products?.map(({ node: { handle } }: ProductEdge) => ({ products: products?.map(({ node: { slug } }: ProductCountableEdge) => ({
node: { node: {
path: `/${handle}`, path: `/${handle}`,
}, },

View File

@ -1,6 +1,6 @@
import { GraphQLFetcherResult } from '@commerce/api' import { GraphQLFetcherResult } from '@commerce/api'
import { getConfig, SaleorConfig } from '../api' import { getConfig, SaleorConfig } from '../api'
import { Product as SaleorProduct } from '../schema' import { ProductCountableEdge } from '../schema'
import { getAllProductsQuery } from '../utils/queries' import { getAllProductsQuery } from '../utils/queries'
import { normalizeProduct } from '../utils/normalize' import { normalizeProduct } from '../utils/normalize'
import { Product } from '@commerce/types' import { Product } from '@commerce/types'
@ -28,7 +28,7 @@ const getAllProducts = async (options: {
) )
const products = const products =
data.products?.edges?.map(({ node: p }: SaleorProduct) => data.products?.edges?.map(({ node: p }: ProductCountableEdge) =>
normalizeProduct(p) normalizeProduct(p)
) ?? [] ) ?? []

View File

@ -1,7 +1,7 @@
import { SWRHook } from '@commerce/utils/types' import { SWRHook } from '@commerce/utils/types'
import useSearch, { UseSearch } from '@commerce/product/use-search' import useSearch, { UseSearch } from '@commerce/product/use-search'
import { ProductEdge } from '../schema' import { ProductCountableEdge } from '../schema'
import { import {
getAllProductsQuery, getAllProductsQuery,
getCollectionProductsQuery, getCollectionProductsQuery,
@ -57,7 +57,9 @@ export const handler: SWRHook<
} }
return { return {
products: edges.map(({ node }: ProductEdge) => normalizeProduct(node)), products: edges.map(({ node }: ProductCountableEdge) =>
normalizeProduct(node)
),
found: !!edges.length, found: !!edges.length,
} }
}, },