Refactored the Request's constructor.

I didn't like how verbose it was, this is somewhat better.
This commit is contained in:
David Diaz 2018-04-26 18:31:31 -06:00
parent d3b78bc27d
commit 358da930e8
2 changed files with 29 additions and 26 deletions

View File

@ -198,6 +198,7 @@ Beau {
"PARAMS": undefined, "PARAMS": undefined,
"PATH": "/posts/1", "PATH": "/posts/1",
"PAYLOAD": undefined, "PAYLOAD": undefined,
"REQUEST": "GET /posts/1",
"VERB": "GET", "VERB": "GET",
"originalRequest": Object { "originalRequest": Object {
"ALIAS": "get-post", "ALIAS": "get-post",
@ -230,6 +231,7 @@ Beau {
"PARAMS": undefined, "PARAMS": undefined,
"PATH": "/user", "PATH": "/user",
"PAYLOAD": undefined, "PAYLOAD": undefined,
"REQUEST": "GET /user",
"VERB": "GET", "VERB": "GET",
"originalRequest": Object { "originalRequest": Object {
"ALIAS": "user", "ALIAS": "user",

View File

@ -16,44 +16,45 @@ class Request {
this.originalRequest = req; this.originalRequest = req;
this.plugins = plugins; this.plugins = plugins;
const { this.loadCofiguration(
REQUEST, [
ALIAS, 'REQUEST',
PAYLOAD, 'ENDPOINT',
ENDPOINT, 'HEADERS',
PARAMS, 'PAYLOAD',
HEADERS, 'PARAMS',
FORM, 'FORM',
COOKIEJAR 'ALIAS',
} = UpperCaseKeys(req); 'COOKIEJAR'
],
req
);
if (!ALIAS) { if (!this.ALIAS) {
throw new Error(`${REQUEST} is missing an alias.`); throw new Error(`${this.REQUEST} is missing an alias.`);
} }
const { verb, path } = this.parseRequest(REQUEST); const { VERB, PATH } = this.parseRequest(this.REQUEST);
this.VERB = verb; this.VERB = VERB;
this.ENDPOINT = ENDPOINT; this.PATH = PATH;
this.PATH = path;
this.HEADERS = HEADERS;
this.PAYLOAD = PAYLOAD;
this.PARAMS = PARAMS;
this.FORM = FORM;
this.ALIAS = ALIAS;
this.COOKIEJAR = COOKIEJAR;
this.DEPENDENCIES = this.findDependencies(req); this.DEPENDENCIES = this.findDependencies(req);
} }
loadCofiguration(keys, obj) {
obj = UpperCaseKeys(obj);
keys.forEach(k => {
this[k] = obj[k];
});
}
parseRequest(request) { parseRequest(request) {
const parts = request.match(requestRegex); const parts = request.match(requestRegex);
return { return {
verb: parts[1], VERB: parts[1],
path: parts[2] PATH: parts[2]
}; };
} }