@andersundsehr/storybook-typo3 0.1.12 ā 0.1.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,10 +18,17 @@ In the case of a html fetch, we want to show the error as html response?
|
|
18
18
|
In any case we should get the error in json so we can handle it in the frontend as we like.
|
19
19
|
Sometimes it can still be text/html so we need to handle that as well.
|
20
20
|
*/
|
21
|
+
let updatableApiKey = import.meta.env.STORYBOOK_TYPO3_KEY;
|
21
22
|
export async function fetchWithUserRetry(url, options, message, resultType = 'json') {
|
22
23
|
try {
|
23
24
|
options = { ...options }; // Clone options to avoid mutating the original
|
24
25
|
options.signal = options.signal || AbortSignal.timeout(5000);
|
26
|
+
options.headers = {
|
27
|
+
...options.headers,
|
28
|
+
'Content-Type': 'application/json',
|
29
|
+
Accept: resultType === 'json' ? 'application/json' : 'text/html',
|
30
|
+
'X-Storybook-TYPO3-Key': updatableApiKey,
|
31
|
+
};
|
25
32
|
const response = await fetch(url, options);
|
26
33
|
if (!response.ok) {
|
27
34
|
return retry(response, url, options, message, resultType);
|
@@ -35,8 +42,21 @@ export async function fetchWithUserRetry(url, options, message, resultType = 'js
|
|
35
42
|
async function retry(errorOrResponse, url, options, message, resultType) {
|
36
43
|
const errorType = await getErrorExplanation(errorOrResponse);
|
37
44
|
let exception = errorOrResponse instanceof Error ? errorOrResponse : undefined;
|
45
|
+
let retry = false;
|
38
46
|
let confirmationMessage = '';
|
39
|
-
if (errorType.errorType === '
|
47
|
+
if (errorType.errorType === 'key') {
|
48
|
+
retry = true;
|
49
|
+
confirmationMessage = `š ${errorType.message}\n\n`
|
50
|
+
+ `š url: ${url}\n\n`
|
51
|
+
+ `š¬ Please insert the correct API key:\ncan be found in the .env file:\n\n`;
|
52
|
+
// if there was no api key, it will be set into the .env from the php code automatically
|
53
|
+
// we wait 1 second to give HMR time to update the .env file and reload the page, otherwise the user has to provide the key
|
54
|
+
if (!updatableApiKey) {
|
55
|
+
await new Promise(resolve => setTimeout(resolve, 1000));
|
56
|
+
}
|
57
|
+
updatableApiKey = prompt(confirmationMessage) || updatableApiKey;
|
58
|
+
}
|
59
|
+
else if (errorType.errorType === 'network') {
|
40
60
|
confirmationMessage = `š+š« A network error occurred while fetching ${message} from TYPO3:\n\n`
|
41
61
|
+ `š¬ ${errorType.message}\n\n`
|
42
62
|
+ `ā© Please check your network connection and try again.\n\n`
|
@@ -67,7 +87,9 @@ async function retry(errorOrResponse, url, options, message, resultType) {
|
|
67
87
|
}
|
68
88
|
confirmationMessage = confirmationMessage.trim();
|
69
89
|
confirmationMessage = confirmationMessage.length > 700 ? confirmationMessage.substring(0, 700 - 3) + '\nā¦' : confirmationMessage;
|
70
|
-
|
90
|
+
if (!retry) {
|
91
|
+
retry = confirm(confirmationMessage + '\n\n Do you want to retry šā');
|
92
|
+
}
|
71
93
|
if (retry) {
|
72
94
|
options.signal = undefined;
|
73
95
|
return fetchWithUserRetry(url, options, message, resultType);
|
@@ -81,8 +103,17 @@ async function getErrorExplanation(errorOrResponse) {
|
|
81
103
|
text = await errorOrResponse.text();
|
82
104
|
}
|
83
105
|
catch (e) {
|
106
|
+
}
|
107
|
+
if (errorOrResponse.status === 401) {
|
108
|
+
return {
|
109
|
+
errorType: 'key',
|
110
|
+
message: text || `The API key is not set or invalid. Maybe you only need to restart Storybook?`,
|
111
|
+
};
|
112
|
+
}
|
113
|
+
if (!text) {
|
84
114
|
return {
|
85
115
|
errorType: 'unknown',
|
116
|
+
statusCode: errorOrResponse.status,
|
86
117
|
message: `Failed to read response from TYPO3: ${errorOrResponse.status}\n ${errorOrResponse.statusText}`,
|
87
118
|
};
|
88
119
|
}
|
@@ -102,6 +133,7 @@ async function getErrorExplanation(errorOrResponse) {
|
|
102
133
|
}
|
103
134
|
return {
|
104
135
|
errorType: 'unknown',
|
136
|
+
statusCode: errorOrResponse.status,
|
105
137
|
message: `Received an unexpected response from TYPO3: ${errorOrResponse.status}\n ${text}`,
|
106
138
|
};
|
107
139
|
}
|
package/package.json
CHANGED