Skip to content

Commit b6ffc81

Browse files
Refactor recent changes
1 parent 369b73d commit b6ffc81

File tree

3 files changed

+64
-120
lines changed

3 files changed

+64
-120
lines changed

README.md

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ This unofficial source plugin makes Optimizely/Episerver API data available in G
2121
- Log level options for `optimizely/episerver` API endpoint requests: `info`, `debug`, `warn`, `error`
2222
- Support for additional headers
2323
- Support for custom request timeout in all `optimizely/episerver` API requests
24-
- **[BETA]** Support for data caching on subsequent source plugin runs
25-
- **[BETA]** Add support for `expanded` data on content blocks such as `images`, `dynamicStyles`, `items`
2624

2725
## Installation and Setup
2826

@@ -125,20 +123,6 @@ options: {
125123
}
126124
```
127125

128-
### [BETA] Caching
129-
130-
Enable caching of the Optimizely/Episerver API requests on subsequent source plugin runs. Supports `true`, `false`.
131-
132-
**Default:** `false`.
133-
134-
```javascript
135-
options: {
136-
// ...
137-
138-
enable_cache: false;
139-
}
140-
```
141-
142126
### Request Timeout
143127

144128
Set a custom request timeout for the Optimizely/Episerver API requests (in milliseconds).

src/gatsby-node.js

Lines changed: 64 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -15,37 +15,30 @@ import Optimizely from "./utils/optimizely";
1515
* @param {Object} log
1616
* @returns {Promise<void>} Node creation promise
1717
*/
18-
const handleCreateNodeFromData = (item, nodeType, helpers, endpoint, log) => {
19-
if (item && Object.prototype.toString.call(item) === "[object Object]" && Object.keys(item)?.length > 0) {
20-
const nodeMetadata = {
21-
...item,
22-
id: helpers.createNodeId(`${nodeType}-${item?.id || item?.name}`),
23-
parent: null,
24-
children: [],
25-
internal: {
26-
type: nodeType,
27-
content: convertObjectToString(item),
28-
contentDigest: helpers.createContentDigest(item)
29-
}
30-
};
31-
32-
const node = Object.assign({}, item, nodeMetadata);
33-
34-
helpers
35-
.createNode(node)
36-
.then(() => {
37-
log.warn(`(OK) [CREATE NODE] ${endpoint} - ${helpers.createNodeId(`${nodeType}-${item.id || item.name}`)}`);
38-
39-
return node;
40-
})
41-
.catch((err) => {
42-
log.error(`(FAIL) [CREATE NODE] ${endpoint} - ${helpers.createNodeId(`${nodeType}-${item.id || item.name}`)}`, err.message);
43-
44-
throw err;
45-
});
18+
const handleCreateNodeFromData = async (item, nodeType, helpers, endpoint, log) => {
19+
const nodeMetadata = {
20+
...item,
21+
id: helpers.createNodeId(`${nodeType}-${item?.id || item?.name}`),
22+
parent: null,
23+
children: [],
24+
internal: {
25+
type: nodeType,
26+
content: convertObjectToString(item),
27+
contentDigest: helpers.createContentDigest(item)
28+
}
29+
};
30+
31+
const node = Object.assign({}, item, nodeMetadata);
32+
33+
try {
34+
await helpers.createNode(node);
35+
36+
log.warn(`(OK) [CREATE NODE] ${endpoint} - ${helpers.createNodeId(`${nodeType}-${item.id || item.name}`)}`);
37+
38+
return node;
39+
} catch (error) {
40+
log.error(`(FAIL) [CREATE NODE] ${endpoint} - ${helpers.createNodeId(`${nodeType}-${item.id || item.name}`)}`, error.message);
4641
}
47-
48-
return Promise.resolve();
4942
};
5043

5144
/**
@@ -112,15 +105,14 @@ exports.onPreInit = () => console.info("@epicdesignlabs/gatsby-source-optimizely
112105
* @param {Object} pluginOptions
113106
* @returns {Promise<void>} Node creation promise
114107
*/
115-
exports.sourceNodes = async ({ actions, cache, createNodeId, createContentDigest }, pluginOptions) => {
108+
exports.sourceNodes = async ({ actions, createNodeId, createContentDigest }, pluginOptions) => {
116109
// Prepare plugin options
117110
const {
118111
auth: { site_url = null, username = null, password = null, grant_type = "password", client_id = "Default", headers = {} },
119112
endpoints = null,
120113
log_level = "info",
121114
response_type = "json",
122-
request_timeout = REQUEST_TIMEOUT,
123-
enable_cache = false
115+
request_timeout = REQUEST_TIMEOUT
124116
} = pluginOptions;
125117

126118
// Custom logger based on the `log_level` plugin option
@@ -132,72 +124,47 @@ exports.sourceNodes = async ({ actions, cache, createNodeId, createContentDigest
132124
createNodeId
133125
});
134126

135-
// Store the response from the Optimizely/Episerver API in the cache
136-
const cacheKey = "gatsby-source-optimizely-api-data-key";
137-
let sourceData = await cache.get(cacheKey);
138-
139-
// Fetch fresh data if nothing is found in the cache or a plugin option has changed
140-
if (!sourceData || !enable_cache) {
141-
// Authenticate with the Optimizely/Episerver
142-
const auth = new Auth({ site_url, username, password, grant_type, client_id, log, response_type, request_timeout });
143-
144-
const authData = await auth.post();
145-
146-
log.warn("Cache is disabled or empty. Fetching fresh data from the Optimizely/Episerver API...");
147-
148-
// Display the auth data
149-
log.info(`(OK) [AUTH] ${convertObjectToString(authData)}`);
150-
151-
// Create a new Optimizely instance
152-
const optimizely = new Optimizely({
153-
site_url,
154-
response_type,
155-
headers: Object.assign(headers, {
156-
"Authorization": `Bearer ${authData.access_token}`,
157-
"Access-Control-Allow-Headers": ACCESS_CONTROL_ALLOW_HEADERS,
158-
"Access-Control-Allow-Credentials": ACCESS_CONTROL_ALLOW_CREDENTIALS,
159-
"Access-Control-Allow-Origin": CORS_ORIGIN
160-
}),
161-
log,
162-
request_timeout
163-
});
164-
165-
// Get the endpoints from the Optimizely/Episerver site and create nodes
166-
await Promise.allSettled(
167-
Object.entries(endpoints).map(
168-
async ([nodeName, endpoint]) =>
169-
await optimizely
170-
.get(endpoint)
171-
.then((res) => [res, nodeName, endpoint])
172-
.catch((err) => {
173-
log.error(`An error occurred while fetching ${endpoint} endpoint data`, err.message);
174-
175-
throw err;
176-
})
177-
)
178-
)
179-
.then(async (res) => {
180-
const data = await cache.set(cacheKey, res);
181-
182-
sourceData = data;
183-
184-
return sourceData;
185-
})
186-
.catch((err) => {
187-
log.error("An error occurred while fetching data from the Optimizely/Episerver API", err.message);
127+
// Authenticate with the Optimizely/Episerver
128+
const auth = new Auth({ site_url, username, password, grant_type, client_id, log, response_type, request_timeout });
188129

189-
throw err;
190-
});
191-
}
130+
const authData = await auth.post();
131+
132+
log.warn("Cache is disabled or empty. Fetching fresh data from the Optimizely/Episerver API...");
192133

193-
// Create nodes from the response data
194-
sourceData.forEach(({ status, value }) =>
195-
status === "fulfilled"
196-
? value[0] && Array.isArray(value[0]) && value[0]?.length > 0
197-
? value[0].map((datum) => handleCreateNodeFromData(datum, value[1], helpers, site_url + REQUEST_URL_SLUG + value[2], log))
198-
: handleCreateNodeFromData(value[0], value[1], helpers, site_url + REQUEST_URL_SLUG + value[2], log)
199-
: null
200-
);
134+
// Display the auth data
135+
log.info(`(OK) [AUTH] ${convertObjectToString(authData)}`);
136+
137+
// Create a new Optimizely instance
138+
const optimizely = new Optimizely({
139+
site_url,
140+
response_type,
141+
headers: Object.assign(headers, {
142+
"Authorization": `Bearer ${authData.access_token}`,
143+
"Access-Control-Allow-Headers": ACCESS_CONTROL_ALLOW_HEADERS,
144+
"Access-Control-Allow-Credentials": ACCESS_CONTROL_ALLOW_CREDENTIALS,
145+
"Access-Control-Allow-Origin": CORS_ORIGIN
146+
}),
147+
log,
148+
request_timeout
149+
});
150+
151+
for (const [nodeName, endpoint] of Object.entries(endpoints)) {
152+
try {
153+
const res = await optimizely.get(endpoint);
154+
if (res && Array.isArray(res) && res?.length > 0) {
155+
await Promise.allSettled(
156+
res.map(async (datum) => {
157+
await handleCreateNodeFromData(datum, nodeName, helpers, site_url + REQUEST_URL_SLUG + endpoint, log);
158+
})
159+
);
160+
} else {
161+
await handleCreateNodeFromData(res, nodeName, helpers, site_url + REQUEST_URL_SLUG + endpoint, log);
162+
}
163+
} catch (error) {
164+
console.log(error);
165+
log.error(`An error occurred while fetching ${endpoint} endpoint data`, error.message);
166+
}
167+
}
201168

202169
log.info("@epicdesignlabs/gatsby-source-optimizely task processing complete!");
203170

src/utils/optimizely.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"use strict";
22

3-
import sleep from "then-sleep";
43
import { CONTENT_ENDPOINT } from "../constants";
54
import Request from "./request";
65

@@ -19,8 +18,6 @@ class Optimizely {
1918

2019
// Handle API requests
2120
async request(method, path, body = null, headers = {}) {
22-
await sleep(this.request_timeout);
23-
2421
// Prepare `path` for request execution
2522
const request = new Request(this.site_url, {
2623
headers: Object.assign({}, this.headers, headers),
@@ -254,16 +251,12 @@ class Optimizely {
254251

255252
// Handle `GET` request
256253
async get(path, headers = {}) {
257-
await sleep(this.request_timeout);
258-
259254
const response = await this.request("get", path, headers);
260255
return response;
261256
}
262257

263258
// Handle `POST` request
264259
async post(path, body, headers = {}) {
265-
await sleep(this.request_timeout);
266-
267260
const response = await this.request("post", path, body, headers);
268261
return response;
269262
}

0 commit comments

Comments
 (0)