UNPKG

@kontent-ai/delivery-sdk

Version:
128 lines 4.35 kB
import { Filters, Parameters } from '../../models'; import { BaseItemListingQuery } from '../common/base-item-listing-query.class'; export class ItemsFeedQuery extends BaseItemListingQuery { constructor(config, queryService) { super(config, queryService); this.config = config; this.queryService = queryService; this._queryConfig = {}; } /** * Gets only item of given type * @param type Codename of type to get */ type(type) { this.parameters.push(new Filters.TypeFilter(type)); return this; } /** * Gets items of given types (logical or) * I.e. get items of either 'Actor' or 'Movie' type * @param types Types to get */ types(types) { this.parameters.push(new Filters.TypeFilter(types)); return this; } /** * Gets only item from given collection * @param collection Codename of collection to get */ collection(collection) { this.parameters.push(new Filters.CollectionFilter(collection)); return this; } /** * Gets items from given collections (logical or) * I.e. get items of either 'default' or 'christmas-campaign' collection * @param collections Collections to get */ collections(collections) { this.parameters.push(new Filters.CollectionFilter(collections)); return this; } /** * Language codename * @param languageCodename Codename of the language */ languageParameter(languageCodename) { this.parameters.push(new Parameters.LanguageParameter(languageCodename)); return this; } /** * Used to limit the number of elements returned by query. * @param elementCodenames Array of element codenames to fetch */ elementsParameter(elementCodenames) { this.parameters.push(new Parameters.ElementsParameter(elementCodenames)); return this; } /** * Used to exclude elements returned by query. * @param elementCodenames Array of element codenames to exclude */ excludeElementsParameter(elementCodenames) { this.parameters.push(new Parameters.ExcludeElementsParameter(elementCodenames)); return this; } toPromise() { var _a; return this.queryService.getItemsFeed(this.getUrl(), (_a = this._queryConfig) !== null && _a !== void 0 ? _a : {}); } getUrl() { const action = '/items-feed'; // add default language is necessary this.processDefaultLanguageParameter(); //process client level archived item exclusion this.processExcludeArchivedItemsParameter(); return super.resolveUrlInternal(action); } /** * Used to configure query * @param queryConfig Query configuration */ queryConfig(queryConfig) { this._queryConfig = queryConfig; return this; } map(json) { return this.queryService.mappingService.itemsFeedResponse(json); } allResponseFactory(items, responses) { if (this.canLinkItems()) { this.linkFeedItems(items, responses); } return { items: items, responses: responses }; } linkFeedItems(items, responses) { // prepare all available items (including components) for linking const allAvailableContentItems = []; // process linked items (modular_content part of the response) for (const response of responses) { allAvailableContentItems.push(...Object.values(response.data.linkedItems) .filter((m) => m !== undefined) .map((m) => m)); } // add standard items for (const item of items) { if (!allAvailableContentItems.find((m) => m.system.codename.toLowerCase() === item.system.codename.toLowerCase())) { allAvailableContentItems.push(item); } } // process main items this.linkItemsInRte(allAvailableContentItems); } canLinkItems() { if (this.config.linkedItemsReferenceHandler === 'ignore') { return false; } if (this._queryConfig.disableItemLinking === true) { return false; } return true; } } //# sourceMappingURL=items-feed-query.class.js.map