Skip to content

Commit

Permalink
Add partial support (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
zackify committed Dec 23, 2016
1 parent 2bd431d commit 698fea6
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 30 deletions.
51 changes: 21 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}}
`
```

Expand Down Expand Up @@ -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!
2 changes: 2 additions & 0 deletions src/index.js
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
23 changes: 23 additions & 0 deletions src/partial.js
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))
})
}
}
34 changes: 34 additions & 0 deletions tests/unit/partial.js
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')
})
})

0 comments on commit 698fea6

Please sign in to comment.