function fetchUserInfo(userId) { return fetch(`https://api.github.com/users/${encodeURIComponent(userId)}`) .then(response => { if (!response.ok) { return Promise.reject(new Error(`${response.status}: ${response.statusText}`)); } else { return response.json(); } }); }
static async getInitialProps({ req }){ const res = await fetch('https://api.github.com/repos/facebook/react') const json = await res.json() return {stars: json.stargazers_count} }
function fetchUserInfo(userId) { const url = `https://api.github.com/users/${encodeURIComponent(userId)}`; return fetch(url).then(response => response.json()); }
/* global setupPolly */ describe('REST Persister', function() { setupPolly({ adapters: ['fetch'], persister: 'rest' }); it('should work', async function() { const res = await fetch('https://jsonplaceholder.typicode.com/posts/1'); const post = await res.json(); expect(res.status).to.equal(200); expect(post.id).to.equal(1); }); });
fetch(process.env.GRAPHQL_ENDPOINT, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ path, dynamic }) }) .then(res => res.json())
async function sendRequest(path, options = {}) { const headers = { 'Content-type': 'application/json; charset=UTF-8', }; const response = await fetch( `${ROOT_URL}${path}`, Object.assign({ method: 'POST', credentials: 'include' }, { headers }, options), ); const data = await response.json(); if (data.error) { throw new Error(data.error); } return data; }
export default async function sendRequest(path, options = {}) { const headers = Object.assign({}, options.headers || {}, { 'Content-type': 'application/json; charset=UTF-8', }); const response = await fetch( `${ROOT_URL}${path}`, Object.assign({ method: 'POST', credentials: 'include' }, options, { headers }), ); const data = await response.json(); if (data.error) { throw new Error(data.error); } return data; }
export default async function sendRequest(path, options = {}) { const headers = { ...(options.headers || {}), 'Content-type': 'application/json; charset=UTF-8' }; const response = await fetch(`${getRootUrl()}${path}`, { method: 'POST', credentials: 'same-origin', ...options, headers, }); const data = await response.json(); if (data.error) { throw new Error(data.error); } return data; }
export function fetchGeographies(url) { return fetch(url) .then(res => { if (!res.ok) { throw Error(res.statusText) } return res.json() }).catch(error => { console.log("There was a problem when fetching the data: ", error) }) }
const _fetchData = async (url, options = {}) => { try { const res = await fetch(url, options) const data = await res.json() return Promise.resolve(Object.keys(data).length > 0 ? data : null) // Return null if data empty } catch (error) { logger.error('CLIENT_FETCH_ERROR', url, error) return Promise.resolve(null) } }
fetch(process.env.GRAPHQL_ENDPOINT, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ path, dynamic }) }) .then(res => res.json())
function fetchUserInfo(userId) { return fetch(`https://api.github.com/users/${encodeURIComponent(userId)}`) .then(response => { if (!response.ok) { return Promise.reject(new Error(`${response.status}: ${response.statusText}`)); } else { return response.json(); } }); }
function fetchUserInfo(userId) { const url = `https://api.github.com/users/${encodeURIComponent(userId)}`; return fetch(url).then(response => response.json()); }
/* global setupPolly */ describe('REST Persister', function() { setupPolly({ adapters: ['fetch'], persister: 'rest' }); it('should work', async function() { const res = await fetch('https://jsonplaceholder.typicode.com/posts/1'); const post = await res.json(); expect(res.status).to.equal(200); expect(post.id).to.equal(1); }); });
async function sendRequest(path, options = {}) { const headers = { 'Content-type': 'application/json; charset=UTF-8', }; const response = await fetch( `${ROOT_URL}${path}`, Object.assign({ method: 'POST', credentials: 'include' }, { headers }, options), ); const data = await response.json(); if (data.error) { throw new Error(data.error); } return data; }