UNPKG

@kineticdata/react

Version:
97 lines (96 loc) 4.41 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault")["default"]; Object.defineProperty(exports, "__esModule", { value: true }); exports.updateWebhook = exports.fetchWebhooks = exports.fetchWebhook = exports.deleteWebhook = exports.createWebhook = void 0; var _axios = _interopRequireDefault(require("axios")); var _helpers = require("../../helpers"); var _http = require("../http"); var validateOptions = function validateOptions(functionName, requiredOptions, options) { var kappSlug = options.kappSlug, _options$webhook = options.webhook, webhook = _options$webhook === void 0 ? {} : _options$webhook; var kappSlugMissing = webhook.type === 'Kapp' && !kappSlug; var missing = requiredOptions.filter(function (requiredOption) { return !options[requiredOption]; }); if (missing.length > 0) { throw new Error("".concat(functionName, " failed! The following required options are missing: ").concat(missing)); } if (kappSlugMissing) { throw new Error("".concat(functionName, " failed! A kappSlug is required when using type: ").concat(webhook.type)); } }; var buildEndpoint = function buildEndpoint(_ref) { var kappSlug = _ref.kappSlug, webhookName = _ref.webhookName; var basePath = kappSlug ? "".concat(_helpers.bundle.apiLocation(), "/kapps/").concat(kappSlug, "/webhooks") : "".concat(_helpers.bundle.apiLocation(), "/webhooks"); return webhookName ? "".concat(basePath, "/").concat(encodeURIComponent(webhookName)) : basePath; }; var fetchWebhooks = exports.fetchWebhooks = function fetchWebhooks() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; validateOptions('fetchWebhooks', [], options); return _axios["default"].get(buildEndpoint(options), { params: (0, _http.paramBuilder)(options), headers: (0, _http.headerBuilder)(options) }).then(function (response) { return { webhooks: response.data.webhooks }; })["catch"](_http.handleErrors); }; var fetchWebhook = exports.fetchWebhook = function fetchWebhook() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; validateOptions('fetchWebhook', ['webhookName'], options); return _axios["default"].get(buildEndpoint(options), { params: (0, _http.paramBuilder)(options), headers: (0, _http.headerBuilder)(options) }).then(function (response) { return { webhook: response.data.webhook }; })["catch"](_http.handleErrors); }; var createWebhook = exports.createWebhook = function createWebhook() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var webhook = options.webhook, kappSlug = options.kappSlug; validateOptions('createWebhook', ['webhook'], options); // For Creates, we don't append the name to the basePath (it goes in the body) // so not using the buildEndpoint function var basePath = kappSlug ? "".concat(_helpers.bundle.apiLocation(), "/kapps/").concat(kappSlug, "/webhooks") : "".concat(_helpers.bundle.apiLocation(), "/webhooks"); return _axios["default"].post(basePath, webhook, { params: (0, _http.paramBuilder)(options), headers: (0, _http.headerBuilder)(options) }).then(function (response) { return { webhook: response.data.webhook }; })["catch"](_http.handleErrors); }; var updateWebhook = exports.updateWebhook = function updateWebhook() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; var webhook = options.webhook; validateOptions('updateWebhook', ['webhookName', 'webhook'], options); return _axios["default"].put(buildEndpoint(options), webhook, { params: (0, _http.paramBuilder)(options), headers: (0, _http.headerBuilder)(options) }).then(function (response) { return { webhook: response.data.webhook }; })["catch"](_http.handleErrors); }; var deleteWebhook = exports.deleteWebhook = function deleteWebhook() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; validateOptions('deleteWebhook', ['webhookName'], options); return _axios["default"]["delete"](buildEndpoint(options), { params: (0, _http.paramBuilder)(options), headers: (0, _http.headerBuilder)(options) }).then(function (response) { return { webhook: response.data.webhook }; })["catch"](_http.handleErrors); };