UNPKG

livechat-widget

Version:

LiveChat Widget for Next.js applications

180 lines (179 loc) 9.58 kB
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; // เปลี่ยนจากค่าคงที่เป็นตัวแปรที่สามารถกำหนดได้ var API_BASE_URL = ''; // เพิ่มฟังก์ชันสำหรับกำหนด API_BASE_URL export function setApiBaseUrl(url) { API_BASE_URL = url; } export function fetchMessages(roomCode_1, appId_1) { return __awaiter(this, arguments, void 0, function (roomCode, appId, limit) { var response, data, error_1; var _a; if (limit === void 0) { limit = 50; } return __generator(this, function (_b) { switch (_b.label) { case 0: _b.trys.push([0, 3, , 4]); return [4 /*yield*/, fetch("".concat(API_BASE_URL, "/chat/rooms/").concat(roomCode, "/apps/").concat(appId, "/messages?limit=").concat(limit), { method: 'GET', headers: { 'Content-Type': 'application/json', }, })]; case 1: response = _b.sent(); if (!response.ok) { // ถ้า API ยังไม่พร้อมใช้งาน ให้ส่งค่า array ว่างกลับไป if (response.status === 404 || response.status === 500) { console.warn("API not available: ".concat(response.status, ". Returning empty array.")); return [2 /*return*/, []]; } throw new Error("Error fetching messages: ".concat(response.status)); } return [4 /*yield*/, response.json()]; case 2: data = _b.sent(); return [2 /*return*/, Array.isArray((_a = data.payload) === null || _a === void 0 ? void 0 : _a.messages) ? data.payload.messages : []]; case 3: error_1 = _b.sent(); console.error('Failed to fetch messages:', error_1); // ส่งค่า array ว่างกลับไปเมื่อเกิดข้อผิดพลาด return [2 /*return*/, []]; case 4: return [2 /*return*/]; } }); }); } export function sendMessage(message) { return __awaiter(this, void 0, void 0, function () { var response, error_2; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); return [4 /*yield*/, fetch("".concat(API_BASE_URL, "/chat/messages"), { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ user_info: message.user_info, user_code: message.user_code, app_id: message.app_id, room_code: message.room_code, message: message.content }), })]; case 1: response = _a.sent(); if (!response.ok) { console.warn("Error sending message: ".concat(response.status)); // ไม่ throw error เพื่อให้แอปทำงานต่อได้ return [2 /*return*/]; } return [3 /*break*/, 3]; case 2: error_2 = _a.sent(); console.error('Failed to send message:', error_2); return [3 /*break*/, 3]; case 3: return [2 /*return*/]; } }); }); } export function getOrCreateRoom(roomCode, appId, name) { return __awaiter(this, void 0, void 0, function () { var response, data, error_3; var _a; return __generator(this, function (_b) { switch (_b.label) { case 0: _b.trys.push([0, 3, , 4]); return [4 /*yield*/, fetch("".concat(API_BASE_URL, "/room"), { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ room_code: roomCode, app_id: appId, name: name, }), })]; case 1: response = _b.sent(); if (!response.ok) { console.warn("Error creating/getting room: ".concat(response.status)); // ส่งค่า default กลับไปเพื่อให้แอปทำงานต่อได้ return [2 /*return*/, { id: 'mock-room-id', roomCode: roomCode, appId: appId, name: name, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() }]; } return [4 /*yield*/, response.json()]; case 2: data = _b.sent(); return [2 /*return*/, ((_a = data.payload) === null || _a === void 0 ? void 0 : _a.room) || { id: 'mock-room-id', roomCode: roomCode, appId: appId, name: name, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() }]; case 3: error_3 = _b.sent(); console.error('Failed to create/get room:', error_3); // ส่งค่า default กลับไปเพื่อให้แอปทำงานต่อได้ return [2 /*return*/, { id: 'mock-room-id', roomCode: roomCode, appId: appId, name: name, createdAt: new Date().toISOString(), updatedAt: new Date().toISOString() }]; case 4: return [2 /*return*/]; } }); }); }