@prass/botpress-native
Version:
A simple and powerful SDK for integrating Botpress Chat API with React Native,
54 lines (51 loc) • 1.93 kB
JavaScript
import { prepareHeaders } from '../../utils/prepareHeaders.js';
import { prepareData } from '../../utils/prepareData.js';
import { handleError } from '../../utils/errorHandler.js';
import { z } from 'zod';
const UpdateUserParamsSchema = 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(),
})
.refine((data) => data.name || data.pictureUrl || data.profile, "At least one field (name, pictureUrl, or profile) must be provided");
/**
* Internal handler for updating the current user's details.
* @param this - Botpress instance context
* @param params - Parameters containing user details to update (name, pictureUrl, profile)
* @returns Promise resolving to the updated user's details
* @throws Error if user key is missing or API call fails
*/
async function handleUpdateUser({ name, pictureUrl, profile }) {
if (!this.userKey)
throw new Error(this.errors.userNotCreated);
const { name: nm, pictureUrl: purl, profile: prf, } = UpdateUserParamsSchema.parse({ name, pictureUrl, profile });
try {
const url = this.ChatApiBaseUrl.getUrl(["users", "me"]);
const { data } = await this.axiosInstance.request({
url,
method: "PUT",
headers: prepareHeaders({
"x-user-key": this.userKey,
}),
data: prepareData({
name: nm,
pictureUrl: purl,
profile: prf,
}),
timeout: this.config.timeout,
});
return data;
}
catch (e) {
return handleError(e, "UpdateUser");
}
}
export { handleUpdateUser };
//# sourceMappingURL=updateUser.js.map