Skip to content

Instantly share code, notes, and snippets.

@vrogueon
Last active March 11, 2022 22:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vrogueon/0d55329e16c18c93f6e1ed95433713c2 to your computer and use it in GitHub Desktop.
Save vrogueon/0d55329e16c18c93f6e1ed95433713c2 to your computer and use it in GitHub Desktop.
// Crear una clase builder para un request http
// (Puede incluir URL, headers, body, timeout, etc…)
'use strict';
class httpBuilder {
url = function(url) {
this.url = url;
return this;
}
headers = function(headers) {
this.headers = headers;
return this;
}
timeout = function(timeout) {
this.timeout = timeout;
return this;
}
body = function(body) {
this.body = body;
return this;
}
build = function() {
return new httpClient(this.url, this.headers, this.timeout, this.body);
}
}
class httpClient {
constructor(url, headers, timeout, body) {
this.url = url;
this.headers = headers;
this.timeout = timeout;
this.body = body;
}
post = function() {
return `hola, soy un post y este es mi bearer ${this.headers.Authorization} y mi body es ${this.body}`;
};
get = function() {
return `hola, soy un get y este es mi timeout ${this.timeout}`;
};
};
const client = new httpBuilder()
.url('localhost:3000')
.headers({Authorization: 'Bearer asd'})
.body({test: 'moons'})
.timeout(3000)
.build();
console.log(client.post());
console.log(client.get());
// Crear un singleton para configuración
class SingleConfig {
static instance;
constructor(config1, config2) {
if(SingleConfig.instance) return this.constructor.instance;
this.config1 = config1;
this.config2 = config2;
SingleConfig.instance = this;
return this.instance;
}
}
const config = new SingleConfig('configuración 1', 'configuración 2');
console.log(config)
const config2 = new SingleConfig('configuración 3', 'configuración 4');
console.log(config2)
const config = new SingleConfig('configuración 1', 'configuración 2');
console.log(config)
const config2 = new SingleConfig('configuración 3', 'configuración 4');
console.log(config2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment