pco-url
v2.0.1
Published
A dumb url origin generator in JS
Downloads
2,302
Readme
DEPRECATED:
USE ORG-SCOPED @planningcenter/url
pco-url
A dumb url origin generator in JS.
Builds environment-considered url origins in JS-land, for Planning Center apps.
Examples
pcoUrl("development")("api")
// => "http://api.pco.test"
pcoUrl("staging")("people")
// => "https://accounts.planningcenteronline.com"
pcoUrl("production")("accounts")
// => "https://people-staging.planningcenteronline.com"
Arguments are curried. You can make a generic env-considered function.
const envPcoUrl = pcoUrl(window.railsEnv)
envPcoUrl("api")
In Planning Center Apps
Planning center apps expose the Rails env as the JS global railsEnv
. Use like so.
pcoUrl(railsEnv)("api")
Fetching example
fetch(`${pcoEnvUrl(env)("api")}/people/v2/me`, {
credentials: "include",
})
.then(res => res.json())
.then(json => json.data)
.then(({ id, attributes }) =>
this.setState({
currentUser: {
id,
...attributes,
},
})
)
.catch(err => console.log(err));
Installation
Script tag on Rails
<script type="javascript" src="https://unpkg.com/[email protected]/lib/pco-url.js"></script>
<!-- exposed as global `pcoUrl` -->
Webpacker on Rails
yarn add pco-url
/* global railsEnv */
import pcoUrl from "pco-url"
pcoUrl(railsEnv)("api")