UNPKG

trello-for-wolves

Version:
179 lines (178 loc) 6.66 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.stringifyQueryParams = exports.UNCHANGED_PARAM_NAMES = void 0; var lodash_snakecase_1 = __importDefault(require("lodash.snakecase")); // These param names should not be re-cased. The API documentation specifies // that these are the actual values to be passed in. exports.UNCHANGED_PARAM_NAMES = [ "allowBillableGuest", "avatarSource", "boardBackgrounds", "boardStars", "callbackURL", "cardFront", "confirmationAccepted", "customBoardBackgrounds", "customEmoji", "customFieldItems", "customFields", "customStickers", "defaultLabels", "defaultLists", "descData", "displayName", "dueComplete", "enterpriseOwned", "fieldGroup", "fileSource", "fullName", "ixLastUpdate", "keepFromSource", "labelNames", "locationName", "mimeType", "modelType", "modelTypes", "myPrefs", "onlyOrgMembers", "orgMemberType", "powerUp", "powerUps", "reactionsSummary", "returnUrl", "savedSearches", "shortUrl", "tosAccepted", "urlSource", "webhook", "webhooks", "website", "zIndex", ]; /** * Creates the query string that will be appended to the endpoint path to perform * the request to the Trello API. * @param queryParamsByName Params name/value object used to build query string. */ function stringifyQueryParams(queryParamsByName) { var validParamsByName = queryParamsByName; var nameValuePairs = []; for (var _i = 0, _a = Object.entries(validParamsByName); _i < _a.length; _i++) { var _b = _a[_i], queryParamName = _b[0], queryParamValue = _b[1]; var isParamADate = queryParamValue instanceof Date; var isNested = typeof queryParamValue === "object" && !isParamADate && !Array.isArray(queryParamValue) && queryParamValue !== null; // If the value of the entry is an object (rather than a value), the // corresponding child properties need to be combined for the URL string. if (isNested) { var nestedString = getNameValueStringForNestedParams(queryParamName, validParamsByName); nameValuePairs.push(nestedString); // These are simple key/value pairs in which the value is a string or // number. } else { // Ensure the separator key specified for handling nested args isn't // present in the query string. if (queryParamName !== "separator") { var validParamName = getParamNameWithValidCasing(queryParamName); var validParamValue = stringifyParamIfDate(queryParamValue); nameValuePairs.push("".concat(validParamName, "=").concat(validParamValue)); } } } return nameValuePairs.join("&"); } exports.stringifyQueryParams = stringifyQueryParams; /** * Returns a string to append to the query string that accommodates for nested * entities. These need to be un-nested to successfully perform the API call. * @param parentParamName Name of the key containing children. * @param queryParams Arguments to parse. * @example * queryParams = { * prefs: { * invitations: "admins" * selfJoin: true, * } * separator: "/", * }; * // prefs/invitations=admins&prefs/selfJoin=true * */ function getNameValueStringForNestedParams(parentParamName, queryParams) { var childGroup = queryParams[parentParamName]; var nameValuePairs = []; var separator = "/"; if ("separator" in queryParams) { separator = queryParams.separator; } for (var _i = 0, _a = Object.entries(childGroup); _i < _a.length; _i++) { var _b = _a[_i], childParamName = _b[0], childParamValue = _b[1]; var validValue = stringifyParamIfDate(childParamValue); var joinedParamName = "".concat(parentParamName).concat(separator).concat(childParamName); nameValuePairs.push("".concat(joinedParamName, "=").concat(validValue)); } return nameValuePairs.join("&"); } /** * Returns the param name for building the query string with the correct casing. * The `queryParamsByName` object passed into the request function has camel * cased keys. The Trello API param names are snake cased (with a few exceptions). */ function getParamNameWithValidCasing(paramName) { if (exports.UNCHANGED_PARAM_NAMES.includes(paramName)) { return paramName; } // All of the params that start with "id" (e.g. idBoard, idCard, etc.) // shouldn't be re-cased. if (paramName.substr(0, 2) === "id") { return paramName; } // Ensure this doesn't get converted to one word. if (paramName === "cardBoard") { return "card_board"; } return reCaseParamNameIfRequired(paramName); } /** * Return the specified paramName with the correct snake casing. */ function reCaseParamNameIfRequired(paramName) { var snakeCasedParamName = (0, lodash_snakecase_1.default)(paramName); var validParamName = snakeCasedParamName; // These are fields that have been re-cased to ensure all the other words // are separated by underscores, but only part of the key needs to be // changed. /* eslint-disable */ var replaceByKey = { member_creator: { search: "_creator", replace: "Creator" }, _voted: { search: "_voted", replace: "Voted" }, plugin_data: { search: "_data", replace: "Data" }, _invited: { search: "_invited", replace: "Invited" }, check_item: { search: "check_item", replace: "checkItem" }, _state: { search: "_state", replace: "State" }, sort_by: { search: "sort_by", replace: "sortBy" }, sort_order: { search: "sort_order", replace: "sortOrder" }, start_index: { search: "start_index", replace: "startIndex" }, }; /* eslint-enable */ for (var _i = 0, _a = Object.entries(replaceByKey); _i < _a.length; _i++) { var _b = _a[_i], field = _b[0], _c = _b[1], search = _c.search, replace = _c.replace; if (validParamName.includes(field)) { validParamName = validParamName.replace(search, replace); } } return validParamName; } /** * Since dates have to be a valid ISO string (or null), this converts a * date instance passed in to a valid value. */ function stringifyParamIfDate(dateValue) { return dateValue instanceof Date ? dateValue.toISOString() : dateValue; }