import {z} from 'zod' export type UserPost = {userId?: string | undefined, name: string, email: string, phone?: string | undefined, role?: string | undefined, expressRoles?: Array | undefined, groups?: Array | undefined, applicationRoles?: Record | undefined, password?: string | undefined, isServiceUser?: boolean | undefined, '...customAttributes'?: string | undefined}; export type CreateApiUsersArgs = {shouldNotify?: boolean | undefined, skipTemporaryPassword?: boolean | undefined, body: UserPost}; export type CreateApiUsersOptions = {baseUrl?: string | undefined, OAuth2?: string | undefined, fetch?: typeof fetch | undefined}; export const userResponse = z.object({userId: z.string(), name: z.string(), email: z.string(), phone: z.string().optional(), role: z.string().optional(), expressRoles: z.array(z.string()).optional(), groups: z.array(z.string()).optional(), applicationRoles: z.object({}).optional(), created: z.string(), modified: z.string(), isServiceUser: z.boolean().optional(), lastLogin: z.string().optional(), "...customAttributes": 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 User * * POST /users */ export const createApiUsers = async (args: CreateApiUsersArgs, options?: CreateApiUsersOptions) => { const {baseUrl = "https://env.unqork.io/api/1.0", fetch: fetchFn = fetch, OAuth2} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.shouldNotify != null) query.append("shouldNotify", String(args.shouldNotify)) if (args.skipTemporaryPassword != null) query.append("skipTemporaryPassword", String(args.skipTemporaryPassword)) if (OAuth2 !== undefined) headers.set('authorization', `Bearer ${OAuth2}`) const url = `${baseUrl.replace(/\/+$/, '')}/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(userResponse, await res.json()) };