Skip to content

Commit a18ca99

Browse files
committed
API: use APIv3 endpoint for resources
Initial implementation as a POC to give it a try. It works fine at first sight and I think it could be a good idea to move forward with it. I didn't find any blocker yet. Closes #356
1 parent 53f0fbe commit a18ca99

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

src/index.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { getReadTheDocsConfig } from "./readthedocs-config";
1+
import {
2+
getReadTheDocsConfig,
3+
getReadTheDocsConfigUsingAPIv3,
4+
} from "./readthedocs-config";
25
import * as notification from "./notification";
36
import * as analytics from "./analytics";
47
import * as search from "./search";
@@ -45,7 +48,8 @@ export function setup() {
4548
}
4649
}
4750

48-
return getReadTheDocsConfig(sendUrlParam);
51+
// return getReadTheDocsConfig(sendUrlParam);
52+
return getReadTheDocsConfigUsingAPIv3(sendUrlParam);
4953
})
5054
.then((config) => {
5155
const loadWhenEmbedded = objectPath.get(

src/readthedocs-config.js

+51
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,57 @@ export function getReadTheDocsConfig(sendUrlParam) {
129129
});
130130
}
131131

132+
export async function getReadTheDocsConfigUsingAPIv3(sendUrlParam) {
133+
// TODO: get the project/version slug from the META tags
134+
const projectResponse = fetch("/_/api/v3/projects/test-builds/");
135+
const translationsResponse = fetch(
136+
"/_/api/v3/projects/test-builds/translations/",
137+
);
138+
const versionResponse = fetch(
139+
"/_/api/v3/projects/test-builds/versions/full-feature/",
140+
);
141+
const activeVersionsResponse = fetch(
142+
"/_/api/v3/projects/test-builds/versions/?active=true",
143+
);
144+
const buildResponse = fetch("/_/api/v3/projects/test-builds/builds/3111/");
145+
146+
const responses = await Promise.all([
147+
projectResponse,
148+
translationsResponse,
149+
versionResponse,
150+
activeVersionsResponse,
151+
buildResponse,
152+
]);
153+
154+
const [project, translations, version, activeVersions, build] =
155+
await Promise.all(responses.map((response) => response.json()));
156+
157+
// TODO: we are missing the data from the `/_/addons/` endpoint that are not resources.
158+
// We need to perform another request for that.
159+
const dataEvent = {
160+
builds: {
161+
current: build,
162+
},
163+
projects: {
164+
current: project,
165+
translations: translations.results,
166+
},
167+
versions: {
168+
active: activeVersions.results,
169+
current: version,
170+
},
171+
};
172+
173+
// Trigger the addons data ready CustomEvent to with the data the user is expecting.
174+
dispatchEvent(
175+
EVENT_READTHEDOCS_ADDONS_DATA_READY,
176+
document,
177+
new ReadTheDocsEventData(dataEvent),
178+
);
179+
180+
return dataEvent;
181+
}
182+
132183
function dispatchEvent(eventName, element, data) {
133184
const event = new CustomEvent(eventName, { detail: data });
134185
element.dispatchEvent(event);

0 commit comments

Comments
 (0)