UNPKG

react-cimpress-comment

Version:

Visualizes comment(s) for a particular platform resource

220 lines (200 loc) 9.48 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } var fetch = require('fetch-retry'); var version = require('../../package.json').version; var CommentsClient = function () { function CommentsClient(accessToken, resourceUri, commentServiceUrl) { _classCallCheck(this, CommentsClient); this.accessToken = accessToken; this.commentServiceUrl = commentServiceUrl || 'https://comment.trdlnk.cimpress.io'; this.resourceUri = resourceUri; this.encodedResourceUri = encodeURIComponent(resourceUri); } _createClass(CommentsClient, [{ key: 'getDefaultConfig', value: function getDefaultConfig(method, jsonPayload) { var headers = new Headers(); headers.append('Authorization', 'Bearer ' + this.accessToken); headers.append('x-cimpress-comments-client-version', version); if (method !== 'GET') { headers.append('Content-Type', 'application/json'); } return { method: method, headers: headers, mode: 'cors', cache: 'default', retries: 3, retryDela: 500, retryOn: [429, 500, 502, 503], body: jsonPayload ? JSON.stringify(jsonPayload) : undefined }; } }, { key: 'getResourceUri', value: function getResourceUri(resourceUri) { if (!resourceUri) { return this.commentServiceUrl + '/v0/resources/' + this.encodedResourceUri; } var encodedResourceUri = encodeURIComponent(resourceUri); return this.commentServiceUrl + '/v0/resources/' + encodedResourceUri; } }, { key: 'getResourceCommentsUri', value: function getResourceCommentsUri(resourceUri) { if (!resourceUri) { return this.commentServiceUrl + '/v0/resources/' + this.encodedResourceUri + '/comments'; } var encodedResourceUri = encodeURIComponent(resourceUri); return this.commentServiceUrl + '/v0/resources/' + encodedResourceUri + '/comments'; } }, { key: 'fetchComments', value: function fetchComments() { var url = this.commentServiceUrl + '/v0/resources?uri=' + this.encodedResourceUri; return fetch(url, this.getDefaultConfig('GET')).then(function (response) { if (response.status === 200) { return response.json().then(function (responseJson) { var comments = responseJson.length ? responseJson[0].comments : []; return { responseJson: comments, userAccessLevel: response.headers.get('x-cimpress-resource-access-level') }; }); } else if (response.status === 401) { throw new Error('Unauthorized'); } else if (response.status === 403) { throw new Error('Forbidden'); } else { throw new Error('Unexpected status code ' + response.status); } }); } }, { key: 'postComment', value: function postComment(comment, visibility) { var _this = this; var url = this.commentServiceUrl + '/v0/resources/' + this.encodedResourceUri + '/comments'; var init = this.getDefaultConfig('POST', { comment: comment, visibility: visibility, referer: window.location.href }); return fetch(url, init).then(function (response) { if (response.status === 201) { return response.json(); } else if (response.status === 401) { throw new Error('Unauthorized'); } else if (response.status === 403) { throw new Error('Forbidden'); } else { throw new Error('Unable to create comment for: ' + _this.resourceUri + ' Status code: ' + response.status + ')'); } }); } }, { key: 'fetchComment', value: function fetchComment(commentUri) { return fetch(commentUri, this.getDefaultConfig('GET')).then(function (response) { if (response.status === 200) { return response.json(); } else if (response.status === 401) { throw new Error('Unauthorized'); } else if (response.status === 403) { throw new Error('Forbidden'); } else { throw new Error('Unable to fetch comment: ' + commentUri + ' (Status code: ' + response.status + ')'); } }); } }, { key: 'deleteComment', value: function deleteComment(commentUri) { return fetch(commentUri, this.getDefaultConfig('DELETE')).then(function (response) { if (response.status === 200) { return; } else if (response.status === 401) { throw new Error('Unauthorized'); } else if (response.status === 403) { throw new Error('Forbidden'); } else { throw new Error('Unable to delete comment: ' + commentUri + ' (Status code: ' + response.status + ')'); } }); } }, { key: 'putComment', value: function putComment(commentUri, comment, visibility) { var _this2 = this; var init = this.getDefaultConfig('PUT', { comment: comment, visibility: visibility, referer: window.location.href }); return fetch(commentUri, init).then(function (response) { if (response.status === 200) { return _this2.fetchComment(commentUri).catch(function () { throw new Error('Error retrieving the comment after putting it'); }); } else if (response.status === 401) { throw new Error('Unauthorized'); } else if (response.status === 403) { throw new Error('Forbidden'); } else { throw new Error('Unable to update comment: ' + commentUri + ' (Status code: ' + response.status + ')'); } }); } }, { key: 'markAsReadAfter', value: function markAsReadAfter(date) { var url = this.commentServiceUrl + '/v0/resources/' + this.encodedResourceUri + '/read'; var init = this.getDefaultConfig('POST', { lastReadDate: date }); return fetch(url, init).then(function (response) { if (response.status === 204 || response.status === 404) { return; } else if (response.status === 401) { throw new Error('Unauthorized'); } else if (response.status === 403) { throw new Error('Forbidden'); } else { throw new Error('Unable to mark comments as read since ' + date + ' (Status code: ' + response.status + ')'); } }); } }, { key: 'getUserInfo', value: function getUserInfo() { var _this3 = this; var url = this.commentServiceUrl + '/v0/resources/' + this.encodedResourceUri + '/userinfo'; return this.fetchComments().then(function (comments) { if (comments.responseJson.length) { return fetch(url, _this3.getDefaultConfig('GET')); } return Promise.resolve({ status: 404 }); }).then(function (response) { if (response.status === 200) { return response.json(); } else if (response.status === 401) { throw new Error('Unauthorized'); } else if (response.status === 403) { throw new Error('Forbidden'); } else if (response.status === 404) { return { unreadCount: 0 }; } else { throw new Error('Unable to fetch user info (Status code: ' + response.status + ')'); } }); } }]); return CommentsClient; }(); exports.default = CommentsClient;