import {z} from 'zod' export type GetApiV1UserOptions = {baseUrl?: string | undefined, defaultValue?: string | undefined, fetch?: typeof fetch | undefined}; export const getApiV1UserResponse = z.object({id: z.number().int().optional(), username: z.string().optional(), email: z.string().optional(), email_verified_at: z.string().nullable().optional(), priority_support: z.boolean().optional(), changelog_last_viewed_at: z.string().nullable().optional(), tour_state: z.string().nullable().optional(), otp_expires_at: z.string().nullable().optional(), otp_sent_at: z.string().nullable().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), bought_theme_ids: z.string().nullable().optional(), referral_code: z.string().nullable().optional(), referral_source: z.string().nullable().optional(), tfa_enabled: z.boolean().optional(), deleted_at: z.string().nullable().optional(), mfa_trusted: z.boolean().optional(), token: z.object({type: z.string().optional(), full_access: z.boolean().optional(), permissions: z.string().optional(), shop_ids: z.string().nullable().optional()}).optional(), sellauth_shop_customer_id: z.string().nullable().optional(), sellauth_shop_balance: z.number().int().optional(), pending_shop_invites_count: z.number().int().optional(), priority_support_joined: z.boolean().optional(), has_unread_changelog: z.boolean().optional(), survey_key: z.string().optional(), survey_token: z.string().nullable().optional(), ignore_otp: z.boolean().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 User * * GET /v1/user */ export const getApiV1User = async (options?: GetApiV1UserOptions) => { const {baseUrl = "https://api.sellauth.com", fetch: fetchFn = fetch, defaultValue} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (defaultValue !== undefined) headers.set('authorization', `Bearer ${defaultValue}`) const url = `${baseUrl.replace(/\/+$/, '')}/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(getApiV1UserResponse, await res.json()) };