@@ -16,7 +16,7 @@ import emitWarningForSchemaVersionMismatch from 'docc-render/utils/schema-versio
16
16
import RedirectError from 'docc-render/errors/RedirectError' ;
17
17
import FetchError from 'docc-render/errors/FetchError' ;
18
18
19
- export async function fetchData ( path , params = { } , options = { } ) {
19
+ async function safeFetch ( path , params = { } , options = { } ) {
20
20
function isBadResponse ( response ) {
21
21
// When this is running in an IDE target, the `fetch` API will be used with
22
22
// custom URL schemes. Right now, WebKit will return successful responses
@@ -50,11 +50,35 @@ export async function fetchData(path, params = {}, options = {}) {
50
50
} ) ;
51
51
}
52
52
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 ) ;
53
65
const json = await response . json ( ) ;
54
66
emitWarningForSchemaVersionMismatch ( json . schemaVersion ) ;
55
67
return json ;
56
68
}
57
69
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
+
58
82
function createDataPath ( path ) {
59
83
const dataPath = path . replace ( / \/ $ / , '' ) ;
60
84
return `${ normalizePath ( [ '/data' , dataPath ] ) } .json` ;
0 commit comments