Created
February 4, 2020 02:18
-
-
Save fkleuver/fde5d6f3add2891d1dd4ccc3fa386855 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 (function () { | |
| let baseUrl, headers, executor; | |
| function forBaseUrl(_baseUrl) { | |
| baseUrl = _baseUrl; | |
| return { withHeaders }; | |
| } | |
| function withHeaders(_headers) { | |
| headers = _headers; | |
| return { usingFetch, usingAxios }; | |
| } | |
| function usingFetch() { | |
| executor = executeWithFetch; | |
| return { build }; | |
| } | |
| function usingAxios() { | |
| executor = executeWithAxios; | |
| return { build }; | |
| } | |
| function build() { | |
| return { | |
| post(endpoint, data) { | |
| return executor({ | |
| url: `${baseUrl}${endpoint}`, | |
| data, | |
| method: 'POST', | |
| headers, | |
| }); | |
| }, | |
| get(endpoint, params) { | |
| return executor({ | |
| url: `${baseUrl}${endpoint}`, | |
| params, | |
| method: 'GET', | |
| headers, | |
| }); | |
| }, | |
| }; | |
| } | |
| return { forBaseUrl }; | |
| })(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment