https://github.com/debdut/url-request
The most advanced HTTP Client with Functional Chaining, Async/Await, Delay, Fork, Infinite Chaining and Repeat for building your Complex APIs easily.
https://github.com/debdut/url-request
axios chainer fork node-fetch request simple url url-builder
Last synced: 4 months ago
JSON representation
The most advanced HTTP Client with Functional Chaining, Async/Await, Delay, Fork, Infinite Chaining and Repeat for building your Complex APIs easily.
- Host: GitHub
- URL: https://github.com/debdut/url-request
- Owner: Debdut
- License: mit
- Created: 2020-07-10T19:30:03.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2023-01-06T11:24:36.000Z (about 3 years ago)
- Last Synced: 2025-06-24T14:11:27.504Z (8 months ago)
- Topics: axios, chainer, fork, node-fetch, request, simple, url, url-builder
- Language: JavaScript
- Homepage: https://github.com/Debdut/url-request#readme
- Size: 389 KB
- Stars: 29
- Watchers: 2
- Forks: 2
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# url-request
[](https://shields.io/) [](https://shields.io/)
[](https://shields.io/) [](https://shields.io/)
> _The most advanced HTTP Client and Url Builder with Functional Chaining, Async/Await, Delay, Fork, Infinite Chaining and Repeat for building your Complex APIs easily._
Alternative to Requests, Axios, and Got.
## Installation
```sh
npm install url-request --save
```
## Initialize
```js
const Url = require('url-request')
```
## Examples
Look [Examples](/examples)
### Repeat
Building a Ping server was never as easy! This pings at an interval of 1s and let's you know if you're connected or not.
```js
Url('https://postman-echo.com/get')
.get()
.then(() => console.log('Ping Successful! Internet is up 🤟'))
.catch(() => console.log('Ping Failed! Internet is down ðŸ˜'))
.repeat(1000, 100) // time 1000ms
```
### GET Request w/ Promise
Send a GET request with an access token, then display the result or catch errors.
`GET https://my-json-server.typicode.com/typicode/demo/posts/comments`
```js
Url('https://my-json-server.typicode.com')
.go('typicode/demo')
.go('posts')
.query({ access_token: 'MyAccessToken' })
.get() // GET Request
.then(json => console.log(json))
.catch(err => console.error('[Error]', err))
```
```js
[{ id: 1, post: 'Post 1' },
{ id: 2, post: 'Post 2' }]
```
### Build A Complex Url
Build Complex Urls with deep paths, multiple queries (lists are supported) and fragments.
```js
const url = Url('https://api.workpay.com')
.go('rooms', 'open', 'users')
.query({ id: [10, 12, 13, 14] })
.query({ access_token: 'my-token', password: 'password'})
.fragment('bio')
.url
// https://api.workpay.com//rooms/open/users?id=10,12,13,14&
// access_token=my-token&password=password#bio
```
### Invoke / Execute
```js
Url('https://postman-echo.com')
.invoke('go', 'post')
.invoke('encodeResponse', null) // To Get Full Response Object
.invoke('post', { foo1: 'bar1', foo2: 'bar2' })
.execute()
.then(res => res.json())
.then(json => console.log(json))
.catch(err => console.error(err))
```
### Fork
```js
const userApi = Url('https://api.workpay.com')
.go('user', 123)
const subscriptionsApi = userApi
.fork()
.fragment('subscriptions')
const achievementsApi = userApi
.fork()
.fragment('achievements')
[ userApi, subscriptionsApi, achievementsApi ]
.forEach(api => api
.then(json => console.log(json))
.catch(err => console.log(err)))
```
### POST Request w/ Promise
A POST request with a body `{ subscrbe: 'Apple Music' }` at `https://api.workpay.com/user/123#subscriptions`
```js
Url('https://api.workpay.com')
.go('user', 123)
.fragment('subscriptions')
.post({ subscrbe: 'Apple Music' })
.then(json => console.log(json)) // { success: true }
.catch(err => console.error(err))
```
### Async/Await
```js
const post = async () => {
const request = Url('https://postman-echo.com')
.go('post')
.post({ foo1: 'bar1', foo2: 'bar2' })
return await request
}
```
### Delay
Delay your requests!
```js
Url('https://postman-echo.com/post')
.delay(2000, () => console.log('Wait 2s'))
.post({ foo1: 'bar1', foo2: 'bar2' })
```
### Infinite Chain
See [Infinite Chain Example](/examples/infinite-chain.js)
```js
Url('https://my-json-server.typicode.com')
.go('typicode/demo')
.go('posts')
.get()
.then(json => console.log('[1]', json))
.catch(err => console.error('[Error]', err))
.go(1)
.get()
.then(json => console.log('[2]', json))
.catch(err => console.error('[Error]', err))
.post()
.post()
... // Keep on going!
```
## Functions
```js
class Url {
// Url Construction
constructor (baseUri) // Start with the baseUri
go (...paths) // Go to Sub Path
query (q) // q is query object
fragment (f) // add fragments like #profile
// Fork
fork ()
// default encoding is 'json', can be null, 'string', 'buffer'
encodeResponse (encoding)
// Set headers
header (headers)
// get the formed url as a string
get url ()
// Requests
get (body, statusCode) // GET Request
post (body, statusCode) // POST Request
put (body, statusCode) // PUT Request
delete (body, statusCode) // DELETE Request
patch (body, statusCode) // PATCH Request
then (func)
catch (func)
finally (func)
delay (time, func)
repeat (time, count)
// Command Control, Lazy Execution
invoke (command, ...args)
execute (invokeCommands)
}
```
## Upcoming
- [ ] OAuth
- [ ] HTTP2
- [ ] Proxy
- [ ] Compression
- [ ] Timeout Handling
- [ ] Custom Hooks
- [ ] Request Cancellation