-
Notifications
You must be signed in to change notification settings - Fork 2
/
WebAPI.Fetch.pas
102 lines (78 loc) · 3.21 KB
/
WebAPI.Fetch.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
unit WebAPI.Fetch;
interface
uses WebAPI.Core, WebAPI.Promises;
type
JResponse = class external 'Response'
Ok : Boolean; external 'ok';
Status : Integer; external 'status';
StatusText : Integer; external 'statusText';
function JSON : JPromise<Variant>; external 'json';
function Text : JPromise<String>; external 'text';
end;
JFormData = class external 'FormData'
constructor Create();
procedure Append(name, value : String); overload; external 'append';
end;
JURLSearchParams = class external 'URLSearchParams'
constructor Create(data : Variant);
end;
function Fetch(res : String) : JPromise<JResponse>; overload; external 'fetch';
function Fetch(res : String; init : Variant) : JPromise<JResponse>; overload; external 'fetch';
procedure FetchJSON(res : String; success : procedure (data : Variant); fail : procedure (r : JResponse)); overload;
procedure FetchJSON(res : String; success : procedure (data : Variant)); overload;
procedure PostRawBody(res, dataType : String; data : Variant; success : procedure (data : Variant); fail : procedure (r : JResponse));
procedure PostJSON(res : String; data : Variant; success : procedure (data : Variant); fail : procedure (r : JResponse));
procedure PostFormData(res : String; data : Variant; success : procedure (data : Variant); fail : procedure (r : JResponse));
procedure PostURLEncoded(res : String; data : Variant; success : procedure (data : Variant); fail : procedure (r : JResponse));
implementation
procedure FetchJSON(res: String; success: procedure (data: Variant); fail: procedure (r: JResponse));
begin
Fetch(res, class
credentials := 'same-origin'
end).&Then(lambda (r : JResponse)
if r.Ok then
r.JSON.&Then(success)
else fail(r)
end).Catch(fail);
end;
procedure FetchJSON(res: String; success: procedure (data: Variant));
begin
Fetch(res, class
credentials := 'same-origin'
end).&Then(lambda (r : JResponse)
if r.Ok then
r.JSON.&Then(success);
end);
end;
procedure PostRawBody(res, dataType: String; data: Variant; success: procedure (data: Variant); fail: procedure (r: JResponse));
begin
var init : Variant = class
'method' := 'POST';
credentials := 'same-origin';
body := data;
end;
if dataType <> '' then
init.headers := class
'Content-Type' := dataType;
end;
Fetch(res, init).&Then(lambda (r : JResponse)
if r.Ok then
r.JSON.&Then(success)
else fail(r)
end).Catch(fail);
end;
procedure PostJSON(res: String; data: Variant; success: procedure (data: Variant); fail: procedure (r: JResponse));
begin
PostRawBody(res, 'application/json', JSON.Stringify(data), success, fail);
end;
procedure PostFormData(res: String; data: Variant; success: procedure (data: Variant); fail: procedure (r: JResponse));
begin
var fd := new JFormData;
for var k in data do
fd.Append(k, JSON.Stringify(data[k]));
PostRawBody(res, '', fd, success, fail);
end;
procedure PostURLEncoded(res: String; data: Variant; success: procedure (data: Variant); fail: procedure (r: JResponse));
begin
PostRawBody(res, 'application/x-www-form-urlencoded', new JURLSearchParams(data), success, fail);
end;