import {z} from 'zod' export type ApiParamsQuerySortBy = string; export type GetApiApiV2AiacasObjectIdPkiHierarchyArgs = {skip?: number | undefined, limit?: number | undefined, type?: ('list' | 'graph') | undefined, sort_by?: ApiParamsQuerySortBy | undefined}; export type GetApiApiV2AiacasObjectIdPkiHierarchyOptions = {baseUrl: string, JWTBearerToken?: string | undefined, fetch?: typeof fetch | undefined}; export const apiParamsQuerySkip = z.number().int().gte(0); export const apiParamsQueryLimit = z.number().int().gte(0); export const getApiApiV2AiacasObjectIdPkiHierarchyResponse = z.object({count: z.number().int().gte(0).optional(), skip: apiParamsQuerySkip.optional(), limit: apiParamsQueryLimit.optional(), data: z.array(z.object({objectID: z.string().optional(), name: z.string().optional(), label: z.string().optional(), kinds: z.array(z.string()).optional()})).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 }; /** * Get PKI hierarchy of AIA CA entity * * GET /api/v2/aiacas/{object_id}/pki-hierarchy * * Also accepts auth: SignedRequest + RequestDate + HMACSignature */ export const getApiApiV2AiacasObjectIdPkiHierarchy = async (args: GetApiApiV2AiacasObjectIdPkiHierarchyArgs, options: GetApiApiV2AiacasObjectIdPkiHierarchyOptions) => { const {baseUrl, fetch: fetchFn = fetch, JWTBearerToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.skip != null) query.append("skip", String(args.skip)) if (args.limit != null) query.append("limit", String(args.limit)) if (args.type != null) query.append("type", String(args.type)) if (args.sort_by != null) query.append("sort_by", String(args.sort_by)) if (JWTBearerToken !== undefined) headers.set('authorization', `Bearer ${JWTBearerToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v2/aiacas/${encodeURIComponent(String(args.object_id))}/pki-hierarchy${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(getApiApiV2AiacasObjectIdPkiHierarchyResponse, await res.json()) };