import {z} from 'zod' export type UserStatus = 'ACTIVE' | 'DEACTIVATED' | 'SOURCE_COUNT_CAPPED' | 'SOURCE_DATA_CAPPED' | 'TRIAL_EXPIRED'; export type UserAdmin = '*' | boolean | Array<{admin?: boolean | undefined, org_id?: number | undefined}>; export type UsersUpdate = {name?: string | undefined, email?: string | undefined, status?: UserStatus | undefined, user_tier_id?: number | undefined, user_tier?: string | undefined, password?: string | undefined, password_confirmation?: string | undefined, password_current?: string | undefined, tos_signed_at?: string | undefined, admin?: UserAdmin | undefined}; export type UpdateApiUsersUserIdArgs = {user_id: number, body: UsersUpdate}; export type UpdateApiUsersUserIdOptions = {baseUrl?: string | undefined, NexlaSessionToken?: string | undefined, fetch?: typeof fetch | undefined}; export const userTier = z.enum(["FREE", "TRIAL", "PAID", "FREE_FOREVER"]); export const userStatus = z.enum(["ACTIVE", "DEACTIVATED", "SOURCE_COUNT_CAPPED", "SOURCE_DATA_CAPPED", "TRIAL_EXPIRED"]); export const orgMembershipStatus = z.enum(["ACTIVE", "DEACTIVATED"]); export const orgMembership = z.object({id: z.number().int().optional(), name: z.string().optional(), "is_admin?": z.boolean().optional(), org_membership_status: orgMembershipStatus.optional()}); export const user = z.object({id: z.number().int().optional(), email: z.string().optional(), full_name: z.string().optional(), super_user: z.boolean().optional(), impersonated: z.boolean().optional(), default_org: z.object({id: z.number().int().optional(), name: z.string().optional()}).optional(), user_tier: userTier.optional(), status: userStatus.optional(), account_locked: z.boolean().optional(), org_memberships: z.array(orgMembership).optional(), email_verified_at: z.string().optional(), tos_signed_at: z.string().optional(), updated_at: z.string().optional(), created_at: 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 }; /** * Modify a User * * PUT /users/{user_id} */ export const updateApiUsersUserId = async (args: UpdateApiUsersUserIdArgs, options?: UpdateApiUsersUserIdOptions) => { const {baseUrl = "https://dataops.nexla.io/nexla-api", fetch: fetchFn = fetch, NexlaSessionToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (NexlaSessionToken !== undefined) headers.set('authorization', `Bearer ${NexlaSessionToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/users/${encodeURIComponent(String(args.user_id))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PUT", 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(user, await res.json()) };