import {z} from 'zod' export type CreateApiUserRequest = {operation?: ({Details?: ({FIRSTNAME: string, LASTNAME: string, USERNAME: string, EMAIL: string, SECONDARYEMAILID?: string | undefined, ROLE?: string | undefined, ROLEID?: number | undefined, DEPARTMENT?: string | undefined, LOCATION?: string | undefined, USERGROUPNAME?: string | undefined, ISSUPERADMIN?: boolean | undefined, WEBACCESS?: boolean | undefined, RESTAPIACCESS?: boolean | undefined, SDKACCESS?: boolean | undefined, ENABLEMOBILEACCESS?: boolean | undefined, ENABLEADDONACCESS?: boolean | undefined, REMOTECONNECTACCESS?: boolean | undefined, REMOTECONNECTVIASSHPROXY?: boolean | undefined, LANGUAGECODE?: string | undefined, ENABLETWOFACTOR?: boolean | undefined, ISAPIUSER?: boolean | undefined, POLICY?: string | undefined, PASSWORD?: string | undefined, RSAUSERNAME?: string | undefined, RADIUSUSERNAME?: string | undefined, PHONEFACTOR_USERNAME?: string | undefined, DUOUSERNAME?: string | undefined, PRIMARYCONTACT?: string | undefined, MOBILE_COUNTRYCODE?: string | undefined, MOBILE?: string | undefined, LANDLINE?: string | undefined, LANDLINE_COUNTRYCODE?: string | undefined, LANDLINE_EXT?: string | undefined, OWNERNAMEOPTION?: boolean | undefined, EXPIRYDATE?: string | undefined, TOKENVALIDITYDAYS?: number | undefined, HOSTCHECK?: boolean | undefined, HOSTNAME?: string | undefined}) | undefined}) | undefined}; export type CreateApiRestapiJsonV1UserArgs = {orgName?: string | undefined, body: CreateApiUserRequest}; export type CreateApiRestapiJsonV1UserOptions = {baseUrl?: string | undefined, AuthToken?: string | undefined, fetch?: typeof fetch | undefined}; export const result = z.object({status: z.enum(["Success", "Failed"]).optional(), message: z.string().optional(), statusCode: z.number().int().optional()}); export const userCreateResponse = z.object({operation: z.object({name: z.string().optional(), result: result.optional(), Details: z.object({AUTHTOKEN: z.string().optional(), USERID: z.number().int().optional()}).optional()}).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 or API user * * POST /restapi/json/v1/user */ export const createApiRestapiJsonV1User = async (args: CreateApiRestapiJsonV1UserArgs, options?: CreateApiRestapiJsonV1UserOptions) => { const {baseUrl = "https://Hostname:Port", fetch: fetchFn = fetch, AuthToken} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/x-www-form-urlencoded") headers.set('accept', "application/json") if (args.orgName != null) headers.set("orgName", String(args.orgName)) if (AuthToken !== undefined) headers.set("AUTHTOKEN", AuthToken) const url = `${baseUrl.replace(/\/+$/, '')}/restapi/json/v1/user${query.size > 0 ? `?${query}` : ''}` const res = await fetchFn(url, {method: "POST", headers, body: new URLSearchParams(Object.entries(args.body).flatMap(([key, value]) => (value == null ? [] : [[key, String(value)]])))}) if (!res.ok) throw new Error(`Request failed with status ${res.status}`, {cause: {status: res.status, body: await res.text()}}) return validateResponse(userCreateResponse, await res.json()) };