@quintype/framework 7.8.0 → 7.9.0-custom-inifinite-scroller.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quintype/framework",
3
- "version": "7.8.0",
3
+ "version": "7.9.0-custom-inifinite-scroller.0",
4
4
  "description": "Libraries to help build Quintype Node.js apps",
5
5
  "main": "index.js",
6
6
  "engines": {
@@ -31,8 +31,8 @@
31
31
  "homepage": "https://github.com/quintype/quintype-node-framework#readme",
32
32
  "dependencies": {
33
33
  "@ampproject/toolbox-optimizer": "2.8.3",
34
- "@quintype/amp": "^2.5.0",
35
- "@quintype/backend": "^2.3.1",
34
+ "@quintype/amp": "2.6.0-custom-infinite-scroll.0",
35
+ "@quintype/backend": "2.3.2-related-stories-inf-scroll.3",
36
36
  "@quintype/components": "^3.0.0",
37
37
  "@quintype/prerender-node": "^3.2.26",
38
38
  "@quintype/seo": "^1.39.0",
@@ -12,7 +12,7 @@ async function storyPageInfiniteScrollHandler(req, res, next, { client, config }
12
12
  client,
13
13
  queryParams: req.query,
14
14
  });
15
- const jsonResponse = await infiniteScrollAmp.getResponse({ itemsTaken: 5 }); // itemsTaken has to match with itemsToTake in ampStoryPageHandler
15
+ const jsonResponse = await infiniteScrollAmp.getResponse();
16
16
  if (jsonResponse instanceof Error) return next(jsonResponse);
17
17
  res.set("Content-Type", "application/json; charset=utf-8");
18
18
  setCorsHeaders({ req, res, next, publisherConfig: config });
@@ -86,15 +86,29 @@ async function ampStoryPageHandler(
86
86
  const seoTags =
87
87
  seoInstance && seoInstance.getMetaTags(config, "story-page-amp", { data: { story, timezone }, config }, { url });
88
88
 
89
- const infiniteScrollAmp = new InfiniteScrollAmp({
90
- ampConfig,
91
- publisherConfig: config,
92
- client,
93
- });
94
- const infiniteScrollInlineConfig = await infiniteScrollAmp.getInitialInlineConfig({
95
- itemsToTake: 5,
96
- storyId: story["story-content-id"],
97
- });
89
+ const infiniteScrollConfig = get(opts, ["featureConfig", "infiniteScroll"], "");
90
+ const infiniteScrollSource = get(infiniteScrollConfig, ["source"], "collection");
91
+ const inlineConfig = get(infiniteScrollConfig, ["inlineConfig"], "");
92
+ const remoteConfigEndpoint = get(infiniteScrollConfig, ["remoteConfigEndpoint"], "");
93
+
94
+ let infiniteScrollInlineConfig = [];
95
+
96
+ if (infiniteScrollSource === "custom") {
97
+ if (!inlineConfig || !remoteConfigEndpoint)
98
+ throw new Error("Required params of 'custom' source (inlineConfig /remoteConfigEndpoint) is missing!!");
99
+ infiniteScrollInlineConfig = await inlineConfig();
100
+ } else {
101
+ const infiniteScrollAmp = new InfiniteScrollAmp({
102
+ story,
103
+ ampConfig,
104
+ publisherConfig: config,
105
+ client,
106
+ infiniteScrollSource,
107
+ });
108
+ infiniteScrollInlineConfig = await infiniteScrollAmp.getInitialInlineConfig({
109
+ storyId: story["story-content-id"],
110
+ });
111
+ }
98
112
  if (infiniteScrollInlineConfig instanceof Error) return next(infiniteScrollInlineConfig);
99
113
  if (infiniteScrollInlineConfig) {
100
114
  set(
@@ -1,9 +1,10 @@
1
1
  class InfiniteScrollAmp {
2
- constructor({ ampConfig, client, publisherConfig, queryParams }) {
2
+ constructor({ story, ampConfig, client, publisherConfig, queryParams, infiniteScrollSource }) {
3
+ this.story = story;
3
4
  this.client = client;
4
5
  this.publisherConfig = publisherConfig;
5
6
  this.queryParams = queryParams;
6
- this.collSlug = "amp-infinite-scroll"; // this is hardcoded to "amp-infinite-scroll" temporarily. Ideally it should come from ampConfig from platform
7
+ this.infiniteScrollSource = infiniteScrollSource;
7
8
  }
8
9
 
9
10
  // eslint-disable-next-line class-methods-use-this
@@ -17,6 +18,14 @@ class InfiniteScrollAmp {
17
18
  );
18
19
  }
19
20
 
21
+ getFilteredApiItems(relatedStories) {
22
+ return relatedStories.filter(
23
+ (story) =>
24
+ story.access !== "subscription" &&
25
+ story["story-template"] !== "visual-story"
26
+ );
27
+ }
28
+
20
29
  formatData({ itemsArr, type }) {
21
30
  // formats configuration as per need of amp infinite scroll
22
31
  const arr = itemsArr.map((item) => ({
@@ -41,34 +50,42 @@ class InfiniteScrollAmp {
41
50
  return `${hostWithProtocol}/${s3Key}?format=webp&w=250`;
42
51
  }
43
52
 
44
- async getResponse({ itemsTaken }) {
53
+ async getInfiniteScrollList(storyId, offset, limit) {
54
+ let filteredItems = [];
55
+ const params = { offset, limit };
56
+ if (this.infiniteScrollSource === "relatedStoriesApi") {
57
+ const relatedStoriesList = await this.story.getRelatedStories(this.client, params);
58
+ if (!relatedStoriesList)
59
+ return new Error();
60
+ return filteredItems = this.getFilteredApiItems(relatedStoriesList);
61
+ } else {
62
+ const collection = await this.client.getCollectionBySlug("amp-infinite-scroll");
63
+ if (!collection || (collection.items && !collection.items.length) || collection.error || collection === null)
64
+ return new Error();
65
+ return filteredItems = this.getFilteredCollItems(collection, storyId);
66
+ }
67
+ }
68
+
69
+ async getResponse() {
45
70
  const { "story-id": storyId } = this.queryParams;
46
71
  if (!storyId) return new Error(`Query param "story-id" missing`);
47
-
48
- const collection = await this.client.getCollectionBySlug(this.collSlug);
49
- if (!collection || collection.error)
50
- return new Error(`Infinite scroll collection ${this.collSlug} returned falsy value`);
51
- const filteredItems = this.getFilteredCollItems(collection, storyId);
52
- const slicedItems = filteredItems.slice(itemsTaken);
72
+ const filteredItems = await this.getInfiniteScrollList(storyId, 5);
73
+ if (filteredItems instanceof Error) return new Error(`Infinite scroll collection amp-infinite-scroll returned falsy value`);
74
+ const slicedItems = filteredItems.slice(5);
53
75
  const formattedData = this.formatData({ itemsArr: slicedItems });
54
-
55
76
  return JSON.stringify(formattedData);
56
77
  }
57
78
 
58
- async getInitialInlineConfig({ itemsToTake, storyId }) {
59
- if (!itemsToTake || !storyId) return new Error("Required params for getInitialInlineConfig missing");
60
-
61
- const collection = await this.client.getCollectionBySlug(this.collSlug);
62
- if (!collection || (collection.items && !collection.items.length) || collection.error) return null;
63
- const filteredItems = this.getFilteredCollItems(collection, storyId);
64
- const slicedItems = filteredItems.slice(0, itemsToTake);
65
- const formattedData = this.formatData({
79
+ async getInitialInlineConfig(storyId) {
80
+ if (!storyId) return new Error("Required params for getInitialInlineConfig missing");
81
+ const filteredItems = await this.getInfiniteScrollList(storyId, 0, 5);
82
+ if (filteredItems instanceof Error) return null;
83
+ const slicedItems = filteredItems.slice(0, 5);
84
+ const formattedData = slicedItems.length > 0 && this.formatData({
66
85
  itemsArr: slicedItems,
67
86
  type: "inline",
68
87
  });
69
-
70
88
  return JSON.stringify(formattedData);
71
89
  }
72
90
  }
73
-
74
91
  module.exports = InfiniteScrollAmp;
@@ -3,8 +3,17 @@
3
3
 
4
4
  const assert = require("assert");
5
5
  const InfiniteScrollAmp = require("../../../server/amp/helpers/infinite-scroll");
6
+ const { getTextStory } = require("../../data/amp-test-data");
6
7
 
7
8
  function getClientStub({
9
+ getStoryById = (id) =>
10
+ new Promise((resolve) => {
11
+ if (id === "4444")
12
+ resolve({
13
+ story: getTextStory({ "story-content-id": "7f3d5bdb-ec52-4047-ac0d-df4036ec974b" }),
14
+ });
15
+ resolve(null);
16
+ }),
8
17
  getCollectionBySlug = (slug) =>
9
18
  new Promise((resolve) => {
10
19
  if (slug === "amp-infinite-scroll")
@@ -19,6 +28,7 @@ function getClientStub({
19
28
  "story-content-id": 1111,
20
29
  slug: "sports/aa",
21
30
  "hero-image-s3-key": "aa/a.jpg",
31
+ access: "public",
22
32
  },
23
33
  },
24
34
  {
@@ -30,6 +40,7 @@ function getClientStub({
30
40
  "story-content-id": 2222,
31
41
  slug: "sports/bb",
32
42
  "hero-image-s3-key": "bb/b.jpg",
43
+ access: "public",
33
44
  },
34
45
  },
35
46
  {
@@ -41,6 +52,7 @@ function getClientStub({
41
52
  "story-content-id": 3333,
42
53
  slug: "sports/cc",
43
54
  "hero-image-s3-key": "cc/c.jpg",
55
+ access: "public",
44
56
  },
45
57
  },
46
58
  {
@@ -52,6 +64,7 @@ function getClientStub({
52
64
  "story-content-id": 4444,
53
65
  slug: "sports/dd",
54
66
  "hero-image-s3-key": "dd/d.jpg",
67
+ access: "public",
55
68
  },
56
69
  },
57
70
  {
@@ -63,6 +76,7 @@ function getClientStub({
63
76
  "story-content-id": 5555,
64
77
  slug: "sports/ee",
65
78
  "hero-image-s3-key": "ee/e.jpg",
79
+ access: "public",
66
80
  },
67
81
  },
68
82
  {
@@ -74,6 +88,31 @@ function getClientStub({
74
88
  "story-content-id": 6666,
75
89
  slug: "sports/ff",
76
90
  "hero-image-s3-key": "ff/f.jpg",
91
+ access: "public",
92
+ },
93
+ },
94
+ {
95
+ type: "story",
96
+ id: 7777,
97
+ story: {
98
+ "story-template": "text",
99
+ headline: "ggg",
100
+ "story-content-id": 7777,
101
+ slug: "sports/gg",
102
+ "hero-image-s3-key": "gg/g.jpg",
103
+ access: "public",
104
+ },
105
+ },
106
+ {
107
+ type: "story",
108
+ id: 8888,
109
+ story: {
110
+ "story-template": "text",
111
+ headline: "hhh",
112
+ "story-content-id": 8888,
113
+ slug: "sports/hh",
114
+ "hero-image-s3-key": "hh/h.jpg",
115
+ access: "public",
77
116
  },
78
117
  },
79
118
  ],
@@ -83,6 +122,7 @@ function getClientStub({
83
122
  } = {}) {
84
123
  return {
85
124
  getCollectionBySlug,
125
+ getStoryById
86
126
  };
87
127
  }
88
128
  const dummyPublisherConfig = {
@@ -96,9 +136,7 @@ describe("getInitialInlineConfig method of InfiniteScrollAmp helper function", f
96
136
  client: getClientStub(),
97
137
  publisherConfig: dummyPublisherConfig,
98
138
  });
99
- const inlineConfig = await infiniteScrollAmp.getInitialInlineConfig({
100
- itemsToTake: 5,
101
- });
139
+ const inlineConfig = await infiniteScrollAmp.getInitialInlineConfig();
102
140
  assert.strictEqual(inlineConfig instanceof Error, true);
103
141
  assert.throws(() => {
104
142
  throw new Error("Required params for getInitialInlineConfig missing");
@@ -117,8 +155,7 @@ describe("getInitialInlineConfig method of InfiniteScrollAmp helper function", f
117
155
  publisherConfig: dummyPublisherConfig,
118
156
  });
119
157
  const inlineConfig = await infiniteScrollAmp.getInitialInlineConfig({
120
- itemsToTake: 5,
121
- storyId: 2222,
158
+ storyId: 1111,
122
159
  });
123
160
  assert.strictEqual(inlineConfig, null);
124
161
  });
@@ -137,8 +174,7 @@ describe("getInitialInlineConfig method of InfiniteScrollAmp helper function", f
137
174
  publisherConfig: dummyPublisherConfig,
138
175
  });
139
176
  const inlineConfig = await infiniteScrollAmp.getInitialInlineConfig({
140
- itemsToTake: 5,
141
- storyId: 2222,
177
+ storyId: 1111,
142
178
  });
143
179
  assert.strictEqual(inlineConfig, null);
144
180
  });
@@ -150,8 +186,7 @@ describe("getInitialInlineConfig method of InfiniteScrollAmp helper function", f
150
186
  publisherConfig: dummyPublisherConfig,
151
187
  });
152
188
  const inlineConfig = await infiniteScrollAmp.getInitialInlineConfig({
153
- itemsToTake: 5,
154
- storyId: 2222,
189
+ storyId: 1111,
155
190
  });
156
191
  assert.strictEqual(false, /sports\/bb/.test(inlineConfig));
157
192
  assert.strictEqual(false, /bb\/b.jpg/.test(inlineConfig));
@@ -164,8 +199,7 @@ describe("getInitialInlineConfig method of InfiniteScrollAmp helper function", f
164
199
  publisherConfig: dummyPublisherConfig,
165
200
  });
166
201
  const inlineConfig = await infiniteScrollAmp.getInitialInlineConfig({
167
- itemsToTake: 5,
168
- storyId: 3333,
202
+ storyId: 1111,
169
203
  });
170
204
  assert.strictEqual(false, /sports\/bb/.test(inlineConfig));
171
205
  assert.strictEqual(false, /bb\/b.jpg/.test(inlineConfig));
@@ -179,8 +213,7 @@ describe("getInitialInlineConfig method of InfiniteScrollAmp helper function", f
179
213
  publisherConfig: dummyPublisherConfig,
180
214
  });
181
215
  const inlineConfig = await infiniteScrollAmp.getInitialInlineConfig({
182
- itemsToTake: 5,
183
- storyId: 2222,
216
+ storyId: 1111,
184
217
  });
185
218
  function isInlineConfigStructureValid(jsonStr) {
186
219
  const stories = JSON.parse(jsonStr);
@@ -207,7 +240,7 @@ describe("getResponse method of InfiniteScrollAmp helper function", function ()
207
240
  publisherConfig: dummyPublisherConfig,
208
241
  queryParams: { foo: "bar" },
209
242
  });
210
- const jsonResponse = await infiniteScrollAmp.getResponse({ itemsTaken: 2 });
243
+ const jsonResponse = await infiniteScrollAmp.getResponse();
211
244
  assert.strictEqual(jsonResponse instanceof Error, true);
212
245
  assert.throws(() => {
213
246
  throw new Error(`Query param "story-id" missing`);
@@ -226,7 +259,7 @@ describe("getResponse method of InfiniteScrollAmp helper function", function ()
226
259
  publisherConfig: dummyPublisherConfig,
227
260
  queryParams: { "story-id": 2222 },
228
261
  });
229
- const jsonResponse = await infiniteScrollAmp.getResponse({ itemsTaken: 2 });
262
+ const jsonResponse = await infiniteScrollAmp.getResponse();
230
263
  assert.strictEqual(jsonResponse instanceof Error, true);
231
264
  });
232
265
  it("should remove current story from response", async function () {
@@ -237,7 +270,7 @@ describe("getResponse method of InfiniteScrollAmp helper function", function ()
237
270
  publisherConfig: dummyPublisherConfig,
238
271
  queryParams: { "story-id": 4444 },
239
272
  });
240
- const jsonResponse = await infiniteScrollAmp.getResponse({ itemsTaken: 2 });
273
+ const jsonResponse = await infiniteScrollAmp.getResponse();
241
274
  assert.strictEqual(false, /sports\/dd/.test(jsonResponse));
242
275
  assert.strictEqual(false, /dd\/d.jpg/.test(jsonResponse));
243
276
  });
@@ -249,7 +282,7 @@ describe("getResponse method of InfiniteScrollAmp helper function", function ()
249
282
  publisherConfig: dummyPublisherConfig,
250
283
  queryParams: { "story-id": 4444 },
251
284
  });
252
- const jsonResponse = await infiniteScrollAmp.getResponse({ itemsTaken: 2 });
285
+ const jsonResponse = await infiniteScrollAmp.getResponse();
253
286
  assert.strictEqual(false, /sports\/bb/.test(jsonResponse));
254
287
  assert.strictEqual(false, /bb\/b.jpg/.test(jsonResponse));
255
288
  });
@@ -262,7 +295,8 @@ describe("getResponse method of InfiniteScrollAmp helper function", function ()
262
295
  publisherConfig: dummyPublisherConfig,
263
296
  queryParams: { "story-id": 4444 },
264
297
  });
265
- const jsonResponse = await infiniteScrollAmp.getResponse({ itemsTaken: 2 });
298
+ const jsonResponse = await infiniteScrollAmp.getResponse();
299
+
266
300
  function isJsonConfigStructureValid(jsonStr) {
267
301
  const parsed = JSON.parse(jsonStr);
268
302
  const stories = parsed.pages;