Skip to content

Commit

Permalink
✨ Specify multiple keys
Browse files Browse the repository at this point in the history
  • Loading branch information
BetaHuhn committed May 15, 2021
1 parent cd6c5c6 commit 02c1a76
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 11 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -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<string> | IOpts, opts: IOpts) => InputValue;
4 changes: 2 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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') {
Expand All @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions lib/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
declare type ModifierFunction = (val: InputValue) => InputValue;
export declare type InputValue = undefined | string | boolean | number | Array<string | boolean | number>;
export interface IOpts {
key?: string;
key?: string | Array<string>;
type?: string;
required?: boolean;
disableable?: boolean;
default?: InputValue;
modifier?: ModifierFunction;
}
export interface IParsedOpts {
key: string;
key: string | Array<string>;
type: string;
required: boolean;
disableable: boolean;
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> | IOpts, opts: IOpts): InputValue => {
let parsedOptions: IOpts
if (typeof key === 'string') {
if (typeof key === 'string' || Array.isArray(key)) {
parsedOptions = {
key: key,
...opts
Expand All @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ type ModifierFunction = (val: InputValue) => InputValue
export type InputValue = undefined | string | boolean | number | Array<string | boolean | number>

export interface IOpts {
key?: string
key?: string | Array<string>
type?: string
required?: boolean,
disableable?: boolean
Expand All @@ -13,7 +13,7 @@ export interface IOpts {
}

export interface IParsedOpts {
key: string
key: string | Array<string>
type: string
required: boolean,
disableable: boolean
Expand Down

0 comments on commit 02c1a76

Please sign in to comment.