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

Proof-of-concept: Rewrite to ts #212

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
refactor: generate declarations
  • Loading branch information
ole-martin committed Dec 3, 2021
commit 71113ab8850038494763de10ff02cd8ecde86b7f
6 changes: 6 additions & 0 deletions @types/globals.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export {};
declare global {
interface Window {
openSimplifiedLoginWidget?: (initialParams: any, loginHandler: any, loginNotYouHandler: any) => void;
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@
"type-check:watch": "npm run type-check -- --watch",
"build": "npm run build:types && npm run build:js",
"build:types": "tsc --emitDeclarationOnly",
"build:js": "babel src --out-dir lib --extensions \".ts,.tsx\" --source-maps inline"
"build:js": "webpack --mode=production"
},
"author": "",
"license": "MIT",
105 changes: 41 additions & 64 deletions src/cache.d.ts
Original file line number Diff line number Diff line change
@@ -1,64 +1,41 @@
/**
* Cache class that attempts WebStorage (session/local storage), and falls back to JS object literal
* @private
*/
export default class Cache {
/**
* @param {Storage} [storeProvider] - A function to return a WebStorage instance (either
* `sessionStorage` or `localStorage` from a `Window` object)
* @throws {SDKError} - If sessionStorage or localStorage are not accessible
*/
constructor(storeProvider?: Storage);
cache: WebStorageCache | LiteralCache;
type: string;
/**
* Get a value from cache (checks that the object has not expired)
* @param {string} key
* @private
* @returns {*} - The value if it exists, otherwise null
*/
private get;
/**
* Set a cache entry
* @param {string} key
* @param {*} value
* @param {Number} expiresIn - Value in milliseconds until the entry expires
* @private
* @returns {void}
*/
private set;
/**
* Delete a cache entry
* @param {string} key
* @private
* @returns {void}
*/
private delete;
}
/**
* Will be used if web storage is available
* @private
*/
declare class WebStorageCache {
/**
* Create web storage cache object
* @param {Storage} store - A reference to either `sessionStorage` or `localStorage` from a
* `Window` object
*/
constructor(store: Storage);
store: Storage;
get: (key: any) => string;
set: (key: any, value: any) => void;
delete: (key: any) => void;
}
/**
* Will be used if session storage is not available
* @private
*/
declare class LiteralCache {
store: {};
get: (key: any) => any;
set: (key: any, value: any) => any;
delete: (key: any) => boolean;
}
export {};
/**
* @typedef {() => Storage} StoreProvider
*/
declare type StoreProvider = () => Storage;
/**
* Cache class that attempts WebStorage (session/local storage), and falls back to JS object literal
* @private
*/
export default class Cache {
#private;
/**
* @param {StoreProvider} [storeProvider] - A function to return a WebStorage instance (either
* `sessionStorage` or `localStorage` from a `Window` object)
* @throws {SDKError} - If sessionStorage or localStorage are not accessible
*/
constructor(storeProvider: StoreProvider);
/**
* Get a value from cache (checks that the object has not expired)
* @param {string} key
* @private
* @returns {*} - The value if it exists, otherwise null
*/
get(key: string): any;
/**
* Set a cache entry
* @param {string} key
* @param {*} value
* @param {Number} expiresIn - Value in milliseconds until the entry expires
* @private
* @returns {void}
*/
set(key: string, value: any, expiresIn?: number): void;
/**
* Delete a cache entry
* @param {string} key
* @private
* @returns {void}
*/
delete(key: string): void;
}
export {};
22 changes: 12 additions & 10 deletions src/cache.ts
Original file line number Diff line number Diff line change
@@ -5,7 +5,9 @@
'use strict';

import SDKError from './SDKError';

/**
* @typedef {() => Storage} StoreProvider
*/
type StoreProvider = () => Storage;
/**
* Check whether we are able to use web storage
@@ -80,20 +82,20 @@ const maxExpiresIn = Math.pow(2, 31) - 1;
* @private
*/
export default class Cache {
cache: WebStorageCache | LiteralCache;
type: string;
#cache: WebStorageCache | LiteralCache;
#type: string;
/**
* @param {StoreProvider} [storeProvider] - A function to return a WebStorage instance (either
* `sessionStorage` or `localStorage` from a `Window` object)
* @throws {SDKError} - If sessionStorage or localStorage are not accessible
*/
constructor(storeProvider: StoreProvider) {
if (webStorageWorks(storeProvider)) {
this.cache = new WebStorageCache(storeProvider());
this.type = 'WebStorage';
this.#cache = new WebStorageCache(storeProvider());
this.#type = 'WebStorage';
} else {
this.cache = new LiteralCache();
this.type = 'ObjectLiteralStorage';
this.#cache = new LiteralCache();
this.#type = 'ObjectLiteralStorage';
}
}

@@ -105,7 +107,7 @@ export default class Cache {
*/
get(key: string) {
try {
const raw = this.cache.get(key);
const raw = this.#cache.get(key);
const obj = raw ? JSON.parse(raw) : null;
if (obj && Number.isInteger(obj.expiresOn) && obj.expiresOn > Date.now()) {
return obj.value;
@@ -133,7 +135,7 @@ export default class Cache {

try {
const expiresOn = Math.floor(Date.now() + expiresIn);
this.cache.set(key, JSON.stringify({ expiresOn, value }));
this.#cache.set(key, JSON.stringify({ expiresOn, value }));
setTimeout(() => this.delete(key), expiresIn);
} catch (e) {
throw new SDKError(e);
@@ -148,7 +150,7 @@ export default class Cache {
*/
delete(key: string) {
try {
this.cache.delete(key);
this.#cache.delete(key);
} catch (e) {
throw new SDKError(e);
}
Loading