mirror of
https://github.com/zigzap/zap.git
synced 2025-10-20 07:04:08 +00:00
add http method PATCH for endpoint listener
This commit is contained in:
parent
3ef7356bbe
commit
a641dcc082
3 changed files with 30 additions and 6 deletions
|
@ -23,6 +23,7 @@ pub fn init(
|
|||
.get = getUser,
|
||||
.post = postUser,
|
||||
.put = putUser,
|
||||
.patch = putUser,
|
||||
.delete = deleteUser,
|
||||
}),
|
||||
};
|
||||
|
|
|
@ -174,26 +174,25 @@
|
|||
getUserList();
|
||||
})
|
||||
.catch((error) => {
|
||||
log("Error posting data");
|
||||
log("Error deleting data");
|
||||
});
|
||||
}
|
||||
|
||||
function changeUser(id) {
|
||||
var firstname = document.getElementById("first_" + id);
|
||||
firstname = firstname.value;
|
||||
const firstname = document.getElementById("first_" + id).value;
|
||||
const lastname = document.getElementById("last_" + id).value;
|
||||
data = {
|
||||
first_name: firstname,
|
||||
last_name: lastname,
|
||||
}
|
||||
sendJSON(data, "/users/" + id, "PUT")
|
||||
sendJSON(data, "/users/" + id, "PATCH")
|
||||
.then((response) => response.json())
|
||||
.then((data) => {
|
||||
log("SUCESS: " + JSON.stringify(data));
|
||||
getUserList();
|
||||
})
|
||||
.catch((error) => {
|
||||
log("Error posting data");
|
||||
log("Error updating data");
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -249,7 +248,7 @@
|
|||
showTable(data);
|
||||
})
|
||||
.catch((error) => {
|
||||
log("Error posting data");
|
||||
log("Error fetching data");
|
||||
});
|
||||
}
|
||||
|
||||
|
|
|
@ -13,6 +13,7 @@ pub const SimpleEndpointSettings = struct {
|
|||
post: ?RequestFn = null,
|
||||
put: ?RequestFn = null,
|
||||
delete: ?RequestFn = null,
|
||||
patch: ?RequestFn = null,
|
||||
/// only applicable to AuthenticatingEndpoint
|
||||
unauthorized: ?RequestFn = null,
|
||||
};
|
||||
|
@ -30,6 +31,7 @@ pub const SimpleEndpoint = struct {
|
|||
.post = s.post orelse &nop,
|
||||
.put = s.put orelse &nop,
|
||||
.delete = s.delete orelse &nop,
|
||||
.patch = s.patch orelse &nop,
|
||||
.unauthorized = s.unauthorized orelse &nop,
|
||||
},
|
||||
};
|
||||
|
@ -50,6 +52,8 @@ pub const SimpleEndpoint = struct {
|
|||
return self.settings.put.?(self, r);
|
||||
if (std.mem.eql(u8, m, "DELETE"))
|
||||
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,
|
||||
.put = if (e.settings.put != null) put else null,
|
||||
.delete = if (e.settings.delete != null) delete else null,
|
||||
.patch = if (e.settings.patch != null) patch else null,
|
||||
.unauthorized = e.settings.unauthorized,
|
||||
}),
|
||||
};
|
||||
|
@ -161,6 +166,25 @@ pub fn AuthenticatingEndpoint(comptime Authenticator: type) type {
|
|||
.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 => {},
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue