UNPKG

@polls-platform/core

Version:

Polls Platform core library

86 lines (85 loc) 3.96 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createPollAsync = exports.createPoll = void 0; const axios_1 = __importDefault(require("axios")); const types_1 = require("../types"); const utils_1 = require("../utils"); const error_1 = require("../error"); const config_1 = require("../config"); const constants_1 = require("../constants"); /* ****************************************************************************************************************** */ // region: Utils /* ****************************************************************************************************************** */ /** * Creates a poll and instantly provides a Poll URL that can be opened in the browser or inside of a <PollView> component. * This is the simplest way to create a poll. Loading and failure states will be handled by the <PollView> component * @category 2. Create Poll * @param pollRequest * @returns Poll Response containing URL */ function createPoll(pollRequest) { var _a, _b, _c; const processedPollRequest = Object.assign(Object.assign({}, pollRequest), { id: (_a = pollRequest.id) !== null && _a !== void 0 ? _a : (0, utils_1.getUuid)() }); const url = (0, utils_1.constructPollUrl)({ pollId: processedPollRequest.id, flow: types_1.OpenPollFlow.createPoll, pollState: processedPollRequest.state, themeName: (_b = processedPollRequest.customData) === null || _b === void 0 ? void 0 : _b.theme, themeObject: (_c = processedPollRequest.customData) === null || _c === void 0 ? void 0 : _c.themeObject, }); // don't await, we will create in background createPollAsync(processedPollRequest).then(); return { url: url }; } exports.createPoll = createPoll; /** * Asynchronously creates a poll and provides a Poll URL that can be opened in the browser or inside of a <PollView> component. * When using this function, you must handle loading and failure states. * For a more simple approach, see the `createPoll` function * @category 2. Create Poll * @param pollRequest * @returns Promise: Poll Response containing URL */ async function createPollAsync(pollRequest) { var _a, _b; const { apiKey, domainConfig: { subdomain }, } = (0, config_1.getConfig)(); const data = JSON.stringify({ poll: pollRequest }); const baseUrl = subdomain.startsWith('staging.') ? constants_1.STAGING_API_BASE_URL : constants_1.API_BASE_URL; const request = { method: 'post', url: `${baseUrl}/polls`, headers: { 'x-api-key': apiKey, 'Content-Type': 'application/json', }, data: data, }; try { const result = await (0, axios_1.default)(request); const parseResult = types_1.CreatePollResult.safeParse(result); if (!parseResult.success) { throw new Error('received unexpected response from Polls Platform API'); } return { url: parseResult.data.data.poll.url }; } catch (error) { let message = 'unknown error'; if (error instanceof Error || axios_1.default.isAxiosError(error)) { message = error.message; if (axios_1.default.isAxiosError(error)) { if ((_a = error.response) === null || _a === void 0 ? void 0 : _a.data) { const dataString = JSON.stringify(error.response.data); if (dataString) { message = `HTTP STATUS CODE: ${error.response.status} ${(_b = error.response.statusText) !== null && _b !== void 0 ? _b : ''} ${dataString}`; } } } } throw (0, error_1.createError)(`an error occurred while creating the poll: ${message}`); } } exports.createPollAsync = createPollAsync; // endregion