Merge pull request #12 from Seich/next-refactor-config-loading

Refactored the Request's constructor.
This commit is contained in:
Sergio Díaz 2018-04-26 18:38:37 -06:00 committed by GitHub
commit 92142c148b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 26 deletions

View File

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

View File

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