import {z} from 'zod' export type GetApiApiV1AuthMeOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const userAccountType = z.enum(["email", "wallet"]); export const userFlagsDto = z.object({introModal: z.boolean().optional()}); export const userStatus = z.enum(["pending", "active", "suspended"]); export const organizationStatus = z.enum(["pending", "active", "suspended"]); export const paymentMethodType = z.enum(["superfluid", "wallet", "allowance_based_recurring", "stripe", "binance_pay"]); export const cryptoCurrency = z.enum(["usdc", "usdt", "pyusd", "eurc", "brett", "dai", "btc", "eth", "matic", "bnb", "sol", "busd", "trx", "food", "bonk", "wif", "goat", "spx", "truf", "pengu"]); export const checkoutSessionSplitWithdrawalAddressDto = z.object({splitPercentage: z.number().gte(0).lte(100), address: z.string()}); export const paymentSettingChainDto = z.object({chainId: z.number(), blacklistCurrencies: z.array(cryptoCurrency).optional(), withdrawalAddress: z.string().optional(), withdrawalAddresses: z.array(checkoutSessionSplitWithdrawalAddressDto).optional()}); export const paymentSettingDto = z.object({id: z.string(), createdAt: z.string().optional(), updatedAt: z.string().optional(), paymentMethodTypes: z.array(paymentMethodType).optional(), allowedChains: z.array(paymentSettingChainDto).optional(), allowedCurrencies: z.array(cryptoCurrency).optional(), preferredChainId: z.number().optional(), preferredCurrency: cryptoCurrency.optional(), allowSwap: z.boolean().optional(), applyFee: z.boolean().optional(), feePercentage: z.number().gte(0).lte(100), applyGasFee: z.boolean().optional(), allowSplitPayments: z.boolean().optional(), slippagePercentage: z.number().gte(0).lte(100).optional()}); export const authOrganizationMinimalDto = z.object({id: z.string(), name: z.string(), type: z.string(), status: organizationStatus, ownerId: z.string(), createdAt: z.string().optional(), updatedAt: z.string().optional(), paymentSetting: paymentSettingDto.optional(), features: z.object({}).optional(), referralCode: z.string().optional()}); export const authUserDto = z.object({id: z.string(), firstName: z.string(), lastName: z.string(), email: z.string(), address: z.string().optional(), role: z.string(), accountType: userAccountType, profilePicture: z.string().optional(), flags: userFlagsDto.optional(), phone: z.string().optional(), position: z.string().optional(), status: userStatus, organization: authOrganizationMinimalDto, createdAt: z.string().optional(), updatedAt: 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 }; /** * GET /api/v1/auth/me */ export const getApiApiV1AuthMe = async (options?: GetApiApiV1AuthMeOptions) => { const {baseUrl = "https://api.copperx.dev", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/auth/me${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(authUserDto, await res.json()) };