UNPKG

app-reviews

Version:

This module help you to have access to latest reviews in App Store and Google Play Store

246 lines (245 loc) 12.3 kB
"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 }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var axios_1 = __importDefault(require("axios")); var regions_json_1 = __importDefault(require("./regions.json")); var AppStoreReviews = /** @class */ (function () { function AppStoreReviews() { var _this = this; this.BASE_URL = "https://itunes.apple.com"; this.STORE_NAME = "App Store"; this.isAppInformationEntry = function (entry) { return !(entry && entry["im:name"]); }; this.parseAppStoreReview = function (entry, region) { return { id: entry.id.label, version: _this.reviewAppVersion(entry), title: entry.title.label, text: entry.content.label, rating: _this.reviewRating(entry), author: _this.reviewAuthor(entry), link: _this.reviewLink(entry), region: region }; }; this.reviewAppVersion = function (review) { return review['im:version'] ? review['im:version'].label : ''; }; this.reviewRating = function (review) { return review['im:rating'] && !isNaN(parseInt(review['im:rating'].label)) ? parseInt(review['im:rating'].label) : -1; }; this.reviewAuthor = function (review) { return review.author ? review.author.name.label : ''; }; this.reviewLink = function (review) { return review.author ? review.author.uri.label : ''; }; this.generateSlackMessage = function (review, appInformation, config) { var stars = ""; for (var i = 0; i < 5; i++) { stars += i < review.rating ? "★" : "☆"; } var color = review.rating >= 4 ? "good" : (review.rating >= 2 ? "warning" : "danger"); var text = ""; text += review.text + "\n"; var footer = ""; if (review.version) { footer += " for v" + review.version; } if (review.link) { footer += " - " + "<" + review.link + "|" + appInformation.appName + ", " + _this.STORE_NAME + " (" + review.region + ") >"; } else { footer += " - " + appInformation.appName + ", " + _this.STORE_NAME + " (" + review.region + ")"; } var title = stars; if (review.title) { title += " – " + review.title; } return JSON.stringify({ "attachments": [ { "mrkdwn_in": ["text", "pretext", "title"], "color": color, "author_name": review.author, "thumb_url": config.showAppIcon ? (appInformation.appIcon) : config.appIcon, "title": title, "text": text, "footer": footer } ] }); }; } AppStoreReviews.prototype.fetch = function (config, publishedReviews) { return __awaiter(this, void 0, void 0, function () { var id, regions, verbose, appInformation, requests, result, reviews, newReviews, newReviewsMap; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: id = config.id, regions = config.regions, verbose = config.verbose; return [4 /*yield*/, this.fetchAppInformation(id)]; case 1: appInformation = _a.sent(); if (regions == 'all') { regions = regions_json_1.default; } if (regions.length == 0) { throw "At least one region should be define in configuration"; } requests = regions.map(function (region) { return _this.fetchAppStoreReviews(id, config.pageRange || 5, region, verbose); }); return [4 /*yield*/, Promise.all(requests)]; case 2: result = _a.sent(); reviews = []; newReviews = reviews.concat.apply(reviews, result).filter(function (review) { return !(publishedReviews[id] !== undefined && publishedReviews[id].indexOf(review.id) >= 0); }); newReviewsMap = {}; newReviewsMap[id] = newReviews.map(this.mapReviewId); return [2 /*return*/, { messages: newReviews.map(function (review) { if (config.generateMessageFromReview !== undefined) { return config.generateMessageFromReview.call(_this, review, appInformation, config); } return _this.generateSlackMessage(review, appInformation, config); }), newReviews: newReviewsMap }]; } }); }); }; AppStoreReviews.prototype.fetchAppInformation = function (appId) { return __awaiter(this, void 0, void 0, function () { var url, res, results, result, err_1; return __generator(this, function (_a) { switch (_a.label) { case 0: url = this.BASE_URL + "/lookup?id=" + appId; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, axios_1.default.get(url)]; case 2: res = _a.sent(); results = res.data.results; if (results === null || results.length == 0) { throw "Couldn't find app[" + appId + "] information"; } result = results[0]; return [2 /*return*/, { appName: result.trackCensoredName, appIcon: result.artworkUrl100, appLink: result.trackViewUrl }]; case 3: err_1 = _a.sent(); throw err_1; case 4: return [2 /*return*/]; } }); }); }; AppStoreReviews.prototype.fetchAppStoreReviews = function (appId, pagesInRange, region, verbose) { return __awaiter(this, void 0, void 0, function () { var reviews, page, pageReview; return __generator(this, function (_a) { switch (_a.label) { case 0: reviews = []; page = 1; _a.label = 1; case 1: if (!(page <= pagesInRange)) return [3 /*break*/, 4]; return [4 /*yield*/, this.fetchAppStoreReviewsByPage(appId, page, region, verbose)]; case 2: pageReview = _a.sent(); reviews = reviews.concat(pageReview); _a.label = 3; case 3: page++; return [3 /*break*/, 1]; case 4: return [2 /*return*/, reviews]; } }); }); }; AppStoreReviews.prototype.fetchAppStoreReviewsByPage = function (appId, page, region, verbose) { return __awaiter(this, void 0, void 0, function () { var url, res, entries, err_2; var _this = this; return __generator(this, function (_a) { switch (_a.label) { case 0: url = this.BASE_URL + "/" + region + "/rss/customerreviews/page=" + page + "/id=" + appId + "/sortBy=mostRecent/json"; _a.label = 1; case 1: _a.trys.push([1, 3, , 4]); return [4 /*yield*/, axios_1.default.get(url)]; case 2: res = _a.sent(); entries = res.data.feed.entry; if (entries == null || entries.length == 0 || !Array.isArray(entries)) { return [2 /*return*/, []]; } return [2 /*return*/, entries .filter(this.isAppInformationEntry) .reverse() .map(function (review) { return _this.parseAppStoreReview(review, region); })]; case 3: err_2 = _a.sent(); if (verbose) console.error("Could'nt fetch data from app store \"" + region + "\", App store may not support that region"); return [2 /*return*/, []]; case 4: return [2 /*return*/]; } }); }); }; AppStoreReviews.prototype.mapReviewId = function (review) { return review.id; }; return AppStoreReviews; }()); exports.default = AppStoreReviews;