trello-for-wolves
Version:
Node.js wrapper for Trello API...for wolves.
102 lines (101 loc) • 4.64 kB
JavaScript
;
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseResource = void 0;
var fetchFromApi_1 = require("../utils/fetchFromApi");
/**
* Base class for resources. All other resources extend this class.
* @class
*/
var BaseResource = /** @class */ (function () {
/**
* @param config TrelloConfig object containing Trello API key, token and rate limiting options.
* @param parentElements Parent path elements.
* @param groupName Group name associated with the resource.
* @param [options] Additional options for the resource.
* @constructor
*/
function BaseResource(config, parentElements, groupName, options) {
if (options === void 0) { options = {}; }
var _a;
this.config = config;
this.parentElements = parentElements;
this.groupName = groupName;
this.identifier = "";
this.pathElements = __spreadArray(__spreadArray([], parentElements, true), [groupName], false);
this.identifier = (_a = options.identifier) !== null && _a !== void 0 ? _a : "";
if (this.identifier) {
this.pathElements.push(this.identifier);
}
}
/**
* Returns true if the path elements of this resource instance contains the
* contents of the specified group name/names argument.
* @example
* const resource = trello.boards("74836e2c91e31d1746008921").actions().getActions();
* // From the `actions` instance:
* console.log(this.isChildOf("board")); // true
* console.log(this.isChildOf("action")); // false
*
* @example
* const resource = trello.boards("74836e2c91e31d1746008921").actions().getActions();
* // From the `actions` instance:
* console.log(this.isChildOf(["board", "enterprise"])); // true
* console.log(this.isChildOf(["action", "organization"])); // false
*/
BaseResource.prototype.isChildOf = function (groupNameOrNames) {
var parentGroupName = this.pathElements[0];
// If the arg is an array, create a RegExp instance that combines all of
// the array values (separated by a pipe) and return the result of testing
// it against the parentGroupName:
if (Array.isArray(groupNameOrNames)) {
var namePattern = groupNameOrNames.join("|");
var nameRegex = new RegExp(namePattern, "ig");
return nameRegex.test(parentGroupName);
}
return parentGroupName.includes(groupNameOrNames);
};
BaseResource.prototype.apiGet = function (endpoint, paramsByName, body) {
var fullEndpoint = this.pathElements.join("/").concat(endpoint);
return this.onApiFetch("GET", fullEndpoint, paramsByName, body);
};
BaseResource.prototype.apiPut = function (endpoint, paramsByName, body) {
var fullEndpoint = this.pathElements.join("/").concat(endpoint);
return this.onApiFetch("PUT", fullEndpoint, paramsByName, body);
};
BaseResource.prototype.apiPost = function (endpoint, paramsByName, body) {
var fullEndpoint = this.pathElements.join("/").concat(endpoint);
return this.onApiFetch("POST", fullEndpoint, paramsByName, body);
};
BaseResource.prototype.apiDelete = function (endpoint, paramsByName, body) {
var fullEndpoint = this.pathElements.join("/").concat(endpoint);
return this.onApiFetch("DELETE", fullEndpoint, paramsByName, body);
};
/**
* Performs the request to the Trello API and returns response or returns the
* URL string associated with the endpoint.
* @param endpoint API endpoint for making request.
* @param method Method to perform (GET, DELETE, POST, PUT).
* @param paramsByName Params to build the full URL.
* @param body Body of the fetch call.
*/
BaseResource.prototype.onApiFetch = function (method, endpoint, paramsByName, body) {
if (body === void 0) { body = {}; }
return (0, fetchFromApi_1.fetchFromApi)({
endpoint: endpoint,
trelloConfig: this.config,
fetchConfig: { body: body, method: method },
paramsByName: paramsByName,
});
};
return BaseResource;
}());
exports.BaseResource = BaseResource;