import {z} from 'zod' export type KmsData = {pin?: string | undefined, token?: string | undefined, uuid_cert?: string | undefined, user?: string | undefined, cloud_token?: string | undefined} | {uuid_cert: string, uuid_pkey: string, user?: string | undefined, pin?: string | undefined, otp?: string | undefined, token?: string | undefined} | {token: string} | {uuid_cert: string, uuid_pkey: string, user?: string | undefined, pin?: string | undefined, otp?: string | undefined, token?: string | undefined} | {uuid_cert?: string | undefined, url: string, token: string}; export type CreateApiFwV1CmsKmsAssinaturasArgs = {Authorization?: string | undefined, kms_type: 'BRYKMS' | 'GOVBR' | 'DINAMO' | 'HSMPKCS1' | 'PSC', body: {kms_data: KmsData, algoritmoHash: string, coAssinatura?: boolean | undefined, formatoAssinatura: string, formatoDadosEntrada: string, padrao: string, perfil: string, hashes: Array, comentarios?: Array | undefined, nomeDocumentos?: Array | undefined, oidPolitica?: string | undefined}}; export type CreateApiFwV1CmsKmsAssinaturasOptions = {baseUrl?: string | undefined, JWTAccessToken?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiFwV1CmsKmsAssinaturasResponse = z.object({assinaturas: z.array(z.string()).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 }; /** * Assinar CMS/CAdES utilizando Certificado em nuvem * * POST /fw/v1/cms/kms/assinaturas */ export const createApiFwV1CmsKmsAssinaturas = async (args: CreateApiFwV1CmsKmsAssinaturasArgs, options?: CreateApiFwV1CmsKmsAssinaturasOptions) => { const {baseUrl = "https://cloud-hom.bry.com.br", fetch: fetchFn = fetch, JWTAccessToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) if (args.kms_type != null) headers.set("kms_type", String(args.kms_type)) if (JWTAccessToken !== undefined) headers.set("Authorization", JWTAccessToken) const url = `${baseUrl.replace(/\/+$/, '')}/fw/v1/cms/kms/assinaturas${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(createApiFwV1CmsKmsAssinaturasResponse, await res.json()) };