Skip to content

Instantly share code, notes, and snippets.

@fkleuver
Created February 4, 2020 01:42
Show Gist options
  • Select an option

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

Select an option

Save fkleuver/e85c22b068c12d50f52edf7f1557eb69 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 {
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