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

Merge pull request #16 from edyu/master

add http method PATCH for endpoint listener
This commit is contained in:
Rene Schallner 2023-05-17 03:03:50 +02:00 committed by GitHub
commit 7e6bda1b8a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 6 deletions

View file

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

View file

@ -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");
});
}

View file

@ -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 => {},
}
}
};
}