citeright-sdk-js
Version:
An SDK to connect to the CiteRight API.
386 lines (385 loc) • 17.1 kB
JavaScript
"use strict";
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;
return g = { next: verb(0), "throw": verb(1), "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 (_) 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 };
}
};
Object.defineProperty(exports, "__esModule", { value: true });
// import * as assert from 'assert';
var APIAgent_1 = require("./APIAgent");
var CallerContext_1 = require("./CallerContext");
/**
* This is the main class that a developer would instantiate when they intend to execute calls against our API. The
* requirements are an API server URL, a client identifier, and a client password. If any user specific functions are
* to be used (like user and company), then the "login" method must be run and satisfied first.
*/
var CiteRightSDK = /** @class */ (function () {
function CiteRightSDK(apiServerUrl, apiGatewayUrl, clientIdentifier, clientIdentifierPassword, callerContext) {
if (callerContext === void 0) { callerContext = null; }
this.apiServerUrl = apiServerUrl;
this.apiGatewayUrl = apiGatewayUrl;
this.clientIdentifier = clientIdentifier;
this.clientIdentifierPassword = clientIdentifierPassword;
this.user = undefined;
if (!callerContext) {
callerContext = CallerContext_1.default.infer({});
}
this.apiAgent = new APIAgent_1.APIAgent(this.apiServerUrl, this.clientIdentifier, this.clientIdentifierPassword, callerContext.toHttpHeaders());
}
/**
* Any time after firing the login() method on this SDK, you should be able to inspect this.isLoggedIn() to verify
* that login was successful.
*
* @returns {boolean}
*/
CiteRightSDK.prototype.isLoggedIn = function () {
return this.apiAgent.isLoggedIn();
};
/**
* Use this function to have our API send a user an email asking them to reset their password. Most commonly, this
* will be triggered by an end user that is trying to recover a lost password.
*
* @param {string} email
* @returns {Promise<boolean>}
*/
CiteRightSDK.prototype.forgotPassword = function (email) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.apiAgent.executeUnauthenticatedAPICall('/users/forgotPassword', 'POST', { email: email })];
});
});
};
CiteRightSDK.prototype.resetForgottenPassword = function (passwordResetToken, newPassword, confirmPassword) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.apiAgent.executeUnauthenticatedAPICall('/users/resetForgottenPassword', 'POST', {
confirmPassword: confirmPassword,
newPassword: newPassword,
passwordResetToken: passwordResetToken,
})];
});
});
};
/**
* This will invalidate the user's auth
*/
CiteRightSDK.prototype.logout = function () {
return this.apiAgent.logout();
};
/**
* Get an oAuth token for the user
* @param {string} email
* @param {string} password
*/
CiteRightSDK.prototype.login = function (email, password) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.apiAgent.login(email, password)];
});
});
};
/**
* This is used to extract a stringified version of the login token that can later be used in reloadFromLoginToken.
* This is used when a user's login needs to survive a reload of the environment it is loaded in (like a browser).
* @returns {string}
*/
CiteRightSDK.prototype.getLoginToken = function () {
return this.apiAgent.getLoginToken();
};
/**
* Use this function to reconstitute a user's logged in session.
*
* @param {string} oauthToken
*/
CiteRightSDK.prototype.reloadFromLoginToken = function (oauthToken) {
return this.apiAgent.reloadFromLoginToken(oauthToken);
};
/**
* This will retrieve the abilities available to the connected user as a serialized object that can be used
* with the ACL class.
*
* @returns {Promise<void>}
*/
CiteRightSDK.prototype.getAbilities = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.apiAgent.executeAuthenticatedApiCall('/abilities', 'GET', {})];
});
});
};
/**
* This will get a list of Companies that the connected user has permission to manage.
*
* @returns {Promise<CompanyDTO[]>}
*/
CiteRightSDK.prototype.getCompanies = function () {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.apiAgent.executeAuthenticatedApiCall('/companies', 'GET', {})];
});
});
};
/**
* This will retrieve a single company by it's id.
*
* @param {string} companyId
* @returns {Promise<CompanyDTO>}
*/
CiteRightSDK.prototype.getCompany = function (companyId) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.apiAgent.executeAuthenticatedApiCall("/companies/" + companyId, 'GET', {})];
case 1: return [2 /*return*/, _a.sent()];
}
});
});
};
/**
* This will create a new company.
*
* @param {CompanyDTO} company
* @returns {Promise<CompanyDTO>}
*/
CiteRightSDK.prototype.createCompany = function (company) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.apiAgent.executeAuthenticatedApiCall('/companies', 'POST', company)];
});
});
};
/**
* This will save changes to an existing company.
*
* @param {CompanyDTO} company
* @returns {Promise<CompanyDTO>}
*/
CiteRightSDK.prototype.updateCompany = function (company) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.apiAgent.executeAuthenticatedApiCall('/companies', 'PUT', company)];
});
});
};
/**
* This will save changes to an existing company subscription.
*
* @param {CompanyDTO} company
* @returns {Promise<CompanyDTO>}
*/
CiteRightSDK.prototype.updateCompanySubscription = function (company) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.apiAgent.executeAuthenticatedApiCall('/companies/subscription', 'PUT', company)];
});
});
};
/**
* This will delete an existing company.
*
* @param {CompanyDTO} company
* @returns {Promise<CompanyDTO>}
*/
CiteRightSDK.prototype.deleteCompany = function (company) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.apiAgent.executeAuthenticatedApiCall('/companies', 'DELETE', company)];
});
});
};
/**
* This will retrieve a list of users for a company by the company id.
* @param {string} companyId
* @returns {Promise<UserDTO[]>}
*/
CiteRightSDK.prototype.getUsersForCompany = function (companyId) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.apiAgent.executeAuthenticatedApiCall("/companies/" + companyId + "/users", 'GET', {})];
});
});
};
/**
* This will delete a user as long as the connected user is a manager of the users company.
*
* @param {UserDTO} user
* @returns {Promise<APIResponse>}
*/
CiteRightSDK.prototype.deleteUser = function (user) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.apiAgent.executeAuthenticatedApiCall('/users', 'DELETE', user)];
});
});
};
/**
* This will create a user
*
* @param {UserDTO} user
* @returns {Promise<UserDTO>}
*/
CiteRightSDK.prototype.createUser = function (user) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.apiAgent.executeAuthenticatedApiCall('/users', 'POST', user)];
});
});
};
/**
* Retrieve a user by their ID.
*
* @param {string} userId
* @returns {Promise<UserDTO>}
*/
CiteRightSDK.prototype.getUser = function (userId) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.apiAgent.executeAuthenticatedApiCall("/users/" + userId, 'GET', {})];
});
});
};
/**
* This will update a user
*
* @param {UserDTO} user
* @returns {Promise<UserDTO>}
*/
CiteRightSDK.prototype.updateUser = function (user) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.apiAgent.executeAuthenticatedApiCall('/users', 'PUT', user)];
});
});
};
/**
* This will attempt to change a password for the connected user. If they don't know their current password, then
* use the forgotPassword function instead.
*
* @param {PasswordChangeDTO} passwordChangeDTO
* @returns {Promise<PasswordValidationModel>}
*/
CiteRightSDK.prototype.changeUserPassword = function (passwordChangeDTO) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.apiAgent.executeAuthenticatedApiCall('/users/changePassword', 'POST', passwordChangeDTO)];
});
});
};
/**
* This will check to see if a user is available for use. Note that we do not normalize the email in this
* response, since that will happen upon actually creating the company.
*
* @param {string} email
* @returns {Promise<UserAvailableModel>}
*/
CiteRightSDK.prototype.checkUserAvailability = function (email) {
return __awaiter(this, void 0, void 0, function () {
return __generator(this, function (_a) {
return [2 /*return*/, this.apiAgent.executeAuthenticatedApiCall('/users/available', 'POST', { email: email })];
});
});
};
/**
* This will register a new "off the street" user into our system and send them a welcome email.
*
* @param {RegistrationRequestDTO} registrationRequestDTO
* @returns {Promise<RegistrationResponseDTO>}
*/
CiteRightSDK.prototype.registerNewCompany = function (registrationRequestDTO) {
return this.apiAgent.executeAuthenticatedApiCall('/registerNewCompany', 'POST', registrationRequestDTO);
};
/**
* This will save an item to the user's library. It is assumed that the metadata has already been saved using
* "parseMetadata" before running this.
*
* @param item
* @param rawHtml
* @param parsedHtml
*/
CiteRightSDK.prototype.saveItem = function (item, rawHtml, parsedHtml) {
return __awaiter(this, void 0, void 0, function () {
var data;
return __generator(this, function (_a) {
data = Object.assign({}, item);
data.raw_html = rawHtml;
data.parsed_html = parsedHtml;
return [2 /*return*/, this.apiAgent.executeAuthenticatedApiCall('/item/parsed', 'POST', data)];
});
});
};
/**
* Send any error data to be logged
*
* @param error
*/
CiteRightSDK.prototype.logException = function (error) {
return this.apiAgent.executeAuthenticatedApiCall('/logException', 'POST', error);
};
/**
* This will return a list of items that the user has in their company library.
*/
CiteRightSDK.prototype.getCompanyLibrary = function (companyId) {
return this.apiAgent.executeAuthenticatedApiCall("/library/company/items/" + companyId, 'GET', {});
};
/**
* This will retrieve an item by the item's vendorDocumentIdentifier for a company.
* @param itemId
*/
CiteRightSDK.prototype.getItemByVendorId = function (companyId, vendorDocumentIdentifier) {
return this.apiAgent.executeAuthenticatedApiCall("/library/company/" + companyId + "/items/" + vendorDocumentIdentifier, 'GET', {});
};
/**
* This will retrieve an item's data by the item's id.
* @param itemId
*/
CiteRightSDK.prototype.getItemData = function (itemId) {
return this.apiAgent.executeAuthenticatedApiCall("/items/" + itemId + "/data", 'GET', {});
};
/**
* This will retrieve an item's data by the item's id.
* @param itemId
*/
CiteRightSDK.prototype.expireContent = function (itemId) {
return this.apiAgent.executeAuthenticatedApiCall("/items/" + itemId + "/data", 'DELETE', {});
};
/**
* This will retrieve an event notification from our document-collaboration-serverless project for a specific user
*/
CiteRightSDK.prototype.getNotificationsForUser = function () {
// assert(this.apiGatewayUrl, 'SDK: Missing API Gateway URL needed to get notifications');
return this.apiAgent.executeAuthenticatedApiCall('/notifications', 'GET', {}, this.apiGatewayUrl);
};
return CiteRightSDK;
}());
exports.CiteRightSDK = CiteRightSDK;