How to use the node-fetch.Request function in node-fetch

To help you get started, we’ve selected a few node-fetch examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github soyuka / rxrest / test / index.js View on Github external
function (req) {
        spy()
        expect(req.method).to.equal('GET')
        return new Request(req.url + '?foo=bar')
      }
    ]
github gja / cloudflare-worker-local / app / worker.js View on Github external
fetchUpstream(urlOrRequest, init) {
    let request = urlOrRequest instanceof Request ? urlOrRequest : new Request(urlOrRequest, init);

    const url = new URL(request.url);
    const originalHost = url.host;

    if (originalHost === this.origin) {
      url.host = this.upstreamHost;
      request = new Request(url, request);
      request.headers.set("Host", originalHost);
    }

    if (init && init.cf) {
      for (var key in init.cf) {
        var val = init.cf[key];
        key = key.split(/(?=[A-Z])/).join('-').toUpperCase();
        request.headers.set(`CF-${key}`, val);
      }
    }

    return fetch(request);
  }
github cedx / akismet.js / lib / client.js View on Github external
async _fetch(endPoint, fields) {
    let body = new URLSearchParams(Object.assign(this.blog.toJSON(), fields));
    if (this.isTest) body.set('is_test', '1');

    let req = new fetch.Request(endPoint.href, {
      method: 'POST',
      headers: {'content-type': 'application/x-www-form-urlencoded', 'user-agent': this.userAgent},
      body
    });

    this.emit('request', req);

    let res;
    try { res = await fetch(req); }
    catch (err) { throw new ClientError(err.message, endPoint); }

    this.emit('response', req, res);

    if (!res.ok) throw new ClientError('An error occurred while querying the end point', endPoint);
    if (res.headers.has('x-akismet-debug-help')) throw new ClientError(res.headers.get('x-akismet-debug-help'), endPoint);
    return res.text();
github cedx / akismet.js / src / client.ts View on Github external
private async _fetch(endPoint: URL, fields: JsonMap): Promise {
    const body = new URLSearchParams(Object.assign(this.blog.toJSON(), fields));
    if (this.isTest) body.set('is_test', '1');

    const req = new Request(endPoint.href, {
      body,
      headers: {'content-type': 'application/x-www-form-urlencoded', 'user-agent': this.userAgent},
      method: 'POST'
    });

    this.emit(Client.eventRequest, req);

    let res: Response;
    try { res = await fetch(req); }
    catch (err) { throw new ClientError(err.message, endPoint); }

    this.emit(Client.eventResponse, req, res);

    if (!res.ok) throw new ClientError('An error occurred while querying the end point', endPoint);
    if (res.headers.has('x-akismet-debug-help')) throw new ClientError(res.headers.get('x-akismet-debug-help')!, endPoint);
    return res.text();
github wheresrhys / fetch-mock / src / server.js View on Github external
import fetch from 'node-fetch';
const Request = fetch.Request;
const Response = fetch.Response;
const Headers = fetch.Headers;
import Stream from 'stream';
import FetchMock from './lib/index';
import http from 'http';
import {  setUrlImplementation  } from './lib/request-utils';
import {URL} from 'whatwg-url'
setUrlImplementation(URL);

FetchMock.global = global;
FetchMock.statusTextMap = http.STATUS_CODES;
FetchMock.Stream = Stream;

FetchMock.config = Object.assign(FetchMock.config, {
	Promise: Promise,
	Request: Request,
github adidas / mashery-toolbelt / src / masheryClient.js View on Github external
function fetchService(serviceId) {
  const url = new URL(`/v3/rest/services/${serviceId}`, MASHERY_HOST);
  const request = new fetch.Request(url.toString(), {
    headers: RequestHeaders
  });

  return fetchMashery(request);
}
github dolthub / dolt / samples / js / dropbox / slurp / src / main.js View on Github external
async function callDropbox(url: string, body: Object): Promise {
  const params = {
    method: 'POST',
    headers: {
      'Authorization': '',
      'Content-type': 'application/json',
    },
    body: JSON.stringify(body),
  };

  if (args['access-token']) {
    params.headers['Authorization'] = `Bearer ${args['access-token']}`;
  }

  const q = new Request(url, params);
  const r = await fetch(q);
  if (r.status !== 200) {
    throw new Error(await r.text());
  }
  return await r.json();
}
github adidas / mashery-toolbelt / src / masheryClient.js View on Github external
function fetchAllServiceEndpoints(serviceId) {
  const url = new URL(`/v3/rest/services/${serviceId}/endpoints`, MASHERY_HOST);
  const request = new fetch.Request(url.toString(), {
    headers: RequestHeaders
  });

  return fetchMashery(request);
}