1- /* global fetch, Request */
1+ /* global fetch, Headers */
22require ( 'isomorphic-fetch' )
33
44function 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
3226proto . 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) {
6867proto . 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
0 commit comments