Skip to content

Return error using custom fetch #382

@villenams

Description

@villenams

I am using a custom fetch function to monitor the upload of a file, everything runs fine, but at the end of the process it always returns an error message.
Maybe I should implement my custom fetch in a different way?

error_with_custom_fetch

const graphql = newGraphqlClient({
  fetch: (
  url,
  options: {
    body?: FormData;
    method?: string;
    headers?: Record<string, string>;
  } = {}
) => {
  return new Promise((resolve, reject) => {
    const xhr = new XMLHttpRequest();

    xhr.open(options.method || "POST", url);

    for (const o in options.headers || {}) {
      xhr.setRequestHeader(o, options.headers[o]);
    }

    xhr.onload = (event) => {
      return resolve({
        ok: true,
        text: () => Promise.resolve(event.target),
        json: () => Promise.resolve(event.target)
      });
    };

    xhr.onerror = reject;

    if (xhr.upload) {
      xhr.upload.onprogress = (event) => {
        console.log(event.loaded / event.total) * 100);
      };
    }

    xhr.send(options.body);
  });
}
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions