I wanted to easily make HTTP requests (both GET & POST) with a simple interface that returns promises.
The popular request
& request-promises
package are good, but I wanted to figure out how to do it w/out using external dependencies.
The key features are:
- the ability to set a timeout
- non-200 responses are considered errors that reject the promise.
- any errors at the TCP socker/DNS level reject the promise.
Hat tip to webivore and the Node HTTP docs, whose learning curve I climbed.
It's great for AWS Lambda.
const url = require('url');
const req = require('httpPromise');
// Simple get request
req("https://httpbin.org/get")
.then((res) => { console.log(res)})
.catch((err) => { console.log("oh no: " + err)});
// Get req w/ timeout and extra headers:
req(Object.assign({}, url.parse("https://httpbin.org/get"), {timeout: 2000, headers: {'X-MyHeader': 'MyValue'}}))
// POST data:
req(Object.assign({}, url.parse("https://httpbin.org/post"), { method: 'POST', headers: {'Content-Type': 'application/x-www-form-urlencoded'}}), "foo=bar")
.then((res) => { console.log(res)})
.catch((err) => { console.log("oh no: " + err)});
@latobibor
I do not know which is your IDE of choice but mine is vscode. As of right now, I have done the following:
Found a usage of a default exported anonymous function (as I have used it above), highlighted the text, and pressed F12. Took me right to the declaration.
Proceeded to highlight the default keyword within that declaration then pressed SHIFT + F12 keys. Gave me a list of all function usages throughout the project.
Held my mouse over a randomly chosen usage from the list just mentioned and could see the type data. As a bonus, went back to the declaration, held my mouse over the default keyword, and saw the type data there as well. I even played a bit with the types to see them changing on the usages.
For the sake of random strangers (maybe even future colleagues) on the internet, please consider that when you make statements such as "No, no, no, please never do this for the sake of your colleagues" and "There is no gain, only loss here." you might be coming on as a bit too aggressive and unwelcoming. These are the sorts of interactions that people attempting to join this field mention when they say that the field is unwelcoming to newcomers. It is just plain bad manners to state something of this nature in a less than compassionate way (we are all learning here). I would note this point in particular because your statement: "Default exporting an anonymous function breaks all IDEs." is just flat out wrong, as I have just confirmed with my testing.
Here is another point I've decided to make. The sentence "Default exporting an anonymous function breaks all IDEs." is not just wrong because vscode indeed is not broken but because you are working from the mistaken belief that the language syntax can somehow break a tool that is meant to understand and properly interpret said syntax. If vscode could not provide any of the data or features it usually provides in this particular instance of a function declaration, that is not the language's fault but a bug or a lack of the tool.
My final point is more controversial: Types are not the be-all and end-all (and that is a good thing). Code style decisions should not be made to accommodate typing but for clarity. You should consider that there might be enough people that feel this way that your above statements may seem elitist and misguided.