Skip to content

Instantly share code, notes, and snippets.

@fkleuver
Created February 4, 2020 02:11
Show Gist options
  • Select an option

  • Save fkleuver/b8dcf03e4a9e5adb9d6932d24943cecc to your computer and use it in GitHub Desktop.

Select an option

Save fkleuver/b8dcf03e4a9e5adb9d6932d24943cecc to your computer and use it in GitHub Desktop.
function executeWithFetch(request) {
// EXERCISE FOR THE READER
}
function executeWithAxios(request) {
// EXERCISE FOR THE READER
}
function ClientBuilder() {
return (function () {
let baseUrl, headers, executor;
return {
forBaseUrl(_baseUrl) {
baseUrl = _baseUrl;
},
withHeaders(_headers) {
headers = _headers;
},
usingFetch() {
executor = executeWithFetch;
},
usingAxios() {
executor = executeWithAxios;
},
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,
});
},
};
},
};
})();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment