Skip to content

Commit af1e54a

Browse files
committed
add json response method
1 parent c887601 commit af1e54a

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

src/browser/fetch/Response.zig

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,16 @@
1717
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1818

1919
const std = @import("std");
20-
const URL = @import("../../url.zig").URL;
21-
const Page = @import("../page.zig").Page;
22-
const Env = @import("../env.zig").Env;
20+
const log = @import("../../log.zig");
2321

2422
const v8 = @import("v8");
2523

26-
const Http = @import("../../http/Http.zig");
2724
const HttpClient = @import("../../http/Client.zig");
25+
const Http = @import("../../http/Http.zig");
26+
const URL = @import("../../url.zig").URL;
27+
const Env = @import("../env.zig").Env;
2828
const Mime = @import("../mime.zig").Mime;
29+
const Page = @import("../page.zig").Page;
2930

3031
// https://developer.mozilla.org/en-US/docs/Web/API/Response
3132
const Response = @This();
@@ -81,6 +82,26 @@ pub fn _text(self: *const Response, page: *Page) !Env.Promise {
8182
return resolver.promise();
8283
}
8384

85+
pub fn _json(self: *const Response, page: *Page) !Env.Promise {
86+
const resolver = Env.PromiseResolver{
87+
.js_context = page.main_context,
88+
.resolver = v8.PromiseResolver.init(page.main_context.v8_context),
89+
};
90+
91+
const p = std.json.parseFromSliceLeaky(
92+
std.json.Value,
93+
page.arena,
94+
self.body,
95+
.{},
96+
) catch |e| {
97+
log.warn(.browser, "invalid json", .{ .err = e, .source = "fetch" });
98+
return error.SyntaxError;
99+
};
100+
101+
try resolver.resolve(p);
102+
return resolver.promise();
103+
}
104+
84105
const testing = @import("../../testing.zig");
85106
test "fetch: response" {
86107
var runner = try testing.jsRunner(testing.tracking_allocator, .{ .url = "https://lightpanda.io" });

0 commit comments

Comments
 (0)