import {z} from 'zod' export type OnlyId = {id?: string | undefined}; export type ReminderRecurringEvent = {endsOnDate?: number | undefined, eventInterval?: number | undefined, eventOccurrenceCount?: number | undefined, eventType?: ('DAILY' | 'MONTHLY' | 'WEEKLY' | 'YEARLY') | undefined, repeatOn?: string | undefined}; export type Reminder = {id?: string | undefined, version?: string | undefined, additionalRecipients?: Array | undefined, ccMail?: string | undefined, createdDate?: number | undefined, entityId?: string | undefined, entityName?: string | undefined, lastModifiedDate?: number | undefined, lastReminderSentDate?: number | undefined, message?: string | undefined, recurringEvent?: ReminderRecurringEvent | undefined, reminderDate?: number | undefined, sendType: 'MAIL' | 'NOTIFICATION', subject: string, userId?: string | undefined}; export type CreateApiReminderArgs = {dryRun?: boolean | undefined, body: Reminder}; export type CreateApiReminderOptions = {baseUrl: string, APIToken?: string | undefined, fetch?: typeof fetch | undefined}; export const onlyId = z.object({id: z.string().optional()}); export const reminderRecurringEvent = z.object({endsOnDate: z.number().int().optional(), eventInterval: z.number().int().optional(), eventOccurrenceCount: z.number().int().optional(), eventType: z.enum(["DAILY", "MONTHLY", "WEEKLY", "YEARLY"]).optional(), repeatOn: z.string().optional()}); export const reminder = z.object({id: z.string().optional(), version: z.string().optional(), additionalRecipients: z.array(onlyId).optional(), ccMail: z.string().optional(), createdDate: z.number().int().optional(), entityId: z.string().optional(), entityName: z.string().optional(), lastModifiedDate: z.number().int().optional(), lastReminderSentDate: z.number().int().optional(), message: z.string().optional(), recurringEvent: reminderRecurringEvent.optional(), reminderDate: z.number().int().optional(), sendType: z.enum(["MAIL", "NOTIFICATION"]), subject: z.string(), userId: 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 }; /** * create a reminder * * POST /reminder */ export const createApiReminder = async (args: CreateApiReminderArgs, options: CreateApiReminderOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.dryRun != null) query.append("dryRun", String(args.dryRun)) if (APIToken !== undefined) headers.set("AuthenticationToken", APIToken) const url = `${baseUrl.replace(/\/+$/, '')}/reminder${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(reminder, await res.json()) };