[Pas2js] window.fetch API

Simon Ameis SAmeis.fpc at web.de
Sat Jul 27 12:27:19 CEST 2019


Hello all,

I'd like to contribute the bindings for the Fetch API as documented
here: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API

The methods in TWindowHelper may be integrated into TJSWindow directly.
However I didn't want to modify the pas2js sources for my project.

 TJSReadableStream = class external name 'ReadableStream'
  private
    flocked: Boolean; external name 'locked';
  public
    property locked: Boolean read flocked;
    constructor new(underlyingSource: TJSObject);
    constructor new(underlyingSource, queueingStrategy: TJSObject);
    function cancel(reason: TJSDOMString): TJSPromise;
    function getReader(): TJSObject; overload;
    function getReader(mode: TJSObject): TJSObject; overload;
    function pipeThrough(transformStream: TJSObject): TJSReadableStream;
overload;
    function pipeThrough(transformStream, options: TJSObject):
TJSReadableStream; overload;
    function pipeTo(destination: TJSObject): TJSPromise; overload;
    function pipeTo(destination, options: TJSObject): TJSPromise; overload;
    function tee(): TJSArray; // array containing two TJSReadableStream
instances
  end;

  TJSBody = class external name 'Body' (TJSObject)
  private
    fbody: TJSReadableStream; external name 'body';
    fbodyUsed: Boolean; external name 'bodyUsed';
  public
    property body: TJSReadableStream read fbody;
    property bodyUsed: Boolean read fbodyUsed;
    function arrayBuffer(): TJSPromise; // resolves to TJSArrayBuffer
    function blob(): TJSPromise; // resolves to TJSBlob
    function json(): TJSPromise; // resolves to JSON / TJSValue
    function text(): TJSPromise; // resolves to USVString, always
decoded using UTF-8
  end;

  TJSResponse = class external name 'Response' (TJSBody)
  private
    fheaders: TJSObject;  external name 'headers';
    fok: Boolean; external name 'ok';
    fredirected: Boolean; external name 'redirected';
    fstatus: NativeInt; external name 'status';
    fstatusText: String; external name 'statusText';
    ftype: String; external name 'type';
    furl: String; external name 'url';
    fuseFinalUrl: Boolean; external name 'useFinalUrl';
  public
    property headers: TJSObject read fheaders; //
    property ok: Boolean read fok;
    property redirected: Boolean read fredirected;
    property status: NativeInt read fstatus;
    property statusText: String read fstatusText; //
    property _type: String read ftype; //
    property url: String read furl; //
    property useFinalUrl: Boolean read fuseFinalUrl write fuseFinalUrl;
    constructor new(body: TJSObject; init: TJSObject); varargs; external
name 'new';
    function clone(): TJSResponse;
    function error(): TJSResponse;
    function redirect(url: String; Status: NativeInt): TJSResponse;
  end;

  TWindowHelper = class helper for TJSWindow
    function fetch(resource: String; init: TJSObject): TJSPromise;
overload; external name 'fetch';
    function fetch(resource: String): TJSPromise; overload; external
name 'fetch';
    function fetch(resource: TJSObject; init: TJSObject): TJSPromise;
overload; external name 'fetch';
    function fetch(resource: TJSObject): TJSPromise; overload; external
name 'fetch';
  end; 

Kind Regards
Simon Ameis



More information about the Pas2js mailing list