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

Allow unserialised data to be written to store #420

Merged
merged 1 commit into from
Mar 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
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;
storage: PersistentStorage<PersistedData<T>>;
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.toString());
await this.storage.setItem(this.key, data);
}

async purge(): Promise<void> {
Expand Down
18 changes: 18 additions & 0 deletions src/__tests__/Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,22 @@ describe('Storage', () => {
await storage.purge();
await expect(storage.read()).resolves.toBe(undefined);
});

describe('when data is an object', () => {
it ('writes an object to persistent storage', async () => {
const obj = {
yo: 'yo yo'
}

await expect(storage.write(obj)).resolves.toBe(undefined);
await expect(storage.read()).resolves.toBe(obj);
})
})

describe('when data is a string', () => {
it ('writes a string to persistent storage', async () => {
await expect(storage.write('yo yo yo')).resolves.toBe(undefined);
await expect(storage.read()).resolves.toBe('yo yo yo');
})
})
});
2 changes: 1 addition & 1 deletion src/storageWrappers/AsyncStorageWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { PersistentStorage } from '../types';
* });
*
*/
export class AsyncStorageWrapper implements PersistentStorage {
export class AsyncStorageWrapper implements PersistentStorage<string> {
// Actual type definition: https://github.com/react-native-async-storage/async-storage/blob/master/types/index.d.ts
private storage;

Expand Down
2 changes: 1 addition & 1 deletion src/storageWrappers/IonicStorageWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PersistentStorage } from '../types';

export class IonicStorageWrapper implements PersistentStorage {
export class IonicStorageWrapper implements PersistentStorage<string> {
// Actual type definition: https://github.com/ionic-team/ionic-storage/blob/main/src/storage.ts#L102
private storage;

Expand Down
2 changes: 1 addition & 1 deletion src/storageWrappers/LocalForageWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PersistentStorage } from '../types';

export class LocalForageWrapper implements PersistentStorage {
export class LocalForageWrapper implements PersistentStorage<string | object> {
// Actual type definition: https://github.com/localForage/localForage/blob/master/typings/localforage.d.ts#L17
private storage;

Expand Down
2 changes: 1 addition & 1 deletion src/storageWrappers/LocalStorageWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PersistentStorage } from '../types';

export class LocalStorageWrapper implements PersistentStorage {
export class LocalStorageWrapper implements PersistentStorage<string> {
// Actual type definition: https://github.com/microsoft/TypeScript/blob/master/lib/lib.dom.d.ts#L15286
private storage;

Expand Down
2 changes: 1 addition & 1 deletion src/storageWrappers/MMKVStorageWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { PersistentStorage } from '../types';
* });
*
*/
export class MMKVStorageWrapper implements PersistentStorage {
export class MMKVStorageWrapper implements PersistentStorage<string> {
// Actual type definition: https://github.com/ammarahm-ed/react-native-mmkv-storage/blob/master/index.d.ts#L27
private storage;

Expand Down
2 changes: 1 addition & 1 deletion src/storageWrappers/SessionStorageWrapper.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { PersistentStorage } from '../types';

export class SessionStorageWrapper implements PersistentStorage {
export class SessionStorageWrapper implements PersistentStorage<string> {
// Actual type definition: https://github.com/microsoft/TypeScript/blob/master/lib/lib.dom.d.ts#L15286
private storage;

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 {
getItem: (key: string) => string | null | Promise<string | null>;
setItem: (key: string, value: string) => void | Promise<void>;
removeItem: (key: string) => void | Promise<void>;
export interface PersistentStorage<T> {
Copy link
Collaborator

@wtrocki wtrocki Mar 26, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will be breaking change

Suggested change
export interface PersistentStorage<T> {
export interface PersistentStorage<T = any> {

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

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