Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

url-factory

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

url-factory

Set a base url and encode params with minimum fuss. Especially useful with JSON API.

  • 1.4.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
10
decreased by-33.33%
Maintainers
1
Weekly downloads
 
Created
Source

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

Keywords

FAQs

Package last updated on 19 Sep 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc