diff options
| author | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-06-06 10:35:14 +0200 |
|---|---|---|
| committer | Nathan Reiner <nathan@nathanreiner.xyz> | 2026-06-06 10:35:14 +0200 |
| commit | 1d6e34388d9d792fd184c8b9e0fb11fb9b7e14f9 (patch) | |
| tree | 2eaaf5077c604c79a359037c772f99bb83839545 /src/http | |
| parent | f39dfd01ab06cf92a3bcad75fcb175a8242ec1b1 (diff) | |
api: add example implementations
Diffstat (limited to 'src/http')
| -rw-r--r-- | src/http/Response.zig | 17 |
1 files changed, 17 insertions, 0 deletions
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, |