Skip to content

Commit

Permalink
chore: remove burst & rate (#23)
Browse files Browse the repository at this point in the history
* chore: remove burst & rate

* perf: return false instead undefined

this avoid casting undefined into a boolean
  • Loading branch information
Kikobeats authored May 7, 2024
1 parent ba14b70 commit eeb61a1
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 39 deletions.
6 changes: 2 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
})
```

Expand All @@ -80,8 +80,6 @@ The **options** accepted are:
- `id`<span class="type">string</span>: The id of the plan, it cannot contain whitespaces.
- `period`<span class="type">string</span>: The time window which the limit applies. It accepts [ms](https://www.npmjs.com/package/ms) syntax.
- `limit`<span class="type">number</span>: The target maximum number of requests that can be made in a given time period.
- `burst`<span class="type">number</span>: The number of concurrent requests.
- `rate`<span class="type">number</span>: The rate, in requests per second.
- `metadata`<span class="type">object</span>: A flat object containing additional information.

Any other field provided will be omitted.
Expand Down
2 changes: 1 addition & 1 deletion src/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down
38 changes: 12 additions & 26 deletions src/plans.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict'

const { pick } = require('./util')
const { assert, assertMetadata } = require('./error')

module.exports = ({ serialize, deserialize, redis, keys, prefix } = {}) => {
Expand All @@ -10,26 +9,21 @@ 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.
*/
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()
Expand Down Expand Up @@ -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')
Expand All @@ -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
}
Expand Down
4 changes: 1 addition & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions test/plans.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
Expand Down

0 comments on commit eeb61a1

Please sign in to comment.