Path, Form, Loaded
On this page:
- spider.Path: Handler parameter: a
:namesegment of the route, converted.id: spider.Path(i64, "id"). - spider.Form: Handler parameter: the submitted form as a struct.
form: spider.Form(Input). - spider.Loaded: Handler parameter: the resource the route's spider.resourcePolicy loaded.
spider.Path
pub fn Path(comptime T: type, comptime name: []const u8) type
A handler parameter filled from a :name segment of the route, already converted: an integer type or []const u8.
fn show(c: *spider.Ctx, id: spider.Path(i64, "id")) !spider.Response {
return c.json(.{ .id = id.value }, .{});
}
A value that is not a T (/posts/abc for an integer, or a number that does not fit in T) never reaches the handler: the request fails with error.InvalidPathParam, a 400. A route without a :name segment of that name fails with error.MissingPathParam. Integers are read in base 10; a []const u8 is the segment as it came in the URL, not percent-decoded.
Fields
value: T
spider.Form
pub fn Form(comptime T: type) type
A handler parameter filled from the submitted form, as Ctx.parseForm would:
const Input = struct { title: []const u8 = "", body: []const u8 = "" };
fn create(c: *spider.Ctx, form: spider.Form(Input)) !spider.Response {
return c.json(.{ .title = form.value.title }, .{});
}
An error of parseForm (error.BodyEmpty for a request without a body) is the request's error, and the handler is not called.
Fields
value: T
spider.Loaded
pub fn Loaded(comptime T: type) type
The resource the route's spider.resourcePolicy(name, T, ...) loaded and allowed: fn edit(post: spider.Loaded(Post), c: *spider.Ctx), then post.value, a *T that lives in the request arena. A route without such a policy fails with error.ResourceNotLoaded (a 500: the route is wired wrong).
Fields
value: *T