Skip to content

Commit a623834

Browse files
committed
Switched back to plain req object (instead of Request)
1 parent 1a6702e commit a623834

2 files changed

Lines changed: 23 additions & 17 deletions

File tree

index.js

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/* global fetch, Request */
1+
/* global fetch, Headers */
22
require('isomorphic-fetch')
33

44
function Client (options) {
@@ -9,12 +9,6 @@ function Client (options) {
99
self.options = options
1010
self.url = options.url
1111

12-
// Request instance that is used for `fetch`ing
13-
self.request = options.request instanceof Request
14-
? options.request
15-
: new Request(self.url, options.request || { method: 'POST' })
16-
self.request.headers.append('content-type', 'application/json')
17-
1812
// A stack of registered listeners
1913
self.listeners = []
2014
}
@@ -32,17 +26,23 @@ var proto = Client.prototype
3226
proto.query = function (query, variables, beforeRequest) {
3327
var self = this
3428

35-
self.request.body = JSON.stringify({
29+
var req = self.options.request || {}
30+
req.method || (req.method = 'POST')
31+
if (!req.headers) {
32+
req.headers = new Headers()
33+
req.headers.set('content-type', 'application/json')
34+
}
35+
req.body = JSON.stringify({
3636
query: query,
3737
variables: variables
3838
})
3939

4040
// 'beforeRequest' is a top priority per-query hook, it should forcibly
4141
// override response even from other hooks.
42-
var result = beforeRequest && beforeRequest(self.request)
42+
var result = beforeRequest && beforeRequest(req)
4343

4444
if (typeof result === 'undefined') {
45-
result = self.emit('request', self.request)
45+
result = self.emit('request', req)
4646

4747
// No 'response' hook here, reserve it for real responses only.
4848

@@ -56,8 +56,7 @@ proto.query = function (query, variables, beforeRequest) {
5656
if (typeof result !== 'undefined') {
5757
result = Promise.resolve(result)
5858
}
59-
60-
return result || self.fetch(self.request)
59+
return result || self.fetch(req)
6160
}
6261

6362
/**
@@ -68,7 +67,7 @@ proto.query = function (query, variables, beforeRequest) {
6867
proto.fetch = function (req) {
6968
var self = this
7069

71-
return fetch(req).then(function (res) {
70+
return fetch(self.url, req).then(function (res) {
7271
// 'response' hook can redefine `res`
7372
var _res = self.emit('response', res)
7473
if (typeof _res !== 'undefined') res = _res

test/specs.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* eslint-env mocha */
2-
/* global Request */
2+
/* global Headers */
33
const { expect } = require('chai')
44
const { Client } = require('..')
55
const server = require('./lib/server')
@@ -75,14 +75,21 @@ describe('GraphQL client', () => {
7575
.query('{}')
7676
})
7777

78-
it('should redefine `Request` instance before querying', (done) => {
79-
const request = new Request(url)
80-
request.headers.set('content-length', 3)
78+
it('should modify request before querying', (done) => {
79+
const headers = new Headers()
80+
headers.set('content-length', 3)
81+
82+
const request = {
83+
method: 'GET',
84+
credentials: 'include',
85+
headers
86+
}
8187

8288
const client = new Client({ url, request })
8389

8490
client.on('request', (req) => {
8591
expect(req.method).to.equal('GET')
92+
expect(req.credentials).to.equal('include')
8693
expect(req.headers.get('content-type')).to.be.falsy
8794
expect(req.headers.get('content-length')).to.equal(3)
8895
done()

0 commit comments

Comments
 (0)