Skip to content

Commit

Permalink
chore: remove built-in support for react native async storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzixxx committed Apr 4, 2022
1 parent d494a66 commit 111b5f7
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 35 deletions.
40 changes: 32 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,30 +142,54 @@ unleash.stop()

### Custom store

This SDK will use [@react-native-async-storage/async-storage](https://react-native-async-storage.github.io/async-storage/) to backup feature toggles locally. This is useful for bootstrapping the SDK the next time the user comes back to your application.
This SDK can work with React Native storage [@react-native-async-storage/async-storage](https://react-native-async-storage.github.io/async-storage/) or [react-native-shared-preferences](https://github.com/sriraman/react-native-shared-preferences) and many more to backup feature toggles locally. This is useful for bootstrapping the SDK the next time the user comes back to your application.

You can provide your own storage implementation.

Example:
Examples:

```js

import SharedPreferences from 'react-native-shared-preferences';
import { UnleashClient } from 'unleash-proxy-client';

const unleash = new UnleashClient({
url: 'https://eu.unleash-hosted.com/hosted/proxy',
clientKey: 'your-proxy-key',
appName: 'my-webapp',
storage: {
save: (name: string, data: any) => SharedPreferences.setItem(name, data),
get: (name: string) => SharedPreferences.getItem(name, (val) => val)
},
storageProvider: {
save: (name: string, data: any) => SharedPreferences.setItem(name, data),
get: (name: string) => SharedPreferences.getItem(name, (val) => val)
},
});
```

```js
import AsyncStorage from '@react-native-async-storage/async-storage';
import { UnleashClient } from 'unleash-proxy-client';

const PREFIX = 'unleash:repository';

const unleash = new UnleashClient({
url: 'https://eu.unleash-hosted.com/hosted/proxy',
clientKey: 'your-proxy-key',
appName: 'my-webapp',
storageProvider: {
save: (name: string, data: any) => {
const repo = JSON.stringify(data);
const key = `${PREFIX}:${name}`;
await AsyncStorage.setItem(key, repo);
},
get: (name: string) => {
const key = `${PREFIX}:${name}`;
const data = await AsyncStorage.getItem(key);
return data ? JSON.parse(data) : undefined;
}
},
});
```
## How to use in node.js

This SDK can also be used in node.js applications (from v1.4.0). Please note that you will need to provide a valid "fetch" implementation. Only ECMAScript modules is exported from this package.
This SDK can also be used in node.js applications (from v1.4.0). Please note that you will need to provide a valid "fetch" implementation. Only ECMAScript modules is exported from this package.

```js
import fetch from 'node-fetch';
Expand Down
21 changes: 0 additions & 21 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
"uglify-js": "^3.6.0"
},
"dependencies": {
"@react-native-async-storage/async-storage": "^1.15.17",
"tiny-emitter": "^2.1.0",
"uuid": "^8.3.2"
}
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { TinyEmitter } from 'tiny-emitter';
import Metrics from './metrics';
import type IStorageProvider from './storage-provider';
import LocalStorageProvider from './storage-provider-local';
import InMemoryStorageProvider from './storage-provider-inmemory';
import LocalStorageProvider from './storage-provider-local';
import EventsHandler from './events-handler';
import { notNullOrUndefined } from './util';

Expand Down
22 changes: 22 additions & 0 deletions src/storage-provider-local.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import LocalStorageProvider from './storage-provider-local';

describe('LocalStorageProvider', () => {
it('should store and retrieve arbitrary values by key', async () => {
const store = new LocalStorageProvider();

await store.save('key1', 'value1');
await store.save('key2', { value2: 'true' });
await store.save('key3', ['value3']);
await store.save('key4', true);

expect(await store.get('key1')).toBe('value1');
expect(await store.get('key2')).toMatchObject({ value2: 'true' });
expect(await store.get('key3')).toMatchObject(['value3']);
expect(await store.get('key4')).toBe(true);
});

it('should return undefined for empty value', async () => {
const store = new LocalStorageProvider();
expect(await store.get('notDefinedKey')).toBe(undefined);
});
});
7 changes: 3 additions & 4 deletions src/storage-provider-local.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import AsyncStorage from '@react-native-async-storage/async-storage';
import type IStorageProvider from './storage-provider';

export default class LocalStorageProvider implements IStorageProvider {
Expand All @@ -8,16 +7,16 @@ export default class LocalStorageProvider implements IStorageProvider {
const repo = JSON.stringify(data);
const key = `${this.prefix}:${name}`;
try {
await AsyncStorage.setItem(key, repo);
window.localStorage.setItem(key, repo);
} catch (ex) {
console.error(ex);
}
}

public async get(name: string) {
public get(name: string) {
try {
const key = `${this.prefix}:${name}`;
const data = await AsyncStorage.getItem(key);
const data = window.localStorage.getItem(key);
return data ? JSON.parse(data) : undefined;
} catch (e) {
// tslint:disable-next-line
Expand Down

0 comments on commit 111b5f7

Please sign in to comment.