private sendRequest()

in appcenter-file-upload-client-node/src/ac-fus-uploader.ts [443:503]


  private sendRequest(requestOptions: any) {
    // Check if the caller specifies a fully qualified url
    // or if it needs the ACFus base domain to be appended.
    let requestUrl;
    if (requestOptions.useBaseDomain === false) {
      requestUrl = requestOptions.url;
    } else {
      requestUrl = this.uploadData.uploadDomain + "/" + requestOptions.url;
    }

    // All the call requires auth then we add the ACFus token
    if (requestOptions.useAuthentication && requestOptions.useAuthentication === true) {
      if (requestUrl.indexOf("?") > 0) {
        requestUrl += "&";
      } else {
        requestUrl += "?";
      }
      requestUrl += `token=${this.uploadData.urlEncodedToken}`;
    }

    // If cache is disabled we add a timestamp to the url.
    if (requestOptions.cache !== undefined && requestOptions.cache === false) {
      requestUrl += "&_=" + new Date().getTime();
    }
    let body;
    if (requestOptions.chunk) {
      // @ts-ignore
      // this check addresses a trailing zeros bug, where part of the chunk will be empty. Simply touching the size is enough to "fix" the problem
      const size = requestOptions.chunk.size;
      body = requestOptions.chunk;
    }
    const self = this;
    fetchWithOptions(requestUrl, {
      method: requestOptions.type,
      headers: {
        "X-Correlation-ID": self.uploadData.correlationId,
      },
      body: body,
      signal: self.uploadStatus.abortController.signal,
    })
      .then((response) => {
        if (!response.ok) {
          throw new ACHttpError(response.status, response.statusText);
        }
        return response.json().catch((error) => {
          throw new ACHttpError(response.status, error);
        });
      })
      .then((json) => {
        if (requestOptions.success) {
          requestOptions.success(json);
        }
      })
      .catch((error) => {
        // Any other status code or if the page has markup it is
        // considered as failed and invokes to the error callback.
        if (requestOptions.error) {
          requestOptions.error(error);
        }
      });
  }