import {z} from 'zod' export type Authv2RoleTypes = 'ROLE_TYPES_SUPPLIER_REPORT_VIEW' | 'ROLE_TYPES_SUPPLIER_FULL' | 'ROLE_TYPES_RETAILER_REPORT_VIEW' | 'ROLE_TYPES_RETAILER_FULL'; export type V2InviteEmailLanguageCode = 'EN_US' | 'CS_CZ' | 'FR_FR' | 'HU_HU' | 'IT_IT' | 'JA_JP' | 'KO_KR' | 'PT_BR' | 'SK_SK' | 'ES_ES'; export type V2InviteTeam = {managerTeamId?: string | undefined, roleIds: Array, teamId: string, emailToInvite: string, inviteUrl?: string | undefined, languageCode?: V2InviteEmailLanguageCode | undefined}; export type V2CreateUserTeamInviteRequest = {inviteTeam: V2InviteTeam}; export type CreateApiV2InviteTeamsArgs = {body: V2CreateUserTeamInviteRequest}; export type CreateApiV2InviteTeamsOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const v2CreateUserTeamInviteResponse = z.object({invitationToken: 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 }; /** * Invite a user to the team * * POST /v2/invite_teams */ export const createApiV2InviteTeams = async (args: CreateApiV2InviteTeamsArgs, options?: CreateApiV2InviteTeamsOptions) => { const {baseUrl = "https://auth..citrusad.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") const url = `${baseUrl.replace(/\/+$/, '')}/v2/invite_teams${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(v2CreateUserTeamInviteResponse, await res.json()) };