1import { Platform } from 'expo-modules-core'; 2import qs from 'qs'; 3// TODO(Bacon): pending react-native-adapter publish after sdk 38 4const isDOMAvailable = Platform.OS === 'web' && 5 typeof window !== 'undefined' && 6 !!window.document?.createElement && 7 typeof URL !== 'undefined'; 8export async function requestAsync(requestUrl, fetchRequest) { 9 if (Platform.OS === 'web' && !isDOMAvailable) { 10 // @ts-ignore 11 return; 12 } 13 const url = new URL(requestUrl); 14 const request = { 15 method: fetchRequest.method, 16 mode: 'cors', 17 headers: {}, 18 }; 19 const isJsonDataType = fetchRequest.dataType?.toLowerCase() === 'json'; 20 if (fetchRequest.headers) { 21 for (const i in fetchRequest.headers) { 22 if (i in fetchRequest.headers) { 23 request.headers[i] = fetchRequest.headers[i]; 24 } 25 } 26 } 27 if (fetchRequest.body) { 28 if (fetchRequest.method?.toUpperCase() === 'POST') { 29 request.body = qs.stringify(fetchRequest.body); 30 } 31 else { 32 for (const key of Object.keys(fetchRequest.body)) { 33 url.searchParams.append(key, fetchRequest.body[key]); 34 } 35 } 36 } 37 if (isJsonDataType && !('Accept' in request.headers)) { 38 // NOTE: Github authentication will return XML if this includes the standard `*/*` 39 request.headers['Accept'] = 'application/json, text/javascript; q=0.01'; 40 } 41 // Fix a problem with React Native `URL` causing a trailing slash to be added. 42 const correctedUrl = url.toString().replace(/\/$/, ''); 43 const response = await fetch(correctedUrl, request); 44 const contentType = response.headers.get('content-type'); 45 if (isJsonDataType || contentType?.includes('application/json')) { 46 return response.json(); 47 } 48 // @ts-ignore: Type 'string' is not assignable to type 'T'. 49 return response.text(); 50} 51//# sourceMappingURL=Fetch.js.map