Skip to content
This repository has been archived by the owner on Mar 17, 2021. It is now read-only.

Commit

Permalink
README docs and others; for now, module is not named so examples are …
Browse files Browse the repository at this point in the history
…using the `request` module
  • Loading branch information
joeymalvinni committed Jan 2, 2021
1 parent 83d7dc2 commit 9fae85b
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 8 deletions.
130 changes: 130 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
#### ***Note:** At this time, the module is not named, therefore, the name `request` is used to represent this module.
 
# **Request**
### A Node.js Request module, built using Promises with 0 dependencies.
 

## **Usage**
```js
// require the request module
const request = require('request')

// supports promise chaining
// use the built-in .json() function to return a JSON object
request.get('http://ip.jsontest.com/?callback=showMyIP').json().then(console.log)
//=> showMyIP({"ip": "127.0.0.1"});

// or use an asynchronous function to handle requests
async function get(){
let response = await request.get('http://ip.jsontest.com/?callback=showMyIP').json()
console.log(get())
//=> showMyIP({"ip": "127.0.0.1"});
}
get()
```

 
# **Docs**

## Functions
 
### **get**       `request.get(url, headers)`

Returns an object of functions that apply proccesing to the response. To get the raw response buffer, use `.buffer()`.

    `.text()`
    Returns the get response result as a string.
        **Example:**
> ```js
> request.get('https://postman-echo.com/get').text().then(console.log)
> ```
    `.json()`
    Returns the response result as a JSON object.
        **Example:**
> ```js
> request.get('https://postman-echo.com/get').json().then(console.log)
> ```
    `.buffer()`
    Returns the get response result as a buffer.
        **Example:**
> ```js
> request.get('https://postman-echo.com/get').buffer().then(console.log)
> ```
    `.error()`
    Useful to check if a request returns an error without stopping the Node proccess, returns `undefined` otherwise.
        **Example:**
> ```js
> console.log(request.get('https://postman-echo.com/get').error())
> //=> undefined
> ```
 
### **post**       `request.post(url, headers)`
Returns an object of functions that apply proccesing to the response. To get the raw response buffer, use `.buffer()`.
    `.text()`
    Returns the post response result as a string.
        **Example:**
> ```js
> request.post('https://postman-echo.com/post', {
> data: JSON.stringify({
> user: 'request',
> password: 'requestx@node',
> email: 'requestx@npm.org'
> })
>}).text().then(console.log)
> ```
    `.json()`
    Returns the response result as a JSON object.
        **Example:**
> ```js
> request.post('https://postman-echo.com/post', {
> data: JSON.stringify({
> user: 'request',
> password: 'requestx@node',
> email: 'requestx@npm.org'
> })
>}).json().then(console.log)
> ```
    `.buffer()`
    Returns the get response result as a buffer.
        **Example:**
> ```js
> request.post('https://postman-echo.com/post', {
> data: JSON.stringify({
> user: 'request',
> password: 'requestx@node',
> email: 'requestx@npm.org'
> })
>}).buffer().then(console.log)
> ```
    `.error()`
    Useful to check if a request returns an error without stopping the Node proccess, returns `undefined` otherwise.
        **Example:**
> ```js
> console.log(request.post('https://postman-echo.com/post', {
> data: JSON.stringify({
> user: 'request',
> password: 'requestx@node',
> email: 'requestx@npm.org'
> })
>}).error())
> //=> undefined
> ```
 
### **proxy**       `request.proxy(proxyUrl)`
Proxies a request. Format the string like `protocol://proxyIp:proxyPort` (e.g. `http://127.0.0.1:3000`). Returns the type of request you want to make. Then, request the resource like normal.
        **Example:**
> ```js
> request.proxy('http://127.0.0.1:80').get('https://wtfismyip.com/json').text().then(console.log)
> ```
2 changes: 1 addition & 1 deletion examples/get.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const request = require('../index')

request.get('http://ip.jsontest.com/?callback=showMyIP').text().then(console.log)
console.log(request.get('http://ip.jsontest.com/?callback=showMyIP').error())

//=> showMyIP({"ip": "127.0.0.1"});
2 changes: 1 addition & 1 deletion examples/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const request = require('../index.js')
request.post('https://postman-echo.com/post', {
data: JSON.stringify({
user: 'request',
password: 'requestx@rules',
password: 'requestx@node',
email: 'requestx@npm.org'
})
}).text().then((res)=>{
Expand Down
4 changes: 1 addition & 3 deletions examples/proxy.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const request = require('../index.js')

request.proxy('http://50.206.25.108:80').get('https://wtfismyip.com/json').text().then((proxiedData)=>{
console.log(proxiedData)
})
request.proxy('http://50.206.25.108:80').get('https://wtfismyip.com/json').text().then(console.log)

/*
request.proxy( PROXY URL ).get( REQUEST URL, REQUEST HEADERS).text().then(console.log)
Expand Down
2 changes: 0 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ function proxyget(url, headers, proxy) {
if(headers) { headers = JSON.parse(validate_headers.addHeaders(JSON.stringify(headers))) }
else headers = JSON.parse(validate_headers.addHeaders(headers))



return {
text: function() {
return getProxy(url, headers, proxy).text()
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"start": "node examples/proxy.js",
"start": "node examples/get.js",
"imagereq": "node examples/image.js",
"proxyget": "node examples/proxy.js",
"getreq": "node examples/get.js",
Expand Down

0 comments on commit 9fae85b

Please sign in to comment.