import {z} from 'zod' export type ApiKeyKey = string; export type GetApiApiKeysApiKeyCertificatesArgs = {apiKey: ApiKeyKey, archived?: boolean | undefined}; export type GetApiApiKeysApiKeyCertificatesOptions = {baseUrl?: string | undefined, ApiKey?: {username: string, password: string} | undefined, fetch?: typeof fetch | undefined}; export const certificateKey = z.string().min(5).max(120); export const certificateFingerprint = z.string(); export const freeTextField = z.string().min(1).max(160); export const created = z.string(); export const apiKeyCertificate = z.object({certificateKey: certificateKey, fingerprint: certificateFingerprint, notBefore: z.string(), notAfter: z.string(), description: freeTextField.optional(), created: created, trustAnchorFingerprint: certificateFingerprint, status: z.enum(["active", "archived"])}); export const apiKeyCertificatesList = z.object({certificates: z.array(apiKeyCertificate)}); 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 Key certificates [EXPERIMENTAL] * * GET /apiKeys/{apiKey}/certificates * * Also accepts auth: BearerToken */ export const getApiApiKeysApiKeyCertificates = async (args: GetApiApiKeysApiKeyCertificatesArgs, options?: GetApiApiKeysApiKeyCertificatesOptions) => { const {baseUrl = "https://eu-west-1.api.silverflow.com/v1", fetch: fetchFn = fetch, ApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.archived != null) query.append("archived", String(args.archived)) if (ApiKey !== undefined) headers.set('authorization', `Basic ${btoa(`${ApiKey.username}:${ApiKey.password}`)}`) const url = `${baseUrl.replace(/\/+$/, '')}/apiKeys/${encodeURIComponent(String(args.apiKey))}/certificates${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(apiKeyCertificatesList, await res.json()) };