import {z} from 'zod' export type QueueUrl = string; export type EmailAddress = {email: string, name?: (string | null) | undefined}; export type ModifiedBy = string | null; export type ModifiedAt = string | null; export type EmailTemplateBase = {id?: number | undefined, url?: string | undefined, name?: string | undefined, queue?: QueueUrl | undefined, organization?: string | undefined, triggers?: Array | undefined, type?: ('rejection' | 'rejection_default' | 'email_with_no_processable_attachments' | 'custom') | undefined, subject?: string | undefined, message?: string | undefined, enabled?: boolean | undefined, automate?: boolean | undefined, to?: Array | undefined, cc?: Array | undefined, bcc?: Array | undefined, modified_by?: ModifiedBy | undefined, modified_at?: ModifiedAt | undefined}; export type CreateApiApiV1EmailTemplatesArgs = {body: EmailTemplateBase}; export type CreateApiApiV1EmailTemplatesOptions = {baseUrl?: string | undefined, BearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const queueUrl = z.string(); export const emailAddress = z.object({email: z.string(), name: z.string().nullable().optional()}); export const modifiedBy = z.string().nullable(); export const modifiedAt = z.string().nullable(); export const emailTemplate = z.object({id: z.number().int(), url: z.string(), name: z.string(), queue: queueUrl, organization: z.string(), triggers: z.array(z.string()), type: z.enum(["rejection", "rejection_default", "email_with_no_processable_attachments", "custom"]), subject: z.string(), message: z.string(), enabled: z.boolean(), automate: z.boolean(), to: z.array(emailAddress), cc: z.array(emailAddress), bcc: z.array(emailAddress), modified_by: modifiedBy, modified_at: modifiedAt}); 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 email template * * POST /api/v1/email_templates */ export const createApiApiV1EmailTemplates = async (args: CreateApiApiV1EmailTemplatesArgs, options?: CreateApiApiV1EmailTemplatesOptions) => { const {baseUrl = "https://example.rossum.app", fetch: fetchFn = fetch, BearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (BearerAuth !== undefined) headers.set('authorization', `Bearer ${BearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/email_templates${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(emailTemplate, await res.json()) };