import {z} from 'zod' export type GetApiDiscountCodesArgs = {isActive?: boolean | undefined, scope?: string | undefined, category?: string | undefined, page?: (number | unknown) | undefined, pageSize?: (number | unknown) | undefined}; export type GetApiDiscountCodesOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const discountCodeDto = z.object({id: z.string().optional(), code: z.string().optional(), name: z.string().optional(), reason: z.string().nullable().optional(), category: z.string().nullable().optional(), applyTo: z.string().optional(), isActive: z.boolean().optional(), discountType: z.string().optional(), discountValue: z.union([z.number(), z.string()]).optional(), scope: z.string().optional(), validFrom: z.string().nullable().optional(), validTo: z.string().nullable().optional(), maxUsageCount: z.union([z.number().int(), z.string()]).nullable().optional(), usageCount: z.union([z.number().int(), z.string()]).optional(), minimumOrderAmount: z.union([z.number(), z.string()]).nullable().optional(), maxDiscountAmount: z.union([z.number(), z.string()]).nullable().optional(), createdAt: z.string().optional(), updatedAt: z.string().optional()}); export const discountCodeListResponseDto = z.object({items: z.array(discountCodeDto).optional(), total: z.union([z.number().int(), 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 }; /** * Get a paginated list of discount codes with optional filters * * GET /DiscountCodes */ export const getApiDiscountCodes = async (args: GetApiDiscountCodesArgs, options?: GetApiDiscountCodesOptions) => { const {baseUrl = "https://api.clinicos.vet/backend/business", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.isActive != null) query.append("isActive", String(args.isActive)) if (args.scope != null) query.append("scope", String(args.scope)) if (args.category != null) query.append("category", String(args.category)) if (args.page != null) query.append("page", String(args.page)) if (args.pageSize != null) query.append("pageSize", String(args.pageSize)) if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/DiscountCodes${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(discountCodeListResponseDto, await res.json()) };