Skip to content

Commit

Permalink
Switched back to plain req object (instead of Request)
Browse files Browse the repository at this point in the history
  • Loading branch information
ilearnio committed Jul 28, 2016
1 parent 1a6702e commit a623834
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
25 changes: 12 additions & 13 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* global fetch, Request */
/* global fetch, Headers */
require('isomorphic-fetch')

function Client (options) {
Expand All @@ -9,12 +9,6 @@ function Client (options) {
self.options = options
self.url = options.url

// Request instance that is used for `fetch`ing
self.request = options.request instanceof Request
? options.request
: new Request(self.url, options.request || { method: 'POST' })
self.request.headers.append('content-type', 'application/json')

// A stack of registered listeners
self.listeners = []
}
Expand All @@ -32,17 +26,23 @@ var proto = Client.prototype
proto.query = function (query, variables, beforeRequest) {
var self = this

self.request.body = JSON.stringify({
var req = self.options.request || {}
req.method || (req.method = 'POST')
if (!req.headers) {
req.headers = new Headers()
req.headers.set('content-type', 'application/json')
}
req.body = JSON.stringify({
query: query,
variables: variables
})

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

if (typeof result === 'undefined') {
result = self.emit('request', self.request)
result = self.emit('request', req)

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

Expand All @@ -56,8 +56,7 @@ proto.query = function (query, variables, beforeRequest) {
if (typeof result !== 'undefined') {
result = Promise.resolve(result)
}

return result || self.fetch(self.request)
return result || self.fetch(req)
}

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

return fetch(req).then(function (res) {
return fetch(self.url, req).then(function (res) {
// 'response' hook can redefine `res`
var _res = self.emit('response', res)
if (typeof _res !== 'undefined') res = _res
Expand Down
15 changes: 11 additions & 4 deletions test/specs.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-env mocha */
/* global Request */
/* global Headers */
const { expect } = require('chai')
const { Client } = require('..')
const server = require('./lib/server')
Expand Down Expand Up @@ -75,14 +75,21 @@ describe('GraphQL client', () => {
.query('{}')
})

it('should redefine `Request` instance before querying', (done) => {
const request = new Request(url)
request.headers.set('content-length', 3)
it('should modify request before querying', (done) => {
const headers = new Headers()
headers.set('content-length', 3)

const request = {
method: 'GET',
credentials: 'include',
headers
}

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

client.on('request', (req) => {
expect(req.method).to.equal('GET')
expect(req.credentials).to.equal('include')
expect(req.headers.get('content-type')).to.be.falsy
expect(req.headers.get('content-length')).to.equal(3)
done()
Expand Down

0 comments on commit a623834

Please sign in to comment.