import {z} from 'zod' export type GetApiProfilesArgs = {'created_at.lt'?: string | undefined, 'created_at.lte'?: string | undefined, 'created_at.eq'?: string | undefined, 'created_at.gte'?: string | undefined, 'created_at.gt'?: string | undefined, limit?: number | undefined, order?: ('DESC' | 'ASC') | undefined, order_by?: 'CREATED_AT' | undefined, page_cursor?: string | undefined, nickname?: string | undefined, ids?: Array | undefined}; export type GetApiProfilesOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const profileType = z.enum(["NORMAL", "DEFAULT"]); export const profile = z.object({id: z.string(), nickname: z.string(), type: profileType, description: z.string().optional(), account_id: z.string().optional()}); export const listProfilesResponse = z.object({items: z.array(profile).optional(), next_page_cursor: 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 }; /** * List Profiles * * GET /profiles */ export const getApiProfiles = async (args: GetApiProfilesArgs, options?: GetApiProfilesOptions) => { const {baseUrl = "https://api.paxos.com/v2", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args['created_at.lt'] != null) query.append("created_at.lt", String(args['created_at.lt'])) if (args['created_at.lte'] != null) query.append("created_at.lte", String(args['created_at.lte'])) if (args['created_at.eq'] != null) query.append("created_at.eq", String(args['created_at.eq'])) if (args['created_at.gte'] != null) query.append("created_at.gte", String(args['created_at.gte'])) if (args['created_at.gt'] != null) query.append("created_at.gt", String(args['created_at.gt'])) if (args.limit != null) query.append("limit", String(args.limit)) if (args.order != null) query.append("order", String(args.order)) if (args.order_by != null) query.append("order_by", String(args.order_by)) if (args.page_cursor != null) query.append("page_cursor", String(args.page_cursor)) if (args.nickname != null) query.append("nickname", String(args.nickname)) for (const item of args.ids ?? []) query.append("ids", String(item)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/profiles${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(listProfilesResponse, await res.json()) };