import {z} from 'zod' export type AuthenticationSourceID = {id?: Record | undefined}; export type CreateAuthenticationSource = {id?: AuthenticationSourceID | undefined, type: string}; export type LocalePreferences = {default?: string | undefined, reports?: string | undefined}; export type UserCreateRole = {allAssetGroups?: boolean | undefined, allSites?: boolean | undefined, id: string, superuser?: boolean | undefined}; export type UserEdit = {assetGroupCount?: UserEdit | undefined, authentication?: CreateAuthenticationSource | undefined, email?: string | undefined, enabled?: boolean | undefined, id?: Record | undefined, locale?: LocalePreferences | undefined, locked?: boolean | undefined, login: string, name: string, password?: string | undefined, passwordResetOnLogin?: boolean | undefined, role: UserCreateRole, siteCount?: UserEdit | undefined}; export type UserCreateResource = {assetGroupCount?: UserEdit | undefined, authentication?: CreateAuthenticationSource | undefined, email?: string | undefined, enabled?: boolean | undefined, id?: Record | undefined, locale?: LocalePreferences | undefined, locked?: boolean | undefined, login: string, name: string, password: string, passwordResetOnLogin?: boolean | undefined, role: UserCreateRole, siteCount?: UserEdit | undefined}; export type CreateApiApi3UsersArgs = {body: UserCreateResource}; export type CreateApiApi3UsersOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const link = z.object({deprecation: z.string().optional(), href: z.string().optional(), hreflang: z.string().optional(), media: z.string().optional(), rel: z.string().optional(), title: z.string().optional(), type: z.string().optional()}); export const referenceResourceUserIDLink = z.object({id: z.object({}).optional(), links: z.array(link)}); 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 }; /** * Users * * POST /api/3/users */ export const createApiApi3Users = async (args: CreateApiApi3UsersArgs, options?: CreateApiApi3UsersOptions) => { const {baseUrl = "https://:", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json;charset=UTF-8") const url = `${baseUrl.replace(/\/+$/, '')}/api/3/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(referenceResourceUserIDLink, await res.json()) };