import {z} from 'zod' export type V2SourceType = 'INVALID' | 'LDAP' | 'LOCAL' | 'AZURE' | 'MANUAL'; export type V2User = {fqn?: string | undefined, displayName?: string | undefined, etag?: string | undefined, loginName: string, firstName?: string | undefined, lastName?: string | undefined, email?: string | undefined, sourceType?: V2SourceType | undefined}; export type CreateApiV2OrganizationsOrganizationUsersArgs = {organization: string, body: {name: string, user: V2User}}; export type CreateApiV2OrganizationsOrganizationUsersOptions = {baseUrl: string, fetch?: typeof fetch | undefined}; export const v2SourceType = z.enum(["INVALID", "LDAP", "LOCAL", "AZURE", "MANUAL"]); export const v2User = z.object({fqn: z.string().optional(), displayName: z.string().optional(), etag: z.string().optional(), loginName: z.string(), firstName: z.string().optional(), lastName: z.string().optional(), email: z.string().optional(), sourceType: v2SourceType.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 }; /** * Deprecated. This method will be removed in future versions of TSB. Use Service Accounts instead. * * POST /v2/organizations/{organization}/users */ export const createApiV2OrganizationsOrganizationUsers = async (args: CreateApiV2OrganizationsOrganizationUsersArgs, options: CreateApiV2OrganizationsOrganizationUsersOptions) => { const {baseUrl, 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/organizations/${encodeURIComponent(String(args.organization))}/users${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(v2User, await res.json()) };