pco-url

A dumb url origin generator in JS

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
pco-url
2.0.16 years ago7 years agoMinified + gzip package size for pco-url in KB

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/pco-url@1.0.0/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")