-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: remove metadata fields on update (#24)
- Loading branch information
Showing
8 changed files
with
94 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
const { errors } = require('./error') | ||
|
||
module.exports = (condition, code, args = () => []) => { | ||
return ( | ||
condition || | ||
(() => { | ||
throw errors[code](args) | ||
})() | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict' | ||
|
||
const { isPlainObject } = require('./util') | ||
const assert = require('./assert') | ||
|
||
const assertMetadata = metadata => { | ||
if (metadata) { | ||
assert(isPlainObject(metadata), 'ERR_METADATA_NOT_FLAT_OBJECT') | ||
Object.keys(metadata).forEach(key => { | ||
assert(!isPlainObject(metadata[key]), 'ERR_METADATA_INVALID', () => [key]) | ||
if (metadata[key] === undefined) delete metadata[key] | ||
}) | ||
|
||
return Object.keys(metadata).length ? metadata : false | ||
} | ||
} | ||
|
||
const clean = metadata => { | ||
for (const [key, value] of Object.entries(metadata)) { | ||
if ([null, ''].includes(value)) delete metadata[key] | ||
} | ||
return metadata | ||
} | ||
|
||
const merge = (metadata, newMetadata) => { | ||
if (newMetadata === null) return null | ||
const mergedMetadata = Object.assign({}, metadata, assertMetadata(newMetadata)) | ||
return clean(mergedMetadata) | ||
} | ||
|
||
const mutate = (obj, opts) => { | ||
const metadata = merge(obj.metadata, opts.metadata) | ||
if (metadata === null || Object.keys(metadata).length === 0) delete obj.metadata | ||
else obj.metadata = metadata | ||
return obj | ||
} | ||
|
||
module.exports = mutate |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -109,7 +109,7 @@ test('.update # error if plan does not exist', async t => { | |
t.is(error.code, 'ERR_PLAN_NOT_EXIST') | ||
}) | ||
|
||
test('.update # add a plan', async t => { | ||
test('.update # plan', async t => { | ||
const plan = await openkey.plans.create({ | ||
id: randomUUID(), | ||
limit: 3, | ||
|
@@ -120,7 +120,7 @@ test('.update # add a plan', async t => { | |
t.is(key.plan, plan.id) | ||
}) | ||
|
||
test('.update # add metadata', async t => { | ||
test('.update # metadata', async t => { | ||
{ | ||
const { value } = await openkey.keys.create() | ||
const key = await openkey.keys.update(value, { metadata: { cc: '[email protected]' } }) | ||
|
@@ -134,6 +134,19 @@ test('.update # add metadata', async t => { | |
t.is(key.metadata.cc, '[email protected]') | ||
t.is(key.metadata.version, 2) | ||
} | ||
{ | ||
const { value } = await openkey.keys.create() | ||
const metadata = { tier: 'free', null: 'null', undefined: 'undefined', empty: '' } | ||
let key = await openkey.keys.update(value, { metadata }) | ||
t.is(key.metadata.null, 'null') | ||
t.is(key.metadata.undefined, 'undefined') | ||
t.is(key.metadata.empty, undefined) | ||
key = await openkey.keys.update(value, { metadata: { ...metadata, null: null, undefined: '' } }) | ||
t.is(key.metadata.null, undefined) | ||
t.is(key.metadata.undefined, undefined) | ||
key = await openkey.keys.update(value, { metadata: null }) | ||
t.is(key.metadata, undefined) | ||
} | ||
}) | ||
|
||
test('.update # error is metadata is not a flat object', async t => { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters