mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 07:03:29 +00:00
add json response method
This commit is contained in:
@@ -17,15 +17,16 @@
|
|||||||
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
const std = @import("std");
|
const std = @import("std");
|
||||||
const URL = @import("../../url.zig").URL;
|
const log = @import("../../log.zig");
|
||||||
const Page = @import("../page.zig").Page;
|
|
||||||
const Env = @import("../env.zig").Env;
|
|
||||||
|
|
||||||
const v8 = @import("v8");
|
const v8 = @import("v8");
|
||||||
|
|
||||||
const Http = @import("../../http/Http.zig");
|
|
||||||
const HttpClient = @import("../../http/Client.zig");
|
const HttpClient = @import("../../http/Client.zig");
|
||||||
|
const Http = @import("../../http/Http.zig");
|
||||||
|
const URL = @import("../../url.zig").URL;
|
||||||
|
const Env = @import("../env.zig").Env;
|
||||||
const Mime = @import("../mime.zig").Mime;
|
const Mime = @import("../mime.zig").Mime;
|
||||||
|
const Page = @import("../page.zig").Page;
|
||||||
|
|
||||||
// https://developer.mozilla.org/en-US/docs/Web/API/Response
|
// https://developer.mozilla.org/en-US/docs/Web/API/Response
|
||||||
const Response = @This();
|
const Response = @This();
|
||||||
@@ -81,6 +82,26 @@ pub fn _text(self: *const Response, page: *Page) !Env.Promise {
|
|||||||
return resolver.promise();
|
return resolver.promise();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn _json(self: *const Response, page: *Page) !Env.Promise {
|
||||||
|
const resolver = Env.PromiseResolver{
|
||||||
|
.js_context = page.main_context,
|
||||||
|
.resolver = v8.PromiseResolver.init(page.main_context.v8_context),
|
||||||
|
};
|
||||||
|
|
||||||
|
const p = std.json.parseFromSliceLeaky(
|
||||||
|
std.json.Value,
|
||||||
|
page.arena,
|
||||||
|
self.body,
|
||||||
|
.{},
|
||||||
|
) catch |e| {
|
||||||
|
log.warn(.browser, "invalid json", .{ .err = e, .source = "fetch" });
|
||||||
|
return error.SyntaxError;
|
||||||
|
};
|
||||||
|
|
||||||
|
try resolver.resolve(p);
|
||||||
|
return resolver.promise();
|
||||||
|
}
|
||||||
|
|
||||||
const testing = @import("../../testing.zig");
|
const testing = @import("../../testing.zig");
|
||||||
test "fetch: response" {
|
test "fetch: response" {
|
||||||
var runner = try testing.jsRunner(testing.tracking_allocator, .{ .url = "https://lightpanda.io" });
|
var runner = try testing.jsRunner(testing.tracking_allocator, .{ .url = "https://lightpanda.io" });
|
||||||
|
|||||||
Reference in New Issue
Block a user