import {z} from 'zod' export type CustomEmailType = 'activate_gapps_user' | 'activate_o365_user' | 'lockout_notice_user' | 'password_expiration' | 'password_expiration_warning' | 'password_reset_confirmation' | 'user_change_password' | 'activate_user_custom'; export type CustomEmail = {id?: string | undefined, type: CustomEmailType, subject: string, title?: string | undefined, header?: string | undefined, body?: string | undefined, button?: string | undefined, nextStepContactInfo?: string | undefined}; export type CreateApiCustomemailsArgs = {'x-org-id'?: string | undefined, body: CustomEmail}; export type CreateApiCustomemailsOptions = {baseUrl?: string | undefined, xApiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const customEmailType = z.enum(["activate_gapps_user", "activate_o365_user", "lockout_notice_user", "password_expiration", "password_expiration_warning", "password_reset_confirmation", "user_change_password", "activate_user_custom"]); export const customEmail = z.object({id: z.string().optional(), type: customEmailType, subject: z.string(), title: z.string().optional(), header: z.string().optional(), body: z.string().optional(), button: z.string().optional(), nextStepContactInfo: 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 custom email configuration * * POST /customemails */ export const createApiCustomemails = async (args: CreateApiCustomemailsArgs, options?: CreateApiCustomemailsOptions) => { const {baseUrl = "https://console.jumpcloud.com/api/v2", fetch: fetchFn = fetch, xApiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['x-org-id'] != null) headers.set("x-org-id", String(args['x-org-id'])) if (xApiKey !== undefined) headers.set("x-api-key", xApiKey) const url = `${baseUrl.replace(/\/+$/, '')}/customemails${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(customEmail, await res.json()) };