import {z} from 'zod' export type GetApiApiKeysArgs = {limit?: number | undefined, offset?: number | undefined, search?: string | undefined, created_by_id?: (Array | null) | undefined, created_after?: string | undefined, created_before?: string | undefined, order_by?: string | undefined}; export type GetApiApiKeysOptions = {baseUrl?: string | undefined, apiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const aPIKeyListItem = z.object({created_at: z.string(), created_by: z.string().optional(), created_by_id: z.string().optional(), health: z.enum(["ok", "read_only", "creator_removed"]), id: z.string(), key_preview: z.string(), last_used_at: z.string().optional(), name: z.string()}); export const aPIKeyListResponse = z.object({api_keys: z.array(aPIKeyListItem).nullable(), limit: z.number().int(), offset: z.number().int(), total: z.number().int()}); 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 API keys * * GET /api-keys */ export const getApiApiKeys = async (args: GetApiApiKeysArgs, options?: GetApiApiKeysOptions) => { const {baseUrl = "https://api.axilio.ai/api/v1", fetch: fetchFn = fetch, apiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (args.search != null) query.append("search", String(args.search)) if (args.created_by_id != null) query.append("created_by_id", args.created_by_id.join(',')) if (args.created_after != null) query.append("created_after", String(args.created_after)) if (args.created_before != null) query.append("created_before", String(args.created_before)) if (args.order_by != null) query.append("order_by", String(args.order_by)) if (apiKeyAuth !== undefined) headers.set("X-Axilio-Api-Key", apiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/api-keys${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(aPIKeyListResponse, await res.json()) };