@@ -23,8 +23,8 @@ const URL = @import("../../url.zig").URL;
2323const Page = @import ("../page.zig" ).Page ;
2424
2525const Response = @import ("./Response.zig" );
26-
2726const Http = @import ("../../http/Http.zig" );
27+ const ReadableStream = @import ("../streams/ReadableStream.zig" );
2828
2929const v8 = @import ("v8" );
3030const Env = @import ("../env.zig" ).Env ;
@@ -37,19 +37,58 @@ pub const RequestInput = union(enum) {
3737 request : * Request ,
3838};
3939
40+ pub const RequestCache = enum {
41+ default ,
42+ @"no-store" ,
43+ reload ,
44+ @"no-cache" ,
45+ @"force-cache" ,
46+ @"only-if-cached" ,
47+
48+ pub fn fromString (str : []const u8 ) ? RequestCache {
49+ for (std .enums .values (RequestCache )) | cache | {
50+ if (std .ascii .eqlIgnoreCase (str , @tagName (cache ))) {
51+ return cache ;
52+ }
53+ } else {
54+ return null ;
55+ }
56+ }
57+ };
58+
59+ pub const RequestCredentials = enum {
60+ omit ,
61+ @"same-origin" ,
62+ include ,
63+
64+ pub fn fromString (str : []const u8 ) ? RequestCredentials {
65+ for (std .enums .values (RequestCredentials )) | cache | {
66+ if (std .ascii .eqlIgnoreCase (str , @tagName (cache ))) {
67+ return cache ;
68+ }
69+ } else {
70+ return null ;
71+ }
72+ }
73+ };
74+
4075// https://developer.mozilla.org/en-US/docs/Web/API/RequestInit
4176pub const RequestInit = struct {
42- method : ? []const u8 = null ,
4377 body : ? []const u8 = null ,
44- integrity : ? []const u8 = null ,
78+ cache : ? []const u8 = null ,
79+ credentials : ? []const u8 = null ,
4580 headers : ? HeadersInit = null ,
81+ integrity : ? []const u8 = null ,
82+ method : ? []const u8 = null ,
4683};
4784
4885// https://developer.mozilla.org/en-US/docs/Web/API/Request/Request
4986const Request = @This ();
5087
5188method : Http.Method ,
5289url : [:0 ]const u8 ,
90+ cache : RequestCache ,
91+ credentials : RequestCredentials ,
5392headers : Headers ,
5493body : ? []const u8 ,
5594body_used : bool = false ,
@@ -68,6 +107,12 @@ pub fn constructor(input: RequestInput, _options: ?RequestInit, page: *Page) !Re
68107 },
69108 };
70109
110+ const body = if (options .body ) | body | try arena .dupe (u8 , body ) else null ;
111+ const cache = (if (options .cache ) | cache | RequestCache .fromString (cache ) else null ) orelse RequestCache .default ;
112+ const credentials = (if (options .credentials ) | creds | RequestCredentials .fromString (creds ) else null ) orelse RequestCredentials .@"same-origin" ;
113+ const integrity = if (options .integrity ) | integ | try arena .dupe (u8 , integ ) else "" ;
114+ const headers = if (options .headers ) | hdrs | try Headers .constructor (hdrs , page ) else Headers {};
115+
71116 const method : Http.Method = blk : {
72117 if (options .method ) | given_method | {
73118 for (std .enums .values (Http .Method )) | method | {
@@ -82,27 +127,33 @@ pub fn constructor(input: RequestInput, _options: ?RequestInit, page: *Page) !Re
82127 }
83128 };
84129
85- const body = if (options .body ) | body | try arena .dupe (u8 , body ) else null ;
86- const integrity = if (options .integrity ) | integ | try arena .dupe (u8 , integ ) else "" ;
87- const headers = if (options .headers ) | hdrs | try Headers .constructor (hdrs , page ) else Headers {};
88-
89130 return .{
90131 .method = method ,
91132 .url = url ,
133+ .cache = cache ,
134+ .credentials = credentials ,
92135 .headers = headers ,
93136 .body = body ,
94137 .integrity = integrity ,
95138 };
96139}
97140
98- // pub fn get_body(self: *const Request) ?[]const u8 {
99- // return self.body;
100- // }
141+ pub fn get_body (self : * const Request , page : * Page ) ! ? * ReadableStream {
142+ if (self .body ) | body | {
143+ const stream = try ReadableStream .constructor (null , null , page );
144+ try stream .queue .append (page .arena , body );
145+ return stream ;
146+ } else return null ;
147+ }
101148
102149pub fn get_bodyUsed (self : * const Request ) bool {
103150 return self .body_used ;
104151}
105152
153+ pub fn get_cache (self : * const Request ) RequestCache {
154+ return self .cache ;
155+ }
156+
106157pub fn get_headers (self : * Request ) * Headers {
107158 return & self .headers ;
108159}
@@ -133,6 +184,8 @@ pub fn _clone(self: *Request, page: *Page) !Request {
133184 return Request {
134185 .body = if (self .body ) | body | try arena .dupe (u8 , body ) else null ,
135186 .body_used = self .body_used ,
187+ .cache = self .cache ,
188+ .credentials = self .credentials ,
136189 .headers = try self .headers .clone (arena ),
137190 .method = self .method ,
138191 .integrity = try arena .dupe (u8 , self .integrity ),
0 commit comments