Skip to content

Commit 02c1a76

Browse files
committed
✨ Specify multiple keys
1 parent cd6c5c6 commit 02c1a76

File tree

6 files changed

+32
-11
lines changed

6 files changed

+32
-11
lines changed

README.md

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Helper for parsing inputs in a GitHub Action
1616
- Throws errors if input is set to [required](#required-inputs) and is missing
1717
- Uses local environment variables (and `.env` files) during [development](#development)
1818
- Specify a [custom function](#modify-the-parsed-input) for advanced parsing
19+
- Supports [array of keys](#key)
1920

2021
## 🚀 Get started
2122

@@ -83,13 +84,19 @@ Here are all the options you can use and there default values:
8384

8485
| Name | Description | Required | Default |
8586
| ------------- | ------------- | ------------- | ------------- |
86-
| `key` | The key of the input option | **Yes** | N/A |
87+
| `key` | The key of the input option (can also be an array of keys) | **Yes** | N/A |
8788
| `type` | The type of the input value (`string`/`boolean`/`number`/`array`) | **No** | `string` |
8889
| `required` | Specify if the input is required | **No** | false |
8990
| `default` | Specify a default value for the input | **No** | N/A |
9091
| `disableable` | Specify if the input should be able to be disabled by setting it to `false` | **No** | `false` |
9192
| `modifier` | A function which gets passed the parsed value as a parameter and returns another value | **No** | N/A |
9293

94+
### Key
95+
96+
You can either specify a single key as a string, or multiple keys as an array of strings.
97+
98+
[See example](#multiple-keys)
99+
93100
### Types
94101

95102
You can specify one of the following types which will determine how the input is parsed:
@@ -246,6 +253,20 @@ const value = parser.getInput({
246253
// Value will be undefined because labels was set to false
247254
```
248255

256+
### Multiple Keys
257+
258+
Action code:
259+
260+
```js
261+
const parser = require('action-input-parser')
262+
263+
const value = parser.getInput({
264+
key: [ 'GITHUB_TOKEN', 'GH_PAT' ]
265+
})
266+
267+
// The first key takes precedence
268+
```
269+
249270
### Modify the parsed input
250271

251272
Action Workflow:

lib/index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
import { IOpts, InputValue } from './types';
2-
export declare const getInput: (key: string | IOpts, opts: IOpts) => InputValue;
2+
export declare const getInput: (key: string | Array<string> | IOpts, opts: IOpts) => InputValue;

lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ var parseValue = function (val, type) {
6262
};
6363
var getInput = function (key, opts) {
6464
var parsedOptions;
65-
if (typeof key === 'string') {
65+
if (typeof key === 'string' || Array.isArray(key)) {
6666
parsedOptions = __assign({ key: key }, opts);
6767
}
6868
else if (typeof key === 'object') {
@@ -76,7 +76,7 @@ var getInput = function (key, opts) {
7676
var options = Object.assign({}, DEFAULT_OPTIONS, parsedOptions);
7777
if (VALID_TYPES.includes(options.type) === false)
7878
throw new Error('option type has to be one of `string | array | boolean | number`');
79-
var val = getEnvVar(options.key);
79+
var val = typeof options.key === 'string' ? getEnvVar(options.key) : options.key.map(function (key) { return getEnvVar(key); }).filter(function (item) { return item; })[0];
8080
if (options.disableable && val === 'false')
8181
return undefined;
8282
var parsed = val !== undefined ? parseValue(val, options.type) : undefined;

lib/types.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
declare type ModifierFunction = (val: InputValue) => InputValue;
22
export declare type InputValue = undefined | string | boolean | number | Array<string | boolean | number>;
33
export interface IOpts {
4-
key?: string;
4+
key?: string | Array<string>;
55
type?: string;
66
required?: boolean;
77
disableable?: boolean;
88
default?: InputValue;
99
modifier?: ModifierFunction;
1010
}
1111
export interface IParsedOpts {
12-
key: string;
12+
key: string | Array<string>;
1313
type: string;
1414
required: boolean;
1515
disableable: boolean;

src/index.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ const parseValue = (val: string, type: string): InputValue => {
5959
return val.trim()
6060
}
6161

62-
export const getInput = (key: string | IOpts, opts: IOpts): InputValue => {
62+
export const getInput = (key: string | Array<string> | IOpts, opts: IOpts): InputValue => {
6363
let parsedOptions: IOpts
64-
if (typeof key === 'string') {
64+
if (typeof key === 'string' || Array.isArray(key)) {
6565
parsedOptions = {
6666
key: key,
6767
...opts
@@ -78,7 +78,7 @@ export const getInput = (key: string | IOpts, opts: IOpts): InputValue => {
7878

7979
if (VALID_TYPES.includes(options.type) === false) throw new Error('option type has to be one of `string | array | boolean | number`')
8080

81-
const val = getEnvVar(options.key)
81+
const val = typeof options.key === 'string' ? getEnvVar(options.key) : options.key.map((key) => getEnvVar(key)).filter((item) => item)[0]
8282

8383
if (options.disableable && val === 'false') return undefined
8484

src/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ type ModifierFunction = (val: InputValue) => InputValue
44
export type InputValue = undefined | string | boolean | number | Array<string | boolean | number>
55

66
export interface IOpts {
7-
key?: string
7+
key?: string | Array<string>
88
type?: string
99
required?: boolean,
1010
disableable?: boolean
@@ -13,7 +13,7 @@ export interface IOpts {
1313
}
1414

1515
export interface IParsedOpts {
16-
key: string
16+
key: string | Array<string>
1717
type: string
1818
required: boolean,
1919
disableable: boolean

0 commit comments

Comments
 (0)