import {z} from 'zod' export type PriceRulesRequest = {created_at_max?: number | undefined, created_at_min?: number | undefined, ends_at_max?: number | undefined, ends_at_min?: number | undefined, fields?: Array | undefined, filter?: string | undefined, ids?: Array | undefined, ignore_id?: number | undefined, limit?: number | undefined, no_prerequisite?: boolean | undefined, page?: number | undefined, shop_id?: number | undefined, since_id?: number | undefined, sort_by?: string | undefined, starts_at_max?: number | undefined, starts_at_min?: number | undefined, target_selection?: string | undefined, times_used?: number | undefined, title?: string | undefined, type?: string | undefined, updated_at_max?: number | undefined, updated_at_min?: number | undefined}; export type GetApiAdminPriceRulesJsonArgs = {body: PriceRulesRequest}; export type GetApiAdminPriceRulesJsonOptions = {baseUrl?: string | undefined, APPACCESSTOKEN?: string | undefined, fetch?: typeof fetch | undefined}; export const quantityRange = z.object({greater_than_or_equal_to: z.number().optional()}); export const shippingPriceRange = z.object({less_than_or_equal_to: z.number().optional()}); export const subtotalRange = z.object({greater_than_or_equal_to: z.number().optional()}); export const toEntitlementQuantityRatio = z.object({entitled_quantity: z.number().optional(), prerequisite_quantity: z.number().optional()}); export const priceRuleDto = z.object({allocation_limit: z.number().int().optional(), allocation_method: z.string().optional(), created_at: z.string().optional(), customer_selection: z.string().optional(), ends_at: z.string().optional(), entitled_collection_ids: z.array(z.number().int()).optional(), entitled_country_ids: z.array(z.number().int()).optional(), entitled_product_ids: z.array(z.number().int()).optional(), entitled_variant_ids: z.array(z.number().int()).optional(), id: z.number().int().optional(), maximum_discount_amount: z.number().optional(), once_per_customer: z.boolean().optional(), prerequisite_collection_ids: z.array(z.number().int()).optional(), prerequisite_customer_ids: z.array(z.number().int()).optional(), prerequisite_emails: z.array(z.string()).optional(), prerequisite_product_ids: z.array(z.number().int()).optional(), prerequisite_quantity_range: quantityRange.optional(), prerequisite_shipping_price_range: shippingPriceRange.optional(), prerequisite_subtotal_range: subtotalRange.optional(), prerequisite_to_entitlement_quantity_ratio: toEntitlementQuantityRatio.optional(), prerequisite_variant_ids: z.array(z.number().int()).optional(), quota_per_customer: z.number().int().optional(), quota_per_store: z.number().int().optional(), shop_id: z.number().int().optional(), starts_at: z.string().optional(), status: z.string().optional(), target_selection: z.string().optional(), target_type: z.string().optional(), title: z.string().optional(), total_redemption: z.number().int().optional(), total_sale_count: z.number().optional(), type: z.string().optional(), updated_at: z.string().optional(), usage_count: z.number().int().optional(), usage_limit: z.number().int().optional(), value: z.number().optional(), value_type: z.string().optional()}); export const priceRulesResponse = z.object({price_rules: z.array(priceRuleDto).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 }; /** * Retrieve a list of price rules * * GET /admin/price_rules.json */ export const getApiAdminPriceRulesJson = async (args: GetApiAdminPriceRulesJsonArgs, options?: GetApiAdminPriceRulesJsonOptions) => { const {baseUrl = "https://shop-name.onshopbase.com", fetch: fetchFn = fetch, APPACCESSTOKEN} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (APPACCESSTOKEN !== undefined) headers.set("APP_ACCESS_TOKEN", APPACCESSTOKEN) const url = `${baseUrl.replace(/\/+$/, '')}/admin/price_rules.json${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", 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(priceRulesResponse, await res.json()) };