Skip to content

Commit 698fea6

Browse files
authored
Add partial support (#16)
1 parent 2bd431d commit 698fea6

File tree

4 files changed

+80
-30
lines changed

4 files changed

+80
-30
lines changed

README.md

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -10,61 +10,52 @@ Proof of concept at making http requests easier to work with in JS / Node. This
1010
npm install legible --save
1111
```
1212

13-
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.
13+
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.
1414

1515
###Example
1616

1717
```js
1818
import request from 'legible'
1919

2020
async function TestRequest() {
21-
let body = {
22-
email: 'test@test.com',
23-
password: 'secret'
21+
let body = {
22+
email: 'test@test.com',
23+
password: 'secret'
2424
}
25-
25+
2626
let response = await request`
2727
url: https://api.myapp.com/register
2828
method: POST
2929
body: ${body}
30-
headers: ${{
31-
Authorization: 'Bearer: token'
30+
headers: ${{
31+
Authorization: 'Bearer: token'
3232
}}
3333
`
3434
}
3535
```
3636

3737
##Why Legible?
3838

39-
**Coming Soon** The following isn't implemented yet.
39+
**New in 0.2.0!**
4040

4141

4242
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:
4343

4444
```js
45-
import { requestable } from 'legible'
46-
//api library tweets.js
47-
export {
48-
register: requestable`
49-
url: 'https://api.twitter.com/register',
50-
headers: ${{
51-
method: 'POST'
52-
}}
53-
`,
54-
tweets: requestable`
55-
url: https://api.twitter.com/register,
56-
`,
57-
}
58-
59-
//using the library
60-
61-
import request from 'legible'
62-
import methods from './tweets'
45+
import { partial } from 'legible'
6346

64-
request.attach('twitter', methods)
47+
const twitter = {
48+
register: partial`
49+
url: https://api.twitter.com/register,
50+
method: POST
51+
`
52+
}
6553

66-
request.twitter.register`
67-
body: ${{ email: 'test@test.com', password: 'Tester' }}
54+
twitter.register`
55+
body: ${{
56+
email: 'test@test.com',
57+
password: 'Tester'
58+
}}
6859
`
6960
```
7061

@@ -95,4 +86,4 @@ Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds
9586
| :---: | :---: |
9687
<!-- ALL-CONTRIBUTORS-LIST:END -->
9788

98-
This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!
89+
This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome!

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
import request from './request'
2+
import partialRequest from './partial'
23

34
export default request
5+
export const partial = partialRequest

src/partial.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import fetch from 'isomorphic-fetch'
2+
import normalize from './utilities/normalize'
3+
4+
/*
5+
defines a partial request,
6+
returns a new function that merges any values
7+
*/
8+
9+
export default (strings, ...vars) => {
10+
let { options, url } = normalize(strings, vars)
11+
12+
return (strings, ...vars) => {
13+
let finalData = normalize(strings, vars)
14+
let mergedOptions = { ...options, ...finalData.options }
15+
16+
return new Promise((resolve, reject) => {
17+
fetch(finalData.url || url, mergedOptions)
18+
.then(response => response.json())
19+
.then(json => resolve(json))
20+
.catch(error => reject(error))
21+
})
22+
}
23+
}

tests/unit/partial.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { partial } from '../../src'
2+
3+
describe('partial', () => {
4+
it('returns value from response', async function () {
5+
const requests = {
6+
login: partial`
7+
url: https://freegeoip.net/json/github.com
8+
`
9+
}
10+
11+
let response = await requests.login`
12+
method: GET
13+
`
14+
expect(response.country_code).to.equal('US')
15+
})
16+
17+
it('returns a new function', async function () {
18+
expect(typeof partial`url: https://freegeoip.net/json/github.com`).to.equal('function')
19+
})
20+
21+
it('overwrites partial data', async function () {
22+
const requests = {
23+
login: partial`
24+
url: https://freegeoip.net/json/github.com
25+
method: POST
26+
`
27+
}
28+
29+
let response = await requests.login`
30+
method: GET
31+
`
32+
expect(response.country_code).to.equal('US')
33+
})
34+
})

0 commit comments

Comments
 (0)