import {z} from 'zod' export type AccessRoles = Array<'owner' | 'collaborator' | 'operator' | 'admin'>; export type OrgMemberMutable = {id: number, access_role?: AccessRoles | undefined} | {email: string, access_role?: AccessRoles | undefined}; export type OrgMemberList = {members?: Array | undefined}; export type UpdateApiOrgsOrgIdMembersArgs = {org_id: number, body: OrgMemberList}; export type UpdateApiOrgsOrgIdMembersOptions = {baseUrl?: string | undefined, NexlaSessionToken?: string | undefined, fetch?: typeof fetch | undefined}; export const accessRoles = z.array(z.enum(["owner", "collaborator", "operator", "admin"])); export const orgMembershipStatus = z.enum(["ACTIVE", "DEACTIVATED"]); export const userStatus = z.enum(["ACTIVE", "DEACTIVATED", "SOURCE_COUNT_CAPPED", "SOURCE_DATA_CAPPED", "TRIAL_EXPIRED"]); export const orgMember = z.object({id: z.number().int().optional(), full_name: z.string().optional(), email: z.string().optional(), "is_admin?": z.boolean().optional(), access_role: accessRoles.optional(), org_membership_status: orgMembershipStatus.optional(), user_status: userStatus.optional()}); export const updateApiOrgsOrgIdMembersResponse = z.array(orgMember); 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 Organization Members * * PUT /orgs/{org_id}/members */ export const updateApiOrgsOrgIdMembers = async (args: UpdateApiOrgsOrgIdMembersArgs, options?: UpdateApiOrgsOrgIdMembersOptions) => { 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/vnd.nexla.api.v1+json") headers.set('accept', "application/json") if (NexlaSessionToken !== undefined) headers.set('authorization', `Bearer ${NexlaSessionToken}`) const url = `${baseUrl.replace(/\/+$/, '')}/orgs/${encodeURIComponent(String(args.org_id))}/members${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(updateApiOrgsOrgIdMembersResponse, await res.json()) };