@@ -16,7 +16,7 @@ import emitWarningForSchemaVersionMismatch from 'docc-render/utils/schema-versio
1616import RedirectError from 'docc-render/errors/RedirectError' ;
1717import 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+
5882function createDataPath ( path ) {
5983 const dataPath = path . replace ( / \/ $ / , '' ) ;
6084 return `${ normalizePath ( [ '/data' , dataPath ] ) } .json` ;
0 commit comments