import {z} from 'zod' export type CreateApiTrustExchangeContentLibraryDocumentArgs = {document_name: string, document_type: string, document_description?: string | undefined, public_trust_page_access?: string | undefined, body: BodyInit}; export type CreateApiTrustExchangeContentLibraryDocumentOptions = {baseUrl?: string | undefined, APIKeyInHeader?: string | undefined, fetch?: typeof fetch | undefined}; export const contentLibraryDocumentVersion = z.object({created_at: z.string().optional(), filename: z.string().optional(), version: z.number().int().optional(), virus_safe: z.boolean().optional()}); export const contentLibraryDocument = z.object({archived: z.boolean().optional(), created_at: z.string().optional(), created_by_email: z.string().optional(), created_by_name: z.string().optional(), description: z.string().optional(), document_type: z.string().optional(), expires_at: z.string().optional(), internal_notes: z.string().optional(), name: z.string().optional(), published_trust_center_uuids: z.array(z.string()).optional(), unpublished_trust_center_uuids: z.array(z.string()).optional(), updated_at: z.string().optional(), updated_by_email: z.string().optional(), updated_by_name: z.string().optional(), usage_count: z.number().int().optional(), uuid: z.string().optional(), versions: z.array(contentLibraryDocumentVersion).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 content library document * * POST /trust_exchange/content_library/document */ export const createApiTrustExchangeContentLibraryDocument = async (args: CreateApiTrustExchangeContentLibraryDocumentArgs, options?: CreateApiTrustExchangeContentLibraryDocumentOptions) => { const {baseUrl = "https://cyber-risk.upguard.com/api/public", fetch: fetchFn = fetch, APIKeyInHeader} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.document_name != null) query.append("document_name", String(args.document_name)) if (args.document_type != null) query.append("document_type", String(args.document_type)) if (args.document_description != null) query.append("document_description", String(args.document_description)) if (args.public_trust_page_access != null) query.append("public_trust_page_access", String(args.public_trust_page_access)) if (APIKeyInHeader !== undefined) headers.set("Authorization", APIKeyInHeader) const url = `${baseUrl.replace(/\/+$/, '')}/trust_exchange/content_library/document${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: args.body}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(contentLibraryDocument, await res.json()) };