Skip to content
This repository has been archived by the owner on Sep 1, 2022. It is now read-only.

Commit

Permalink
fix(storage): return type unkown
Browse files Browse the repository at this point in the history
`any` is too loose a value, and consumers
should make appropriate decision about
the data, so `unknown` is more appropriate.

The motivation comes from a bug that made its
way into production recently.
  • Loading branch information
mxdvl committed Aug 4, 2022
1 parent f80ad67 commit 05bc766
Showing 1 changed file with 8 additions and 18 deletions.
26 changes: 8 additions & 18 deletions src/storage/storage.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { isObject } from '../isObject/isObject';
import { isString } from '../isString/isString';

class StorageFactory {
#storage: Storage | undefined; // https://mdn.io/Private_class_fields

Expand All @@ -22,27 +25,19 @@ class StorageFactory {
return Boolean(this.#storage);
}

/* eslint-disable
@typescript-eslint/no-unsafe-assignment,
@typescript-eslint/no-explicit-any,
@typescript-eslint/no-unsafe-argument
--
- we're using the `try` to handle anything bad happening
- JSON.parse returns an `any`, we really are with an `any`
*/
/**
* Retrieve an item from storage.
*
* @param key - the name of the item
*/
get(key: string): any {
get(key: string): unknown {
try {
const { value, expires } = JSON.parse(
this.#storage?.getItem(key) ?? '',
);
const data: unknown = JSON.parse(this.#storage?.getItem(key) ?? '');
if (!isObject(data)) return null;
const { value, expires } = data;

// is this item has passed its sell-by-date, remove it
if (expires && new Date() > new Date(expires)) {
if (isString(expires) && new Date() > new Date(expires)) {
this.remove(key);
return null;
}
Expand All @@ -52,11 +47,6 @@ class StorageFactory {
return null;
}
}
/* eslint-enable
@typescript-eslint/no-unsafe-assignment,
@typescript-eslint/no-explicit-any,
@typescript-eslint/no-unsafe-argument
*/

/**
* Save a value to storage.
Expand Down

0 comments on commit 05bc766

Please sign in to comment.