import {z} from 'zod' export type GetApiCompaniesCompanyKeyIncentivesCodesIncentiveCodeRedemptionsArgs = {'X-API-Version'?: string | undefined, 'Content-Type': string, company_key: string, incentive_code: string}; export type GetApiCompaniesCompanyKeyIncentivesCodesIncentiveCodeRedemptionsOptions = {baseUrl?: string | undefined, basicAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const incentiveCodeIssuanceResponse = z.object({id: z.number().int().optional(), external_issuee_id: z.string().optional(), issuee_id: z.string().optional(), referring_application: z.string().optional(), referring_application_ref_id: z.string().optional(), incentive_code: z.object({code: z.string().optional(), url: z.string().optional()}).optional(), incentive_pool: z.object({id: z.number().int().optional(), url: z.number().int().optional()}).optional(), created_at: z.string().optional(), updated_at: z.string().optional()}); export const incentiveCodeIssuancesResponse = z.array(incentiveCodeIssuanceResponse); 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 redemptions for a code * * GET /companies/{company_key}/incentives/codes/{incentive_code}/redemptions */ export const getApiCompaniesCompanyKeyIncentivesCodesIncentiveCodeRedemptions = async (args: GetApiCompaniesCompanyKeyIncentivesCodesIncentiveCodeRedemptionsArgs, options?: GetApiCompaniesCompanyKeyIncentivesCodesIncentiveCodeRedemptionsOptions) => { const {baseUrl = "https://public-api.vibescm.com", fetch: fetchFn = fetch, basicAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['X-API-Version'] != null) headers.set("X-API-Version", String(args['X-API-Version'])) if (args['Content-Type'] != null) headers.set("Content-Type", String(args['Content-Type'])) if (basicAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${basicAuth.username}:${basicAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/companies/${encodeURIComponent(String(args.company_key))}/incentives/codes/${encodeURIComponent(String(args.incentive_code))}/redemptions${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(incentiveCodeIssuancesResponse, await res.json()) };