UNPKG

@prass/botpress-native

Version:

A simple and powerful SDK for integrating Botpress Chat API with React Native,

54 lines (51 loc) 1.82 kB
import { prepareData } from '../../utils/prepareData.js'; import { prepareHeaders } from '../../utils/prepareHeaders.js'; import { handleError } from '../../utils/errorHandler.js'; import { z } from 'zod'; const CreateUserParamsSchema = z.object({ id: z.string().optional(), name: z.string().optional(), pictureUrl: z .string() .optional() .refine((val) => !val || /^https?:\/\/[^\s]+$/.test(val), "Picture URL must be a valid URL if provided"), profile: z .string() .max(1000, "Profile must be less than 1000 characters") .optional(), }); /** * Internal handler for creating a new user. * @param this - Botpress instance context * @param params - Parameters containing user details (id, name, pictureUrl, profile) * @returns Promise resolving to the created user's details * @throws Error if API call fails */ async function handleCreateUser({ id, name, pictureUrl, profile }) { try { const { id: uid, name: nm, pictureUrl: purl, profile: prf, } = CreateUserParamsSchema.parse({ id, name, pictureUrl, profile }); const url = this.ChatApiBaseUrl.getUrl(["users"]); const payload = prepareData({ id: uid, name: nm, pictureUrl: purl, profile: prf, }); const { data } = await this.axiosInstance.request({ url, method: "POST", headers: prepareHeaders(), data: payload, timeout: this.config.timeout, }); if (data === null || data === void 0 ? void 0 : data.key) { this.setUserKey(data.key); } return data; } catch (err) { return handleError(err, "CreateUser"); } } export { handleCreateUser }; //# sourceMappingURL=createUser.js.map