Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add storage implementation if localStorage is not available #113

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/catcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import type { JavaScriptCatcherIntegrations } from './types/integrations';
import { EventRejectedError } from './errors';
import type { HawkJavaScriptEvent } from './types';
import { isErrorProcessed, markErrorAsProcessed } from './utils/event';
import { catcherStorage } from './modules/storage';

/**
* Allow to use global VERSION, that will be overwritten by Webpack
Expand Down Expand Up @@ -154,13 +155,13 @@ export default class Catcher {
private static getGeneratedUser(): AffectedUser {
let userId: string;
const LOCAL_STORAGE_KEY = 'hawk-user-id';
const storedId = localStorage.getItem(LOCAL_STORAGE_KEY);
const storedId = catcherStorage.getItem(LOCAL_STORAGE_KEY);

if (storedId) {
userId = storedId;
} else {
userId = id();
localStorage.setItem(LOCAL_STORAGE_KEY, userId);
catcherStorage.setItem(LOCAL_STORAGE_KEY, userId);
}

return {
Expand Down
100 changes: 100 additions & 0 deletions src/modules/storage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/**
* Implements Web Storage API if localStorage is not available.
* So it can be available in node.js environment
*/
class CatcherStorage implements Storage {
private items: { [key: string]: string } = {};

/**
* Returns the value of the item with the specified key.
*
* @param key - The key of the item you want to retrieve
*/
public getItem(key: string): string | null {
if (this.isLocalStorageAvailable()) {
return localStorage.getItem(key);
}

return this.items[key] || null;
}

/**
* Sets the value of the pair identified by key to value, creating a new key/value pair if none existed for key previously.
*
* @param key - The key of the item you want to store
* @param value - The value of the item you want to store
*/
public setItem(key: string, value: string): void {
Comment on lines +5 to +27
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets make this class more abstract:

export interface HawkStorageLike {
  getItem: (key: string) => string | undefined;
  setItem(key: string) => boolean;
}

export class CatcherStorage {
  constructor(private readonly storage: HawkStorageLike){}
  
  getItem(){
    return this.storage.getItem()
  }
  
  setItem(){
    return this.storage.setItem()
  }
}

and catcher will set available implementation:

// catcher.ts

prepateStorage(){
  const isLocalStorageAvailable = ...
  const storageImplementation = isLocalStorageAvailable ? LocalStorage : CustomStorage

  this.storage = new CatcherStorage(storageImplementation)
}

and user can pass own implementation through the catcher initial settings:

import { HawkCatcher, type HawkStorageLike } from '@hawk.so/javascript'

class ReactNativeStorage implements HawkStorageLike {
 ...
}

new HawkCatcher {
  //...
  storage: new ReactNativeStorge()
}

if (this.isLocalStorageAvailable()) {
localStorage.setItem(key, value);
} else {
this.items[key] = value;
}
}

/**
* Removes the key/value pair with the given key from the list associated with the object, if a key/value pair with the given key exists.
*
* @param key - The key of the item you want to remove
*/
public removeItem(key: string): void {
if (this.isLocalStorageAvailable()) {
localStorage.removeItem(key);
} else {
delete this.items[key];
}
}

/**
* Empties the list associated with the object of all key/value pairs, if there are any.
*/
public clear(): void {
if (this.isLocalStorageAvailable()) {
localStorage.clear();
} else {
this.items = {};
}
}

/**
* Returns the number of key/value pairs currently present in the list associated with the object.
*/
public get length(): number {
if (this.isLocalStorageAvailable()) {
return localStorage.length;
}

return Object.keys(this.items).length;
}

/**
* Returns the name of the nth key in the list.
*
* @param index - The index of the key you want to get
*/
public key(index: number): string | null {
if (this.isLocalStorageAvailable()) {
return localStorage.key(index);
}

return Object.keys(this.items)[index] || null;
}

/**
* Checks if localStorage is available
*/
private isLocalStorageAvailable(): boolean {
try {
const testKey = '__test__';

localStorage.setItem(testKey, testKey);
localStorage.removeItem(testKey);

return true;
} catch (e) {
return false;
}
}
}

export const catcherStorage = new CatcherStorage();
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
},
"include": [
"src/**/*",
"src/types/*"
"src/types/*",
"src/modules/*",
],
}
Loading
Loading