UNPKG

@bonniernews/abbe-api-utils

Version:

Utilities for converting to Abbe article format

363 lines (362 loc) 10.4 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.PremiumLevel = exports.PresenceState = exports.RelevanceLength = exports.NewsValue = exports.ArticleState = void 0; const Puff_1 = __importDefault(require("./Puff")); const Content_1 = __importDefault(require("../draftjs/types/Content")); const schemaValidator_1 = require("../schemaValidator"); const utils_1 = require("../draftjs/utils"); class Article { constructor() { this._presenceState = PresenceState.ACTIVE; this._functionTags = []; this._stories = []; this._bylines = []; this._categories = []; this._locations = []; this._tags = []; this._sections = []; this._points = []; } static initialize() { return new Article(); } static initializeContent() { return Content_1.default.initialize(); } get uuid() { return this._uuid; } setUuid(value) { this._uuid = value; return this; } get headline() { return this._headline; } setHeadline(value) { this._headline = value; return this; } get articleState() { return this._articleState; } setArticleState(value) { this._articleState = value; return this; } get newsValue() { return this._newsValue; } setNewsValue(value) { this._newsValue = value; return this; } get originalSource() { return this._originalSource; } setOriginalSource(value) { this._originalSource = value; return this; } get relevanceLength() { var _a; return (_a = this._relevanceLength) !== null && _a !== void 0 ? _a : RelevanceLength.NORMAL; } setRelevanceLength(value) { this._relevanceLength = value; return this; } get presenceState() { return this._presenceState; } get premiumLevel() { return this._premiumLevel; } setPremiumLevel(value) { this._premiumLevel = value; return this; } get publishedAt() { return this._publishedAt; } setPublishedAt(value) { this._publishedAt = value; return this; } setContent(value) { this._content = value; return this; } get content() { return this._content; } get functionTags() { return this._functionTags; } setFunctionTags(value) { this._functionTags = value; return this; } get stories() { return this._stories; } setStories(value) { this._stories = value; return this; } get bylines() { return this._bylines; } setBylines(value) { this._bylines = value; return this; } get temporaryByline() { return this._temporaryByline; } setTemporaryByline(value) { this._temporaryByline = value; return this; } get articleType() { return this._articleType; } setArticleType(value) { this._articleType = value; return this; } get categories() { return this._categories; } setCategories(value) { this._categories = value; return this; } get locations() { return this._locations; } setLocations(value) { this._locations = value; return this; } get tags() { return this._tags; } setTags(value) { this._tags = value; return this; } get puff() { return this._puff; } setPuff(value) { this._puff = value; return this; } get channels() { return this._channels; } setChannels(value) { this._channels = value; return this; } get created() { return this._created; } setCreated(value) { this._created = value; return this; } get updated() { return this._updated; } setUpdated(value) { this._updated = value; return this; } get externalId() { return this._externalId; } setExternalId(value) { this._externalId = value; return this; } get sections() { return this._sections; } setSections(value) { this._sections = value; return this; } get points() { return this._points; } setPoints(value) { this._points = value || []; return this; } get unpublishedAt() { return this._unpublishedAt; } setUnpublishedAt(value) { this._unpublishedAt = value; return this; } get expireRelevanceAt() { return this._expireRelevanceAt; } setExpireRelevanceAt(value) { this._expireRelevanceAt = value; this._relevanceLength = RelevanceLength.CUSTOM; return this; } get liveStart() { return this._liveStart; } setLiveStart(value) { this._liveStart = value; return this; } get liveStop() { return this._liveStop; } setLiveStop(value) { this._liveStop = value; return this; } get toolsCategories() { return this._toolsCategories; } setToolsCategories(value) { this._toolsCategories = value; return this; } get publishingGroup() { return this._publishingGroup; } setPublishingGroup(value) { this._publishingGroup = value; return this; } get externalUrl() { return this._externalUrl; } setExternalUrl(value) { this._externalUrl = value; return this; } async validate() { if (!this.content) { throw new Error("Article content must be set"); } this._content.validate(); const abbeFormat = this.toAbbeFormat(); return abbeFormat; } async validateSchema(abbeUrl) { const abbeFormat = this.toAbbeFormat(); await (0, schemaValidator_1.validateArticleSchema)(abbeFormat, abbeUrl); return this; } async finalize() { var _a; (_a = this._content) === null || _a === void 0 ? void 0 : _a.addTitleIfMissing(this._headline); if (!this.puff) { this._puff = Puff_1.default.initialize().setHeadLine(this.headline); } const validatedAbbeFormat = await this.validate(); return validatedAbbeFormat; } _toUuidObjects(uuids) { if (!uuids) return null; return [...new Set(uuids)].map((uuid) => ({ uuid })); } _formatDate(date) { return date ? date.toISOString() : undefined; } _toUnstyledTextContent(text) { if (!text) return undefined; return JSON.stringify(Content_1.default.initialize().addText(text).finalize({ validate: false })); } toAbbeFormat() { var _a; const article = { uuid: this.uuid, headline: this.headline, articleState: this.articleState, newsValue: this.newsValue, originalSource: this.originalSource, relevanceLength: this.relevanceLength, presenceState: this.presenceState, premiumLevel: this.premiumLevel, publishedAt: this._formatDate(this.publishedAt), content: JSON.stringify(this.content.finalize()), functionTags: this._toUuidObjects(this.functionTags), stories: this._toUuidObjects(this.stories), bylines: this._toUuidObjects(this.bylines), temporaryByline: this._toUnstyledTextContent(this._temporaryByline), articleTypes: this._toUuidObjects([this.articleType]), categories: this._toUuidObjects(this.categories), locations: this._toUuidObjects(this.locations), tags: this._toUuidObjects(this.tags), channels: this._toUuidObjects(this.channels), created: this._formatDate(this.created), updated: this._formatDate(this.updated), unpublishedAt: this._formatDate(this.unpublishedAt), expireRelevanceAt: this._formatDate(this.expireRelevanceAt), liveStart: this._formatDate(this.liveStart), liveStop: this._formatDate(this.liveStop), externalId: this.externalId, externalUrl: this.externalUrl, sections: this._toUuidObjects(this.sections), points: this.points, toolsCategories: (_a = this._toUuidObjects(this.toolsCategories)) !== null && _a !== void 0 ? _a : [], publishingGroup: this.publishingGroup ? { uuid: this.publishingGroup } : undefined, puff: this.puff.finalize(), graphicsBylines: [], photoBylines: [], topWidgets: [], bottomWidgets: [], preambleAreaWidgets: [], }; (0, utils_1.removeUndefinedProperties)(article); return article; } } exports.default = Article; var ArticleState; (function (ArticleState) { ArticleState["PUBLISHED"] = "PUBLISHED"; ArticleState["DRAFT"] = "DRAFT"; })(ArticleState || (exports.ArticleState = ArticleState = {})); var NewsValue; (function (NewsValue) { NewsValue["ZERO"] = "ZERO"; NewsValue["ONE"] = "ONE"; NewsValue["TWO"] = "TWO"; NewsValue["THREE"] = "THREE"; NewsValue["FOUR"] = "FOUR"; NewsValue["FIVE"] = "FIVE"; NewsValue["SIX"] = "SIX"; })(NewsValue || (exports.NewsValue = NewsValue = {})); var RelevanceLength; (function (RelevanceLength) { RelevanceLength["SHORT"] = "SHORT"; RelevanceLength["NORMAL"] = "NORMAL"; RelevanceLength["LONG"] = "LONG"; RelevanceLength["CUSTOM"] = "CUSTOM"; })(RelevanceLength || (exports.RelevanceLength = RelevanceLength = {})); var PresenceState; (function (PresenceState) { PresenceState["ACTIVE"] = "ACTIVE"; })(PresenceState || (exports.PresenceState = PresenceState = {})); var PremiumLevel; (function (PremiumLevel) { PremiumLevel["NONE"] = "NONE"; PremiumLevel["PLUS"] = "PLUS"; })(PremiumLevel || (exports.PremiumLevel = PremiumLevel = {}));