Updated types

This commit is contained in:
Luis Alvarez
2020-10-19 12:52:09 -05:00
parent 9309bff517
commit f4bc27666b
7 changed files with 24 additions and 28 deletions

View File

@@ -1,7 +1,4 @@
import getAllProducts, {
Products,
Product,
} from '../../operations/get-all-products'
import getAllProducts, { ProductEdge } from '../../operations/get-all-products'
import type { ProductsHandlers } from '../products'
const SORT: { [key: string]: string | undefined } = {
@@ -54,14 +51,13 @@ const getProducts: ProductsHandlers['getProducts'] = async ({
variables: { first: LIMIT, entityIds },
})
// Put the products in an object that we can use to get them by id
const productsById = graphqlData.products.reduce<{ [k: number]: Product }>(
(prods, p) => {
prods[p.node.entityId] = p
return prods
},
{}
)
const products: Products = found ? [] : graphqlData.products
const productsById = graphqlData.products.reduce<{
[k: number]: ProductEdge
}>((prods, p) => {
prods[p.node.entityId] = p
return prods
}, {})
const products: ProductEdge[] = found ? [] : graphqlData.products
// Populate the products array with the graphql products, in the order
// assigned by the list of entity ids

View File

@@ -4,11 +4,11 @@ import createApiHandler, {
BigcommerceHandler,
} from '../utils/create-api-handler'
import { BigcommerceApiError } from '../utils/errors'
import type { Products } from '../operations/get-all-products'
import type { ProductEdge } from '../operations/get-all-products'
import getProducts from './handlers/get-products'
export type SearchProductsData = {
products: Products
products: ProductEdge[]
found: boolean
}

View File

@@ -1,7 +1,7 @@
import type {
GetAllProductsQuery,
GetAllProductsQueryVariables,
} from 'lib/bigcommerce/schema'
} from '@lib/bigcommerce/schema'
import type { RecursivePartial, RecursiveRequired } from '../utils/types'
import filterEdges from '../utils/filter-edges'
import { productConnectionFragment } from '../fragments/product'
@@ -47,12 +47,12 @@ export type ProductEdge = NonNullable<
NonNullable<GetAllProductsQuery['site']['products']['edges']>[0]
>
export type Product = ProductEdge
export type Products = ProductEdge[]
export type ProductNode = ProductEdge['node']
export type GetAllProductsResult<
T extends Record<keyof GetAllProductsResult, any[]> = { products: Products }
T extends Record<keyof GetAllProductsResult, any[]> = {
products: ProductEdge[]
}
> = T
const FIELDS = [

View File

@@ -33,13 +33,13 @@ export const getProductQuery = /* GraphQL */ `
${productInfoFragment}
`
export type Product = Extract<
export type ProductNode = Extract<
GetProductQuery['site']['route']['node'],
{ __typename: 'Product' }
>
export type GetProductResult<
T extends { product?: any } = { product?: Product }
T extends { product?: any } = { product?: ProductNode }
> = T
export type ProductVariables = Images &