diff --git a/README.md b/README.md index 892395d..fb1a735 100644 --- a/README.md +++ b/README.md @@ -70,8 +70,8 @@ const plan = await openkey.plans.create({ name: 'free tier', description: 'this is optional', metadata: { tier: 'free' }, - quota: { limit: 3000, period: 'day' }, - throttle: { burstLimit: 1000, rateLimit: 10 } + limit: 3000, + period: '1d' }) ``` @@ -80,8 +80,6 @@ The **options** accepted are: - `id`string: The id of the plan, it cannot contain whitespaces. - `period`string: The time window which the limit applies. It accepts [ms](https://www.npmjs.com/package/ms) syntax. - `limit`number: The target maximum number of requests that can be made in a given time period. -- `burst`number: The number of concurrent requests. -- `rate`number: The rate, in requests per second. - `metadata`object: A flat object containing additional information. Any other field provided will be omitted. diff --git a/src/error.js b/src/error.js index e51645a..2a74dd0 100644 --- a/src/error.js +++ b/src/error.js @@ -42,7 +42,7 @@ const assertMetadata = metadata => { assert(!isPlainObject(metadata[key]), 'ERR_METADATA_INVALID', () => [key]) if (metadata[key] === undefined) delete metadata[key] }) - return Object.keys(metadata).length ? metadata : undefined + return Object.keys(metadata).length ? metadata : false } } diff --git a/src/plans.js b/src/plans.js index 61242f0..acd1d85 100644 --- a/src/plans.js +++ b/src/plans.js @@ -1,6 +1,5 @@ 'use strict' -const { pick } = require('./util') const { assert, assertMetadata } = require('./error') module.exports = ({ serialize, deserialize, redis, keys, prefix } = {}) => { @@ -10,9 +9,7 @@ module.exports = ({ serialize, deserialize, redis, keys, prefix } = {}) => { * @param {Object} options - The options for creating a plan. * @param {string} options.id - The id of the plan. * @param {number} [options.limit] - The target maximum number of requests that can be made in a given time period. - * @param {string} [options.period] - The time period in which the limit applies. Valid values are "DAY", "WEEK" or "MONTH". - * @param {number} [options.burst] - The burst limit of the plan. - * @param {number} [options.rate] - The rate limit of the plan. + * @param {string} [options.period] - The time period in which the limit applies. * @param {Object} [options.metadata] - Any extra information can be attached here. * * @returns {Object} The plan object. @@ -20,16 +17,13 @@ module.exports = ({ serialize, deserialize, redis, keys, prefix } = {}) => { const create = async (opts = {}) => { assert(typeof opts.id === 'string' && opts.id.length > 0, 'ERR_PLAN_ID_REQUIRED') assert(!/\s/.test(opts.id), 'ERR_PLAN_INVALID_ID') - const plan = Object.assign( - { - limit: assert(typeof opts.limit === 'number' && opts.limit > 0 && opts.limit, 'ERR_PLAN_INVALID_LIMIT'), - period: assert( - typeof opts.period === 'string' && opts.period.length > 0 && opts.period, - 'ERR_PLAN_INVALID_PERIOD' - ) - }, - pick(opts, ['rate', 'burst']) - ) + const plan = { + limit: assert(typeof opts.limit === 'number' && opts.limit > 0 && opts.limit, 'ERR_PLAN_INVALID_LIMIT'), + period: assert( + typeof opts.period === 'string' && opts.period.length > 0 && opts.period, + 'ERR_PLAN_INVALID_PERIOD' + ) + } const metadata = assertMetadata(opts.metadata) if (metadata) plan.metadata = metadata plan.createdAt = plan.updatedAt = Date.now() @@ -79,23 +73,13 @@ module.exports = ({ serialize, deserialize, redis, keys, prefix } = {}) => { * @param {Object} options - The options for updating a plan. * @param {number} [options.limit] - The target maximum number of requests that can be made in a given time period. * @param {string} [options.period] - The time period in which the limit applies. Valid values are "DAY", "WEEK" or "MONTH". - * @param {number} [options.burst] - The burst limit of the plan. - * @param {number} [options.rate] - The rate limit of the plan. * @param {object} [options.metadata] - Any extra information can be attached here. * * @returns {Object} The updated plan. */ const update = async (id, opts) => { - const currentPlan = await retrieve(id, { throwError: true }) - const metadata = Object.assign({}, currentPlan.metadata, assertMetadata(opts.metadata)) - - const plan = Object.assign( - currentPlan, - { - updatedAt: Date.now() - }, - pick(opts, ['rate', 'burst']) - ) + const plan = await retrieve(id, { throwError: true }) + const metadata = Object.assign({}, plan.metadata, assertMetadata(opts.metadata)) if (opts.limit) { plan.limit = assert(typeof opts.limit === 'number' && opts.limit > 0 && opts.limit, 'ERR_PLAN_INVALID_LIMIT') @@ -108,6 +92,8 @@ module.exports = ({ serialize, deserialize, redis, keys, prefix } = {}) => { ) } + plan.updatedAt = Date.now() + if (Object.keys(metadata).length) plan.metadata = metadata return (await redis.set(prefixKey(id), await serialize(plan))) && plan } diff --git a/test/index.js b/test/index.js index 61f973e..c132f6d 100644 --- a/test/index.js +++ b/test/index.js @@ -32,9 +32,7 @@ test('compression support', async t => { id, limit: 1, period: '1s', - metadata: { tier: 'free' }, - burst: 1000, - rate: 10 + metadata: { tier: 'free' } } await openkey.plans.create(props) diff --git a/test/plans.js b/test/plans.js index 3256b1f..fe9fdf9 100644 --- a/test/plans.js +++ b/test/plans.js @@ -145,17 +145,13 @@ test('.create', async t => { id, limit: 1, period: '1s', - metadata: { tier: 'free' }, - burst: 1000, - rate: 10 + metadata: { tier: 'free' } }) t.truthy(plan.createdAt) t.is(plan.createdAt, plan.updatedAt) t.deepEqual(plan.metadata, { tier: 'free' }) t.is(plan.period, '1s') - t.is(plan.rate, 10) - t.is(plan.burst, 1000) t.is(plan.limit, 1) t.is(plan.id, id) })