import {z} from 'zod' export type EntityReference = {entityId?: string | undefined, entityName?: string | undefined}; export type OnlyId = {id?: string | undefined}; export type CustomAttribute = {attributeDefinitionId?: string | undefined, booleanValue?: boolean | undefined, dateValue?: number | undefined, entityId?: string | undefined, entityReferences?: Array | undefined, numberValue?: unknown | undefined, selectedValueId?: string | undefined, selectedValues?: Array | undefined, stringValue?: string | undefined}; export type User = {id?: string | undefined, version?: string | undefined, canEditDashboard?: boolean | undefined, createdDate?: number | undefined, customAttributes?: Array | undefined, email: string, faxNumber?: string | undefined, firstName?: string | undefined, imageId?: string | undefined, lastModifiedDate?: number | undefined, lastName?: string | undefined, licenses?: Array | undefined, mobilePhoneNumber?: string | undefined, phoneNumber?: string | undefined, status?: ('ACTIVE' | 'DEPARTURE' | 'NOT_ACTIVE') | undefined, superUser?: boolean | undefined, title?: string | undefined, userRoles?: Array | undefined, username?: string | undefined}; export type CreateApiUserArgs = {dryRun?: boolean | undefined, body: User}; export type CreateApiUserOptions = {baseUrl: string, APIToken?: string | undefined, fetch?: typeof fetch | undefined}; export const entityReference = z.object({entityId: z.string().optional(), entityName: z.string().optional()}); export const onlyId = z.object({id: z.string().optional()}); export const customAttribute = z.object({attributeDefinitionId: z.string().optional(), booleanValue: z.boolean().optional(), dateValue: z.number().int().optional(), entityId: z.string().optional(), entityReferences: z.array(entityReference).optional(), numberValue: z.string().optional(), selectedValueId: z.string().optional(), selectedValues: z.array(onlyId).optional(), stringValue: z.string().optional()}); export const user = z.object({id: z.string().optional(), version: z.string().optional(), canEditDashboard: z.boolean().optional(), createdDate: z.number().int().optional(), customAttributes: z.array(customAttribute).optional(), email: z.string(), faxNumber: z.string().optional(), firstName: z.string().optional(), imageId: z.string().optional(), lastModifiedDate: z.number().int().optional(), lastName: z.string().optional(), licenses: z.array(z.string()).optional(), mobilePhoneNumber: z.string().optional(), phoneNumber: z.string().optional(), status: z.enum(["ACTIVE", "DEPARTURE", "NOT_ACTIVE"]).optional(), superUser: z.boolean().optional(), title: z.string().optional(), userRoles: z.array(onlyId).optional(), username: 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 }; /** * create a user * * POST /user */ export const createApiUser = async (args: CreateApiUserArgs, options: CreateApiUserOptions) => { const {baseUrl, fetch: fetchFn = fetch, APIToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.dryRun != null) query.append("dryRun", String(args.dryRun)) if (APIToken !== undefined) headers.set("AuthenticationToken", APIToken) const url = `${baseUrl.replace(/\/+$/, '')}/user${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(user, await res.json()) };