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 removeSubscriptionFromServer(endpoint) { const encodedKey = btoa(String.fromCharCode.apply(null, new Uint8Array(key))); const encodedAuth = btoa(String.fromCharCode.apply(null, new Uint8Array(auth))); fetch(schnack_host + '/unsubscribe', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ publicKey: encodedKey, auth: encodedAuth, endpoint }) }).then(res => { // eslint-disable-next-line no-console console.log('Unsubscribed successfully! ' + JSON.stringify(res)); }); }
function fetchUserInfo(userId) { const url = `https://api.github.com/users/${encodeURIComponent(userId)}`; return fetch(url).then(response => response.json()); }
it('.filter()', async function() { const { server } = this.polly; server .get('/ping') .filter(req => req.query.num === '1') .intercept((req, res) => res.sendStatus(201)); server .get('/ping') .filter(req => req.query.num === '2') .intercept((req, res) => res.sendStatus(202)); expect((await fetch('/ping?num=1')).status).to.equal(201); expect((await fetch('/ping?num=2')).status).to.equal(202); });
/* 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 terminalData(stream, data) { const content = await(await fetch(`data:application/octet-stream;base64,${data}`)).text(); if (content.startsWith('Debugger listening on') || content.startsWith('Debugger attached.') || content.startsWith('Waiting for the debugger to disconnect...')) return; await Ndb.backend.writeTerminalData(stream, data); this.dispatchEventToListeners(Ndb.NodeProcessManager.Events.TerminalData, content); }
it('.recordingName()', async function() { const { server } = this.polly; let recordingName; server .get('/ping') .recordingName('Override') .intercept((req, res) => { recordingName = req.recordingName; res.sendStatus(200); }); expect((await fetch('/ping')).status).to.equal(200); expect(recordingName).to.equal('Override'); });
fetch(process.env.GRAPHQL_ENDPOINT, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ path, dynamic }) }) .then(res => res.json())
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) } }
it('.filter() - can access params', async function() { const { server } = this.polly; let id; server .get('/ping/:id') .filter(req => { id = req.params.id; return true; }) .intercept((req, res) => res.sendStatus(201)); expect((await fetch('/ping/1')).status).to.equal(201); expect(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())
function sendSubscriptionToServer(endpoint, key, auth) { const encodedKey = btoa(String.fromCharCode.apply(null, new Uint8Array(key))); const encodedAuth = btoa(String.fromCharCode.apply(null, new Uint8Array(auth))); fetch(schnack_host + '/subscribe', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ publicKey: encodedKey, auth: encodedAuth, endpoint }) }).then(res => { // eslint-disable-next-line no-console console.log('Subscribed successfully! ' + JSON.stringify(res)); }); }
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(); } }); }