import {z} from 'zod' export type OrganizationRoles = 'organizationOwner' | 'organizationMember' | 'projectCreator' | 'organizationBillingAdmin' | 'organizationBillingViewer' | 'organizationReadOnly'; export type ProjectRoles = 'projectOwner' | 'projectManager' | 'projectViewer' | 'projectDataReaderWriter' | 'projectDataReader'; export type ResourceType = 'project'; export type Resource = {type?: ResourceType | undefined, id: string, roles: Array}; export type PatchEntry = {op: 'add' | 'remove', path: string, value?: (Array | Array | Resource) | undefined}; export type PatchUserRequest = Array; export type PatchApiV4OrganizationsOrganizationIdUsersUserIdArgs = {body: PatchUserRequest}; export type PatchApiV4OrganizationsOrganizationIdUsersUserIdOptions = {baseUrl?: string | undefined, token?: string | undefined, fetch?: typeof fetch | undefined}; export const email = z.string(); export const organizationRoles = z.enum(["organizationOwner", "organizationMember", "projectCreator", "organizationBillingAdmin", "organizationBillingViewer", "organizationReadOnly"]); export const resourceType = z.literal("project"); export const projectRoles = z.enum(["projectOwner", "projectManager", "projectViewer", "projectDataReaderWriter", "projectDataReader"]); export const resource = z.object({type: resourceType.optional(), id: z.string(), roles: z.array(projectRoles)}); export const couchbaseAuditData = z.object({createdBy: z.string(), createdAt: z.string(), modifiedBy: z.string(), modifiedAt: z.string(), version: z.number().int()}); export const getUserResponse = z.object({id: z.string(), name: z.string().max(128), email: email, status: z.enum(["verified", "not-verified", "pending-primary"]), inactive: z.boolean(), organizationId: z.string(), organizationRoles: z.array(organizationRoles), lastLogin: z.string().optional(), region: z.string().optional(), timeZone: z.string().optional(), enableNotifications: z.boolean().optional(), expiresAt: z.string().optional(), resources: z.array(resource).optional(), audit: couchbaseAuditData}); 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 }; /** * Update User * * PATCH /v4/organizations/{organizationId}/users/{userId} */ export const patchApiV4OrganizationsOrganizationIdUsersUserId = async (args: PatchApiV4OrganizationsOrganizationIdUsersUserIdArgs, options?: PatchApiV4OrganizationsOrganizationIdUsersUserIdOptions) => { const {baseUrl = "https://cloudapi.cloud.couchbase.com", fetch: fetchFn = fetch, token} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (token !== undefined) headers.set('authorization', `Bearer ${token}`) const url = `${baseUrl.replace(/\/+$/, '')}/v4/organizations/${encodeURIComponent(String(args.organizationId))}/users/${encodeURIComponent(String(args.userId))}${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "PATCH", 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(getUserResponse, await res.json()) };