import {z} from 'zod' export type NotificationCommonElementType = 'UNSUBSCRIBE'; export type NotificationCommonEmailElementWS = {content: string, defaultCommonElement?: boolean | undefined, enabled?: boolean | undefined, id?: number | undefined, locale: string, partner?: string | undefined, type: NotificationCommonElementType}; export type CreateApiNotificationV1TemplatesCommonEmailArgs = {body: NotificationCommonEmailElementWS}; export type CreateApiNotificationV1TemplatesCommonEmailOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiNotificationV1TemplatesCommonEmailResponse = z.object({}); 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 or update a common email template element * * POST /notification/v1/templates/common/email */ export const createApiNotificationV1TemplatesCommonEmail = async (args: CreateApiNotificationV1TemplatesCommonEmailArgs, options?: CreateApiNotificationV1TemplatesCommonEmailOptions) => { const {baseUrl = "https://marketplace.appdirect.com/api", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/notification/v1/templates/common/email${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(createApiNotificationV1TemplatesCommonEmailResponse, await res.json()) };