Skip to content

Commit f3eaa7a

Browse files
committed
API: use <meta> to define supported API version
Let users to define `<meta name="readthedocs-api-version" content="1.0">` to tell Read the Docs client what is the API scheme version supported by them. When our client request the API data, if the `api_version` returned does not match with the one expected by the user, another request is done to force a particular API scheme version. Then, we dispatch a `readthedocsdataready` custom event and expose `window.readthedocs`, to let our users know this data is ready to be consumed by their own integrations. Closes #60 Closes #61
1 parent 2ec01e9 commit f3eaa7a

File tree

2 files changed

+71
-7
lines changed

2 files changed

+71
-7
lines changed

public/index.html

+7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
11
<html>
22
<head>
3+
<meta name="readthedocs-api-version" content="1">
34
<title>Documentation Addons - Read the Docs</title>
5+
<script>
6+
// Example of using "readthedocsdataready" with a different API version supported
7+
document.addEventListener("readthedocsdataready", function(event) {
8+
console.debug(event.detail);
9+
});
10+
</script>
411
</head>
512
<body>
613

src/readthedocs-config.js

+64-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
import { CLIENT_VERSION } from "./utils";
22

3+
/**
4+
* Get the Read the Docs API version supported by user's integrations.
5+
*
6+
*/
7+
function _getMetadataAPIVersion() {
8+
const meta = document.querySelector("meta[name=readthedocs-api-version]");
9+
if (meta !== undefined) {
10+
return meta.getAttribute("content");
11+
}
12+
return undefined;
13+
}
14+
315
/**
416
* Load Read the Docs configuration from API endpoint.
517
*
@@ -19,11 +31,56 @@ export function getReadTheDocsConfig() {
1931
return fetch(url, {
2032
method: "GET",
2133
headers: { "X-RTD-Hosting-Integrations-Version": CLIENT_VERSION },
22-
}).then((response) => {
23-
if (!response.ok) {
24-
console.debug("Error parsing configuration data");
25-
return undefined;
26-
}
27-
return response.json();
28-
});
34+
})
35+
.then((response) => {
36+
if (!response.ok) {
37+
console.debug("Error parsing configuration data");
38+
return undefined;
39+
}
40+
return response.json();
41+
})
42+
.then((data) => {
43+
// We force the user to define the `<meta>` tag to be able to use Read the Docs data directly.
44+
// This is to keep forward/backward compatibility without breaking integrations.
45+
const metadataAPIVersion = _getMetadataAPIVersion();
46+
if (metadataAPIVersion !== undefined) {
47+
if (metadataAPIVersion !== data.api_version) {
48+
// When the API scheme version returned doesn't match the one defined via `<meta>` tag by the user,
49+
// we perform another request to get the Read the Docs response in the structure
50+
// that's supported by the user and dispatch a custom event letting them know
51+
// this data is ready to be consumed under `event.detail` or `window.readthedocs`.
52+
fetch(url, {
53+
method: "GET",
54+
headers: {
55+
"X-RTD-Hosting-Integrations-Version": CLIENT_VERSION,
56+
"X-RTD-Hosting-Integrations-API-Version": metadataAPIVersion,
57+
},
58+
})
59+
.then((response) => {
60+
if (!response.ok) {
61+
console.debug("Error parsing configuration data");
62+
return undefined;
63+
}
64+
return response.json();
65+
})
66+
.then((data) => {
67+
const readthedocsDataReady = new CustomEvent(
68+
"readthedocsdataready",
69+
{ detail: data }
70+
);
71+
window.readthedocs = data;
72+
document.dispatchEvent(readthedocsDataReady);
73+
return undefined;
74+
});
75+
} else {
76+
const readthedocsDataReady = new CustomEvent("readthedocsdataready", {
77+
detail: data,
78+
});
79+
window.readthedocs = data;
80+
document.dispatchEvent(readthedocsDataReady);
81+
}
82+
}
83+
84+
return data;
85+
});
2986
}

0 commit comments

Comments
 (0)