Skip to content

Commit

Permalink
Return null if FastCache item does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
Zeryther committed Jun 17, 2023
1 parent 6dfda54 commit 70922ab
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
18 changes: 14 additions & 4 deletions src/fastcache.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,20 @@ describe('FastCache', () => {

const item = await fastcache.get('key');

expect(item.key).toBe('key');
expect(item.value).toBe('value');
expect(item.expiration).toBe(1234567890);
expect(item.byteSize).toBe(6);
expect(item).not.toBeNull();

expect(item?.key).toBe('key');
expect(item?.value).toBe('value');
expect(item?.expiration).toBe(1234567890);
expect(item?.byteSize).toBe(6);
});

it('should return null if the item does not exist', async () => {
mock.onGet('/fastcache?key=key').reply(404);

const item = await fastcache.get('key');

expect(item).toBeNull();
});

it('should be able to set a FastCache item', async () => {
Expand Down
16 changes: 12 additions & 4 deletions src/fastcache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,21 @@ export default class FastCache {
* Required API Key permission: `fastcache:read`
*
* @param key The key of the FastCache item
* @returns The FastCache item
* @returns The FastCache item. Returns null if the item does not exist.
* @see https://docs.gigadrive.network/products/fastcache#retrieve-an-item
*/
async get(key: string): Promise<FastCacheItem> {
const { data } = await this.axios.get(`/fastcache?key=${key}`);
async get(key: string): Promise<FastCacheItem | null> {
try {
const { data } = await this.axios.get(`/fastcache?key=${key}`);

return data;
return data;
} catch (e) {
if (e.response.status === 404) {
return null;
}

throw e;
}
}

/**
Expand Down

0 comments on commit 70922ab

Please sign in to comment.