UNPKG

@prass/botpress-native

Version:

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

47 lines (44 loc) 1.84 kB
import { prepareHeaders } from '../../utils/prepareHeaders.js'; import { handleError } from '../../utils/errorHandler.js'; import { z } from 'zod'; const GetOrCreateConversationParamsSchema = z.object({ id: z.string().nonempty("Conversation ID is required"), }); /** * Handles the API request to retrieve an existing conversation or create a new one. * * This function sends a POST request to the Botpress API to get or create a conversation. * * @param this - The `Botpress` class instance, providing access to class properties like `ChatApiBaseUrl` and `userKey`. * @param {GetOrCreateConversationParams} params - Parameters for retrieving or creating a conversation. * @param {string} params.id - The unique identifier for the conversation. * * @returns {Promise<CreateConversationResponse>} The retrieved or newly created conversation data. * @throws {Exception} If the request fails, throws a custom exception with relevant error details. */ async function handleGetOrCreateConversation({ id }) { if (!this.userKey) throw new Error(this.errors.userNotCreated); const { id: cid } = GetOrCreateConversationParamsSchema.parse({ id }); try { const url = this.ChatApiBaseUrl.getUrl(["conversations"]); // Perform the POST request to retrieve or create a conversation const { data } = await this.axiosInstance.request({ url, method: "POST", headers: prepareHeaders({ "x-user-key": this.userKey, }), data: { id: cid, }, timeout: this.config.timeout, }); return data; } catch (err) { return handleError(err, "GetOrCreateConversation"); } } export { handleGetOrCreateConversation }; //# sourceMappingURL=getOrCreateConversation.js.map