From 1d6e34388d9d792fd184c8b9e0fb11fb9b7e14f9 Mon Sep 17 00:00:00 2001 From: Nathan Reiner Date: Sat, 6 Jun 2026 10:35:14 +0200 Subject: api: add example implementations --- src/http/Response.zig | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'src/http/Response.zig') diff --git a/src/http/Response.zig b/src/http/Response.zig index 8d10992..d54792b 100644 --- a/src/http/Response.zig +++ b/src/http/Response.zig @@ -5,6 +5,15 @@ pub const Message = union(enum) { static: []const u8, streaming: std.Io.File, + pub fn json(arena: std.mem.Allocator, value: anytype) !Message { + var allocating: std.Io.Writer.Allocating = .init(arena); + + var jsonify: std.json.Stringify = .{ .writer = &allocating.writer }; + try jsonify.write(value); + + return .string(try allocating.toOwnedSlice()); + } + pub fn string(content: []const u8) Message { return .{ .static = content }; } @@ -27,6 +36,14 @@ pub fn ok(message: Message) @This() { return with(.ok, message); } +pub fn ok_or_error(message: anyerror!Message) @This() { + return ok(message catch return with(.internal_server_error, .void)); +} + +pub fn bad_request(message: Message) @This() { + return with(.bad_request, message); +} + pub fn send( self: *const @This(), io: std.Io, -- cgit v1.2.3-70-g09d2