mirror of
https://github.com/jetzig-framework/jetzig.git
synced 2025-05-14 14:06:08 +00:00
Fix custom routes
Fix CLI view generator
This commit is contained in:
parent
b95506caf9
commit
78b6938530
@ -117,7 +117,7 @@ fn writeAction(allocator: std.mem.Allocator, writer: anytype, action: Action) !v
|
||||
const function = try std.fmt.allocPrint(
|
||||
allocator,
|
||||
\\pub fn {s}({s}request: *jetzig.{s}) !jetzig.View {{
|
||||
\\ return request.render({s});
|
||||
\\ {s}return request.render({s});
|
||||
\\}}
|
||||
\\
|
||||
\\
|
||||
@ -131,7 +131,7 @@ fn writeAction(allocator: std.mem.Allocator, writer: anytype, action: Action) !v
|
||||
if (action.static) "StaticRequest" else "Request",
|
||||
switch (action.method) {
|
||||
.index, .post, .new => "",
|
||||
.get, .put, .patch, .delete => "\n _ = id;",
|
||||
.get, .put, .patch, .delete => "_ = id;\n ",
|
||||
},
|
||||
switch (action.method) {
|
||||
.index, .get, .new => ".ok",
|
||||
|
@ -166,6 +166,9 @@ pub fn route(
|
||||
@memcpy(&view_name, module_name);
|
||||
std.mem.replaceScalar(u8, &view_name, '.', '/');
|
||||
|
||||
const args_fields = std.meta.fields(std.meta.ArgsTuple(@TypeOf(viewFn)));
|
||||
const legacy = args_fields.len > 0 and args_fields[args_fields.len - 1].type == *jetzig.Data;
|
||||
|
||||
self.custom_routes.append(.{
|
||||
.id = "custom",
|
||||
.name = member,
|
||||
@ -175,9 +178,18 @@ pub fn route(
|
||||
.uri_path = path,
|
||||
.layout = if (@hasDecl(module, "layout")) module.layout else null,
|
||||
.view = comptime switch (viewType(path)) {
|
||||
.with_id => .{ .custom = .{ .with_id = viewFn } },
|
||||
.with_args => .{ .custom = .{ .with_args = viewFn } },
|
||||
.without_id => .{ .custom = .{ .without_id = viewFn } },
|
||||
.with_id => if (legacy)
|
||||
.{ .legacy_with_id = viewFn }
|
||||
else
|
||||
.{ .with_id = viewFn },
|
||||
.without_id => if (legacy)
|
||||
.{ .legacy_without_id = viewFn }
|
||||
else
|
||||
.{ .without_id = viewFn },
|
||||
.with_args => if (legacy)
|
||||
.{ .legacy_with_args = viewFn }
|
||||
else
|
||||
.{ .with_args = viewFn },
|
||||
},
|
||||
.template = self.allocator.dupe(u8, &template) catch @panic("OOM"),
|
||||
.json_params = &.{},
|
||||
|
@ -129,15 +129,24 @@ pub fn validateFormat(self: Route, request: *const jetzig.http.Request) bool {
|
||||
|
||||
fn renderFn(self: Route, request: *jetzig.http.Request) anyerror!jetzig.views.View {
|
||||
return switch (self.view) {
|
||||
.without_id => |func| try func(request),
|
||||
.legacy_without_id => |func| try func(request, request.response_data),
|
||||
.with_id => |func| try func(request.path.resource_id, request),
|
||||
.without_id => |func| try func(request),
|
||||
.with_args => |func| try func(
|
||||
try request.path.resourceArgs(self, request.allocator),
|
||||
request,
|
||||
),
|
||||
.legacy_with_id => |func| try func(
|
||||
request.path.resource_id,
|
||||
request,
|
||||
request.response_data,
|
||||
),
|
||||
else => unreachable,
|
||||
.legacy_without_id => |func| try func(request, request.response_data),
|
||||
.legacy_with_args => |func| try func(
|
||||
try request.path.resourceArgs(self, request.allocator),
|
||||
request,
|
||||
request.response_data,
|
||||
),
|
||||
else => unreachable, // renderStaticFn is called for static routes, we can never get here.
|
||||
};
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user