import {z} from 'zod' export type GetApiResourcesMailV1ConfigsTemplatesArgs = {type?: ('ResetPassword' | 'ActivateUser' | 'InviteToTenant' | 'PwnedPassword' | 'MagicLink' | 'OTC' | 'ConnectNewDevice' | 'UserUsedInvitation' | 'ResetPhoneNumber' | 'BulkInvitesToTenant' | 'MFAEnroll' | 'MFAUnenroll' | 'NewMFAMethod' | 'MFARecoveryCode' | 'RemoveMFAMethod' | 'EmailVerification' | 'BruteForceProtection' | 'SuspiciousIP' | 'MFAOTC' | 'ImpossibleTravel' | 'BotDetection' | 'SmsAuthenticationEnabled' | 'UnlockUser' | 'UnlockUserSuccess' | 'ActivateUserWithCode' | 'InviteToTenantWithCode') | undefined}; export type GetApiResourcesMailV1ConfigsTemplatesOptions = {baseUrl?: string | undefined, bearer?: string | undefined, fetch?: typeof fetch | undefined}; export const getTemplateResponseDto = z.object({htmlTemplate: z.string(), senderEmail: z.string(), redirectURL: z.string(), successRedirectUrl: z.string(), subject: z.string(), fromName: z.string(), active: z.boolean(), type: z.object({}), redirectURLPattern: z.string().optional(), successRedirectUrlPattern: z.string().optional()}); export const getApiResourcesMailV1ConfigsTemplatesResponse = z.array(getTemplateResponseDto); 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 }; /** * Get template * * GET /resources/mail/v1/configs/templates */ export const getApiResourcesMailV1ConfigsTemplates = async (args: GetApiResourcesMailV1ConfigsTemplatesArgs, options?: GetApiResourcesMailV1ConfigsTemplatesOptions) => { const {baseUrl = "http://localhost:8181", fetch: fetchFn = fetch, bearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('accept', "application/json") if (args.type != null) query.append("type", String(args.type)) if (bearer !== undefined) headers.set('authorization', `Bearer ${bearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/resources/mail/v1/configs/templates${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "GET", headers}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(getApiResourcesMailV1ConfigsTemplatesResponse, await res.json()) };