import {z} from 'zod' export type SearchCertificateCriteriaRestRequest = {property?: 'QUERY, SERIAL_NUMBER, USERNAME, SUBJECT_DN, SUBJECT_ALT_NAME, EXTERNAL_ACCOUNT_BINDING_ID, END_ENTITY_PROFILE, CERTIFICATE_PROFILE, EXTERNAL_ACCOUNT_BINDING_ID, CA, STATUS, ISSUED_DATE, EXPIRE_DATE, REVOCATION_DATE' | undefined, value?: string | undefined, operation?: ('EQUAL' | 'LIKE' | 'BEGINS_WITH' | 'BEFORE' | 'AFTER') | undefined}; export type SearchCertificatesRestRequest = {max_number_of_results?: number | undefined, criteria?: Array | undefined}; export type CreateApiV1CertificateSearchArgs = {body: SearchCertificatesRestRequest}; export type CreateApiV1CertificateSearchOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const certificateRestResponse = z.object({certificate: z.string().optional(), serial_number: z.string().optional(), response_format: z.string().optional(), certificate_chain: z.array(z.string()).optional(), certificate_profile: z.string().optional(), end_entity_profile: z.string().optional()}); export const searchCertificatesRestResponse = z.object({certificates: z.array(certificateRestResponse).optional(), more_results: z.boolean().optional()}); export const createApiV1CertificateSearchResponse = z.array(searchCertificatesRestResponse); 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 }; /** * Searches for certificates confirming given criteria. * * POST /v1/certificate/search */ export const createApiV1CertificateSearch = async (args: CreateApiV1CertificateSearchArgs, options: CreateApiV1CertificateSearchOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v1/certificate/search${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: JSON.stringify(args.body)}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(createApiV1CertificateSearchResponse, await res.json()) };