import {z} from 'zod' export type CreateUserRequest = {createRole?: boolean | undefined, defaultApp?: string | undefined, email?: string | undefined, forceChangePass?: boolean | undefined, fullName?: string | undefined, name: string, password: string, roles?: Array | undefined}; export type CreateApiStackAdminconfigV2UsersArgs = {stack: string, 'Federated-Search-Manage-Ack'?: string | undefined, body: CreateUserRequest}; export type CreateApiStackAdminconfigV2UsersOptions = {baseUrl: string, bearerAuth?: string | undefined, fetch?: typeof fetch | undefined}; export const usersResponse = z.object({capabilities: z.array(z.string()), defaultApp: z.string(), defaultAppSource: z.string(), email: z.string(), fullName: z.string(), lastSuccessfulLogin: z.string().optional(), lockedOut: z.boolean(), name: z.string(), roles: z.array(z.string())}); 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 new user * * POST /{stack}/adminconfig/v2/users */ export const createApiStackAdminconfigV2Users = async (args: CreateApiStackAdminconfigV2UsersArgs, options: CreateApiStackAdminconfigV2UsersOptions) => { const {baseUrl, fetch: fetchFn = fetch, bearerAuth} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args['Federated-Search-Manage-Ack'] != null) headers.set("Federated-Search-Manage-Ack", String(args['Federated-Search-Manage-Ack'])) if (bearerAuth !== undefined) headers.set('authorization', `Bearer ${bearerAuth}`) const url = `${baseUrl.replace(/\/+$/, '')}/${encodeURIComponent(String(args.stack))}/adminconfig/v2/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(usersResponse, await res.json()) };