import {z} from 'zod' export type UpdateApiV1EmailTemplatesEmailTemplateIdJsonArgs = {token: string, emailTemplateId: number, body: {from_email?: string | undefined, reply_to_email?: string | undefined, subject?: string | undefined, email_text_content?: string | undefined}}; export type UpdateApiV1EmailTemplatesEmailTemplateIdJsonOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const updateApiV1EmailTemplatesEmailTemplateIdJsonResponse = z.object({id: z.number().int().optional(), created: z.string().optional(), modified: z.string().optional(), from_email: z.string().optional(), reply_to_email: z.string().optional(), name: z.string().optional(), description: z.string().optional(), subject: z.string().optional(), email_text_content: z.string().optional(), email_variables: z.string().optional(), display_name: 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 }; /** * Update an email templates * * PUT /v1/email_templates/{emailTemplateId}.json */ export const updateApiV1EmailTemplatesEmailTemplateIdJson = async (args: UpdateApiV1EmailTemplatesEmailTemplateIdJsonArgs, options: UpdateApiV1EmailTemplatesEmailTemplateIdJsonOptions) => { const {baseUrl, fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.token != null) query.append("token", String(args.token)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/email_templates/${encodeURIComponent(String(args.emailTemplateId))}.json${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(updateApiV1EmailTemplatesEmailTemplateIdJsonResponse, await res.json()) };