gdpr-guard-local
A simple local storage adapter for gdpr-guard
This library predefines logic to store/save and restore gdpr-guard
state from localStorage.
If you need any help, you're more than welcome on my official Discord server dedicated to my open-source projects.
You can read the online documentation.
How to import
This library re-exports what's exported by gdpr-guard
.
import {
defaults,
LocalStorageSavior,
} from "gdpr-guard-local"
const {
defaults,
LocalStorageSavior,
} = gdprGuardLocal;
LocalStorageSavior
LocalStorageSavior
is a class that specializes the Savior API for local storage.
It is mainly for libraries developers or can be used as parameters for a library.
To construct one, simply pass a LocalStorageConfig
object followed by a LocalStoreFactory
(both are optional with default values):
interface LocalStorageConfig{
storeKey: string;
versionKey: string;
version: string|number;
comparator: (oldVersion, newVersion) => boolean;
expiration: () => Date;
}
type LocalStoreFactory = () => LocalStore;
interface LocalStore{
has(key: string): Promise<boolean>;
set(key: string, value: any, expiration: Date): Promise<void>;
remove(key: string): Promise<void>;
get(key: string): Promise<any>;
removeExpiredKeys(): Promise<void>;
}
const savior = new LocalStorageSavior(myConfig, myStoreFactory);
To help users and developers, we provide default values as well as ways to easily create new values for the parameters.
Defaults
const {
defaultStoreFactory,
expiration,
defaultConfig,
makeConfig,
} = defaults;
The makeConfig
function is a utility function that merges the user provided config object with the default config:
declare const makeConfig: (config: Partial<LocalStorageConfig> = {}): LocalStorageConfig;
For instance, you can customize the savior like this:
const savior = new LocalStorageSavior(
makeConfig({
version: "v1.0.0",
}),
defaultStoreFactory,
);