Skip to content

Commit 44fb72f

Browse files
committed
Move fetchText to data.js
Also: fetchText now shares a backing helper function with fetchData. This avoids code repetition and helps fetchText handle more edge cases.
1 parent f716411 commit 44fb72f

File tree

3 files changed

+28
-27
lines changed

3 files changed

+28
-27
lines changed

src/utils/custom-scripts.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@
88
* See https://swift.org/CONTRIBUTORS.txt for Swift project authors
99
*/
1010

11-
import fetchText from 'docc-render/utils/fetch-text';
1211
import {
1312
copyPresentProperties,
1413
copyPropertyIfPresent,
1514
has,
1615
mustNotHave,
1716
} from 'docc-render/utils/object-properties';
1817
import { resolveAbsoluteUrl } from 'docc-render/utils/url-helper';
18+
import { fetchText } from 'docc-render/utils/data';
1919

2020
/** Enum for the allowed values of the `run` property in a custom script. */
2121
const Run = {
@@ -135,7 +135,7 @@ async function evalScript(customScript) {
135135

136136
if (has(customScript, 'integrity')) {
137137
// External script with integrity. Must also use CORS.
138-
codeToEval = await fetchText(customScript.url, {
138+
codeToEval = await fetchText(customScript.url, {}, {
139139
integrity: customScript.integrity,
140140
crossOrigin: 'anonymous',
141141
});
@@ -150,7 +150,7 @@ async function evalScript(customScript) {
150150

151151
if (has(customScript, 'integrity')) {
152152
// Local script with integrity. Do not use CORS.
153-
codeToEval = await fetchText(url, { integrity: customScript.integrity });
153+
codeToEval = await fetchText(url, {}, { integrity: customScript.integrity });
154154
} else {
155155
// Local script without integrity.
156156
codeToEval = await fetchText(url);

src/utils/data.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import emitWarningForSchemaVersionMismatch from 'docc-render/utils/schema-versio
1616
import RedirectError from 'docc-render/errors/RedirectError';
1717
import FetchError from 'docc-render/errors/FetchError';
1818

19-
export async function fetchData(path, params = {}, options = {}) {
19+
async function safeFetch(path, params = {}, options = {}) {
2020
function isBadResponse(response) {
2121
// When this is running in an IDE target, the `fetch` API will be used with
2222
// custom URL schemes. Right now, WebKit will return successful responses
@@ -50,11 +50,35 @@ export async function fetchData(path, params = {}, options = {}) {
5050
});
5151
}
5252

53+
return response;
54+
}
55+
56+
/**
57+
* Fetch the contents of a file as an object.
58+
* @param {string} path The file path.
59+
* @param {any} params Object containing URL query parameters.
60+
* @param {RequestInit} options Fetch options.
61+
* @returns {Promise<any>} The contents of the file.
62+
*/
63+
export async function fetchData(path, params = {}, options = {}) {
64+
const response = await safeFetch(path, params, options);
5365
const json = await response.json();
5466
emitWarningForSchemaVersionMismatch(json.schemaVersion);
5567
return json;
5668
}
5769

70+
/**
71+
* Fetch the contents of a file as text.
72+
* @param {string} path The file path.
73+
* @param {any} params Object containing URL query parameters.
74+
* @param {RequestInit} options Fetch options.
75+
* @returns {Promise<string>} The text contents of the file.
76+
*/
77+
export async function fetchText(path, params = {}, options = {}) {
78+
const response = await safeFetch(path, params, options);
79+
return response.text();
80+
}
81+
5882
function createDataPath(path) {
5983
const dataPath = path.replace(/\/$/, '');
6084
return `${normalizePath(['/data', dataPath])}.json`;

src/utils/fetch-text.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)