From 358da930e84b5d0598538b1533bb4ddf984b67b8 Mon Sep 17 00:00:00 2001 From: David Diaz Date: Thu, 26 Apr 2018 18:31:31 -0600 Subject: [PATCH] Refactored the Request's constructor. I didn't like how verbose it was, this is somewhat better. --- src/__tests__/__snapshots__/beau.spec.js.snap | 2 + src/request.js | 53 ++++++++++--------- 2 files changed, 29 insertions(+), 26 deletions(-) diff --git a/src/__tests__/__snapshots__/beau.spec.js.snap b/src/__tests__/__snapshots__/beau.spec.js.snap index 723eb90..285efc8 100644 --- a/src/__tests__/__snapshots__/beau.spec.js.snap +++ b/src/__tests__/__snapshots__/beau.spec.js.snap @@ -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", diff --git a/src/request.js b/src/request.js index 6b1c8e8..59e7e82 100644 --- a/src/request.js +++ b/src/request.js @@ -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] }; }