Skip to content

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

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

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
2fdb6c8
API: use `<meta>` to define supported API version
humitos Apr 27, 2023
0590838
Merge branch 'main' of github.com:readthedocs/readthedocs-client into…
humitos Sep 13, 2023
26bab87
Allow CORS on development server
humitos Sep 13, 2023
c3b0edb
Serve the .js file from where the server is running
humitos Sep 13, 2023
a31c257
Merge branch 'main' of github.com:readthedocs/readthedocs-client into…
humitos Sep 19, 2023
f8de57b
Update the code with the latest changes and ideas
humitos Sep 19, 2023
cb12f5d
Merge branch 'main' of github.com:readthedocs/readthedocs-client into…
humitos Sep 21, 2023
c28fe6d
Typo
humitos Sep 21, 2023
2a6ed87
Merge branch 'main' of github.com:readthedocs/readthedocs-client into…
humitos Nov 21, 2023
63f6421
rebuild assets
humitos Mar 18, 2024
7784008
Merge branch 'main' of github.com:readthedocs/addons into humitos/cus…
humitos Mar 18, 2024
d947647
merge conflict
humitos Mar 18, 2024
e18bd1c
Minor updates after merging
humitos Mar 19, 2024
e8ec758
Throw an exception and handle it properly
humitos Mar 19, 2024
7a5c1e4
Simplify dispatching the event
humitos Mar 19, 2024
fcfa1e6
Catch the error properly
humitos Mar 19, 2024
98768a4
Refactor `getReadTheDocsConfig` to use `Promise` (#286)
humitos Apr 15, 2024
9af151c
Merge branch 'main' of github.com:readthedocs/addons into humitos/cus…
humitos Apr 15, 2024
6019cc0
Merge branch 'humitos/custom-event' of github.com:readthedocs/addons …
humitos Apr 15, 2024
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) {
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