exports.handler = function(request,response,state) {
I can see in the PUT example, we can get JSON body from the state? Why not get it from the request body?
var title = $tw.utils.decodeURIComponentSafe(state.params[0]),
fields = $tw.utils.parseJSONSafe(state.data);
So what is this state actually? Is there a doc for it, or where is the essential code of it? I can write a TS type for it if I understand it.
Hi @linonetwo the “state” object should perhaps have been called “context” – it is a collection of properties that (mostly) relate to the state of the server, rather than the individual request.
I say “most”, because as you can see from the code, there is actually a bunch of properties that are derived from the current request (eg urlInfo), which should perhaps have been a separate object.
var state = {};
state.wiki = options.wiki || self.wiki;
state.boot = options.boot || self.boot;
state.server = self;
state.urlInfo = url.parse(request.url);
state.queryParameters = querystring.parse(state.urlInfo.query);
state.pathPrefix = options.pathPrefix || this.get("path-prefix") || "";
state.sendResponse = sendResponse.bind(self,request,response);
// Get the principals authorized to access this resource
state.authorizationType = options.authorizationType || this.methodMappings[request.method] || "readers";
// Check for the CSRF header if this is a write
if(!this.csrfDisable && state.authorizationType === "writers" && request.headers["x-requested-with"] !== "TiddlyWiki") {
response.writeHead(403,"'X-Requested-With' header required to login to '" + this.servername + "'");
response.end();
return;
}
// Check whether anonymous access is granted
state.allowAnon = this.isAuthorized(state.authorizationType,null);
1 Like
Thank you Jeremy, I think context may be better, most server frameworks use it.
I can start using context in my new plugin…