Skip to content

Commit

Permalink
Merge pull request #8 from microlinkhq/120
Browse files Browse the repository at this point in the history
refactor: use 120 line length
  • Loading branch information
Kikobeats authored Jan 14, 2024
2 parents cbfdcf3 + cef7495 commit d8ab406
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 76 deletions.
2 changes: 1 addition & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
max_line_length = 80
max_line_length = 120
indent_brace_style = 1TBS
spaces_around_operators = true
quote_type = auto
Expand Down
23 changes: 5 additions & 18 deletions src/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@ export default ({ serialize, deserialize, plans, redis } = {}) => {
* @returns {Object} The created key.
*/
const create = async (opts = {}) => {
assert(
typeof opts.name === 'string' && opts.name.length > 0,
'The argument `name` is required.'
)
assert(typeof opts.name === 'string' && opts.name.length > 0, 'The argument `name` is required.')
const key = pick(opts, KEY_FIELDS.concat(KEY_FIELDS_OBJECT))
key.id = await uid({ redis, prefix: KEY_PREFIX, size: 5 })
key.createdAt = key.updatedAt = Date.now()
Expand All @@ -42,10 +39,7 @@ export default ({ serialize, deserialize, plans, redis } = {}) => {
*
* @returns {Object|null} The key object, null if it doesn't exist.
*/
const retrieve = async (
keyId,
{ throwError = false, validate = true } = {}
) => {
const retrieve = async (keyId, { throwError = false, validate = true } = {}) => {
const key = await redis.get(getKey(keyId, { validate }))
if (throwError) {
assert(key !== null, `The key \`${keyId}\` does not exist.`)
Expand All @@ -67,15 +61,10 @@ export default ({ serialize, deserialize, plans, redis } = {}) => {
throwError: true,
validate: false
})
assert(
plan === null,
`The key \`${keyId}\` is associated with the plan \`${getKey.plan}\``
)
assert(plan === null, `The key \`${keyId}\` is associated with the plan \`${getKey.plan}\``)
}
const isDeleted = (await redis.del(getKey(keyId, { verify: true }))) === 1
return (
assert(isDeleted, `The key \`${keyId}\` does not exist.`) || isDeleted
)
return assert(isDeleted, `The key \`${keyId}\` does not exist.`) || isDeleted
}

/**
Expand Down Expand Up @@ -109,9 +98,7 @@ export default ({ serialize, deserialize, plans, redis } = {}) => {
*/
const list = async () => {
const keyIds = await redis.keys(`${KEY_PREFIX}*`)
return Promise.all(
keyIds.map(keyIds => retrieve(keyIds, { validate: false }))
)
return Promise.all(keyIds.map(keyIds => retrieve(keyIds, { validate: false })))
}

const getKey = validateKey({ prefix: KEY_PREFIX })
Expand Down
30 changes: 7 additions & 23 deletions src/plans.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,12 @@ export default ({ serialize, deserialize, redis } = {}) => {
* @returns {Object|null} The plan object, null if it doesn't exist.
*/
const create = async (opts = {}) => {
assert(
typeof opts.name === 'string' && opts.name.length > 0,
'The argument `name` is required.'
)
assert(typeof opts.name === 'string' && opts.name.length > 0, 'The argument `name` is required.')
assert(
PLAN_QUOTA_PERIODS.includes(opts.quota?.period),
`The argument \`quota.period\` must be ${PLAN_QUOTA_PERIODS.map(
period => `\`${period}\``
).join(' or ')}.`
)
assert(
opts.quota.limit > 0,
'The argument `quota.limit` must be a positive number.'
`The argument \`quota.period\` must be ${PLAN_QUOTA_PERIODS.map(period => `\`${period}\``).join(' or ')}.`
)
assert(opts.quota.limit > 0, 'The argument `quota.limit` must be a positive number.')
const plan = pick(opts, PLAN_FIELDS.concat(PLAN_FIELDS_OBJECT))
plan.id = await uid({ redis, prefix: PLAN_PREFIX, size: 5 })
plan.createdAt = plan.updatedAt = Date.now()
Expand All @@ -53,10 +45,7 @@ export default ({ serialize, deserialize, redis } = {}) => {
*
* @returns {Object} The plan.
*/
const retrieve = async (
planId,
{ throwError = false, validate = true } = {}
) => {
const retrieve = async (planId, { throwError = false, validate = true } = {}) => {
const plan = await redis.get(getKey(planId, { validate }))
if (throwError) {
assert(plan !== null, `The plan \`${planId}\` does not exist.`)
Expand All @@ -73,11 +62,8 @@ export default ({ serialize, deserialize, redis } = {}) => {
* @returns {boolean} Whether the plan was deleted or not.
*/
const del = async planId => {
const isDeleted =
(await redis.del(getKey(planId, { validate: true }))) === 1
return (
assert(isDeleted, `The plan \`${planId}\` does not exist.`) || isDeleted
)
const isDeleted = (await redis.del(getKey(planId, { validate: true }))) === 1
return assert(isDeleted, `The plan \`${planId}\` does not exist.`) || isDeleted
}

/**
Expand Down Expand Up @@ -116,9 +102,7 @@ export default ({ serialize, deserialize, redis } = {}) => {
*/
const list = async () => {
const planIds = await redis.keys(`${PLAN_PREFIX}*`)
return Promise.all(
planIds.map(planId => retrieve(planId, { validate: false }))
)
return Promise.all(planIds.map(planId => retrieve(planId, { validate: false })))
}

const getKey = validateKey({ prefix: PLAN_PREFIX })
Expand Down
5 changes: 1 addition & 4 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { getRandomValues } from 'crypto'
const BASE_58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz'

const rand = size =>
getRandomValues(new Uint8Array(size)).reduce(
(id, value) => id + BASE_58.charAt(value % BASE_58.length),
''
)
getRandomValues(new Uint8Array(size)).reduce((id, value) => id + BASE_58.charAt(value % BASE_58.length), '')

export const uid = async ({ redis, prefix = '', size }) => {
let uid
Expand Down
8 changes: 2 additions & 6 deletions test/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,14 @@ test('.create # `name` is required', async t => {
})

test('.create # error if plan is invalid', async t => {
const error = await t.throwsAsync(
keys.create({ name: '[email protected]', plan: 123 })
)
const error = await t.throwsAsync(keys.create({ name: '[email protected]', plan: 123 }))

t.is(error.message, 'The id `123` must to start with `plan_`.')
t.is(error.name, 'TypeError')
})

test('.create # error if plan does not exist', async t => {
const error = await t.throwsAsync(
keys.create({ name: '[email protected]', plan: 'plan_123' })
)
const error = await t.throwsAsync(keys.create({ name: '[email protected]', plan: 'plan_123' }))

t.is(error.message, 'The plan `plan_123` does not exist.')
t.is(error.name, 'TypeError')
Expand Down
31 changes: 7 additions & 24 deletions test/plans.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,26 @@ test('.create # `name` is required', async t => {
test('.create # `quota` is required', async t => {
{
const error = await t.throwsAsync(plans.create({ name: 'free tier' }))
t.is(
error.message,
'The argument `quota.period` must be `day` or `week` or `month`.'
)
t.is(error.message, 'The argument `quota.period` must be `day` or `week` or `month`.')
t.is(error.name, 'TypeError')
}
{
const error = await t.throwsAsync(
plans.create({ name: 'free tier', quota: {} })
)
t.is(
error.message,
'The argument `quota.period` must be `day` or `week` or `month`.'
)
const error = await t.throwsAsync(plans.create({ name: 'free tier', quota: {} }))
t.is(error.message, 'The argument `quota.period` must be `day` or `week` or `month`.')
t.is(error.name, 'TypeError')
}
{
const error = await t.throwsAsync(
plans.create({ name: 'free tier', quota: { period: 'today' } })
)
t.is(
error.message,
'The argument `quota.period` must be `day` or `week` or `month`.'
)
const error = await t.throwsAsync(plans.create({ name: 'free tier', quota: { period: 'today' } }))
t.is(error.message, 'The argument `quota.period` must be `day` or `week` or `month`.')
t.is(error.name, 'TypeError')
}
{
const error = await t.throwsAsync(
plans.create({ name: 'free tier', quota: { period: 'week' } })
)
const error = await t.throwsAsync(plans.create({ name: 'free tier', quota: { period: 'week' } }))
t.is(error.message, 'The argument `quota.limit` must be a positive number.')
t.is(error.name, 'TypeError')
}
{
const error = await t.throwsAsync(
plans.create({ name: 'free tier', quota: { period: 'week', limit: 0 } })
)
const error = await t.throwsAsync(plans.create({ name: 'free tier', quota: { period: 'week', limit: 0 } }))
t.is(error.message, 'The argument `quota.limit` must be a positive number.')
t.is(error.name, 'TypeError')
}
Expand Down

0 comments on commit d8ab406

Please sign in to comment.