import {z} from 'zod' export type CreateApiV1ShopsShopIdCheckoutArgs = {body: {cart: Array<{productId?: (number | null) | undefined, variantId?: (number | null) | undefined, parentVariantId?: (number | null) | undefined, name?: (string | null) | undefined, price?: (number | null) | undefined, deliverables: Array, instructions?: (string | null) | undefined, dynamic_url?: (string | null) | undefined, quantity: number, subscribe?: (boolean | null) | undefined, custom_fields?: (Array) | undefined}>, currency?: (string | null) | undefined, ip?: (string | null) | undefined, country_code?: (string | null) | undefined, user_agent?: (string | null) | undefined, asn?: (number | null) | undefined, email?: (string | null) | undefined, discord_user_id?: (string | null) | undefined, discord_user_username?: (string | null) | undefined, discord_access_token?: (string | null) | undefined, discord_refresh_token?: (string | null) | undefined, coupon?: (string | null) | undefined, gateway?: (string | null) | undefined, payment_method_id?: (number | null) | undefined, newsletter?: (boolean | null) | undefined, affiliate?: (string | null) | undefined, billing_name?: (string | null) | undefined, billing_address_line1?: (string | null) | undefined, billing_address_line2?: (string | null) | undefined, billing_city?: (string | null) | undefined, billing_state?: (string | null) | undefined, billing_postcode?: (string | null) | undefined, billing_country?: (string | null) | undefined, shipping_name?: (string | null) | undefined, shipping_address_line1?: (string | null) | undefined, shipping_address_line2?: (string | null) | undefined, shipping_city?: (string | null) | undefined, shipping_state?: (string | null) | undefined, shipping_postcode?: (string | null) | undefined, shipping_country?: (string | null) | undefined, shipping_phone?: (string | null) | undefined, metadata?: (Array) | undefined, attribution?: ({utm_source?: (string | null) | undefined, utm_medium?: (string | null) | undefined, utm_campaign?: (string | null) | undefined, utm_term?: (string | null) | undefined, utm_content?: (string | null) | undefined, referrer_host?: (string | null) | undefined} | null) | undefined}}; export type CreateApiV1ShopsShopIdCheckoutOptions = {baseUrl?: string | undefined, defaultValue?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1ShopsShopIdCheckoutResponse = z.union([z.object({success: z.boolean().optional(), invoice_id: z.number().int().optional(), invoice_url: z.string().optional(), url: z.string().optional()}), z.object({success: z.boolean().optional(), invoice_id: z.number().int().optional(), invoice_url: z.string().optional(), url: 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 }; /** * Create Checkout Session * * POST /v1/shops/{shopId}/checkout */ export const createApiV1ShopsShopIdCheckout = async (args: CreateApiV1ShopsShopIdCheckoutArgs, options?: CreateApiV1ShopsShopIdCheckoutOptions) => { const {baseUrl = "https://api.sellauth.com", fetch: fetchFn = fetch, defaultValue} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (defaultValue !== undefined) headers.set('authorization', `Bearer ${defaultValue}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/shops/${encodeURIComponent(String(args.shopId))}/checkout${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV1ShopsShopIdCheckoutResponse, await res.json()) };