1
0
Fork 0
mirror of https://github.com/zigzap/zap.git synced 2025-10-20 15:14:08 +00:00

add http method PATCH for endpoint listener

This commit is contained in:
Ed Yu 2023-05-16 14:34:47 -07:00
parent 3ef7356bbe
commit a641dcc082
3 changed files with 30 additions and 6 deletions

View file

@ -23,6 +23,7 @@ pub fn init(
.get = getUser, .get = getUser,
.post = postUser, .post = postUser,
.put = putUser, .put = putUser,
.patch = putUser,
.delete = deleteUser, .delete = deleteUser,
}), }),
}; };

View file

@ -174,26 +174,25 @@
getUserList(); getUserList();
}) })
.catch((error) => { .catch((error) => {
log("Error posting data"); log("Error deleting data");
}); });
} }
function changeUser(id) { function changeUser(id) {
var firstname = document.getElementById("first_" + id); const firstname = document.getElementById("first_" + id).value;
firstname = firstname.value;
const lastname = document.getElementById("last_" + id).value; const lastname = document.getElementById("last_" + id).value;
data = { data = {
first_name: firstname, first_name: firstname,
last_name: lastname, last_name: lastname,
} }
sendJSON(data, "/users/" + id, "PUT") sendJSON(data, "/users/" + id, "PATCH")
.then((response) => response.json()) .then((response) => response.json())
.then((data) => { .then((data) => {
log("SUCESS: " + JSON.stringify(data)); log("SUCESS: " + JSON.stringify(data));
getUserList(); getUserList();
}) })
.catch((error) => { .catch((error) => {
log("Error posting data"); log("Error updating data");
}); });
} }
@ -249,7 +248,7 @@
showTable(data); showTable(data);
}) })
.catch((error) => { .catch((error) => {
log("Error posting data"); log("Error fetching data");
}); });
} }

View file

@ -13,6 +13,7 @@ pub const SimpleEndpointSettings = struct {
post: ?RequestFn = null, post: ?RequestFn = null,
put: ?RequestFn = null, put: ?RequestFn = null,
delete: ?RequestFn = null, delete: ?RequestFn = null,
patch: ?RequestFn = null,
/// only applicable to AuthenticatingEndpoint /// only applicable to AuthenticatingEndpoint
unauthorized: ?RequestFn = null, unauthorized: ?RequestFn = null,
}; };
@ -30,6 +31,7 @@ pub const SimpleEndpoint = struct {
.post = s.post orelse &nop, .post = s.post orelse &nop,
.put = s.put orelse &nop, .put = s.put orelse &nop,
.delete = s.delete orelse &nop, .delete = s.delete orelse &nop,
.patch = s.patch orelse &nop,
.unauthorized = s.unauthorized orelse &nop, .unauthorized = s.unauthorized orelse &nop,
}, },
}; };
@ -50,6 +52,8 @@ pub const SimpleEndpoint = struct {
return self.settings.put.?(self, r); return self.settings.put.?(self, r);
if (std.mem.eql(u8, m, "DELETE")) if (std.mem.eql(u8, m, "DELETE"))
return self.settings.delete.?(self, r); return self.settings.delete.?(self, r);
if (std.mem.eql(u8, m, "PATCH"))
return self.settings.patch.?(self, r);
} }
} }
}; };
@ -74,6 +78,7 @@ pub fn AuthenticatingEndpoint(comptime Authenticator: type) type {
.post = if (e.settings.post != null) post else null, .post = if (e.settings.post != null) post else null,
.put = if (e.settings.put != null) put else null, .put = if (e.settings.put != null) put else null,
.delete = if (e.settings.delete != null) delete else null, .delete = if (e.settings.delete != null) delete else null,
.patch = if (e.settings.patch != null) patch else null,
.unauthorized = e.settings.unauthorized, .unauthorized = e.settings.unauthorized,
}), }),
}; };
@ -161,6 +166,25 @@ pub fn AuthenticatingEndpoint(comptime Authenticator: type) type {
.Handled => {}, .Handled => {},
} }
} }
/// here, the auth_endpoint will be passed in
pub fn patch(e: *SimpleEndpoint, r: zap.SimpleRequest) void {
const authEp: *Self = @fieldParentPtr(Self, "auth_endpoint", e);
switch (authEp.authenticator.authenticateRequest(&r)) {
.AuthFailed => {
if (e.settings.unauthorized) |unauthorized| {
unauthorized(authEp.endpoint, r);
return;
} else {
r.setStatus(.unauthorized);
r.sendBody("UNAUTHORIZED") catch return;
return;
}
},
.AuthOK => authEp.endpoint.settings.patch.?(authEp.endpoint, r),
.Handled => {},
}
}
}; };
} }