import {z} from 'zod' export type GetApiCouponsArgs = {'x-simple-workspace': string, 'x-piriod-test-mode'?: boolean | undefined, page?: number | undefined, page_size?: number | undefined, ordering?: string | undefined, name?: string | undefined, apply_on?: ('line' | 'invoice') | undefined, duration_scheme?: ('one_time' | 'forever' | 'limited') | undefined, amount?: number | undefined, currency?: string | undefined}; export type GetApiCouponsOptions = {baseUrl?: string | undefined, TokenAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const coupon = z.object({id: z.string().optional(), name: z.string().max(56), amount: z.number(), currency: z.string(), discount_scheme: z.enum(["%", "$"]).optional(), duration_scheme: z.enum(["one_time", "forever", "limited"]).optional(), duration_times: z.number().int().nullable().optional(), valid_until: z.string().nullable().optional(), apply_on: z.enum(["line", "invoice"]).optional(), status: z.enum(["active", "archived"]).optional(), archived: z.string().nullable().optional(), created: z.string().optional(), updated: z.string().optional()}); export const getApiCouponsResponse = z.object({count: z.number().int(), next: z.string().nullable().optional(), previous: z.string().nullable().optional(), results: z.array(coupon)}); 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 }; /** * List coupons * * GET /coupons/ */ export const getApiCoupons = async (args: GetApiCouponsArgs, options?: GetApiCouponsOptions) => { const {baseUrl = "https://api.piriod.com", fetch: fetchFn = fetch, TokenAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['x-simple-workspace'] != null) headers.set("x-simple-workspace", String(args['x-simple-workspace'])) if (args['x-piriod-test-mode'] != null) headers.set("x-piriod-test-mode", String(args['x-piriod-test-mode'])) if (args.page != null) query.append("page", String(args.page)) if (args.page_size != null) query.append("page_size", String(args.page_size)) if (args.ordering != null) query.append("ordering", String(args.ordering)) if (args.name != null) query.append("name", String(args.name)) if (args.apply_on != null) query.append("apply_on", String(args.apply_on)) if (args.duration_scheme != null) query.append("duration_scheme", String(args.duration_scheme)) if (args.amount != null) query.append("amount", String(args.amount)) if (args.currency != null) query.append("currency", String(args.currency)) if (TokenAuth !== undefined) headers.set("Authorization", TokenAuth) const url = `${baseUrl.replace(/\/+$/, '')}/coupons/${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(getApiCouponsResponse, await res.json()) };