Issue
I ran across this in an NPM module that I am using and I don't know what it means
getStorage?: () => StorageType
where getStorage is used:
export interface IAuthTokenInterceptorConfig {
header?: string
headerPrefix?: string
requestRefresh: TokenRefreshRequest
tokenExpireFudge?: number
getStorage?: () => StorageType
}
Here is the definition of storage type:
export type StorageType = {
remove(key: string): void
set(key: string, value: string): void
get(value: string): string | null
}
secondly... I think it is trying to tell me that I can provide my own storage provider.. but how do I do that?
Solution
getStorage
is an optional property that if defined, must be a function that returns a StorageType
, aka a factory.
For example, you could define a factory function to return a localStorage
backed StorageType
...
class LocalStorageType implements StorageType {
remove(key: string) {
localStorage.removeItem(key);
}
set(key: string, value: string) {
localStorage.setItem(key, value);
}
get(key: string) {
return localStorage.getItem(key);
}
}
const myAuthTokenInterceptorConfig: IAuthTokenInterceptorConfig = {
// other properties...
getStorage: () => new LocalStorageType(),
}
Answered By - Phil
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.