xhr.ts 540 B

123456789101112131415161718
  1. export interface BrowserTransportOptions {
  2. url: string;
  3. data: any;
  4. }
  5. export const makeXHRRequest = (options: BrowserTransportOptions): PromiseLike<XMLHttpRequestResponseType> => {
  6. return new Promise((resolve, reject) => {
  7. const xhr = new XMLHttpRequest();
  8. xhr.onerror = reject;
  9. xhr.onreadystatechange = (): void => {
  10. if (xhr.readyState === 4) {
  11. resolve(xhr.response);
  12. }
  13. };
  14. xhr.open('POST', options.url);
  15. xhr.send(options.data);
  16. });
  17. };