import {z} from 'zod' export type CreateApiV1UserArgs = {Authorization: string, body: {create_agent: boolean, status: boolean, block_web_login: boolean, login_based_calling: boolean, user_for_cdr: Record, agent_group?: Array | undefined, department?: Array | undefined, time_group?: number | undefined, name: string, number: string, email: string, login_id: string, user_role?: number | undefined, password?: string | undefined, caller_id?: Array | undefined, route_call_through?: number | undefined, assign_extension?: boolean | undefined}}; export type CreateApiV1UserOptions = {baseUrl?: string | undefined, fetch?: typeof fetch | undefined}; export const createApiV1UserResponse = z.object({success: z.boolean().optional(), message: z.string().optional(), errors: z.unknown(), data: z.object({id: z.number().int().optional(), agent_id: z.string().optional(), agent_extension: z.string().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 User * * POST /v1/user */ export const createApiV1User = async (args: CreateApiV1UserArgs, options?: CreateApiV1UserOptions) => { const {baseUrl = "https://api-smartflo.tatateleservices.com", fetch: fetchFn = fetch} = options ?? {} const query = new URLSearchParams() const headers = new Headers() headers.set('content-type', "application/json") headers.set('accept', "application/json") if (args.Authorization != null) headers.set("Authorization", String(args.Authorization)) const url = `${baseUrl.replace(/\/+$/, '')}/v1/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(createApiV1UserResponse, await res.json()) };