import {z} from 'zod' export type GetApiAdminInviteCodesArgs = {address?: string | undefined, status?: string | undefined, sort_direction?: ('SORT_DIRECTION_UNSPECIFIED' | 'SORT_DIRECTION_ASC' | 'SORT_DIRECTION_DESC') | undefined, limit?: number | undefined, page?: number | undefined, created_by?: string | undefined, code?: string | undefined, description?: string | undefined}; export type GetApiAdminInviteCodesOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const apiInviteCodeItem = z.object({code: z.string().optional(), owner_address: z.string().optional(), source: z.string().optional(), status: z.string().optional(), redeemed_by: z.string().optional(), redeemed_at: z.string().optional(), description: z.string().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), created_by: z.string().optional()}); export const apiListInviteCodesResponse = z.object({codes: z.array(apiInviteCodeItem).optional(), total: z.number().int().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 }; /** * Get invite codes * * GET /admin/invite/codes */ export const getApiAdminInviteCodes = async (args: GetApiAdminInviteCodesArgs, options?: GetApiAdminInviteCodesOptions) => { const {baseUrl = "https://api.testnet.rise.trade", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.address != null) query.append("address", String(args.address)) if (args.status != null) query.append("status", String(args.status)) if (args.sort_direction != null) query.append("sort_direction", String(args.sort_direction)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.page != null) query.append("page", String(args.page)) if (args.created_by != null) query.append("created_by", String(args.created_by)) if (args.code != null) query.append("code", String(args.code)) if (args.description != null) query.append("description", String(args.description)) const url = `${baseUrl.replace(/\/+$/, '')}/admin/invite/codes${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(apiListInviteCodesResponse, await res.json()) };