Skip to content

Commit 8bc23f0

Browse files
committed
feat: return validation result in obj get
1 parent 65ecca6 commit 8bc23f0

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,13 @@ If a `loader` is present, it will be called with the missing keys and those keys
138138
get('indexKeyForValue', undefined, undefined, 'obj')
139139

140140
// returns the cached data alongside the meta
141+
// also includes the result of the validation according to expire parameter
141142
{
142143
data: 'foo',
143144
meta: {
144145
date: '2021-01-15T18:21:00.152Z',
145-
}
146+
},
147+
valid: true,
146148
}
147149
```
148150

src/methods/get.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { addListener, cachedObj, debugLog, dispatch, expire, reactCache, setProp
33

44
type valueTypes = {
55
data: cachedObj['data'],
6-
obj: cachedObj,
6+
obj: cachedObj & {valid: boolean},
77
}
88

99
export type getValue<
@@ -64,11 +64,12 @@ export function get<
6464
const missing: string[] = []
6565

6666
keys.forEach(key => {
67-
if(!cache[key].promise && !verifyEntry(cache[key], expire)) {
67+
const valid = verifyEntry(cache[key], expire)
68+
if(!cache[key].promise && !valid) {
6869
missing.push(key)
6970
}
7071
if (returnType === 'obj') {
71-
values[key] = cache[key].obj as getValue<T>
72+
values[key] = (cache[key].obj ? { ...cache[key].obj, valid } : undefined) as getValue<T>
7273
} else {
7374
values[key] = cache[key].obj?.data as getValue<T>
7475
}

test/methods/get.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,44 @@ it('call loader for missing entries', async () => {
7878
expect(loader).toBeCalledWith(['faa'])
7979
})
8080

81+
it('verify entries per callback', async () => {
82+
const { api } = await setupApi({cacheValues: { foo: 'FOO', bar: 'BAR'}})
83+
84+
const expire = jest.fn(o => o.data === 'FOO')
85+
86+
const value = api.get(['foo', 'bar'], undefined, expire, 'obj')
87+
88+
expect(expire).toBeCalledTimes(2)
89+
expect(expire).toHaveBeenNthCalledWith(1, expect.objectContaining({data: 'FOO'}))
90+
expect(expire).toHaveBeenNthCalledWith(2, expect.objectContaining({data: 'BAR'}))
91+
expect(value).toEqual({
92+
foo: expect.objectContaining({data: 'FOO', valid: false}),
93+
bar: expect.objectContaining({data: 'BAR', valid: true}),
94+
})
95+
})
96+
97+
it('verify entries per age', async () => {
98+
const { api } = await setupApi({
99+
cacheObjects: {
100+
faa: { data: 'a', meta: { date: new Date() } },
101+
fee: { data: 'b', meta: { date: (new Date()).toString() } },
102+
fii: { data: 'c', meta: { date: undefined } },
103+
foo: { data: 'd', meta: { date: new Date('2001-02-03T04:05:06Z') } },
104+
fuu: { data: 'e', meta: { date: '2001-02-03T04:05:06Z' } },
105+
},
106+
})
107+
108+
const value = api.get(['faa', 'fee', 'fii', 'foo', 'fuu'], undefined, 10000, 'obj')
109+
110+
expect(value).toEqual({
111+
'faa': expect.objectContaining({data: 'a', valid: true}),
112+
'fee': expect.objectContaining({data: 'b', valid: true}),
113+
'fii': expect.objectContaining({data: 'c', valid: true}),
114+
'foo': expect.objectContaining({data: 'd', valid: false}),
115+
'fuu': expect.objectContaining({data: 'e', valid: false}),
116+
})
117+
})
118+
81119
it('call loader for expired (per callback) entries', async () => {
82120
const { cache, rerender, api } = await setupApi({cacheValues: { foo: 'bar' }, idbValues: { fuu: 'baz' } })
83121
const loader = jest.fn(() => {

0 commit comments

Comments
 (0)