import {z} from 'zod' export type CreateApiUsersArgs = {body: {user: {name?: string | undefined, image?: (string | null) | undefined, send_emails?: boolean | undefined, confirm?: boolean | undefined, access_enabled?: boolean | undefined, password_flow_enabled?: boolean | undefined, metadata?: Record | undefined, notes?: (string | null) | undefined, email: string}}}; export type CreateApiUsersOptions = {baseUrl?: string | undefined, KisiLogin?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiUsersResponse = z.object({id: z.number().int(), resource_type: z.literal("User"), name: z.string(), email: z.string(), created_at: z.string(), updated_at: z.string(), metadata: z.object({}), image: z.string().nullable(), otp_required_for_login: z.boolean(), password_flow_enabled: z.boolean(), access_enabled: z.boolean(), last_accessed_at: z.string().nullable(), scim_access_enabled: z.boolean(), notes: z.string().nullable(), organization_id: z.number().int(), confirmed: z.boolean(), groups_count: z.number().int()}); 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 * * Also accepts auth: OAuth2 */ export const createApiUsers = async (args: CreateApiUsersArgs, options?: CreateApiUsersOptions) => { const {baseUrl = "https://api.kisi.io", fetch: fetchFn = fetch, KisiLogin} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (KisiLogin !== undefined) headers.set("Authorization", KisiLogin) 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(createApiUsersResponse, await res.json()) };