import {z} from 'zod' export type CompanyUserAccessDTO = {trackersRead?: (boolean | null) | undefined, trackersWrite?: (boolean | null) | undefined, trackersDelete?: (boolean | null) | undefined, trackersShowOwnOnly?: (boolean | null) | undefined, balancesRead?: (boolean | null) | undefined, balancesWrite?: (boolean | null) | undefined, balancesDelete?: (boolean | null) | undefined, landingsRead?: (boolean | null) | undefined, landingsWrite?: (boolean | null) | undefined, landingsDelete?: (boolean | null) | undefined, landingsShowOwnOnly?: (boolean | null) | undefined, pwaRead?: (boolean | null) | undefined, pwaWrite?: (boolean | null) | undefined, pwaDelete?: (boolean | null) | undefined, pwaShowOwnOnly?: (boolean | null) | undefined, pushRead?: (boolean | null) | undefined, pushWrite?: (boolean | null) | undefined, pushDelete?: (boolean | null) | undefined, pushShowOwnOnly?: (boolean | null) | undefined, pixelsRead?: (boolean | null) | undefined, pixelsWrite?: (boolean | null) | undefined, pixelsDelete?: (boolean | null) | undefined, pixelsShowOwnOnly?: (boolean | null) | undefined, offersRead?: (boolean | null) | undefined, offersWrite?: (boolean | null) | undefined, offersDelete?: (boolean | null) | undefined, offersShowOwnOnly?: (boolean | null) | undefined, appEventsRead?: (boolean | null) | undefined, appEventsShowOwnOnly?: (boolean | null) | undefined, teamsRead?: (boolean | null) | undefined, teamsWrite?: (boolean | null) | undefined, teamsDelete?: (boolean | null) | undefined, membersRead?: (boolean | null) | undefined, membersWrite?: (boolean | null) | undefined, membersDelete?: (boolean | null) | undefined}; export type CreateCompanyUserDTO = {username: string, email: string, expiresAt?: (string | null) | undefined, teamId?: (string | null) | undefined, rolePresetSlug: string, companyId: string, accessList?: (CompanyUserAccessDTO | null) | undefined}; export type CreateApiApiV1CompanyUserArgs = {body: CreateCompanyUserDTO}; export type CreateApiApiV1CompanyUserOptions = {baseUrl: string, OAuth2PasswordBearer?: string | undefined, fetch?: typeof fetch | undefined}; export const invitationLinkDTO = z.object({hash: 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 }; /** * Invite a user to a company * * POST /api/v1/company-user */ export const createApiApiV1CompanyUser = async (args: CreateApiApiV1CompanyUserArgs, options: CreateApiApiV1CompanyUserOptions) => { const {baseUrl, fetch: fetchFn = fetch, OAuth2PasswordBearer} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (OAuth2PasswordBearer !== undefined) headers.set('authorization', `Bearer ${OAuth2PasswordBearer}`) const url = `${baseUrl.replace(/\/+$/, '')}/api/v1/company-user${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(invitationLinkDTO, await res.json()) };