1- import axios from 'axios'
21import HelperModule from '@codeceptjs/helper'
32
43/**
54 * GraphQL helper allows to send additional requests to a GraphQl endpoint during acceptance tests.
6- * [Axios](https://github.com/axios/axios) library is used to perform requests.
5+ * native fetch API is used to perform requests.
76 *
87 * ## Configuration
98 *
@@ -39,7 +38,6 @@ import HelperModule from '@codeceptjs/helper'
3938class GraphQL extends Helper {
4039 constructor ( config ) {
4140 super ( config )
42- this . axios = axios . create ( )
4341 this . headers = { }
4442 this . options = {
4543 timeout : 10000 ,
@@ -50,15 +48,10 @@ class GraphQL extends Helper {
5048 }
5149 this . options = Object . assign ( this . options , config )
5250 this . headers = { ...this . options . defaultHeaders }
53- this . axios . defaults . headers = this . options . defaultHeaders
5451 }
5552
5653 static _checkRequirements ( ) {
57- try {
58- require ( 'axios' )
59- } catch ( e ) {
60- return [ 'axios' ]
61- }
54+ return null
6255 }
6356
6457 static _config ( ) {
@@ -72,18 +65,18 @@ class GraphQL extends Helper {
7265 }
7366
7467 /**
75- * Executes query via axios call
68+ * Executes query
7669 *
7770 * @param {object } request
7871 */
7972 async _executeQuery ( request ) {
80- this . axios . defaults . timeout = request . timeout || this . options . timeout
73+ const timeout = request . timeout || this . options . timeout
8174
8275 if ( this . headers && this . headers . auth ) {
8376 request . auth = this . headers . auth
8477 }
8578
86- request . headers = Object . assign ( request . headers , {
79+ request . headers = Object . assign ( request . headers || { } , {
8780 'Content-Type' : 'application/json' ,
8881 } )
8982
@@ -96,12 +89,65 @@ class GraphQL extends Helper {
9689 this . debugSection ( 'Request' , JSON . stringify ( request ) )
9790
9891 let response
92+ const controller = new AbortController ( )
93+ const id = setTimeout ( ( ) => controller . abort ( ) , timeout )
94+
95+ const url = request . url ? ( request . baseURL ? request . baseURL + request . url : request . url ) : request . baseURL
96+ const fetchOptions = {
97+ method : request . method || 'POST' ,
98+ headers : request . headers || { } ,
99+ signal : controller . signal ,
100+ }
101+
102+ if ( request . data ) {
103+ fetchOptions . body = typeof request . data === 'object' ? JSON . stringify ( request . data ) : request . data
104+ }
105+
106+ if ( request . auth ) {
107+ const { username, password } = request . auth
108+ const auth = Buffer . from ( `${ username } :${ password } ` ) . toString ( 'base64' )
109+ fetchOptions . headers . Authorization = `Basic ${ auth } `
110+ }
111+
99112 try {
100- response = await this . axios ( request )
113+ const fetchResponse = await fetch ( url , fetchOptions )
114+ clearTimeout ( id )
115+
116+ const headers = { }
117+ fetchResponse . headers . forEach ( ( value , key ) => {
118+ headers [ key ] = value
119+ } )
120+
121+ let data
122+ const contentType = headers [ 'content-type' ]
123+ if ( contentType && contentType . includes ( 'application/json' ) ) {
124+ data = await fetchResponse . json ( )
125+ } else {
126+ data = await fetchResponse . text ( )
127+ try {
128+ data = JSON . parse ( data )
129+ } catch ( e ) {
130+ // not a json
131+ }
132+ }
133+
134+ response = {
135+ data,
136+ status : fetchResponse . status ,
137+ statusText : fetchResponse . statusText ,
138+ headers,
139+ config : request ,
140+ }
141+
142+ if ( ! fetchResponse . ok ) {
143+ this . debugSection ( 'Response' , `Response error. Status code: ${ fetchResponse . status } ` )
144+ }
101145 } catch ( err ) {
102- if ( ! err . response ) throw err
103- this . debugSection ( 'Response' , `Response error. Status code: ${ err . response . status } ` )
104- response = err . response
146+ clearTimeout ( id )
147+ if ( err . name === 'AbortError' ) {
148+ throw new Error ( `Request timed out after ${ timeout } ms` )
149+ }
150+ throw err
105151 }
106152
107153 if ( this . options . onResponse ) {
@@ -113,7 +159,7 @@ class GraphQL extends Helper {
113159 }
114160
115161 /**
116- * Prepares request for axios call
162+ * Prepares request for fetch call
117163 *
118164 * @param {object } operation
119165 * @param {object } headers
0 commit comments