import {z} from 'zod' export type V2CurrentUserJoinTeamRequest = {invitationToken: string}; export type CreateApiV2JoinTeamByTokenArgs = {body: V2CurrentUserJoinTeamRequest}; export type CreateApiV2JoinTeamByTokenOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const v2Subject = z.enum(["SUBJECT_AD", "SUBJECT_BILLING_ACCOUNT", "SUBJECT_CAMPAIGN", "SUBJECT_CAMPAIGN_APPROVAL", "SUBJECT_CAMPAIGN_QUESTION", "SUBJECT_CATALOG", "SUBJECT_CATALOG_PRODUCT", "SUBJECT_CONTACT", "SUBJECT_CONTENT_STANDARD", "SUBJECT_CROSS_SELL_CATEGORY", "SUBJECT_CUSTOMER", "SUBJECT_FEED_SETTINGS", "SUBJECT_FILTER_MAPPING", "SUBJECT_LEDGER", "SUBJECT_LINK", "SUBJECT_NAMESPACE", "SUBJECT_ORDER", "SUBJECT_PARTNER_INVITE", "SUBJECT_PAYMENT", "SUBJECT_PRODUCT", "SUBJECT_REPORTING", "SUBJECT_REPORTING_UNTHROTTLED", "SUBJECT_ROLE", "SUBJECT_SUGGESTED_SEARCH_TERM", "SUBJECT_SUPPLIER_INFO", "SUBJECT_SYSTEM_PERMISSION", "SUBJECT_TEAM_ACCOUNT", "SUBJECT_TEAM_INVITE", "SUBJECT_TEAM_PERMISSION", "SUBJECT_USER_ACCOUNT", "SUBJECT_SYSTEM_USER_PERMISSION", "SUBJECT_SEGMENTS"]); export const v2Action = z.enum(["ACTION_READ", "ACTION_READ_WRITE"]); export const v2Permission = z.object({subject: v2Subject.optional(), action: v2Action.optional()}); export const v2PermissionList = z.object({permissions: z.array(v2Permission).optional()}); export const v2UserAccess = z.object({id: z.string().optional(), namespace: z.string().optional(), systemPermissions: z.array(v2Permission).optional(), consumedPermissions: z.record(z.string(), v2PermissionList).optional(), isAdmin: z.boolean().optional(), clientIp: z.string().optional()}); export const v2CurrentUserJoinTeamResponse = z.object({userAccess: v2UserAccess.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 }; /** * Join a team with invitation token * * POST /v2/join-team-by-token */ export const createApiV2JoinTeamByToken = async (args: CreateApiV2JoinTeamByTokenArgs, options?: CreateApiV2JoinTeamByTokenOptions) => { 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/join-team-by-token${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(v2CurrentUserJoinTeamResponse, await res.json()) };