Created
February 4, 2020 01:42
-
-
Save fkleuver/e85c22b068c12d50f52edf7f1557eb69 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function executeWithFetch(request) { | |
| // EXERCISE FOR THE READER | |
| } | |
| function executeWithAxios(request) { | |
| // EXERCISE FOR THE READER | |
| } | |
| function ClientBuilder() { | |
| return { | |
| forBaseUrl(baseUrl) { | |
| this.baseUrl = baseUrl; | |
| return this; | |
| }, | |
| withHeaders(headers) { | |
| this.headers = headers; | |
| return this; | |
| }, | |
| usingFetch() { | |
| this.executor = executeWithFetch; | |
| return this; | |
| }, | |
| usingAxios() { | |
| this.executor = executeWithAxios; | |
| return this; | |
| }, | |
| build() { | |
| return new Client(this.baseUrl, this.headers, this.executor); | |
| }, | |
| } | |
| } | |
| class Client { | |
| constructor(baseUrl, headers, executor) { | |
| this.baseUrl = baseUrl; | |
| this.headers = headers; | |
| this.executor = executor; | |
| } | |
| post(endpoint, data) { | |
| const url = `${this.baseUrl}${endpoint}`; | |
| return this.executor({ | |
| url, | |
| data, | |
| method: 'POST', | |
| headers: this.headers | |
| }); | |
| } | |
| get(endpoint, params) { | |
| const url = `${this.baseUrl}${endpoint}`; | |
| return this.executor({ | |
| url, | |
| params, | |
| method: 'GET', | |
| headers: this.headers | |
| }); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment