Last active
March 11, 2022 22:55
-
-
Save vrogueon/0d55329e16c18c93f6e1ed95433713c2 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
// 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