import {z} from 'zod' export type GetApiProfilesOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const profile = z.object({id: z.string().optional(), display_name: z.string().optional(), email: z.string().optional(), username: z.string().optional(), type: z.enum(["user", "organization"]).optional(), picture: z.string().optional(), company_type: z.string().optional(), company_name: z.string().optional(), currency: z.string().optional(), vat_number: z.string().optional(), company_role: z.string().optional(), website_url: z.string().optional(), new_ui: z.boolean().optional(), ui_colorscheme: z.string().optional(), default_catalog: z.string().optional(), project_options_url: z.string().optional(), marketing: z.boolean().optional(), created_at: z.string().optional(), updated_at: z.string().optional(), billing_contact: z.string().optional(), current_trial: z.object({active: z.boolean().optional(), created: z.string().optional(), description: z.string().optional(), expiration: z.string().optional(), current: z.object({formatted: z.string().optional(), amount: z.string().optional(), currency: z.string().optional(), currency_symbol: z.string().optional()}).optional(), spend: z.object({formatted: z.string().optional(), amount: z.string().optional(), currency: z.string().optional(), currency_symbol: z.string().optional()}).optional(), spend_remaining: z.object({formatted: z.string().optional(), amount: z.string().optional(), currency: z.string().optional(), currency_symbol: z.string().optional(), unlimited: z.boolean().optional()}).optional(), projects: z.object({id: z.string().optional(), name: z.string().optional(), total: z.object({amount: z.number().int().optional(), currency_code: z.string().optional(), currency_symbol: z.string().optional(), formatted: z.string().optional()}).optional()}).optional(), pending_verification: z.literal("credit-card").nullable().optional(), model: z.string().optional(), days_remaining: z.number().int().optional()}).optional(), invoiced: z.boolean().optional()}); export const halLinks = z.object({self: z.object({title: z.string().optional(), href: z.string().optional()}).optional(), previous: z.object({title: z.string().optional(), href: z.string().optional()}).optional(), next: z.object({title: z.string().optional(), href: z.string().optional()}).optional()}); export const getApiProfilesResponse = z.object({count: z.number().int().optional(), profiles: z.array(profile).optional(), _links: halLinks.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 user profiles * * GET /profiles */ export const getApiProfiles = async (options?: GetApiProfilesOptions) => { const {baseUrl = "https://api.upsun.com", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") 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(getApiProfilesResponse, await res.json()) };