4
0
forked from crowetic/commerce

Updated types

This commit is contained in:
Luis Alvarez 2020-10-05 01:13:20 -05:00
parent 2f35358942
commit d095ac9f70
3 changed files with 24 additions and 17 deletions

View File

@ -16,17 +16,22 @@ export const getAllProductPathsQuery = /* GraphQL */ `
}
`
export interface GetAllProductPathsResult<T> {
products: T extends GetAllProductPathsQuery
? NonNullable<T['site']['products']['edges']>
: unknown
}
export type ProductPaths = NonNullable<
GetAllProductPathsQuery['site']['products']['edges']
>
export type GetAllProductPathsResult<
T extends { products: any[] } = { products: ProductPaths }
> = T
async function getAllProductPaths(opts?: {
config?: BigcommerceConfig
}): Promise<GetAllProductPathsResult<GetAllProductPathsQuery>>
}): Promise<GetAllProductPathsResult>
async function getAllProductPaths<T, V = any>(opts: {
async function getAllProductPaths<
T extends { products: any[] },
V = any
>(opts: {
query: string
config?: BigcommerceConfig
}): Promise<GetAllProductPathsResult<T>>
@ -37,7 +42,7 @@ async function getAllProductPaths({
}: {
query?: string
config?: BigcommerceConfig
} = {}): Promise<GetAllProductPathsResult<GetAllProductPathsQuery>> {
} = {}): Promise<GetAllProductPathsResult> {
config = getConfig(config)
// RecursivePartial forces the method to check for every prop in the data, which is
// required in case there's a custom `query`

View File

@ -37,11 +37,13 @@ export const getAllProductsQuery = /* GraphQL */ `
${productInfoFragment}
`
export interface GetAllProductsResult<T> {
products: T extends GetAllProductsQuery
? NonNullable<T['site']['products']['edges']>
: unknown
}
export type Products = NonNullable<
GetAllProductsQuery['site']['products']['edges']
>
export type GetAllProductsResult<
T extends { products: any[] } = { products: Products }
> = T
export type ProductVariables = Images &
Omit<GetAllProductsQueryVariables, keyof ProductImageVariables>
@ -49,9 +51,9 @@ export type ProductVariables = Images &
async function getAllProducts(opts?: {
variables?: ProductVariables
config?: BigcommerceConfig
}): Promise<GetAllProductsResult<GetAllProductsQuery>>
}): Promise<GetAllProductsResult>
async function getAllProducts<T, V = any>(opts: {
async function getAllProducts<T extends { products: any[] }, V = any>(opts: {
query: string
variables?: V
config?: BigcommerceConfig
@ -65,7 +67,7 @@ async function getAllProducts({
query?: string
variables?: ProductVariables
config?: BigcommerceConfig
} = {}): Promise<GetAllProductsResult<GetAllProductsQuery>> {
} = {}): Promise<GetAllProductsResult> {
config = getConfig(config)
const variables: GetAllProductsQueryVariables = {
...config.imageVariables,

View File

@ -50,7 +50,7 @@ async function getProduct(opts: {
config?: BigcommerceConfig
}): Promise<GetProductResult>
async function getProduct<T, V = any>(opts: {
async function getProduct<T extends { product?: any }, V = any>(opts: {
query: string
variables: V
config?: BigcommerceConfig