src/App.js
/**
   * function that fetches an API request based on state valeus. Used in both form fetch as well as prev req
   * click fetch
   * @return  {void}
   */
function that fetches an API request based on state valeus. Used in both form fetch as well as prev req
click fetch
@return  {void}
function that fetches an API request based on state valeus . Used in both form fetch as well as prev req click fetch @return { void }
async apiFetch() {
    let baseURL = this.state.url;
    if (baseURL) {
      try {
        this.setState({
          loading: true,
        });
        const res = await fetch(baseURL, {
          method: this.state.reqType,
          headers: {
            'Content-Type': 'application/json',
          },
          body:
            this.state.reqType === 'GET' || this.state.reqType === 'DELETE'
              ? null
              : JSON.stringify(JSON.parse(this.state.reqBody)),
        });
        if (res) {
          const jsonRes = await res.json();
          const newHeaders = {};
          for (const entry of res.headers.entries()) {
            newHeaders[entry[0]] = entry[1];
          }
          await this.setState({
            results: jsonRes.results || jsonRes,
            count: jsonRes.count || 1,
            headers: newHeaders,
          });
          if (this.state.results.error) {
            throw new Error('500 error');
          }
          const currOutput = {
            headers: this.state.headers,
            results: this.state.results,
            count: this.state.count,
            url: baseURL,
            reqType: this.state.reqType,
            reqBody: this.state.reqBody,
          };
          this.updateHistory(currOutput);
          this.setState({
            output: currOutput,
          });
        }
      } catch (e) {
        console.error('Error: could not perform operation.', e.message);
        const errorOutput =
          e.message === '500 error'
            ? this.state.results
            : {
                error: 'could not perform operation',
                message: e.message,
              };
        errorOutput.url = baseURL;
        errorOutput.reqType = this.state.reqType;
        this.setState({
          output: errorOutput,
        });
      } finally {
        this.setState({
          loading: false,
        });
      }
    }
  }
async apiFetch ( ) { let baseURL = this . state . url ; if ( baseURL ) { try { this . setState ( { loading : true , } ) ; const res = await fetch ( baseURL , { method : this . state . reqType , headers : { 'Content-Type' : 'application/json' , } , body : this . state . reqType === 'GET' || this . state . reqType === 'DELETE' ? null : JSON . stringify ( JSON . parse ( this . state . reqBody ) ) , } ) ; if ( res ) { const jsonRes = await res . json ( ) ; const newHeaders = { } ; for ( const entry of res . headers . entries ( ) ) { newHeaders [ entry [ 0 ] ] = entry [ 1 ] ; } await this . setState ( { results : jsonRes . results || jsonRes , count : jsonRes . count || 1 , headers : newHeaders , } ) ; if ( this . state . results . error ) { throw new Error ( '500 error' ) ; } const currOutput = { headers : this . state . headers , results : this . state . results , count : this . state . count , url : baseURL , reqType : this . state . reqType , reqBody : this . state . reqBody , } ; this . updateHistory ( currOutput ) ; this . setState ( { output : currOutput , } ) ; } } catch ( e ) { console . error ( 'Error: could not perform operation.' , e . message ) ; const errorOutput = e . message === '500 error' ? this . state . results : { error : 'could not perform operation' , message : e . message , } ; errorOutput . url = baseURL ; errorOutput . reqType = this . state . reqType ; this . setState ( { output : errorOutput , } ) ; } finally { this . setState ( { loading : false , } ) ; } } }
function that fetches an API request based on state valeus.
function that fetches an API request based on state valeus .
/*eslint-disable no-unused-vars*/ /*eslint-disable no-unused-vars*/ // this.state.history.push(errorOutput);