diff --git a/README.md b/README.md index fb9a485..92a422d 100644 --- a/README.md +++ b/README.md @@ -16,6 +16,7 @@ Helper for parsing inputs in a GitHub Action - Throws errors if input is set to [required](#required-inputs) and is missing - Uses local environment variables (and `.env` files) during [development](#development) - Specify a [custom function](#modify-the-parsed-input) for advanced parsing +- Supports [array of keys](#key) ## 🚀 Get started @@ -83,13 +84,19 @@ Here are all the options you can use and there default values: | Name | Description | Required | Default | | ------------- | ------------- | ------------- | ------------- | -| `key` | The key of the input option | **Yes** | N/A | +| `key` | The key of the input option (can also be an array of keys) | **Yes** | N/A | | `type` | The type of the input value (`string`/`boolean`/`number`/`array`) | **No** | `string` | | `required` | Specify if the input is required | **No** | false | | `default` | Specify a default value for the input | **No** | N/A | | `disableable` | Specify if the input should be able to be disabled by setting it to `false` | **No** | `false` | | `modifier` | A function which gets passed the parsed value as a parameter and returns another value | **No** | N/A | +### Key + +You can either specify a single key as a string, or multiple keys as an array of strings. + +[See example](#multiple-keys) + ### Types You can specify one of the following types which will determine how the input is parsed: @@ -246,6 +253,20 @@ const value = parser.getInput({ // Value will be undefined because labels was set to false ``` +### Multiple Keys + +Action code: + +```js +const parser = require('action-input-parser') + +const value = parser.getInput({ + key: [ 'GITHUB_TOKEN', 'GH_PAT' ] +}) + +// The first key takes precedence +``` + ### Modify the parsed input Action Workflow: diff --git a/lib/index.d.ts b/lib/index.d.ts index a39adef..0ab348e 100644 --- a/lib/index.d.ts +++ b/lib/index.d.ts @@ -1,2 +1,2 @@ import { IOpts, InputValue } from './types'; -export declare const getInput: (key: string | IOpts, opts: IOpts) => InputValue; +export declare const getInput: (key: string | Array | IOpts, opts: IOpts) => InputValue; diff --git a/lib/index.js b/lib/index.js index 925f899..19fcecb 100644 --- a/lib/index.js +++ b/lib/index.js @@ -62,7 +62,7 @@ var parseValue = function (val, type) { }; var getInput = function (key, opts) { var parsedOptions; - if (typeof key === 'string') { + if (typeof key === 'string' || Array.isArray(key)) { parsedOptions = __assign({ key: key }, opts); } else if (typeof key === 'object') { @@ -76,7 +76,7 @@ var getInput = function (key, opts) { var options = Object.assign({}, DEFAULT_OPTIONS, parsedOptions); if (VALID_TYPES.includes(options.type) === false) throw new Error('option type has to be one of `string | array | boolean | number`'); - var val = getEnvVar(options.key); + var val = typeof options.key === 'string' ? getEnvVar(options.key) : options.key.map(function (key) { return getEnvVar(key); }).filter(function (item) { return item; })[0]; if (options.disableable && val === 'false') return undefined; var parsed = val !== undefined ? parseValue(val, options.type) : undefined; diff --git a/lib/types.d.ts b/lib/types.d.ts index 073c190..7ff7f2c 100644 --- a/lib/types.d.ts +++ b/lib/types.d.ts @@ -1,7 +1,7 @@ declare type ModifierFunction = (val: InputValue) => InputValue; export declare type InputValue = undefined | string | boolean | number | Array; export interface IOpts { - key?: string; + key?: string | Array; type?: string; required?: boolean; disableable?: boolean; @@ -9,7 +9,7 @@ export interface IOpts { modifier?: ModifierFunction; } export interface IParsedOpts { - key: string; + key: string | Array; type: string; required: boolean; disableable: boolean; diff --git a/src/index.ts b/src/index.ts index 35f59b1..109abb4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -59,9 +59,9 @@ const parseValue = (val: string, type: string): InputValue => { return val.trim() } -export const getInput = (key: string | IOpts, opts: IOpts): InputValue => { +export const getInput = (key: string | Array | IOpts, opts: IOpts): InputValue => { let parsedOptions: IOpts - if (typeof key === 'string') { + if (typeof key === 'string' || Array.isArray(key)) { parsedOptions = { key: key, ...opts @@ -78,7 +78,7 @@ export const getInput = (key: string | IOpts, opts: IOpts): InputValue => { if (VALID_TYPES.includes(options.type) === false) throw new Error('option type has to be one of `string | array | boolean | number`') - const val = getEnvVar(options.key) + const val = typeof options.key === 'string' ? getEnvVar(options.key) : options.key.map((key) => getEnvVar(key)).filter((item) => item)[0] if (options.disableable && val === 'false') return undefined diff --git a/src/types.ts b/src/types.ts index 18223a4..f5529a6 100644 --- a/src/types.ts +++ b/src/types.ts @@ -4,7 +4,7 @@ type ModifierFunction = (val: InputValue) => InputValue export type InputValue = undefined | string | boolean | number | Array export interface IOpts { - key?: string + key?: string | Array type?: string required?: boolean, disableable?: boolean @@ -13,7 +13,7 @@ export interface IOpts { } export interface IParsedOpts { - key: string + key: string | Array type: string required: boolean, disableable: boolean