Skip to content

Commit

Permalink
Fix Storage API types
Browse files Browse the repository at this point in the history
  • Loading branch information
dchambers committed Oct 18, 2020
1 parent db03990 commit d5f016c
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ persistCache({
cache: ApolloCache<TSerialized>,

// Reference to your storage provider.
storage: PersistentStorage<TPersisted>,
storage: PersistentStorage,

/**
* Trigger options.
Expand Down
2 changes: 1 addition & 1 deletion examples/web/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { PersistentStorage } from "apollo3-cache-persist/types";

let persistor;

class LocalStoragePersistedStorage implements PersistentStorage<string> {
class LocalStoragePersistedStorage implements PersistentStorage {
getItem(key: string): string | null {
return localStorage.getItem(key);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from './types';

export default class Storage<T> {
storage: PersistentStorage<PersistedData<T>>;
storage: PersistentStorage;
key: string;

constructor(options: ApolloPersistOptions<T>) {
Expand All @@ -20,7 +20,7 @@ export default class Storage<T> {
}

async write(data: PersistedData<T>): Promise<void> {
await this.storage.setItem(this.key, data);
await this.storage.setItem(this.key, data.toString());
}

async purge(): Promise<void> {
Expand Down
6 changes: 3 additions & 3 deletions src/__mocks__/MockStorage.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { PersistentStorage } from '../types';

export default class MockStorage<T> implements PersistentStorage<T> {
export default class MockStorage implements PersistentStorage {
storage: Map<string, string>;

constructor() {
this.storage = new Map();
}

setItem(key: string, data: T): Promise<any> {
setItem(key: string, data: string): Promise<any> {
return new Promise(resolve => {
this.storage.set(key, data);
resolve();
Expand All @@ -21,7 +21,7 @@ export default class MockStorage<T> implements PersistentStorage<T> {
});
}

getItem(key: string): Promise<T> {
getItem(key: string): Promise<string> {
return new Promise((resolve, reject) => {
resolve(this.storage.get(key));
});
Expand Down
4 changes: 2 additions & 2 deletions src/__mocks__/MockStorageSync.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { PersistentStorage } from '../types';

export default class MockStorageSync<T> implements PersistentStorage<T> {
export default class MockStorageSync implements PersistentStorage {
storage: Map<string, string>;

constructor() {
this.storage = new Map();
}

setItem(key: string, data): void {
setItem(key: string, data: string): void {
this.storage.set(key, data);
}

Expand Down
10 changes: 5 additions & 5 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ export type TriggerFunction = (persist: () => void) => TriggerUninstallFunction;

export type PersistedData<T> = T | string | null;

export interface PersistentStorage<T> {
getItem: (key: string) => Promise<T | null> | T | null;
setItem: (key: string, data: T) => Promise<T> | Promise<void> | void | T;
removeItem: (key: string) => Promise<T> | Promise<void> | void;
export interface PersistentStorage {
getItem: (key: string) => string | null | Promise<string | null>;
setItem: (key: string, value: string) => void | Promise<void>;
removeItem: (key: string) => void | Promise<void>;
}

export interface ApolloPersistOptions<TSerialized> {
cache: ApolloCache<TSerialized>;
storage: PersistentStorage<PersistedData<TSerialized>>;
storage: PersistentStorage;
trigger?: 'write' | 'background' | TriggerFunction | false;
debounce?: number;
key?: string;
Expand Down

0 comments on commit d5f016c

Please sign in to comment.