Skip to content

Instantly share code, notes, and snippets.

View oleh-zaporozhets's full-sized avatar

Oleh Zaporozhets oleh-zaporozhets

View GitHub Profile
import Storage from './storage';
enum Locals {
ACCESS_TOKEN = 'access_token',
REFRESH_TOKEN = 'refresh_token'
}
class Tokens extends Storage<Locals> {
constructor() {
super();
interface IStorage {
getItem(key: string): string | null;
setItem(key: string, value: string): void;
removeItem(key: string): void;
}
export default abstract class Storage<T extends string> {
private readonly storage: IStorage;
public constructor(getStorage = (): IStorage => window.localStorage) {
protected clearItem(key: T): void {
this.storage.removeItem(key);
}
protected clearItems(keys: T[]): void {
keys.forEach((key) => this.clearItem(key));
}
protected set(key: T, value: string): void {
this.storage.setItem(key, value);
}
protected get(key: T): string | null {
return this.storage.getItem(key);
}
abstract class Storage<T extends string> {
private readonly storage: IStorage;
public constructor(getStorage = (): IStorage => window.localStorage) {
this.storage = getStorage();
}
}
abstract class Storage<T extends string> {
private readonly storage: IStorage;
}
interface IStorage {
getItem(key: string): string | null;
setItem(key: string, value: string): void;
removeItem(key: string): void;
}
abstract class Storage<T extends string> {}
class MainApi extends HttpClient {
private static classInstance?: MainApi;
private constructor() {
super('https://api.awesome-site.com');
}
public static getInstance() {
if (!this.classInstance) {
this.classInstance = new MainApi();