@quintype/framework 7.9.0 → 7.10.1-custom-infinite-scroll.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/CHANGELOG.md CHANGED
@@ -2,6 +2,14 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ## [7.10.0](https://github.com/quintype/quintype-node-framework/compare/v7.7.7...v7.10.0) (2022-10-03)
6
+
7
+
8
+ ### Features
9
+
10
+ * add video support to web stories ([#322](https://github.com/quintype/quintype-node-framework/issues/322)) ([f41b82b](https://github.com/quintype/quintype-node-framework/commit/f41b82be8285b555970527204ec6478f21c47782))
11
+ * **SW:** Disable sw shell ([#321](https://github.com/quintype/quintype-node-framework/issues/321)) ([6d04bc8](https://github.com/quintype/quintype-node-framework/commit/6d04bc87dd42c647ec9763eca430ada2f4a4d8ea))
12
+
5
13
  ## [7.9.0](https://github.com/quintype/quintype-node-framework/compare/v7.8.0...v7.9.0) (2022-10-03)
6
14
 
7
15
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@quintype/framework",
3
- "version": "7.9.0",
3
+ "version": "7.10.1-custom-infinite-scroll.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",
35
+ "@quintype/backend": "^2.3.2",
36
36
  "@quintype/components": "^3.0.0",
37
37
  "@quintype/prerender-node": "^3.2.26",
38
38
  "@quintype/seo": "^1.39.0",
@@ -1,18 +1,23 @@
1
1
  const { AmpConfig } = require("../../impl/api-client-impl");
2
2
  const InfiniteScrollAmp = require("../helpers/infinite-scroll");
3
3
  const { setCorsHeaders } = require("../helpers");
4
+ const get = require("lodash/get");
5
+ const cloneDeep = require("lodash/cloneDeep");
4
6
 
5
7
  // eslint-disable-next-line consistent-return
6
- async function storyPageInfiniteScrollHandler(req, res, next, { client, config }) {
8
+ async function storyPageInfiniteScrollHandler(req, res, next, { client, config, ...rest }) {
7
9
  const ampConfig = await config.memoizeAsync("amp-config", async () => await AmpConfig.getAmpConfig(client));
10
+ const opts = cloneDeep(rest);
11
+ const infiniteScrollSource = get(opts, ["featureConfig", "infiniteScroll", "source"], "collection");
8
12
 
9
13
  const infiniteScrollAmp = new InfiniteScrollAmp({
10
14
  ampConfig,
11
15
  publisherConfig: config,
12
16
  client,
13
17
  queryParams: req.query,
18
+ infiniteScrollSource,
14
19
  });
15
- const jsonResponse = await infiniteScrollAmp.getResponse({ itemsTaken: 5 }); // itemsTaken has to match with itemsToTake in ampStoryPageHandler
20
+ const jsonResponse = await infiniteScrollAmp.getResponse();
16
21
  if (jsonResponse instanceof Error) return next(jsonResponse);
17
22
  res.set("Content-Type", "application/json; charset=utf-8");
18
23
  setCorsHeaders({ req, res, next, publisherConfig: config });
@@ -86,15 +86,28 @@ 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
+ ampConfig,
103
+ publisherConfig: config,
104
+ client,
105
+ infiniteScrollSource,
106
+ });
107
+ infiniteScrollInlineConfig = await infiniteScrollAmp.getInitialInlineConfig({
108
+ storyId: story["story-content-id"],
109
+ });
110
+ }
98
111
  if (infiniteScrollInlineConfig instanceof Error) return next(infiniteScrollInlineConfig);
99
112
  if (infiniteScrollInlineConfig) {
100
113
  set(
@@ -1,9 +1,9 @@
1
1
  class InfiniteScrollAmp {
2
- constructor({ ampConfig, client, publisherConfig, queryParams }) {
2
+ constructor({ ampConfig, client, publisherConfig, queryParams, infiniteScrollSource }) {
3
3
  this.client = client;
4
4
  this.publisherConfig = publisherConfig;
5
5
  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
6
+ this.infiniteScrollSource = infiniteScrollSource;
7
7
  }
8
8
 
9
9
  // eslint-disable-next-line class-methods-use-this
@@ -17,12 +17,20 @@ class InfiniteScrollAmp {
17
17
  );
18
18
  }
19
19
 
20
+ getFilteredApiItems(relatedStories) {
21
+ return relatedStories.filter(
22
+ (story) =>
23
+ story.access !== "subscription" &&
24
+ story["story-template"] !== "visual-story"
25
+ );
26
+ }
27
+
20
28
  formatData({ itemsArr, type }) {
21
29
  // formats configuration as per need of amp infinite scroll
22
30
  const arr = itemsArr.map((item) => ({
23
31
  image: this.getImagePath(item),
24
- title: item.story.headline,
25
- url: `/amp/story/${item.story.slug}`,
32
+ title: item.headline,
33
+ url: `/amp/story/${item.slug}`,
26
34
  }));
27
35
  switch (type) {
28
36
  case "inline":
@@ -36,39 +44,54 @@ class InfiniteScrollAmp {
36
44
 
37
45
  getImagePath(item) {
38
46
  const cdnImage = this.publisherConfig["cdn-image"];
39
- const s3Key = item.story["hero-image-s3-key"];
47
+ const s3Key = item["hero-image-s3-key"];
40
48
  const hostWithProtocol = /^https:\/\//.test(cdnImage) ? cdnImage : `https://${cdnImage}`;
41
49
  return `${hostWithProtocol}/${s3Key}?format=webp&w=250`;
42
50
  }
43
51
 
44
- async getResponse({ itemsTaken }) {
52
+ async getInfiniteScrollList({ storyId, type, offset = 0, limit = null }) {
53
+ let filteredItems = [];
54
+ const params = {
55
+ ...(offset && { offset }),
56
+ ...(limit && { limit }),
57
+ };
58
+ if (this.infiniteScrollSource === "relatedStoriesApi") {
59
+ const relatedStoriesList = await this.client.getRelatedStories(storyId, null, params);
60
+ if (!relatedStoriesList)
61
+ return new Error();
62
+ return filteredItems = this.getFilteredApiItems(relatedStoriesList["related-stories"]);
63
+ } else {
64
+ const collection = await this.client.getCollectionBySlug("amp-infinite-scroll");
65
+ if (!collection || (collection.items && !collection.items.length) || collection.error || collection === null)
66
+ return new Error();
67
+ const collectionItems = this.getFilteredCollItems(collection, storyId).map(items => items.story);
68
+ return filteredItems = type === "inlineConfig"
69
+ ? collectionItems.slice(0, 5)
70
+ : collectionItems.slice(5);
71
+ }
72
+ }
73
+
74
+ async getResponse() {
45
75
  const { "story-id": storyId } = this.queryParams;
46
76
  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);
53
- const formattedData = this.formatData({ itemsArr: slicedItems });
54
-
77
+ const filteredItems =
78
+ await this.getInfiniteScrollList({ storyId, type: "remoteConfig", offset: 5 });
79
+ if (filteredItems instanceof Error) return new Error(`Infinite scroll collection amp-infinite-scroll returned falsy value`);
80
+ const formattedData = this.formatData({ itemsArr: filteredItems });
55
81
  return JSON.stringify(formattedData);
56
82
  }
57
83
 
58
- async getInitialInlineConfig({ itemsToTake, storyId }) {
59
- if (!itemsToTake || !storyId) return new Error("Required params for getInitialInlineConfig missing");
84
+ async getInitialInlineConfig({ storyId }) {
85
+ if (!storyId) return new Error("Required params for getInitialInlineConfig missing");
86
+ const filteredItems =
87
+ await this.getInfiniteScrollList({ storyId, type: "inlineConfig", offset: 0, limit: 5 });
60
88
 
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({
66
- itemsArr: slicedItems,
89
+ if (filteredItems instanceof Error) return null;
90
+ const formattedData = filteredItems.length > 0 && this.formatData({
91
+ itemsArr: filteredItems,
67
92
  type: "inline",
68
93
  });
69
-
70
94
  return JSON.stringify(formattedData);
71
95
  }
72
96
  }
73
-
74
97
  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({ storyId: "" });
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;