@prass/botpress-native
Version:
A simple and powerful SDK for integrating Botpress Chat API with React Native,
48 lines (45 loc) • 1.91 kB
JavaScript
import { prepareHeaders } from '../../utils/prepareHeaders.js';
import { handleError } from '../../utils/errorHandler.js';
import { z } from 'zod';
const CreateConversationParamsSchema = z.object({
id: z.string().optional(),
});
/**
* Handles the logic to create a new conversation with the specified ID.
*
* This function sends a POST request to the Botpress API to create a new conversation. It checks for
* the presence of both the user key (to ensure that the user has been created) and the conversation ID
* before proceeding with the creation request.
*
* @param this - The Botpress class instance, providing access to class properties like `ChatApiBaseUrl` and `userKey`.
* @param {DeleteConversationParams} params - The parameters containing the conversation ID.
* @param {string} params.id - The unique identifier for the conversation to create.
*
* @returns {Promise<void>} Resolves when the conversation has been successfully created.
* @throws {Exception} If the request fails, throws a custom exception with relevant error details.
*/
async function handleCreateConversation({ id }) {
// Ensure user key is present
if (!this.userKey)
throw new Error(this.errors.userNotCreated);
const { id: cid } = CreateConversationParamsSchema.parse({ id });
try {
const url = this.ChatApiBaseUrl.getUrl(["conversations"]);
// Create the conversation by sending a POST request
const { data } = await this.axiosInstance.request({
url,
method: "POST",
headers: prepareHeaders({
"x-user-key": this.userKey,
}),
timeout: this.config.timeout,
data: { id: cid },
});
return data;
}
catch (e) {
return handleError(e, "CreateConversation");
}
}
export { handleCreateConversation };
//# sourceMappingURL=createConversation.js.map