url-factory
Advanced tools
Comparing version 1.1.0 to 1.2.0
'use strict'; | ||
exports.__esModule = true; | ||
exports.createUrlBuilder = exports.encodeParams = undefined; | ||
exports.encodeParams = undefined; | ||
@@ -37,3 +37,3 @@ var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; | ||
var createUrlBuilder = exports.createUrlBuilder = function createUrlBuilder() { | ||
exports.default = function () { | ||
var baseUrl = arguments.length <= 0 || arguments[0] === undefined ? '' : arguments[0]; | ||
@@ -40,0 +40,0 @@ var baseParams = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; |
@@ -7,3 +7,3 @@ { | ||
], | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"description": "Set a base url and encode params with minimum fuss. Especially useful with JSON API.", | ||
@@ -10,0 +10,0 @@ "main": "lib/index.js", |
@@ -1,7 +0,72 @@ | ||
Boilerplate for developing small npm modules with ES2015. Borrows heavily from the folks at rackt. | ||
###Getting started: | ||
1. Develop in ES6. | ||
Use `createUrlBuilder` to set your base url and default params you want to include with each request. | ||
2. `npm version [patch|minor|major]` | ||
For example, at foo company, make a new file: `app/utils/fooUrl.js` | ||
``` | ||
import urlFactory from 'url-factory' | ||
3. `npm publish` | ||
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?related=[4566,7489]&filter[foo]=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[user]=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](https://github.com/Patreon/url-factory/issues) |
6804
73