UNPKG

@prass/botpress-native

Version:

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

53 lines (50 loc) 1.9 kB
import { prepareData } from '../../utils/prepareData.js'; import { prepareHeaders } from '../../utils/prepareHeaders.js'; import { handleError } from '../../utils/errorHandler.js'; import { z } from 'zod'; const GetOrCreateUserParamsSchema = z.object({ 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 retrieving an existing user or creating a new one if not found. * @param this - Botpress instance context * @param params - Parameters containing user details (name, pictureUrl, profile) * @returns Promise resolving to the user’s details (either existing or newly created) * @throws Error if user key is missing or API call fails */ async function handleGetOrCreateUser({ name, pictureUrl, profile }) { if (!this.userKey) throw new Error(this.errors.userNotCreated); const { name: nm, pictureUrl: purl, profile: prf, } = GetOrCreateUserParamsSchema.parse({ name, pictureUrl, profile }); try { const url = this.ChatApiBaseUrl.getUrl(["users", "get-or-create"]); const payload = prepareData({ name: nm, pictureUrl: purl, profile: prf, }); const { data } = await this.axiosInstance.request({ url, method: "POST", headers: prepareHeaders({ "x-user-key": this.userKey, }), data: payload, timeout: this.config.timeout, }); return data; } catch (err) { return handleError(err, "GetOrCreateUser"); } } export { handleGetOrCreateUser }; //# sourceMappingURL=getOrCreateUser.js.map