import {z} from 'zod' export type ReferralCodeSchema = {billing_tier?: ('PAYGO' | 'PRO' | 'ENTERPRISE' | 'ENTERPRISE_LEGACY_2025' | 'PRO_LEGACY_2025' | null) | undefined, code: string, credit_expiry_days?: (number | null) | undefined, do_not_delete?: (boolean | null) | undefined, freeze_date?: (string | null) | undefined, is_plg?: (boolean | null) | undefined, manage_system_domain_id?: (string | null) | undefined, ordway_customer_id?: (string | null) | undefined, sales_force_id?: (string | null) | undefined, valid_to: string, value: number}; export type CreateApiV1ReferralCodesArgs = {'App-ID': string, 'Auth-Token': string, body: ReferralCodeSchema}; export type CreateApiV1ReferralCodesOptions = {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 }; /** * Create a new referral_code * * POST /v1/referral_codes/ */ export const createApiV1ReferralCodes = async (args: CreateApiV1ReferralCodesArgs, options: CreateApiV1ReferralCodesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") 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/${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", 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(referralCodeSchema, await res.json()) };