import {z} from 'zod' export type GetApiUserServiceApiKeysArgs = {search?: string | undefined, type?: ('TEAM' | 'USER' | 'SA') | undefined, email?: string | undefined, user_id?: string | undefined, team_id?: string | undefined, limit?: number | undefined, offset?: number | undefined}; export type GetApiUserServiceApiKeysOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const apiKey = z.object({id: z.string(), key: z.string(), is_legacy: z.boolean(), account_id: z.string(), type: z.enum(["TEAM", "USER"]), user_id: z.string().nullable().optional(), user_name: z.string().nullable().optional(), team_id: z.string().nullable().optional(), team_name: z.string().nullable().optional(), alias: z.string().nullable().optional(), description: z.string().nullable().optional(), expires_at: z.string().nullable().optional(), last_used: z.string().nullable().optional(), created_by: z.string().nullable().optional(), created_by_name: z.string().nullable().optional(), created_at: z.string(), updated_at: z.string().optional()}); export const apiKeyListResponse = z.object({api_keys: z.array(apiKey).optional(), total_count: z.number().int().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 API keys * * GET /user-service/api-keys */ export const getApiUserServiceApiKeys = async (args: GetApiUserServiceApiKeysArgs, options?: GetApiUserServiceApiKeysOptions) => { const {baseUrl = "https://api.firefly.ai/v2", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.search != null) query.append("search", String(args.search)) if (args.type != null) query.append("type", String(args.type)) if (args.email != null) query.append("email", String(args.email)) if (args.user_id != null) query.append("user_id", String(args.user_id)) if (args.team_id != null) query.append("team_id", String(args.team_id)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.offset != null) query.append("offset", String(args.offset)) if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/user-service/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()) };