fetch.js 893 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import { BaseClient, BaseResponse } from './base.js';
  2. class FetchResponse extends BaseResponse {
  3. /**
  4. * BaseResponse facade for fetch API Response
  5. * @param {Response} response
  6. */
  7. constructor(response) {
  8. super();
  9. this.response = response;
  10. }
  11. get status() {
  12. return this.response.status;
  13. }
  14. getHeader(name) {
  15. return this.response.headers.get(name);
  16. }
  17. async getData() {
  18. const data = this.response.arrayBuffer
  19. ? await this.response.arrayBuffer()
  20. : (await this.response.buffer()).buffer;
  21. return data;
  22. }
  23. }
  24. export class FetchClient extends BaseClient {
  25. constructor(url, credentials) {
  26. super(url);
  27. this.credentials = credentials;
  28. }
  29. async request({ headers, credentials, signal } = {}) {
  30. const response = await fetch(this.url, {
  31. headers, credentials, signal,
  32. });
  33. return new FetchResponse(response);
  34. }
  35. }