import {z} from 'zod' export type InviteAdminUserPrivileges = {feature?: ('my_plan' | 'api' | 'user_management' | 'app_management') | undefined, permissions?: (Array<'all' | 'none'>) | undefined}; export type InviteAdminUser = {email: string, all_features_access: boolean, groupIds?: Array | undefined, privileges: Array}; export type CreateApiCorporateUserInvitationSendArgs = {body: InviteAdminUser}; export type CreateApiCorporateUserInvitationSendOptions = {baseUrl?: string | undefined, apiKey?: string | undefined, fetch?: typeof fetch | undefined}; export const inviteAdminUserPrivileges = z.object({feature: z.enum(["my_plan", "api", "user_management", "app_management"]).optional(), permissions: z.array(z.enum(["all", "none"])).optional()}); export const inviteAdminUser = z.object({email: z.string(), all_features_access: z.boolean(), groupIds: z.array(z.string()).optional(), privileges: z.array(inviteAdminUserPrivileges)}); 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 }; /** * Send invitation to an admin user * * POST /corporate/user/invitation/send * * Also accepts auth: partner-key */ export const createApiCorporateUserInvitationSend = async (args: CreateApiCorporateUserInvitationSendArgs, options?: CreateApiCorporateUserInvitationSendOptions) => { const {baseUrl = "https://api.brevo.com/v3", fetch: fetchFn = fetch, apiKey} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (apiKey !== undefined) headers.set("api-key", apiKey) const url = `${baseUrl.replace(/\/+$/, '')}/corporate/user/invitation/send${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(inviteAdminUser, await res.json()) };