import {z} from 'zod' export type Contact = {first_name?: string | undefined, last_name?: string | undefined, telephone?: string | undefined, email?: string | undefined, job_title?: string | undefined, country?: string | undefined, city?: string | undefined, state?: string | undefined, zip?: string | undefined, contact_type: 'VERIFIED_CONTACT' | 'ORGANIZATION_CONTACT', id?: number | undefined}; export type Organization = {id: number, contacts: Array}; export type CreateCredentialRequestV2 = {label: string, countryCode?: string | undefined, productName: string, organization: Organization, userId: string}; export type CreateApiApiV2CredentialArgs = {body: CreateCredentialRequestV2}; export type CreateApiApiV2CredentialOptions = {baseUrl?: string | undefined, ApiKeyAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const credentialResponse = z.object({id: z.string().optional(), account_id: z.string().optional(), userId: z.string().optional(), credential_id: z.string().optional(), name: z.string().optional(), profile_id: z.string().optional(), first_credential: 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 }; /** * Create credential v2 flow supports credential creation with CertCentral, Trustlink, MPKI8 and Dcone CA * * POST /api/v2/credential */ export const createApiApiV2Credential = async (args: CreateApiApiV2CredentialArgs, options?: CreateApiApiV2CredentialOptions) => { 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(/\/+$/, '')}/api/v2/credential${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(credentialResponse, await res.json()) };