import type { ServerResponse } from 'http' import type { LoginOperation } from '../types/login' import type { GetAllPagesOperation, GetPageOperation } from '../types/page' import type { GetSiteInfoOperation } from '../types/site' import type { GetCustomerWishlistOperation } from '../types/wishlist' import type { GetAllProductPathsOperation, GetAllProductsOperation, GetProductOperation, } from '../types/product' import type { APIProvider, CommerceAPI } from '.' const noop = () => { throw new Error('Not implemented') } export const OPERATIONS = [ 'login', 'getAllPages', 'getPage', 'getSiteInfo', 'getCustomerWishlist', 'getAllProductPaths', 'getAllProducts', 'getProduct', ] as const export const defaultOperations = OPERATIONS.reduce((ops, k) => { ops[k] = noop return ops }, {} as { [K in AllowedOperations]: typeof noop }) export type AllowedOperations = typeof OPERATIONS[number] export type Operations

= { login: { (opts: { variables: T['variables'] config?: P['config'] res: ServerResponse }): Promise ( opts: { variables: T['variables'] config?: P['config'] res: ServerResponse } & OperationOptions ): Promise } getAllPages: { (opts?: { config?: P['config'] preview?: boolean }): Promise ( opts: { config?: P['config'] preview?: boolean } & OperationOptions ): Promise } getPage: { (opts: { variables: T['variables'] config?: P['config'] preview?: boolean }): Promise ( opts: { variables: T['variables'] config?: P['config'] preview?: boolean } & OperationOptions ): Promise } getSiteInfo: { (opts: { config?: P['config'] preview?: boolean }): Promise ( opts: { config?: P['config'] preview?: boolean } & OperationOptions ): Promise } getCustomerWishlist: { (opts: { variables: T['variables'] config?: P['config'] includeProducts?: boolean }): Promise ( opts: { variables: T['variables'] config?: P['config'] includeProducts?: boolean } & OperationOptions ): Promise } getAllProductPaths: { (opts: { variables?: T['variables'] config?: P['config'] }): Promise ( opts: { variables?: T['variables'] config?: P['config'] } & OperationOptions ): Promise } getAllProducts: { (opts: { variables?: T['variables'] config?: P['config'] preview?: boolean }): Promise ( opts: { variables?: T['variables'] config?: P['config'] preview?: boolean } & OperationOptions ): Promise } getProduct: { (opts: { variables: T['variables'] config?: P['config'] preview?: boolean }): Promise ( opts: { variables: T['variables'] config?: P['config'] preview?: boolean } & OperationOptions ): Promise } } export type APIOperations

= { [K in keyof Operations

]?: (ctx: OperationContext

) => Operations

[K] } export type AllOperations

= { [K in keyof APIOperations

]-?: P['operations'][K] extends ( ...args: any ) => any ? ReturnType : typeof noop } export type OperationContext

= { commerce: CommerceAPI

} export type OperationOptions = | { query: string; url?: never } | { query?: never; url: string }