@@ -27,7 +27,7 @@ function _getMetadataAddonsAPIVersion() {
27
27
* It uses META HTML tags to get project/version slugs and `sendUrlParam` to
28
28
* decide whether or not sending `url=`.
29
29
*/
30
- function _getAPIURL ( sendUrlParam ) {
30
+ function _getApiUrl ( sendUrlParam ) {
31
31
const metaProject = document . querySelector (
32
32
"meta[name='readthedocs-project-slug']" ,
33
33
) ;
@@ -69,65 +69,72 @@ function _getAPIURL(sendUrlParam) {
69
69
*
70
70
*/
71
71
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" ,
82
78
} )
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
+ } ) ;
101
110
102
- fetch ( url , {
103
- method : "GET" ,
104
- } )
105
- . then ( ( response ) => {
111
+ fetch ( userApiUrl , {
112
+ method : "GET" ,
113
+ } ) . then ( ( response ) => {
106
114
if ( ! response . ok ) {
107
- throw "Error parsing configuration data" ;
115
+ reject (
116
+ "Error hitting addons API endpoint for user api-version" ,
117
+ ) ;
108
118
}
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 ( ) ;
120
122
} ) ;
121
- } else {
122
- dispatchEvent ( EVENT_READTHEDOCS_ADDONS_DATA_READY , document , data ) ;
123
- }
124
- }
123
+ }
125
124
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
+ } ) ;
131
138
}
132
139
133
140
function dispatchEvent ( eventName , element , data ) {
0 commit comments