-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
80 additions
and
30 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,61 +10,52 @@ Proof of concept at making http requests easier to work with in JS / Node. This | |
npm install legible --save | ||
``` | ||
|
||
A request library using template literals. Making requests has never been so straight forward! Make it easy for users to adopt your api, document it using this library, and everyone will understand making requests. | ||
A request library using template literals. Making requests has never been so straight forward! Make it easy for users to adopt your api, document it using this library, and everyone will understand making requests. | ||
|
||
###Example | ||
|
||
```js | ||
import request from 'legible' | ||
|
||
async function TestRequest() { | ||
let body = { | ||
email: '[email protected]', | ||
password: 'secret' | ||
let body = { | ||
email: '[email protected]', | ||
password: 'secret' | ||
} | ||
|
||
let response = await request` | ||
url: https://api.myapp.com/register | ||
method: POST | ||
body: ${body} | ||
headers: ${{ | ||
Authorization: 'Bearer: token' | ||
headers: ${{ | ||
Authorization: 'Bearer: token' | ||
}} | ||
` | ||
} | ||
``` | ||
|
||
##Why Legible? | ||
|
||
**Coming Soon** The following isn't implemented yet. | ||
**New in 0.2.0!** | ||
|
||
|
||
Using template strings, we can pull out variables easily and keep requests as `legible` as possible. Imagine splitting out your code like this using api libraries that include requests like so: | ||
|
||
```js | ||
import { requestable } from 'legible' | ||
//api library tweets.js | ||
export { | ||
register: requestable` | ||
url: 'https://api.twitter.com/register', | ||
headers: ${{ | ||
method: 'POST' | ||
}} | ||
`, | ||
tweets: requestable` | ||
url: https://api.twitter.com/register, | ||
`, | ||
} | ||
//using the library | ||
import request from 'legible' | ||
import methods from './tweets' | ||
import { partial } from 'legible' | ||
|
||
request.attach('twitter', methods) | ||
const twitter = { | ||
register: partial` | ||
url: https://api.twitter.com/register, | ||
method: POST | ||
` | ||
} | ||
|
||
request.twitter.register` | ||
body: ${{ email: '[email protected]', password: 'Tester' }} | ||
twitter.register` | ||
body: ${{ | ||
email: '[email protected]', | ||
password: 'Tester' | ||
}} | ||
` | ||
``` | ||
|
||
|
@@ -95,4 +86,4 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds | |
| :---: | :---: | | ||
<!-- ALL-CONTRIBUTORS-LIST:END --> | ||
|
||
This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome! | ||
This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome! |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
import request from './request' | ||
import partialRequest from './partial' | ||
|
||
export default request | ||
export const partial = partialRequest |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import fetch from 'isomorphic-fetch' | ||
import normalize from './utilities/normalize' | ||
|
||
/* | ||
defines a partial request, | ||
returns a new function that merges any values | ||
*/ | ||
|
||
export default (strings, ...vars) => { | ||
let { options, url } = normalize(strings, vars) | ||
|
||
return (strings, ...vars) => { | ||
let finalData = normalize(strings, vars) | ||
let mergedOptions = { ...options, ...finalData.options } | ||
|
||
return new Promise((resolve, reject) => { | ||
fetch(finalData.url || url, mergedOptions) | ||
.then(response => response.json()) | ||
.then(json => resolve(json)) | ||
.catch(error => reject(error)) | ||
}) | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { partial } from '../../src' | ||
|
||
describe('partial', () => { | ||
it('returns value from response', async function () { | ||
const requests = { | ||
login: partial` | ||
url: https://freegeoip.net/json/github.com | ||
` | ||
} | ||
|
||
let response = await requests.login` | ||
method: GET | ||
` | ||
expect(response.country_code).to.equal('US') | ||
}) | ||
|
||
it('returns a new function', async function () { | ||
expect(typeof partial`url: https://freegeoip.net/json/github.com`).to.equal('function') | ||
}) | ||
|
||
it('overwrites partial data', async function () { | ||
const requests = { | ||
login: partial` | ||
url: https://freegeoip.net/json/github.com | ||
method: POST | ||
` | ||
} | ||
|
||
let response = await requests.login` | ||
method: GET | ||
` | ||
expect(response.country_code).to.equal('US') | ||
}) | ||
}) |