Skip to content

Commit eb5506b

Browse files
committed
Refactor getReadTheDocsConfig to use Promise
Follow Anthony's suggestion from #64 (comment)
1 parent 81e0dc0 commit eb5506b

File tree

3 files changed

+69
-62
lines changed

3 files changed

+69
-62
lines changed

dist/readthedocs-addons.js

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/readthedocs-addons.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/readthedocs-config.js

Lines changed: 61 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function _getMetadataAddonsAPIVersion() {
2727
* It uses META HTML tags to get project/version slugs and `sendUrlParam` to
2828
* decide whether or not sending `url=`.
2929
*/
30-
function _getAPIURL(sendUrlParam) {
30+
function _getApiUrl(sendUrlParam) {
3131
const metaProject = document.querySelector(
3232
"meta[name='readthedocs-project-slug']",
3333
);
@@ -69,65 +69,72 @@ function _getAPIURL(sendUrlParam) {
6969
*
7070
*/
7171
export function getReadTheDocsConfig(sendUrlParam) {
72-
const url = _getAPIURL(sendUrlParam);
73-
74-
return fetch(url, {
75-
method: "GET",
76-
})
77-
.then((response) => {
78-
if (!response.ok) {
79-
throw "Error parsing configuration data";
80-
}
81-
return response.json();
72+
return new Promise((resolve, reject) => {
73+
let dataUser;
74+
const defaultApiUrl = _getApiUrl(sendUrlParam);
75+
76+
fetch(defaultApiUrl, {
77+
method: "GET",
8278
})
83-
.then((data) => {
84-
// We force the user to define the `<meta>` tag to be able to use Read the Docs data directly.
85-
// This is to keep forward/backward compatibility without breaking integrations.
86-
const metadataAddonsAPIVersion = _getMetadataAddonsAPIVersion();
87-
if (metadataAddonsAPIVersion !== undefined) {
88-
if (metadataAddonsAPIVersion !== data.api_version) {
89-
// When the API scheme version returned doesn't match the one defined via `<meta>` tag by the user,
90-
// we perform another request to get the Read the Docs response in the structure
91-
// that's supported by the user and dispatch a custom event letting them know
92-
// this data is ready to be consumed under `event.detail`.
93-
94-
url =
95-
ADDONS_API_ENDPOINT +
96-
new URLSearchParams({
97-
url: window.location.href,
98-
"client-version": CLIENT_VERSION,
99-
"api-version": metadataAddonsAPIVersion,
100-
});
79+
.then((response) => {
80+
if (!response.ok) {
81+
reject("Error hitting addons API endpoint");
82+
}
83+
// Use the addons API data response as `dataUser`
84+
dataUser = response.json();
85+
return dataUser;
86+
})
87+
.then((data) => {
88+
// Create a new Promise here to handle the user request in a different async task.
89+
// This allows us to start executing our integration independently from the use one.
90+
new Promise((resolve, reject) => {
91+
// We force the user to define the `<meta>` tag to be able to use Read the Docs data directly.
92+
// This is to keep forward/backward compatibility without breaking integrations.
93+
const metadataAddonsAPIVersion = _getMetadataAddonsAPIVersion();
94+
95+
if (
96+
metadataAddonsAPIVersion !== undefined &&
97+
metadataAddonsAPIVersion !== data.api_version
98+
) {
99+
// When the API scheme version returned doesn't match the one defined via `<meta>` tag by the user,
100+
// we perform another request to get the Read the Docs response in the structure
101+
// that's supported by the user and dispatch a custom event letting them know
102+
// this data is ready to be consumed under `event.detail`.
103+
const userApiUrl =
104+
ADDONS_API_ENDPOINT +
105+
new URLSearchParams({
106+
url: window.location.href,
107+
"client-version": CLIENT_VERSION,
108+
"api-version": metadataAddonsAPIVersion,
109+
});
101110

102-
fetch(url, {
103-
method: "GET",
104-
})
105-
.then((response) => {
111+
fetch(userApiUrl, {
112+
method: "GET",
113+
}).then((response) => {
106114
if (!response.ok) {
107-
throw "Error parsing configuration data";
115+
reject(
116+
"Error hitting addons API endpoint for user api-version",
117+
);
108118
}
109-
return response.json();
110-
})
111-
.then((data) => {
112-
dispatchEvent(
113-
EVENT_READTHEDOCS_ADDONS_DATA_READY,
114-
document,
115-
data,
116-
);
117-
})
118-
.catch((error) => {
119-
console.error(error);
119+
// If the user defined a meta HTML tag with a different api-version,
120+
// use the new API data response as `dataUser`
121+
dataUser = response.json();
120122
});
121-
} else {
122-
dispatchEvent(EVENT_READTHEDOCS_ADDONS_DATA_READY, document, data);
123-
}
124-
}
123+
}
125124

126-
return data;
127-
})
128-
.catch((error) => {
129-
console.error(error);
130-
});
125+
// Trigger the addons data ready CustomEvent to with the data the user is expecting.
126+
dispatchEvent(
127+
EVENT_READTHEDOCS_ADDONS_DATA_READY,
128+
document,
129+
dataUser,
130+
);
131+
});
132+
133+
resolve(data);
134+
});
135+
}).catch((error) => {
136+
console.error(error);
137+
});
131138
}
132139

133140
function dispatchEvent(eventName, element, data) {

0 commit comments

Comments
 (0)