From 9a71aaed1a1df35895ed24b225cb008e15da4bdc Mon Sep 17 00:00:00 2001 From: Stanley Chin Date: Fri, 23 Aug 2024 14:12:55 -0400 Subject: [PATCH 01/11] Add scopes as an option for zcache usage --- packages/core/src/tools/create-cache.js | 29 ++++++++--- packages/core/test/tools/create-cache.js | 63 +++++++++++++++++++----- 2 files changed, 74 insertions(+), 18 deletions(-) diff --git a/packages/core/src/tools/create-cache.js b/packages/core/src/tools/create-cache.js index b6ab6e01e..9af58fd24 100644 --- a/packages/core/src/tools/create-cache.js +++ b/packages/core/src/tools/create-cache.js @@ -7,7 +7,13 @@ const ensureJSONEncodable = require('./ensure-json-encodable'); const createCache = (input) => { const rpc = _.get(input, '_zapier.rpc'); - const runValidationChecks = (rpc, key, value = null, ttl = null) => { + const runValidationChecks = ( + rpc, + key, + value = null, + ttl = null, + scopes = null + ) => { if (!rpc) { throw new Error('rpc is not available'); } @@ -20,23 +26,32 @@ const createCache = (input) => { throw new TypeError('ttl must be an integer'); } + if ( + scopes != null && + !scopes.every((scope) => scope === 'user' || scope === 'auth') + ) { + throw new TypeError( + 'scopes must be an array of strings with values "user" or "auth"' + ); + } + ensureJSONEncodable(value); }; return { - get: async (key) => { + get: async (key, scopes = null) => { runValidationChecks(rpc, key); const result = await rpc('zcache_get', key); return result ? JSON.parse(result) : null; }, - set: async (key, value, ttl = null) => { - runValidationChecks(rpc, key, value, ttl); + set: async (key, value, ttl = null, scopes = null) => { + runValidationChecks(rpc, key, value, ttl, scopes); - return await rpc('zcache_set', key, JSON.stringify(value), ttl); + return await rpc('zcache_set', key, JSON.stringify(value), ttl, scopes); }, - delete: async (key) => { - runValidationChecks(rpc, key); + delete: async (key, scopes) => { + runValidationChecks(rpc, key, scopes); return await rpc('zcache_delete', key); }, diff --git a/packages/core/test/tools/create-cache.js b/packages/core/test/tools/create-cache.js index 25ac09234..b744f0b0d 100644 --- a/packages/core/test/tools/create-cache.js +++ b/packages/core/test/tools/create-cache.js @@ -10,7 +10,7 @@ describe('zcache: get, set, delete', () => { const cache = createCache({ _zapier: { rpc } }); it('zcache_get: should return the cache entry of an existing key', async () => { - const value = {entity:'Zapier', colors: ['Orange', 'black']}; + const value = { entity: 'Zapier', colors: ['Orange', 'black'] }; mockRpcCall(JSON.stringify(value)); const result = await cache.get('existing-key'); @@ -27,11 +27,10 @@ describe('zcache: get, set, delete', () => { it('zcache_get: should throw error for non-string keys', async () => { await cache.get(12345).should.be.rejectedWith('key must be a string'); }); - - it('zcache_set: should set a cache entry based on the app\'s rate-limit', async () => { - const key1 = 'random-key1' - const key2 = 'random-key2' - const value = {entity:'Zapier', colors: ['Orange', 'black']}; + it("zcache_set: should set a cache entry based on the app's rate-limit", async () => { + const key1 = 'random-key1'; + const key2 = 'random-key2'; + const value = { entity: 'Zapier', colors: ['Orange', 'black'] }; const valueLength = JSON.stringify(value).length; // in bytes/minute @@ -53,14 +52,24 @@ describe('zcache: get, set, delete', () => { it('zcache_set: should throw error for values that are not JSON-encodable', async () => { const key = 'random-key'; let value = console; - await cache.set(key, value).should.be.rejectedWith("Type 'object' is not JSON-encodable (path: '')"); - - value = () => { 'this is a function' }; - await cache.set(key, value).should.be.rejectedWith("Type 'function' is not JSON-encodable (path: '')"); + await cache + .set(key, value) + .should.be.rejectedWith("Type 'object' is not JSON-encodable (path: '')"); + + value = () => { + 'this is a function'; + }; + await cache + .set(key, value) + .should.be.rejectedWith( + "Type 'function' is not JSON-encodable (path: '')" + ); }); it('zcache_set: should throw error for a non-integer ttl', async () => { - await cache.set('random-key', 'random-value', 'twenty').should.be.rejectedWith('ttl must be an integer'); + await cache + .set('random-key', 'random-value', 'twenty') + .should.be.rejectedWith('ttl must be an integer'); }); it('zcache_delete: should delete the cache entry of an existing key', async () => { @@ -76,4 +85,36 @@ describe('zcache: get, set, delete', () => { const result = await cache.delete('non-existing-key'); should(result).eql(false); }); + + describe('scopes', () => { + beforeEach(() => { + mockRpcCall('ok'); + }); + it('zcache_set: no scopes is ok', async () => { + const res = await cache.set('key', 'ok'); + should(res).eql('ok'); + }); + it('zcache_set: empty array scopes is ok', async () => { + const res = await cache.set('key', 'ok', 1, []); + should(res).eql('ok'); + }); + it('zcache_set: user and auth scopes is ok', async () => { + const res = await cache.set('key', 'ok', 1, ['user', 'auth']); + should(res).eql('ok'); + }); + it('zcache_set: bad scopes is not ok', async () => { + await cache + .set('key', 'ok', 1, ['bad', 'scope']) + .should.be.rejectedWith( + 'scopes must be an array of strings with values "user" or "auth"' + ); + }); + it('zcache_set: mix of good and bad is not ok', async () => { + await cache + .set('key', 'ok', 1, ['bad', 'auth']) + .should.be.rejectedWith( + 'scopes must be an array of strings with values "user" or "auth"' + ); + }); + }); }); From c1a82df92a8b6d7c0a829b3ca57df501041837df Mon Sep 17 00:00:00 2001 From: Stanley Chin Date: Fri, 23 Aug 2024 14:30:30 -0400 Subject: [PATCH 02/11] Line --- packages/core/test/tools/create-cache.js | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/core/test/tools/create-cache.js b/packages/core/test/tools/create-cache.js index b744f0b0d..411e7f7f6 100644 --- a/packages/core/test/tools/create-cache.js +++ b/packages/core/test/tools/create-cache.js @@ -27,6 +27,7 @@ describe('zcache: get, set, delete', () => { it('zcache_get: should throw error for non-string keys', async () => { await cache.get(12345).should.be.rejectedWith('key must be a string'); }); + it("zcache_set: should set a cache entry based on the app's rate-limit", async () => { const key1 = 'random-key1'; const key2 = 'random-key2'; From ee296ce5db08f98ccba010af2a6517a12caa13e1 Mon Sep 17 00:00:00 2001 From: Stanley Chin Date: Fri, 23 Aug 2024 14:35:32 -0400 Subject: [PATCH 03/11] mock rpc individual test --- packages/core/test/tools/create-cache.js | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/core/test/tools/create-cache.js b/packages/core/test/tools/create-cache.js index 411e7f7f6..3d5cf3d93 100644 --- a/packages/core/test/tools/create-cache.js +++ b/packages/core/test/tools/create-cache.js @@ -88,22 +88,26 @@ describe('zcache: get, set, delete', () => { }); describe('scopes', () => { - beforeEach(() => { - mockRpcCall('ok'); - }); it('zcache_set: no scopes is ok', async () => { + mockRpcCall('ok'); const res = await cache.set('key', 'ok'); should(res).eql('ok'); }); it('zcache_set: empty array scopes is ok', async () => { + mockRpcCall('ok'); + const res = await cache.set('key', 'ok', 1, []); should(res).eql('ok'); }); it('zcache_set: user and auth scopes is ok', async () => { + mockRpcCall('ok'); + const res = await cache.set('key', 'ok', 1, ['user', 'auth']); should(res).eql('ok'); }); it('zcache_set: bad scopes is not ok', async () => { + mockRpcCall('ok'); + await cache .set('key', 'ok', 1, ['bad', 'scope']) .should.be.rejectedWith( @@ -111,6 +115,8 @@ describe('zcache: get, set, delete', () => { ); }); it('zcache_set: mix of good and bad is not ok', async () => { + mockRpcCall('ok'); + await cache .set('key', 'ok', 1, ['bad', 'auth']) .should.be.rejectedWith( From 6eed0effc47567214f8969ad6596a60c4788423f Mon Sep 17 00:00:00 2001 From: Stanley Chin Date: Mon, 26 Aug 2024 15:18:41 -0400 Subject: [PATCH 04/11] scopes -> scope --- packages/core/src/tools/create-cache.js | 27 +++++++++++------------- packages/core/test/tools/create-cache.js | 14 ++++++------ 2 files changed, 19 insertions(+), 22 deletions(-) diff --git a/packages/core/src/tools/create-cache.js b/packages/core/src/tools/create-cache.js index 9af58fd24..66e7ebe24 100644 --- a/packages/core/src/tools/create-cache.js +++ b/packages/core/src/tools/create-cache.js @@ -12,7 +12,7 @@ const createCache = (input) => { key, value = null, ttl = null, - scopes = null + scope = null ) => { if (!rpc) { throw new Error('rpc is not available'); @@ -26,12 +26,9 @@ const createCache = (input) => { throw new TypeError('ttl must be an integer'); } - if ( - scopes != null && - !scopes.every((scope) => scope === 'user' || scope === 'auth') - ) { + if (scope != null && !scope.every((v) => v === 'user' || v === 'auth')) { throw new TypeError( - 'scopes must be an array of strings with values "user" or "auth"' + 'scope must be an array of strings with values "user" or "auth"' ); } @@ -39,21 +36,21 @@ const createCache = (input) => { }; return { - get: async (key, scopes = null) => { - runValidationChecks(rpc, key); + get: async (key, scope = null) => { + runValidationChecks(rpc, key, scope); - const result = await rpc('zcache_get', key); + const result = await rpc('zcache_get', key, scope); return result ? JSON.parse(result) : null; }, - set: async (key, value, ttl = null, scopes = null) => { - runValidationChecks(rpc, key, value, ttl, scopes); + set: async (key, value, ttl = null, scope = []) => { + runValidationChecks(rpc, key, value, ttl, scope); - return await rpc('zcache_set', key, JSON.stringify(value), ttl, scopes); + return await rpc('zcache_set', key, JSON.stringify(value), ttl, scope); }, - delete: async (key, scopes) => { - runValidationChecks(rpc, key, scopes); + delete: async (key, scope = []) => { + runValidationChecks(rpc, key, scope); - return await rpc('zcache_delete', key); + return await rpc('zcache_delete', key, scope); }, }; }; diff --git a/packages/core/test/tools/create-cache.js b/packages/core/test/tools/create-cache.js index 3d5cf3d93..56e790e10 100644 --- a/packages/core/test/tools/create-cache.js +++ b/packages/core/test/tools/create-cache.js @@ -87,31 +87,31 @@ describe('zcache: get, set, delete', () => { should(result).eql(false); }); - describe('scopes', () => { - it('zcache_set: no scopes is ok', async () => { + describe('scope', () => { + it('zcache_set: no scope is ok', async () => { mockRpcCall('ok'); const res = await cache.set('key', 'ok'); should(res).eql('ok'); }); - it('zcache_set: empty array scopes is ok', async () => { + it('zcache_set: empty array scope is ok', async () => { mockRpcCall('ok'); const res = await cache.set('key', 'ok', 1, []); should(res).eql('ok'); }); - it('zcache_set: user and auth scopes is ok', async () => { + it('zcache_set: user and auth scope is ok', async () => { mockRpcCall('ok'); const res = await cache.set('key', 'ok', 1, ['user', 'auth']); should(res).eql('ok'); }); - it('zcache_set: bad scopes is not ok', async () => { + it('zcache_set: bad scope is not ok', async () => { mockRpcCall('ok'); await cache .set('key', 'ok', 1, ['bad', 'scope']) .should.be.rejectedWith( - 'scopes must be an array of strings with values "user" or "auth"' + 'scope must be an array of strings with values "user" or "auth"' ); }); it('zcache_set: mix of good and bad is not ok', async () => { @@ -120,7 +120,7 @@ describe('zcache: get, set, delete', () => { await cache .set('key', 'ok', 1, ['bad', 'auth']) .should.be.rejectedWith( - 'scopes must be an array of strings with values "user" or "auth"' + 'scope must be an array of strings with values "user" or "auth"' ); }); }); From 68ff21d2318ecd7fac97c1054c3c625fee2239b0 Mon Sep 17 00:00:00 2001 From: Stanley Chin Date: Mon, 26 Aug 2024 15:43:58 -0400 Subject: [PATCH 05/11] delete tests --- packages/core/test/tools/create-cache.js | 74 ++++++++++++------------ 1 file changed, 37 insertions(+), 37 deletions(-) diff --git a/packages/core/test/tools/create-cache.js b/packages/core/test/tools/create-cache.js index 56e790e10..4339dcc75 100644 --- a/packages/core/test/tools/create-cache.js +++ b/packages/core/test/tools/create-cache.js @@ -87,41 +87,41 @@ describe('zcache: get, set, delete', () => { should(result).eql(false); }); - describe('scope', () => { - it('zcache_set: no scope is ok', async () => { - mockRpcCall('ok'); - const res = await cache.set('key', 'ok'); - should(res).eql('ok'); - }); - it('zcache_set: empty array scope is ok', async () => { - mockRpcCall('ok'); - - const res = await cache.set('key', 'ok', 1, []); - should(res).eql('ok'); - }); - it('zcache_set: user and auth scope is ok', async () => { - mockRpcCall('ok'); - - const res = await cache.set('key', 'ok', 1, ['user', 'auth']); - should(res).eql('ok'); - }); - it('zcache_set: bad scope is not ok', async () => { - mockRpcCall('ok'); - - await cache - .set('key', 'ok', 1, ['bad', 'scope']) - .should.be.rejectedWith( - 'scope must be an array of strings with values "user" or "auth"' - ); - }); - it('zcache_set: mix of good and bad is not ok', async () => { - mockRpcCall('ok'); - - await cache - .set('key', 'ok', 1, ['bad', 'auth']) - .should.be.rejectedWith( - 'scope must be an array of strings with values "user" or "auth"' - ); - }); - }); + // describe('scope', () => { + // it('zcache_set: no scope is ok', async () => { + // mockRpcCall('ok'); + // const res = await cache.set('key', 'ok'); + // should(res).eql('ok'); + // }); + // it('zcache_set: empty array scope is ok', async () => { + // mockRpcCall('ok'); + + // const res = await cache.set('key', 'ok', 1, []); + // should(res).eql('ok'); + // }); + // it('zcache_set: user and auth scope is ok', async () => { + // mockRpcCall('ok'); + + // const res = await cache.set('key', 'ok', 1, ['user', 'auth']); + // should(res).eql('ok'); + // }); + // it('zcache_set: bad scope is not ok', async () => { + // mockRpcCall('ok'); + + // await cache + // .set('key', 'ok', 1, ['bad', 'scope']) + // .should.be.rejectedWith( + // 'scope must be an array of strings with values "user" or "auth"' + // ); + // }); + // it('zcache_set: mix of good and bad is not ok', async () => { + // mockRpcCall('ok'); + + // await cache + // .set('key', 'ok', 1, ['bad', 'auth']) + // .should.be.rejectedWith( + // 'scope must be an array of strings with values "user" or "auth"' + // ); + // }); + // }); }); From b56585d5bd7c560fac6b30f89258db70d2ba7997 Mon Sep 17 00:00:00 2001 From: Stanley Chin Date: Mon, 26 Aug 2024 15:51:44 -0400 Subject: [PATCH 06/11] re-add test, no secondary describe --- packages/core/test/tools/create-cache.js | 72 ++++++++++++------------ 1 file changed, 35 insertions(+), 37 deletions(-) diff --git a/packages/core/test/tools/create-cache.js b/packages/core/test/tools/create-cache.js index 4339dcc75..911a28fca 100644 --- a/packages/core/test/tools/create-cache.js +++ b/packages/core/test/tools/create-cache.js @@ -87,41 +87,39 @@ describe('zcache: get, set, delete', () => { should(result).eql(false); }); - // describe('scope', () => { - // it('zcache_set: no scope is ok', async () => { - // mockRpcCall('ok'); - // const res = await cache.set('key', 'ok'); - // should(res).eql('ok'); - // }); - // it('zcache_set: empty array scope is ok', async () => { - // mockRpcCall('ok'); - - // const res = await cache.set('key', 'ok', 1, []); - // should(res).eql('ok'); - // }); - // it('zcache_set: user and auth scope is ok', async () => { - // mockRpcCall('ok'); - - // const res = await cache.set('key', 'ok', 1, ['user', 'auth']); - // should(res).eql('ok'); - // }); - // it('zcache_set: bad scope is not ok', async () => { - // mockRpcCall('ok'); - - // await cache - // .set('key', 'ok', 1, ['bad', 'scope']) - // .should.be.rejectedWith( - // 'scope must be an array of strings with values "user" or "auth"' - // ); - // }); - // it('zcache_set: mix of good and bad is not ok', async () => { - // mockRpcCall('ok'); - - // await cache - // .set('key', 'ok', 1, ['bad', 'auth']) - // .should.be.rejectedWith( - // 'scope must be an array of strings with values "user" or "auth"' - // ); - // }); - // }); + it('zcache_set: no scope is ok', async () => { + mockRpcCall('ok'); + const res = await cache.set('key', 'ok'); + should(res).eql('ok'); + }); + it('zcache_set: empty array scope is ok', async () => { + mockRpcCall('ok'); + + const res = await cache.set('key', 'ok', 1, []); + should(res).eql('ok'); + }); + it('zcache_set: user and auth scope is ok', async () => { + mockRpcCall('ok'); + + const res = await cache.set('key', 'ok', 1, ['user', 'auth']); + should(res).eql('ok'); + }); + it('zcache_set: bad scope is not ok', async () => { + mockRpcCall('ok'); + + await cache + .set('key', 'ok', 1, ['bad', 'scope']) + .should.be.rejectedWith( + 'scope must be an array of strings with values "user" or "auth"' + ); + }); + it('zcache_set: mix of good and bad is not ok', async () => { + mockRpcCall('ok'); + + await cache + .set('key', 'ok', 1, ['bad', 'auth']) + .should.be.rejectedWith( + 'scope must be an array of strings with values "user" or "auth"' + ); + }); }); From 48f4bb07901367d999a064fbe678fd8e9ad3333e Mon Sep 17 00:00:00 2001 From: Stanley Chin Date: Mon, 26 Aug 2024 15:57:57 -0400 Subject: [PATCH 07/11] rm test again --- packages/core/test/tools/create-cache.js | 36 ------------------------ 1 file changed, 36 deletions(-) diff --git a/packages/core/test/tools/create-cache.js b/packages/core/test/tools/create-cache.js index 911a28fca..422c72479 100644 --- a/packages/core/test/tools/create-cache.js +++ b/packages/core/test/tools/create-cache.js @@ -86,40 +86,4 @@ describe('zcache: get, set, delete', () => { const result = await cache.delete('non-existing-key'); should(result).eql(false); }); - - it('zcache_set: no scope is ok', async () => { - mockRpcCall('ok'); - const res = await cache.set('key', 'ok'); - should(res).eql('ok'); - }); - it('zcache_set: empty array scope is ok', async () => { - mockRpcCall('ok'); - - const res = await cache.set('key', 'ok', 1, []); - should(res).eql('ok'); - }); - it('zcache_set: user and auth scope is ok', async () => { - mockRpcCall('ok'); - - const res = await cache.set('key', 'ok', 1, ['user', 'auth']); - should(res).eql('ok'); - }); - it('zcache_set: bad scope is not ok', async () => { - mockRpcCall('ok'); - - await cache - .set('key', 'ok', 1, ['bad', 'scope']) - .should.be.rejectedWith( - 'scope must be an array of strings with values "user" or "auth"' - ); - }); - it('zcache_set: mix of good and bad is not ok', async () => { - mockRpcCall('ok'); - - await cache - .set('key', 'ok', 1, ['bad', 'auth']) - .should.be.rejectedWith( - 'scope must be an array of strings with values "user" or "auth"' - ); - }); }); From fc09d39a2ef39b52ee3dbb0e4edd22017a231190 Mon Sep 17 00:00:00 2001 From: Stanley Chin Date: Mon, 26 Aug 2024 16:38:10 -0400 Subject: [PATCH 08/11] Bring back test but use bool val --- packages/core/test/tools/create-cache.js | 36 ++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/packages/core/test/tools/create-cache.js b/packages/core/test/tools/create-cache.js index 422c72479..74ed0acac 100644 --- a/packages/core/test/tools/create-cache.js +++ b/packages/core/test/tools/create-cache.js @@ -86,4 +86,40 @@ describe('zcache: get, set, delete', () => { const result = await cache.delete('non-existing-key'); should(result).eql(false); }); + + it('zcache_set: no scope is ok', async () => { + mockRpcCall(true); + const res = await cache.set('key', 'ok'); + should(res).eql(true); + }); + it('zcache_set: empty array scope is ok', async () => { + mockRpcCall(true); + + const res = await cache.set('key', 'ok', 1, []); + should(res).eql(true); + }); + it('zcache_set: user and auth scope is ok', async () => { + mockRpcCall(true); + + const res = await cache.set('key', 'ok', 1, ['user', 'auth']); + should(res).eql(true); + }); + it('zcache_set: bad scope is not ok', async () => { + mockRpcCall(true); + + await cache + .set('key', 'ok', 1, ['bad', 'scope']) + .should.be.rejectedWith( + 'scope must be an array of strings with values "user" or "auth"' + ); + }); + it('zcache_set: mix of good and bad is not ok', async () => { + mockRpcCall(true); + + await cache + .set('key', 'ok', 1, ['bad', 'auth']) + .should.be.rejectedWith( + 'scope must be an array of strings with values "user" or "auth"' + ); + }); }); From 3aa21c7feccec8da2a3b338c2436a6f06194ea5d Mon Sep 17 00:00:00 2001 From: Stanley Chin Date: Mon, 26 Aug 2024 16:44:14 -0400 Subject: [PATCH 09/11] rm mock if not calling the cache --- packages/core/test/tools/create-cache.js | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packages/core/test/tools/create-cache.js b/packages/core/test/tools/create-cache.js index 74ed0acac..35465a5a7 100644 --- a/packages/core/test/tools/create-cache.js +++ b/packages/core/test/tools/create-cache.js @@ -105,8 +105,6 @@ describe('zcache: get, set, delete', () => { should(res).eql(true); }); it('zcache_set: bad scope is not ok', async () => { - mockRpcCall(true); - await cache .set('key', 'ok', 1, ['bad', 'scope']) .should.be.rejectedWith( @@ -114,8 +112,6 @@ describe('zcache: get, set, delete', () => { ); }); it('zcache_set: mix of good and bad is not ok', async () => { - mockRpcCall(true); - await cache .set('key', 'ok', 1, ['bad', 'auth']) .should.be.rejectedWith( From eb4b0dc5e676af68a0eea8ed288e020392a028b9 Mon Sep 17 00:00:00 2001 From: Stanley Chin Date: Wed, 28 Aug 2024 11:48:57 -0400 Subject: [PATCH 10/11] no scope as string --- packages/core/src/tools/create-cache.js | 5 ++++- packages/core/test/tools/create-cache.js | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/core/src/tools/create-cache.js b/packages/core/src/tools/create-cache.js index 66e7ebe24..bd41da0e1 100644 --- a/packages/core/src/tools/create-cache.js +++ b/packages/core/src/tools/create-cache.js @@ -26,7 +26,10 @@ const createCache = (input) => { throw new TypeError('ttl must be an integer'); } - if (scope != null && !scope.every((v) => v === 'user' || v === 'auth')) { + if ( + (scope != null && !_.isArray(scope)) || + !scope.every((v) => v === 'user' || v === 'auth') + ) { throw new TypeError( 'scope must be an array of strings with values "user" or "auth"' ); diff --git a/packages/core/test/tools/create-cache.js b/packages/core/test/tools/create-cache.js index 35465a5a7..b30d51dde 100644 --- a/packages/core/test/tools/create-cache.js +++ b/packages/core/test/tools/create-cache.js @@ -104,6 +104,13 @@ describe('zcache: get, set, delete', () => { const res = await cache.set('key', 'ok', 1, ['user', 'auth']); should(res).eql(true); }); + it('zcache_set: scope as string is not ok', async () => { + await cache + .set('key', 'ok', 1, 'bad') + .should.be.rejectedWith( + 'scope must be an array of strings with values "user" or "auth"' + ); + }); it('zcache_set: bad scope is not ok', async () => { await cache .set('key', 'ok', 1, ['bad', 'scope']) From 0fbf903e6d8665aba929630d8a85db2afb29435c Mon Sep 17 00:00:00 2001 From: Stanley Chin Date: Wed, 28 Aug 2024 12:08:31 -0400 Subject: [PATCH 11/11] refine conditional --- packages/core/src/tools/create-cache.js | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/core/src/tools/create-cache.js b/packages/core/src/tools/create-cache.js index bd41da0e1..96040c464 100644 --- a/packages/core/src/tools/create-cache.js +++ b/packages/core/src/tools/create-cache.js @@ -27,8 +27,9 @@ const createCache = (input) => { } if ( - (scope != null && !_.isArray(scope)) || - !scope.every((v) => v === 'user' || v === 'auth') + scope !== null && + (!Array.isArray(scope) || + !scope.every((v) => v === 'user' || v === 'auth')) ) { throw new TypeError( 'scope must be an array of strings with values "user" or "auth"' @@ -45,12 +46,12 @@ const createCache = (input) => { const result = await rpc('zcache_get', key, scope); return result ? JSON.parse(result) : null; }, - set: async (key, value, ttl = null, scope = []) => { + set: async (key, value, ttl = null, scope = null) => { runValidationChecks(rpc, key, value, ttl, scope); return await rpc('zcache_set', key, JSON.stringify(value), ttl, scope); }, - delete: async (key, scope = []) => { + delete: async (key, scope = null) => { runValidationChecks(rpc, key, scope); return await rpc('zcache_delete', key, scope);