import {z} from 'zod' export type SearchEndEntityCriteriaRestRequest = {property?: ('QUERY' | 'END_ENTITY_PROFILE' | 'CERTIFICATE_PROFILE' | 'CA' | 'STATUS' | 'MODIFIED_BEFORE' | 'MODIFIED_AFTER') | undefined, value?: string | undefined, operation?: ('EQUAL' | 'LIKE') | undefined}; export type SearchEndEntitiesRestRequest = {max_number_of_results?: number | undefined, current_page?: number | undefined, criteria?: Array | undefined}; export type CreateApiV1EndentitySearchArgs = {body: SearchEndEntitiesRestRequest}; export type CreateApiV1EndentitySearchOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const extendedInformationRestResponseComponent = z.object({name: z.string().optional(), value: z.string().optional()}); export const endEntityRestResponse = z.object({username: z.string().optional(), dn: z.string().optional(), subject_alt_name: z.string().optional(), email: z.string().optional(), status: z.literal("NEW, FAILED, INITIALIZED, INPROCESS, GENERATED, REVOKED, HISTORICAL, KEYRECOVERY, WAITINGFORADDAPPROVAL").optional(), token: z.literal("USERGENERATED, P12, BCFKS, JKS, PEM").optional(), extension_data: z.array(extendedInformationRestResponseComponent).optional()}); export const searchEndEntitiesRestResponse = z.object({end_entities: z.array(endEntityRestResponse).optional(), more_results: z.boolean().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 }; /** * Searches for end entity confirming given criteria. * * POST /v1/endentity/search */ export const createApiV1EndentitySearch = async (args: CreateApiV1EndentitySearchArgs, options: CreateApiV1EndentitySearchOptions) => { 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/endentity/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(searchEndEntitiesRestResponse, await res.json()) };