###Getting started:
Use the default urlFactory method to set your base url and default params you want to include with each request.
For example, at foo company, make a new file: app/utils/fooUrl.js
import urlFactory from 'url-factory'
const fooUrl = urlFactory('https://foo.com', { 'apiVersion': '1.0'})
export default fooUrl
Then consume the util:
import fetch from 'isomorphic-fetch'
import fooUrl from 'app/utils/fooUrl'
const params = {
'relatedUsers': [4566, 7489],
'filter': {
'foo': 'bar',
}
}
const url = fooUrl('users/1234', params)
// => 'https://foo.com/users/1234?relatedUsers=4566%2C7489&filter%5Bfoo%5D=bar&apiVersion=1.0'
fetch(url)
###Example with JSON API:
In app/utils/json-api-url.js
:
import urlFactory from 'url-factory'
const jsonApiUrl = urlFactory('https://api.foo.com', { 'json_api_version': '1.0'})
export default jsonApiUrl
Then consume the util:
import fetch from 'isomorphic-fetch'
import jsonApiUrl from 'app/utils/json-api-url'
const params = {
'include': ['posts'],
'fields': {
'user': ['full_name']
}
}
const url = jsonApiUrl('users/1234', params)
// => 'https://api.foo.com/users/1234?include=posts&fields%5Buser%5D=full_name&json_api_version=1.0'
fetch(url)
If you're just interested in the params encoding you can also import that helper by itself:
import fetch from 'isomorphic-fetch'
import { encodeParams } from 'url-factory'
const params = encodeParams({'foo': true, 'bar': 'baz'})
// => 'foo=true&bar=baz'
fetch('https://myBaseUrl.com/users/1234' + '?' + params)