Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: use <meta> to define supported API version and trigger CustomEvent #64

Merged
merged 19 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions dist/readthedocs-addons.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/readthedocs-addons.js.map

Large diffs are not rendered by default.

10 changes: 10 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
<html>
<head>
<meta name="readthedocs-addons-api-version" content="1" />
<title>Documentation Addons - Read the Docs</title>
<meta name="readthedocs-project-slug" content="test-builds" />
<meta name="readthedocs-version-slug" content="latest" />
<script>
// Example of using "readthedocs-addons-data-ready" with a different API version supported
document.addEventListener(
"readthedocs-addons-data-ready",
function (event) {
console.debug(event.detail);
}
);
</script>
<meta name="readthedocs-resolver-filename" content="/index.html" />
</head>
<body>
Expand Down
2 changes: 2 additions & 0 deletions src/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ export const EVENT_READTHEDOCS_DOCDIFF_ADDED_REMOVED_SHOW =
export const EVENT_READTHEDOCS_DOCDIFF_HIDE = "readthedocs-docdiff-hide";
export const EVENT_READTHEDOCS_FLYOUT_SHOW = "readthedocs-flyout-show";
export const EVENT_READTHEDOCS_FLYOUT_HIDE = "readthedocs-flyout-hide";
export const EVENT_READTHEDOCS_ADDONS_DATA_READY =
"readthedocs-addons-data-ready";
84 changes: 76 additions & 8 deletions src/readthedocs-config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
import { default as fetch } from "unfetch";
import { EVENT_READTHEDOCS_ADDONS_DATA_READY } from "./events";
import {
CLIENT_VERSION,
IS_TESTING,
ADDONS_API_VERSION,
ADDONS_API_ENDPOINT,
IS_TESTING,
} from "./utils";

/**
* Get the Read the Docs API version supported by user's integrations.
*
*/
function _getMetadataAddonsAPIVersion() {
const meta = document.querySelector(
"meta[name=readthedocs-addons-api-version]",
);
if (meta !== undefined) {
return meta.getAttribute("content");
}
return undefined;
}

/**
* Load Read the Docs configuration from API endpoint.
*
Expand Down Expand Up @@ -46,11 +61,64 @@ export function getReadTheDocsConfig(sendUrlParam) {

return fetch(url, {
method: "GET",
}).then((response) => {
if (!response.ok) {
console.debug("Error parsing configuration data");
return undefined;
}
return response.json();
});
})
.then((response) => {
if (!response.ok) {
throw "Error parsing configuration data";
}
return response.json();
})
.then((data) => {
// We force the user to define the `<meta>` tag to be able to use Read the Docs data directly.
// This is to keep forward/backward compatibility without breaking integrations.
const metadataAddonsAPIVersion = _getMetadataAddonsAPIVersion();
if (metadataAddonsAPIVersion !== undefined) {
humitos marked this conversation as resolved.
Show resolved Hide resolved
if (metadataAddonsAPIVersion !== data.api_version) {
// When the API scheme version returned doesn't match the one defined via `<meta>` tag by the user,
// we perform another request to get the Read the Docs response in the structure
// that's supported by the user and dispatch a custom event letting them know
// this data is ready to be consumed under `event.detail`.

url =
ADDONS_API_ENDPOINT +
new URLSearchParams({
url: window.location.href,
"client-version": CLIENT_VERSION,
"api-version": metadataAddonsAPIVersion,
});

fetch(url, {
method: "GET",
})
.then((response) => {
if (!response.ok) {
throw "Error parsing configuration data";
}
return response.json();
})
.then((data) => {
dispatchEvent(
EVENT_READTHEDOCS_ADDONS_DATA_READY,
document,
data,
);
})
.catch((error) => {
console.error(error);
});
} else {
dispatchEvent(EVENT_READTHEDOCS_ADDONS_DATA_READY, document, data);
}
}

return data;
})
.catch((error) => {
console.error(error);
});
}

function dispatchEvent(eventName, element, data) {
const event = new CustomEvent(eventName, { detail: data });
element.dispatchEvent(event);
}
7 changes: 7 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ module.exports = (env, argv) => {
ignored: ["/node_modules/", "**/node_modules/"],
},
devServer: {
// Allow CORS when working locally
headers: {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods":
"GET, POST, PUT, DELETE, PATCH, OPTIONS",
"Access-Control-Allow-Headers": "*",
},
open: false,
port: 8000,
hot: false,
Expand Down