import {z} from 'zod' export type GetApiApiV1UserOptions = {baseUrl: string, YourAPIKey?: string | undefined, fetch?: typeof fetch | undefined}; export const iUserRead = z.object({first_name: z.string(), last_name: z.string(), email: z.string(), is_active: z.boolean().optional(), is_superuser: z.boolean().optional(), birthdate: z.string().nullable().optional(), is_enterprise: z.boolean().nullable().optional(), role_id: z.string().nullable().optional(), phone: z.string().nullable().optional(), state: z.string().nullable().optional(), country: z.string().nullable().optional(), address: z.string().nullable().optional(), active_billing_plan_id: z.string().nullable().optional(), next_billing_plan_id: z.string().nullable().optional(), stripe_customer_id: z.string().nullable().optional(), stripe_subscription_id: z.string().nullable().optional(), stripe_subscription_status: z.string().nullable().optional(), stripe_has_payment_method: z.boolean().nullable().optional(), stripe_card_brand: z.string().nullable().optional(), stripe_card_last4: z.string().nullable().optional(), is_trial: z.boolean().nullable().optional(), used_trial: z.boolean().nullable().optional(), is_paying: z.boolean().nullable().optional(), stripe_last_payment_date: z.string().nullable().optional(), billing_updated_at: z.string().nullable().optional(), last_credits_refresh: z.string().nullable().optional(), next_credits_refresh: z.string().nullable().optional(), tutorial_completed_at: z.string().nullable().optional(), id: z.string(), follower_count: z.number().int().nullable().optional(), following_count: z.number().int().nullable().optional()}).and(z.record(z.string(), z.unknown())); export const iGetResponseBaseIUserRead = z.object({message: z.string().nullable().optional(), meta: z.union([z.record(z.string(), z.unknown()), z.unknown()]).nullable().optional(), data: iUserRead.nullable().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 My Data * * GET /api/v1/user * * Also accepts auth: HTTPBearer */ export const getApiApiV1User = async (options: GetApiApiV1UserOptions) => { const {baseUrl, fetch: fetchFn = fetch, YourAPIKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (YourAPIKey !== undefined) query.set("api_key", YourAPIKey) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/user${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(iGetResponseBaseIUserRead, await res.json()) };