Skip to content
Permalink
Browse files

feat: requireEnvKeys

  • Loading branch information...
kirillgroshkov committed May 10, 2019
1 parent 0d99abe commit 814bc9996f779b79af4b55c1e4734f2a01df9463
Showing with 851 additions and 858 deletions.
  1. +7 −8 package.json
  2. +2 −0 src/index.ts
  3. +19 −0 src/util/env.util.test.ts
  4. +28 −0 src/util/env.util.ts
  5. +795 −850 yarn.lock
@@ -3,20 +3,19 @@
"version": "5.0.2",
"dependencies": {
"@types/hapi__joi": "^15.0.1",
"luxon": "^1.4.3"
"luxon": "^1.13.2"
},
"peerDependencies": {
"@hapi/joi": "^15.0.0",
"@naturalcycles/js-lib": ">=4"
"@naturalcycles/js-lib": ">=5"
},
"devDependencies": {
"@hapi/joi": "^15.0.1",
"@naturalcycles/dev-lib": "^7.0.0",
"@naturalcycles/js-lib": "^4.0.0",
"@hapi/joi": "^15.0.2",
"@naturalcycles/dev-lib": "^7.4.0",
"@naturalcycles/js-lib": "^5.0.1",
"@naturalcycles/semantic-release": "^1.0.0",
"@types/node": "^11.13.4",
"jest": "^24.1.0",
"typescript": "^3.0.0"
"@types/node": "^12.0.0",
"jest": "^24.8.0"
},
"files": [
"dist",
@@ -1,4 +1,5 @@
import { processSharedUtil } from './infra/process.shared.util'
import { requireEnvKeys } from './util/env.util'
import { unzipBuffer, unzipToString, zipBuffer, zipString } from './util/zip.util'
import { ExtendedJoi, Joi } from './validation/joi/joi.extensions'
import {
@@ -64,4 +65,5 @@ export {
unzipBuffer,
zipString,
unzipToString,
requireEnvKeys,
}
@@ -0,0 +1,19 @@
import { requireEnvKeys } from '../index'

test('requireEnvKeys', () => {
expect(() => requireEnvKeys('NON_EXISTING')).toThrow()

process.env.AAAA = 'aaaa'
expect(requireEnvKeys('AAAA')).toEqual({
AAAA: 'aaaa',
})

process.env.BBBB = '' // not allowed
expect(() => requireEnvKeys('BBBB')).toThrow()

process.env.CCCC = 'cccc'
expect(requireEnvKeys('AAAA', 'CCCC')).toEqual({
AAAA: 'aaaa',
CCCC: 'cccc',
})
})
@@ -0,0 +1,28 @@
import { ValuesOf } from '@naturalcycles/js-lib'

/*
type ObjectWithKeysOf<T extends readonly string[]> = {
[k in ValuesOf<T>]: string
}
*/

/**
* @example
*
* const {a, b} = requreEnvKeys(['a', 'b'])
*
* Will throw if any of the passed keys is not defined.
*/
export function requireEnvKeys<T extends readonly string[]> (
...keys: T
): { [k in ValuesOf<T>]: string } {
return keys.reduce(
(r, k) => {
const v = process.env[k]
if (!v) throw new Error(`${k} env variable is required, but missing`)
r[k] = v
return r
},
{} as ReturnType<typeof requireEnvKeys>,
)
}

0 comments on commit 814bc99

Please sign in to comment.
You can’t perform that action at this time.