import {z} from 'zod' export type OnrampProvider = 'bridge' | 'bridge-sandbox'; export type CreateApiV1UsersUserIdFiatKycLinkArgs = {user_id: string, 'privy-app-id': string, body: {provider: OnrampProvider, full_name?: string | undefined, email: string, type?: ('individual' | 'business') | undefined, endorsements?: Array<'sepa'> | undefined, redirect_uri?: string | undefined}}; export type CreateApiV1UsersUserIdFiatKycLinkOptions = {baseUrl?: string | undefined, appSecretAuth?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const createApiV1UsersUserIdFiatKycLinkResponse = z.object({id: z.string(), customer_id: z.string(), full_name: z.string(), email: z.string(), kyc_link: z.string(), kyc_status: z.string(), rejection_reasons: z.array(z.object({developer_reason: z.string(), reason: z.string(), created_at: z.string()})), tos_link: z.string(), tos_status: z.enum(["pending", "approved"]), persona_inquiry_type: z.string().optional(), created_at: z.string()}); 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 a KYC link for a user * * POST /v1/users/{user_id}/fiat/kyc_link */ export const createApiV1UsersUserIdFiatKycLink = async (args: CreateApiV1UsersUserIdFiatKycLinkArgs, options?: CreateApiV1UsersUserIdFiatKycLinkOptions) => { const {baseUrl = "https://api.privy.io", fetch: fetchFn = fetch, appSecretAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['privy-app-id'] != null) headers.set("privy-app-id", String(args['privy-app-id'])) if (appSecretAuth !== undefined) headers.set('authorization', `Basic ${btoa(`${appSecretAuth.username}:${appSecretAuth.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/v1/users/${encodeURIComponent(String(args.user_id))}/fiat/kyc_link${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV1UsersUserIdFiatKycLinkResponse, await res.json()) };