fetch method

Future<CFetchResponse> fetch({
  1. required CFetchAction action,
  2. required String url,
  3. bool? adAuctionHeaders,
  4. CObject? body,
  5. String? cache,
  6. String? credentials,
  7. CObject? headers,
  8. String? integrity,
  9. bool? keepalive,
  10. String? mode,
  11. String? priority,
  12. String? redirect,
  13. String? referrer,
  14. String? referrerPolicy,
  15. AbortSignal? signal,
})

Implements the ability to fetch a server's REST API endpoint to retrieve and manage data. The actions for the REST API are controlled via the CFetchAction enumerated values with optional items to pass to the endpoint. The result is a CFetchResponse wrapping the REST API endpoint response to the request.

See https://developer.mozilla.org/en-US/docs/Web/API/RequestInit for details of all the options.

Implementation

Future<CFetchResponse> fetch({
  required CFetchAction action,
  required String url,
  bool? adAuctionHeaders,
  CObject? body,
  String? cache,
  String? credentials,
  CObject? headers,
  String? integrity,
  bool? keepalive,
  String? mode,
  String? priority,
  String? redirect,
  String? referrer,
  String? referrerPolicy,
  web.AbortSignal? signal,
}) async {
  try {
    // Form the request for the fetch call.
    var request = web.RequestInit();
    request.method = action.name.toUpperCase();
    if (adAuctionHeaders != null) {
      request.adAuctionHeaders = adAuctionHeaders;
    }
    if (body != null) {
      request.body = body.jsify();
    }
    if (cache != null) {
      request.cache = cache;
    }
    if (credentials != null) {
      request.credentials = credentials;
    }
    if (headers != null) {
      request.headers = headers.jsify() as JSObject;
    }
    if (integrity != null) {
      request.integrity = integrity;
    }
    if (keepalive != null) {
      request.keepalive = keepalive;
    }
    if (mode != null) {
      request.mode = mode;
    }
    if (priority != null) {
      request.priority = priority;
    }
    if (redirect != null) {
      request.redirect = redirect;
    }
    if (referrer != null) {
      request.referrer = referrer;
    }
    if (referrerPolicy != null) {
      request.referrerPolicy = referrerPolicy;
    }
    if (signal != null) {
      request.signal = signal;
    }

    // Go perform the fetch
    var resp = await web.window.fetch(url.toJS, request).toDart;
    var contentType = resp.headers.get(HttpHeaders.contentTypeHeader) ?? '';
    var status = resp.status;
    var statusText = resp.statusText;
    var data = contentType.containsIgnoreCase("application/json")
        ? await resp.json().toDart
        : contentType.containsIgnoreCase("form-data")
            ? await resp.formData().toDart
            : contentType.containsIgnoreCase("application/octet-stream")
                ? await resp.blob().toDart
                : contentType.containsIgnoreCase("text/")
                    ? await resp.text().toDart
                    : "";
    return CFetchResponse(data, status, statusText);
  } catch (ex, st) {
    codemelted_logger.error(data: ex.toString(), stackTrace: st);
    return CFetchResponse(null, 500, ex.toString());
  }
}