Dive into secure and efficient coding practices with our curated list of the top 10 examples showcasing 'whatwg-fetch' in functional components in JavaScript. Our advanced machine learning engine meticulously scans each line of code, cross-referencing millions of open source libraries to ensure your implementation is not just functional, but also robust and secure. Elevate your React applications to new heights by mastering the art of handling side effects, API calls, and asynchronous operations with confidence and precision.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
* @providesModule fetch
*
*/
/* globals Headers, Request, Response */
'use strict';
import whatwg from 'whatwg-fetch';
if (whatwg && whatwg.fetch) {
module.exports = whatwg;
} else {
module.exports = {fetch, Headers, Request, Response};
}
function ensurePageDescStorage(targetPath) {
if (!pageOutlines) {
throw new Error('Outline data is not loaded.');
}
let pagePath = targetPath.split('.')[0];
// Configuration like `option.color`, `option.backgroundColor` is in the `option` page.
// Configuration like `option.series-bar` is in the `option.series-bar` page.
let partionKey = !pageOutlines[pagePath] || !targetPath
? rootName
: rootName + '.' + pagePath;
if (!descStorage[partionKey]) {
let url = `${baseUrl}/${partionKey}.json?${docVersion}`;
let fetcher = fetch(url).then(response => response.json());
descStorage[partionKey] = {
fetcher
};
fetcher.then(map => {
descStorage[partionKey].indexer = createIndexer(map, pagePath);
});
}
return descStorage[partionKey];
}
/**
function checkValidServiceWorker (swUrl) {
// Check if the service worker can be found. If it can't reload the page.
fetch(swUrl)
.then(response => {
// Ensure service worker exists, and that we really are getting a JS file.
if (
response.status === 404 ||
response.headers.get('content-type').indexOf('javascript') === -1
) {
// No service worker found. Probably a different app. Reload the page.
navigator.serviceWorker.ready.then(registration => {
registration.unregister().then(() => {
window.location.reload()
})
})
} else {
// Service worker found. Proceed as normal.
registerValidSW(swUrl)
}
// Ensure environment variables are read.
const path = require("path");
const fs = require("fs-extra");
const dotenv = require("dotenv");
process.env = {
...process.env,
...dotenv.parse(fs.readFileSync(path.resolve(process.cwd(), ".env"))),
};
const fetchPolyfill = require("whatwg-fetch");
global.fetch = fetchPolyfill.fetch;
global.Request = fetchPolyfill.Request;
global.Headers = fetchPolyfill.Headers;
global.Response = fetchPolyfill.Response;
const axios = require("axios");
const httpAdapter = require("axios/lib/adapters/http");
axios.defaults.adapter = httpAdapter;
require("moment-timezone");
jest.doMock("moment", () => {
const moment = jest.requireActual("moment");
moment.tz.setDefault("America/New_York");
return moment;
});
const mockAsyncStorage = require("@react-native-community/async-storage/jest/async-storage-mock");
async cleanUp() {
return fetch('/__cleanup-mocks');
}
};
const textOk = (body) => {
const mockResponse = new Response(body, {
status: 200,
headers: {
'Content-type': 'text/html; utf-8'
}
});
return Promise.resolve(mockResponse);
};
const jsonError = (status, statusText) => {
const mockResponse = new Response('', {
status: status,
statusText: statusText,
headers: {
'Content-type': 'application/json',
},
});
return Promise.resolve(mockResponse);
};
const jsonOk = (body) => {
const mockResponse = new Response(JSON.stringify(body), {
status: 200,
headers: {
'Content-type': 'application/json'
}
});
return Promise.resolve(mockResponse);
};
const jsonOk = (body) => {
const mockResponse = new Response(JSON.stringify(body), {
status: 200,
headers: {
'Content-type': 'application/json; utf-8'
}
});
return Promise.resolve(mockResponse);
};
async componentDidMount() {
const res = await fetch('/playground/context');
const result = await res.json();
this.setState({
cubejsToken: result.cubejsToken,
apiUrl: result.apiUrl || window.location.href.split('#')[0].replace(/\/$/, '')
});
}