import {z} from 'zod' export type GetApiV1DiscountsSearchArgs = {page_number?: number | undefined, page_size?: number | undefined, product_id?: string | undefined, status?: ('active' | 'deleted') | undefined, type?: ('percentage' | 'fixed') | undefined, created_after?: string | undefined, created_before?: string | undefined}; export type GetApiV1DiscountsSearchOptions = {baseUrl?: string | undefined, ApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const environmentMode = z.enum(["test", "prod", "sandbox"]); export const discountEntity = z.object({id: z.string(), mode: environmentMode, object: z.string(), status: z.enum(["deleted", "active", "draft", "expired", "scheduled"]), name: z.string(), code: z.string(), type: z.enum(["percentage", "fixed"]), amount: z.number().optional(), currency: z.string().optional(), percentage: z.number().optional(), expiry_date: z.string().optional(), max_redemptions: z.number().optional(), duration: z.enum(["forever", "once", "repeating"]).optional(), duration_in_months: z.number().optional(), applies_to_products: z.array(z.string()).optional(), redeem_count: z.number().optional()}); export const paginationEntity = z.object({total_records: z.number(), total_pages: z.number(), current_page: z.number(), next_page: z.number().nullable(), prev_page: z.number().nullable()}); export const discountListEntity = z.object({items: z.array(discountEntity), pagination: paginationEntity}); 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 discounts * * GET /v1/discounts/search */ export const getApiV1DiscountsSearch = async (args: GetApiV1DiscountsSearchArgs, options?: GetApiV1DiscountsSearchOptions) => { const {baseUrl = "https://api.creem.io", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.page_number != null) query.append("page_number", String(args.page_number)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.product_id != null) query.append("product_id", String(args.product_id)) if (args.status != null) query.append("status", String(args.status)) if (args.type != null) query.append("type", String(args.type)) if (args.created_after != null) query.append("created_after", String(args.created_after)) if (args.created_before != null) query.append("created_before", String(args.created_before)) if (ApiKey !== undefined) headers.set("x-api-key", ApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/v1/discounts/search${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(discountListEntity, await res.json()) };