https://github.com/jimthedev/url-env
Select an environment by url
https://github.com/jimthedev/url-env
environment nodejs url
Last synced: 2 months ago
JSON representation
Select an environment by url
- Host: GitHub
- URL: https://github.com/jimthedev/url-env
- Owner: jimthedev
- Created: 2020-05-05T02:38:04.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-10-12T18:25:26.000Z (over 3 years ago)
- Last Synced: 2025-03-08T04:19:15.335Z (2 months ago)
- Topics: environment, nodejs, url
- Language: JavaScript
- Homepage:
- Size: 2.93 KB
- Stars: 1
- Watchers: 2
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# url-env
> Select an environment by url
## Install
```bash
npm install url-env
```## Usage
Make sure you include polyfills for Object.prototype.entries and String.prototype.startsWith if you need this to work with older browsers. We recommend https://polyfill.io but if you are using a framework then it may already have this covered for you.
[Codesandbox example](https://codesandbox.io/s/stoic-chandrasekhar-popoh?file=/src/App.js)
```js
const { fromObject } = require('url-env');const availableEvironments = {
'https://localhost:9300': 'local',
'https://dev.mydomain.com': 'dev',
'https://test.mydomain.com': 'test',
'https://mydomain.com': 'prod'
};const {pair, url, env} = fromObject(availableEvironments);
// When window.location.href starts with https://dev.mydomain.com
console.log(pair); // ['https://dev.mydomain.com', 'dev']
console.log(env); // https://dev.mydomain.com
console.log(url); // dev```
Using strings other than window.location.href (useful when mocking)
```js
const {pair, url, env} = urlEnv.fromObject(availableEvironments, 'http://mymockurl');
```