import {z} from 'zod' export type ReferralStatus = 'CREDIT_PENDING' | 'CREDIT_AWARDED' | 'CREDIT_EXPIRED' | 'INVITE_SENT'; export type GetApiCampaignIdParticipantParticipantIdOrEmailReferralsArgs = {id: string, participantIdOrEmail: string, sortBy?: ('updatedAt' | 'createdAt' | 'email' | 'firstName' | 'lastName' | 'referralStatus' | 'referralTriggeredAt') | undefined, desc?: boolean | undefined, limit?: number | undefined, offset?: number | undefined, email?: string | undefined, firstName?: string | undefined, lastName?: string | undefined, referralStatus?: ReferralStatus | undefined, nextId?: string | undefined}; export type GetApiCampaignIdParticipantParticipantIdOrEmailReferralsOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const referralStatus = z.enum(["CREDIT_PENDING", "CREDIT_AWARDED", "CREDIT_EXPIRED", "INVITE_SENT"]); export const referral = z.object({id: z.string(), email: z.string(), firstName: z.string().nullable().optional(), lastName: z.string().nullable().optional(), referralStatus: referralStatus, referredBy: z.string(), createdAt: z.number().int(), updatedAt: z.number().int()}); export const referralListResponse = z.object({referrals: z.array(referral), limit: z.number().int(), nextOffset: z.number().int().nullable().optional(), nextId: z.string().nullable().optional(), more: z.boolean()}); 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 participant referrals and invites * * GET /campaign/{id}/participant/{participantIdOrEmail}/referrals */ export const getApiCampaignIdParticipantParticipantIdOrEmailReferrals = async (args: GetApiCampaignIdParticipantParticipantIdOrEmailReferralsArgs, options?: GetApiCampaignIdParticipantParticipantIdOrEmailReferralsOptions) => { const {baseUrl = "https://api.growsurf.com/v2", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.sortBy != null) query.append("sortBy", String(args.sortBy)) if (args.desc != null) query.append("desc", String(args.desc)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.email != null) query.append("email", String(args.email)) if (args.firstName != null) query.append("firstName", String(args.firstName)) if (args.lastName != null) query.append("lastName", String(args.lastName)) if (args.referralStatus != null) query.append("referralStatus", String(args.referralStatus)) if (args.nextId != null) query.append("nextId", String(args.nextId)) if (apiKeyAuth !== undefined) headers.set('authorization', `Bearer ${apiKeyAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/campaign/${encodeURIComponent(String(args.id))}/participant/${encodeURIComponent(String(args.participantIdOrEmail))}/referrals${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(referralListResponse, await res.json()) };