import {z} from 'zod' export type DeleteApiTemplatesAutomatedEmailsTypeArgs = {type: string}; export type DeleteApiTemplatesAutomatedEmailsTypeOptions = {baseUrl?: string | undefined, Bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const automatedEmailDto = z.object({type: z.string(), name: z.string(), description: z.string(), defaultSubject: z.string(), defaultBody: z.string(), variables: z.array(z.string()), blockVariables: z.array(z.string()), emailSubject: z.string().nullable().optional(), emailBody: z.string().nullable().optional(), customised: z.boolean().optional(), updatedAt: z.string().nullable().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 }; /** * Reset to default: deletes the clinic's wording, so the stock email goes out again. * * DELETE /templates/automated-emails/{type} */ export const deleteApiTemplatesAutomatedEmailsType = async (args: DeleteApiTemplatesAutomatedEmailsTypeArgs, options?: DeleteApiTemplatesAutomatedEmailsTypeOptions) => { const {baseUrl = "https://api.clinicos.vet/backend/business", fetch: fetchFn = fetch, Bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (Bearer !== undefined) headers.set('authorization', `Bearer ${Bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/templates/automated-emails/${encodeURIComponent(String(args.type))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "DELETE", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(automatedEmailDto, await res.json()) };