import {z} from 'zod' export type CreateApiV2InvitesManagerArgs = {body: {emails: Array, role: 'team_admin' | 'team_finance_admin' | 'team_viewer' | 'team_payment_initiator' | 'org_admin' | 'org_finance_admin' | 'org_viewer' | 'org_hr_admin', nanoid: string | string}}; export type CreateApiV2InvitesManagerOptions = {baseUrl?: string | undefined, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV2InvitesManagerResponse = z.object({success: z.literal(true), data: z.object({invites: z.array(z.string().min(15).max(15)), typed_data: z.object({domain: z.object({name: z.string(), version: z.string(), chainId: z.number(), verifyingContract: z.string()}), types: z.object({SetRolesForwardRequest: z.array(z.object({name: z.string(), type: z.string()})), SetRole: z.array(z.object({name: z.string(), type: z.string()}))}), typed_data: z.object({from: z.string(), to: z.string(), salt: z.string(), expires: z.string(), data: z.array(z.object({role: z.string(), account: z.string()}))}), primary_type: z.string()})})}); 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 admin invites typed data * * POST /v2/invites/manager */ export const createApiV2InvitesManager = async (args: CreateApiV2InvitesManagerArgs, options?: CreateApiV2InvitesManagerOptions) => { const {baseUrl = "https://b2b-api.dev-riseworks.io", fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/v2/invites/manager${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(createApiV2InvitesManagerResponse, await res.json()) };