3.0.0 • Published 7 years ago

url-factory v3.0.0

Weekly downloads
141
License
MIT
Repository
github
Last release
7 years ago

###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)

extra: encodeParams:

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)

Missing something? Let us know!

File an issue

3.0.0

7 years ago

2.0.0

7 years ago

1.4.3

8 years ago

1.4.2

8 years ago

1.4.1

8 years ago

1.4.0

8 years ago

1.3.0

8 years ago

1.2.0

8 years ago

1.1.0

8 years ago

1.0.4

9 years ago

1.0.3

9 years ago

1.0.2

9 years ago

1.0.1

9 years ago

1.0.0

9 years ago