Skip to content

Commit

Permalink
feat: add withVault helper function
Browse files Browse the repository at this point in the history
  • Loading branch information
Seldszar committed Sep 7, 2020
1 parent 579e42f commit 0a66e9d
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 17 deletions.
30 changes: 28 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,37 @@
import Conf = require('conf');
import Conf, {Options as BaseOptions} from 'conf';

declare namespace Vault {
interface Options<T> extends Omit<Conf.Options<T>, 'configName' | 'cwd' | 'projectName' | 'projectSuffix' | 'projectVersion'> {}
/**
* The vault option.
*/
interface Options<T> extends Omit<BaseOptions<T>, 'configName' | 'cwd' | 'projectName' | 'projectSuffix' | 'projectVersion'> {}

/**
* A function returning the NodeCG instance and its vault.
*/
type Handler<T> = (nodecg: any, vault: Vault<T>) => void;
}

declare class Vault<T = any> extends Conf<T> {
/**
* Creates a new vault.
* @param nodecg the NodeCG instance
* @param options this vault options
*/
constructor(nodecg: any, options?: Vault.Options<T>);

/**
* Creates a wrapper returning the NodeCG instance and its vault.
* @param handler the handler
*/
static withVault<T = any>(handler: Vault.Handler<T>): void;

/**
* Creates a wrapper returning the NodeCG instance and its vault.
* @param options this vault options
* @param handler the handler
*/
static withVault<T = any>(options: Vault.Options<T>, handler: Vault.Handler<T>): void;
}

export = Vault;
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,12 @@ class Vault extends Conf {
}
}

Vault.withVault = (options, callback) => {
if (typeof options === 'function') {
return Vault.withVault(undefined, options);
}

return nodecg => callback(nodecg, new Vault(nodecg, options));
};

module.exports = Vault;
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@
"vault"
],
"dependencies": {
"conf": "^6.2.4"
"conf": "^7.1.2"
},
"devDependencies": {
"@commitlint/cli": "^9.0.1",
"@commitlint/config-conventional": "^9.0.1",
"ava": "^3.9.0",
"husky": "^4.2.5",
"lint-staged": "^10.2.11",
"tsd": "^0.11.0",
"xo": "^0.32.0"
"@commitlint/cli": "^9.1.2",
"@commitlint/config-conventional": "^9.1.2",
"ava": "^3.12.1",
"husky": "^4.3.0",
"lint-staged": "^10.3.0",
"tsd": "^0.13.1",
"xo": "^0.33.1"
},
"commitlint": {
"extends": [
Expand Down
38 changes: 33 additions & 5 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# nodecg-vault

> Add vaults to NodeCG for storing bundle secrets.
> Add vaults to NodeCG for storing bundle secrets
This module is built on top of `conf`, providing a way to store bundle secrets without having to worry about security concerns.

# Install

Expand All @@ -10,19 +12,45 @@ $ npm install nodecg-vault

# Usage

You can create a vault by simply constructing a new `Vault` instance.

```javascript
const Vault = require('nodecg-vault');

export = (nodecg) => {
export = nodecg => {
const vault = new Vault(nodecg);

// Writes a new secret
vault.set("mySecret", "Spark is the best!");
vault.set('mySecret', 'Spark is the best!');

// Reads the secret
console.log(vault.get("mySecret"));
console.log(vault.get('mySecret'));

// Deletes the secret
vault.delete("mySecret");
vault.delete('mySecret');
};
```

You can also use the `withVault` helper function to wrap the extension entrypoint and automatically return the associated vault in addition to the NodeCG instance.

```javascript
const {withVault} = require('nodecg-vault');

const options = {
defaults: {
mySecret: 'Spark is the best!'
}
};

export = withVault(options, (nodecg, vault) => {
console.log(vault.get('mySecret'));
});
```

## Author

Alexandre Breteau - [@0xSeldszar](https://twitter.com/0xSeldszar)

## License

MIT © [Alexandre Breteau](https://seldszar.fr)
23 changes: 21 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const test = require('ava');
const fs = require('fs');
const Vault = require('.');

const nodecg = {
const nodecgInstance = {
bundleName: 'lorem',
bundleVersion: '1.0.0'
};
Expand All @@ -12,11 +12,30 @@ test.afterEach(() => {
});

test('creates a new vault', t => {
const vault = new Vault(nodecg, {
const vault = new Vault(nodecgInstance, {
defaults: {
mySecret: 'Spark is the best!'
}
});

t.is(vault.get('mySecret'), 'Spark is the best!');
});

test('creates a vault wrapper', async t => {
const options = {
defaults: {
mySecret: 'Spark is the best!'
}
};

await new Promise(resolve => {
const wrapped = Vault.withVault(options, (nodecg, vault) => {
t.is(nodecg, nodecgInstance);
t.is(vault.constructor, Vault);

resolve();
});

wrapped(nodecgInstance);
});
});

0 comments on commit 0a66e9d

Please sign in to comment.