import {z} from 'zod' export type GetApiV1ReferralArgs = {order?: 'desc' | undefined, limit?: number | undefined, cursor?: string | undefined, subaccount?: unknown | undefined, orderBy?: 'createdAt' | undefined, 'X-Ethereal-Auth': string, 'X-Ethereal-Sender': unknown, 'X-Ethereal-Signature': unknown, 'X-Ethereal-Intent': number, 'X-Ethereal-SignedAt': string}; export type GetApiV1ReferralOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const referralDto = z.object({code: z.string().optional(), codeUsageRemaining: z.number().int(), createdAt: z.number().int(), id: z.string(), referee: z.string(), refereeTotalPoints: z.string(), referrer: z.string().optional()}); export const pageOfReferralDtos = z.object({data: z.array(referralDto), hasNext: z.boolean(), nextCursor: 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 }; /** * Returns paginated list of referrals for the sender * * GET /v1/referral */ export const getApiV1Referral = async (args: GetApiV1ReferralArgs, options?: GetApiV1ReferralOptions) => { const {baseUrl = "https://api.ethereal.trade", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.order != null) query.append("order", String(args.order)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.cursor != null) query.append("cursor", String(args.cursor)) if (args.subaccount != null) query.append("subaccount", String(args.subaccount)) if (args.orderBy != null) query.append("orderBy", String(args.orderBy)) if (args['X-Ethereal-Auth'] != null) headers.set("X-Ethereal-Auth", String(args['X-Ethereal-Auth'])) if (args['X-Ethereal-Sender'] != null) headers.set("X-Ethereal-Sender", String(args['X-Ethereal-Sender'])) if (args['X-Ethereal-Signature'] != null) headers.set("X-Ethereal-Signature", String(args['X-Ethereal-Signature'])) if (args['X-Ethereal-Intent'] != null) headers.set("X-Ethereal-Intent", String(args['X-Ethereal-Intent'])) if (args['X-Ethereal-SignedAt'] != null) headers.set("X-Ethereal-SignedAt", String(args['X-Ethereal-SignedAt'])) const url = `${baseUrl.replace(/\/+$/, '')}/v1/referral${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(pageOfReferralDtos, await res.json()) };