import {z} from 'zod' export type GetApiStoresStoreKeyOrdersArgs = {storeKey: string, orderNumber?: string | undefined, customerName?: string | undefined, customerEmail?: string | undefined, minOrderValue?: number | undefined, maxOrderValue?: number | undefined, channelIds?: string | undefined, orderStatusIds?: string | undefined, pickupStatusIds?: string | undefined, deliveryTypeIds?: string | undefined, presaleStatusIds?: string | undefined, orderTypeIds?: string | undefined, sort?: string | undefined, offset?: number | undefined, limit?: number | undefined}; export type GetApiStoresStoreKeyOrdersOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const tcgApiModelCommonPagedApiResultSystemString = z.object({totalItems: z.number().int().optional(), success: z.boolean().optional(), errors: z.array(z.string()).optional(), results: z.array(z.string()).optional()}); export const validateResponse = async ( schema: { readonly '~standard': { readonly validate: (value: unknown) => ({ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }) | Promise<{ readonly value: Output; readonly issues?: undefined } | { readonly issues: ReadonlyArray<{ readonly message: string; readonly path?: ReadonlyArray | undefined }> }> } }, value: unknown ): Promise => { const result = await schema['~standard'].validate(value) if (result.issues !== undefined) { const detail = result.issues .map(({ message, path }) => path?.length ? `${path.map((segment) => String(typeof segment === 'object' ? segment.key : segment)).join('.')}: ${message}` : message ) .join('; ') throw new Error(`Response does not match the schema: ${detail}`, { cause: { issues: result.issues } }) } return result.value }; /** * Search Orders * * GET /stores/{storeKey}/orders */ export const getApiStoresStoreKeyOrders = async (args: GetApiStoresStoreKeyOrdersArgs, options?: GetApiStoresStoreKeyOrdersOptions) => { const {baseUrl = "https://api.tcgplayer.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.orderNumber != null) query.append("orderNumber", String(args.orderNumber)) if (args.customerName != null) query.append("customerName", String(args.customerName)) if (args.customerEmail != null) query.append("customerEmail", String(args.customerEmail)) if (args.minOrderValue != null) query.append("minOrderValue", String(args.minOrderValue)) if (args.maxOrderValue != null) query.append("maxOrderValue", String(args.maxOrderValue)) if (args.channelIds != null) query.append("channelIds", String(args.channelIds)) if (args.orderStatusIds != null) query.append("orderStatusIds", String(args.orderStatusIds)) if (args.pickupStatusIds != null) query.append("pickupStatusIds", String(args.pickupStatusIds)) if (args.deliveryTypeIds != null) query.append("deliveryTypeIds", String(args.deliveryTypeIds)) if (args.presaleStatusIds != null) query.append("presaleStatusIds", String(args.presaleStatusIds)) if (args.orderTypeIds != null) query.append("orderTypeIds", String(args.orderTypeIds)) if (args.sort != null) query.append("sort", String(args.sort)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.limit != null) query.append("limit", String(args.limit)) const url = `${baseUrl.replace(/\/+$/, '')}/stores/${encodeURIComponent(String(args.storeKey))}/orders${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(tcgApiModelCommonPagedApiResultSystemString, await res.json()) };