import {z} from 'zod' export type EventFeedRequestInfo = {requestId: string}; export type EventFeedFileInfo = {fileKey: string, filename: string, isAvailable?: boolean | undefined, href?: string | undefined, isInlined?: boolean | undefined}; export type EventFeedUser = {identifier: string, displayName?: string | undefined, portrait?: EventFeedFileInfo | undefined, headline?: string | undefined, href?: string | undefined}; export type EventFeedProperty = {name: string, value: string, xheader?: boolean | undefined}; export type EventFeedOverview = {owner: EventFeedUser, threadId?: string | undefined, eventFeedProperties?: Array | undefined, subjectOverride?: string | undefined}; export type EventFeedSimpleUser = {identifier?: string | undefined, displayName?: string | undefined, href?: string | undefined}; export type EventFeedEventContent = {text?: string | undefined, textType?: ('plain' | 'html') | undefined, textHref?: string | undefined, file?: EventFeedFileInfo | undefined}; export type EventFeedFieldValue = {content?: EventFeedEventContent | undefined, originalContent?: EventFeedEventContent | undefined}; export type EventFeedField = {fieldTitle?: string | undefined, fieldValues?: Array | undefined}; export type EventFeedEvent = {eventTime: string, user: EventFeedUser, eventType: 'Post' | 'Post Edited' | 'Post Deleted' | 'Response to Post' | 'Response to Post Edited' | 'Response to Post Deleted' | 'Reaction' | 'Profile' | 'Profile Deleted' | 'Profile Edited' | 'User Added' | 'User Removed' | 'History Post', participants?: Array | undefined, body?: EventFeedEventContent | undefined, originalBody?: EventFeedEventContent | undefined, eventFields?: Array | undefined, indentation?: number | undefined}; export type EventFeedSimpleEvent = {eventTime: string, user: EventFeedSimpleUser, eventType: 'Reaction'}; export type EventFeedSection = {title?: string | undefined, events?: Array | undefined, simpleEvents?: Array | undefined}; export type EventFeedCard = {title?: string | undefined, collapsible?: boolean | undefined, collapsed?: boolean | undefined, sections: Array}; export type EventFeed = {requestInfo: EventFeedRequestInfo, overview: EventFeedOverview, cards: Array}; export type CreateApiOcEventArgs = {Host: string, body: EventFeed}; export type CreateApiOcEventOptions = {baseUrl?: string | undefined, oAuth2ClientCredentials?: string | undefined, fetch?: typeof fetch | undefined}; export const createEventFeedResponse = z.object({reconciliationId: 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 }; /** * Archive one or many Events. * * POST /oc-event */ export const createApiOcEvent = async (args: CreateApiOcEventArgs, options?: CreateApiOcEventOptions) => { const {baseUrl = "https://eventfeed.api.globalrelay.com/v2", fetch: fetchFn = fetch, oAuth2ClientCredentials} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Host != null) headers.set("Host", String(args.Host)) if (oAuth2ClientCredentials !== undefined) headers.set('authorization', `Bearer ${oAuth2ClientCredentials}`) const url = `${baseUrl.replace(/\/+$/, '')}/oc-event${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(createEventFeedResponse, await res.json()) };