Skip to content

Commit

Permalink
Add options builder
Browse files Browse the repository at this point in the history
  • Loading branch information
baranga committed Jun 4, 2019
1 parent c1e2d15 commit f4758d8
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
28 changes: 28 additions & 0 deletions src/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @typedef MongoTenantOptions
* @property {string} tenantIdGetter
* @property {string} accessorMethod
* @property {*} tenantIdType
* @property {string} tenantIdKey
* @property {boolean} requireTenantId
*/

/**
* Sanitize plugin options
* @param {object} [input]
* @param {string} [input.tenantIdGetter=tenantId]
* @param {string} [input.accessorMethod=String]
* @param {*} [input.tenantIdType=byTenant]
* @param {string} [input.tenantIdKey=getTenantId]
* @param {string} [input.requireTenantId=true]
* @returns {MongoTenantOptions}
*/
const options = (input = {}) => ({
tenantIdKey: input.tenantIdKey || 'tenantId',
tenantIdType: input.tenantIdType || String,
accessorMethod: input.accessorMethod || 'byTenant',
tenantIdGetter: input.tenantIdGetter || 'getTenantId',
requireTenantId: input.requireTenantId === true,
});

module.exports = options;
21 changes: 21 additions & 0 deletions src/options.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const options = require('./options');

describe('options', () => {
const assertCompleteOptions = (value) => {
expect(value).toHaveProperty('tenantIdKey');
expect(value).toHaveProperty('tenantIdType');
expect(value).toHaveProperty('accessorMethod');
expect(value).toHaveProperty('tenantIdGetter');
expect(value).toHaveProperty('requireTenantId');
};

it('creates default options', () => {
const result = options();
assertCompleteOptions(result);
});

it('adds default option values', () => {
const result = options({tenantIdKey: 'tenant_id'});
assertCompleteOptions(result);
});
});

0 comments on commit f4758d8

Please sign in to comment.