import {z} from 'zod' export type GetApiApiV1CertificatesArgs = {hostNamePattern?: string | undefined, certType?: string | undefined, certSubType?: string | undefined}; export type GetApiApiV1CertificatesOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const v1alpha1RepositoryCertificate = z.object({certData: z.string().optional(), certInfo: z.string().optional(), certSubType: z.string().optional(), certType: z.string().optional(), serverName: z.string().optional()}); export const v1ListMeta = z.object({continue: z.string().optional(), resourceVersion: z.string().optional(), selfLink: z.string().optional()}); export const v1alpha1RepositoryCertificateList = z.object({items: z.array(v1alpha1RepositoryCertificate).optional(), metadata: v1ListMeta.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 all available repository certificates * * GET /api/v1/certificates */ export const getApiApiV1Certificates = async (args: GetApiApiV1CertificatesArgs, options: GetApiApiV1CertificatesOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.hostNamePattern != null) query.append("hostNamePattern", String(args.hostNamePattern)) if (args.certType != null) query.append("certType", String(args.certType)) if (args.certSubType != null) query.append("certSubType", String(args.certSubType)) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/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(v1alpha1RepositoryCertificateList, await res.json()) };