import {z} from 'zod' export type CredentialsInfoRequest = {certificates?: string | undefined, lang?: string | undefined, clientData?: string | undefined, credentialID: string, certInfo?: boolean | undefined, authInfo?: boolean | undefined}; export type CreateApiCscV0CredentialsInfoArgs = {body: CredentialsInfoRequest}; export type CreateApiCscV0CredentialsInfoOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const credentialInfoKey = z.object({status: z.string().optional(), algo: z.array(z.string()).optional(), len: z.number().int().optional(), curve: z.string().optional()}); export const credentialInfoCert = z.object({status: z.string().optional(), certificates: z.array(z.string()).optional(), issuerDN: z.string().optional(), serialNumber: z.string().optional(), subjectDN: z.string().optional(), validFrom: z.string().optional(), validTo: z.string().optional()}); export const credentialInfoPin = z.object({presence: z.string().optional(), format: z.string().optional(), label: z.string().optional(), description: z.string().optional()}); export const credentialInfoOtp = z.object({presence: z.string().optional(), type: z.string().optional(), provider: z.string().optional(), format: z.string().optional(), label: z.string().optional(), description: z.string().optional(), ID: z.string().optional()}); export const credentialsInfoResponse = z.object({description: z.string().optional(), authMode: z.string().optional(), lang: z.string().optional(), key: credentialInfoKey.optional(), cert: credentialInfoCert.optional(), SCAL: z.string().optional(), PIN: credentialInfoPin.optional(), OTP: credentialInfoOtp.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 credential details * * POST /csc/v0/credentials/info * * Also accepts auth: OAuth2_clientCredentials, OAuth2_authorizationCode */ export const createApiCscV0CredentialsInfo = async (args: CreateApiCscV0CredentialsInfoArgs, options?: CreateApiCscV0CredentialsInfoOptions) => { const {baseUrl = "https://one.digicert.com/documentmanager", fetch: fetchFn = fetch, ApiKeyAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (ApiKeyAuth !== undefined) headers.set("X-API-Key", ApiKeyAuth) const url = `${baseUrl.replace(/\/+$/, '')}/csc/v0/credentials/info${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(credentialsInfoResponse, await res.json()) };