import {z} from 'zod' export type GetApiV1ReferralCodesCodeArgs = {'App-ID': string, 'Auth-Token': string, code: string}; export type GetApiV1ReferralCodesCodeOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const referralCodeSchema = z.object({billing_tier: z.enum(["PAYGO", "PRO", "ENTERPRISE", "ENTERPRISE_LEGACY_2025", "PRO_LEGACY_2025"]).nullable().optional(), code: z.string(), credit_expiry_days: z.number().int().nullable().optional(), do_not_delete: z.boolean().nullable().optional(), freeze_date: z.string().nullable().optional(), is_plg: z.boolean().nullable().optional(), manage_system_domain_id: z.string().nullable().optional(), ordway_customer_id: z.string().nullable().optional(), sales_force_id: z.string().nullable().optional(), valid_to: z.string(), value: z.number()}); 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 referral_code * * GET /v1/referral_codes/{code}/ */ export const getApiV1ReferralCodesCode = async (args: GetApiV1ReferralCodesCodeArgs, options: GetApiV1ReferralCodesCodeOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['App-ID'] != null) headers.set("App-ID", String(args['App-ID'])) if (args['Auth-Token'] != null) headers.set("Auth-Token", String(args['Auth-Token'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/referral_codes/${encodeURIComponent(String(args.code))}/${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(referralCodeSchema, await res.json()) };