Skip to content

Commit f3d5bc0

Browse files
oscbmateusz1913danychi
authored
fix: async-storage as optional peer dependency (#779)
- Makes `@react-native-async-storage/async-storage` and `react-native-get-random-values` as peer dependencies, async-storage is now optional in both core and sovran. PR by: [@mateusz1913](https://github.com/mateusz1913) - Remove sovran type exports. PR by: [@danychi](https://github.com/danychi) - Fixing jest setup for tests for mocking `UUID` and `react-native-get-random-values` for all tests by default. - Fixed the async-storage mock Co-authored-by: Mateusz Mędrek <[email protected]> Co-authored-by: Daniel Alarcón Amador <[email protected]>
1 parent 4734389 commit f3d5bc0

File tree

24 files changed

+71
-38
lines changed

24 files changed

+71
-38
lines changed

README.md

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,24 @@ The hassle-free way to add Segment analytics to your React-Native app.
5050

5151
## Installation
5252

53-
Install `@segment/analytics-react-native`, [`@segment/sovran-react-native`](https://github.com/segmentio/sovran-react-native), [`react-native-get-random-values`](https://github.com/LinusU/react-native-get-random-values) and [`react-native-async-storage/async-storage`](https://github.com/react-native-async-storage/async-storage):
53+
Install `@segment/analytics-react-native`, [`@segment/sovran-react-native`](https://github.com/segmentio/analytics-react-native/blob/master/packages/sovran) and [`react-native-get-random-values`](https://github.com/LinusU/react-native-get-random-values):
5454

5555
```sh
56-
yarn add @segment/analytics-react-native @segment/sovran-react-native @react-native-async-storage/async-storage
56+
yarn add @segment/analytics-react-native @segment/sovran-react-native react-native-get-random-values
5757
# or
58-
npm install --save @segment/analytics-react-native @segment/sovran-react-native @react-native-async-storage/async-storage
58+
npm install --save @segment/analytics-react-native @segment/sovran-react-native react-native-get-random-values
5959
```
6060

61+
If you want to use the default persistor for the Segment Analytics client, you also have to install [`react-native-async-storage/async-storage`](https://github.com/react-native-async-storage/async-storage).
62+
63+
```sh
64+
yarn add @react-native-async-storage/async-storage
65+
# or
66+
npm install --save @react-native-async-storage/async-storage
67+
```
68+
69+
*Note: If you wish to use your own persistence layer you can use the `storePersistor` option when initializing the client. Read more [Client Options](#client-options)*
70+
6171
For iOS, install native modules with:
6272

6373
```sh
@@ -119,7 +129,7 @@ You must pass at least the `writeKey`. Additional configuration options are list
119129
| `trackDeepLinks` | false | Enable automatic tracking for when the user opens the app via a deep link (Note: Requires additional setup on iOS, [see instructions](#ios-deep-link-tracking-setup)) |
120130
| `defaultSettings` | undefined | Settings that will be used if the request to get the settings from Segment fails. Type: [SegmentAPISettings](https://github.com/segmentio/analytics-react-native/blob/c0a5895c0c57375f18dd20e492b7d984393b7bc4/packages/core/src/types.ts#L293-L299) |
121131
| `autoAddSegmentDestination`| true | Set to false to skip adding the SegmentDestination plugin |
122-
| `storePersistor` | undefined | A custom persistor for the store that `analytics-react-native` leverages. Must match `Persistor` interface exported from [sovran-react-native](https://github.com/segmentio/sovran-react-native).|
132+
| `storePersistor` | undefined | A custom persistor for the store that `analytics-react-native` leverages. Must match [`Persistor`](https://github.com/segmentio/analytics-react-native/blob/master/packages/sovran/src/persistor/persistor.ts#L1-L18) interface exported from [sovran-react-native](https://github.com/segmentio/analytics-react-native/blob/master/packages/sovran).|
123133
| `proxy` | undefined | `proxy` is a batch url to post to instead of 'https://api.segment.io/v1/b'. |
124134
| `errorHandler` | undefined | Create custom actions when errors happen, see [Handling errors](#handling-errors) |
125135

jesttemplate

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { compilerOptions } = require('./tsconfig');
44
module.exports = {
55
preset: 'react-native',
66
roots: ['<rootDir>'],
7-
setupFiles: ['../../core/src/__tests__/__helpers__/setup.js'],
7+
setupFiles: ['../../core/src/__tests__/__helpers__/setup.ts'],
88
testPathIgnorePatterns: ['.../../core/src/__tests__/__helpers__/'],
99
modulePathIgnorePatterns: ['/lib/'],
1010
transform: {

packages/core/jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ module.exports = {
22
preset: 'react-native',
33
testPathIgnorePatterns: ['<rootDir>/src/__tests__/__helpers__/'],
44
modulePathIgnorePatterns: ['/lib/'],
5+
setupFiles: ['<rootDir>/src/__tests__/__helpers__/setup.ts'],
56
};

packages/core/package.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,22 @@
4747
},
4848
"homepage": "https://github.com/segmentio/analytics-react-native#readme",
4949
"dependencies": {
50-
"@react-native-async-storage/async-storage": "^1.15.17",
5150
"@segment/sovran-react-native": "^0.4.5",
5251
"deepmerge": "^4.2.2",
5352
"js-base64": "^3.7.2",
54-
"react-native-get-random-values": "^1.8.0",
5553
"uuid": "^9.0.0"
5654
},
5755
"peerDependencies": {
56+
"react-native-get-random-values": "1.x",
57+
"@react-native-async-storage/async-storage": "1.x",
5858
"react": "*",
5959
"react-native": "*"
6060
},
61+
"peerDependenciesMeta": {
62+
"@react-native-async-storage/async-storage": {
63+
"optional": true
64+
}
65+
},
6166
"devDependencies": {
6267
"@babel/core": "^7.12.9",
6368
"@babel/runtime": "^7.12.5",

packages/core/src/__mocks__/@react-native-async-storage/async-storage.js

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
jest.fn();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// This is our basic setup for all JS Tests
2+
3+
jest.mock('react-native');
4+
jest.mock('uuid');
5+
jest.mock('react-native-get-random-values');
6+
jest.mock('@react-native-async-storage/async-storage', () =>
7+
require('@react-native-async-storage/async-storage/jest/async-storage-mock')
8+
);

packages/core/src/storage/__tests__/sovranStorage.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { createCallbackManager as mockCreateCallbackManager } from '../../__test
44
import { SovranStorage } from '../sovranStorage';
55

66
jest.mock('uuid');
7+
jest.mock('react-native-get-random-values');
78

89
jest.mock('@segment/sovran-react-native', () => ({
910
registerBridgeStore: jest.fn(),

packages/plugins/plugin-adjust/jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { compilerOptions } = require('./tsconfig');
44
module.exports = {
55
preset: 'react-native',
66
roots: ['<rootDir>'],
7-
setupFiles: ['../../core/src/__tests__/__helpers__/setup.js'],
7+
setupFiles: ['../../core/src/__tests__/__helpers__/setup.ts'],
88
testPathIgnorePatterns: ['.../../core/src/__tests__/__helpers__/'],
99
modulePathIgnorePatterns: ['/lib/'],
1010
transform: {

packages/plugins/plugin-advertising-id/jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { compilerOptions } = require('./tsconfig');
44
module.exports = {
55
preset: 'react-native',
66
roots: ['<rootDir>'],
7-
setupFiles: ['../../core/src/__tests__/__helpers__/setup.js'],
7+
setupFiles: ['../../core/src/__tests__/__helpers__/setup.ts'],
88
testPathIgnorePatterns: ['.../../core/src/__tests__/__helpers__/'],
99
modulePathIgnorePatterns: ['/lib/'],
1010
transform: {

packages/plugins/plugin-amplitudeSession/jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { compilerOptions } = require('./tsconfig');
44
module.exports = {
55
preset: 'react-native',
66
roots: ['<rootDir>'],
7-
setupFiles: ['../../core/src/__tests__/__helpers__/setup.js'],
7+
setupFiles: ['../../core/src/__tests__/__helpers__/setup.ts'],
88
testPathIgnorePatterns: ['.../../core/src/__tests__/__helpers__/'],
99
modulePathIgnorePatterns: ['/lib/'],
1010
transform: {

packages/plugins/plugin-appsflyer/jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { compilerOptions } = require('./tsconfig');
44
module.exports = {
55
preset: 'react-native',
66
roots: ['<rootDir>'],
7-
setupFiles: ['../../core/src/__tests__/__helpers__/setup.js'],
7+
setupFiles: ['../../core/src/__tests__/__helpers__/setup.ts'],
88
testPathIgnorePatterns: ['.../../core/src/__tests__/__helpers__/'],
99
modulePathIgnorePatterns: ['/lib/'],
1010
transform: {

packages/plugins/plugin-branch/jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { compilerOptions } = require('./tsconfig');
44
module.exports = {
55
preset: 'react-native',
66
roots: ['<rootDir>'],
7-
setupFiles: ['../../core/src/__tests__/__helpers__/setup.js'],
7+
setupFiles: ['../../core/src/__tests__/__helpers__/setup.ts'],
88
testPathIgnorePatterns: ['.../../core/src/__tests__/__helpers__/'],
99
modulePathIgnorePatterns: ['/lib/'],
1010
transform: {

packages/plugins/plugin-braze-middleware/jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { compilerOptions } = require('./tsconfig');
44
module.exports = {
55
preset: 'react-native',
66
roots: ['<rootDir>'],
7-
setupFiles: ['../../core/src/__tests__/__helpers__/setup.js'],
7+
setupFiles: ['../../core/src/__tests__/__helpers__/setup.ts'],
88
testPathIgnorePatterns: ['.../../core/src/__tests__/__helpers__/'],
99
modulePathIgnorePatterns: ['/lib/'],
1010
transform: {

packages/plugins/plugin-braze/jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { compilerOptions } = require('./tsconfig');
44
module.exports = {
55
preset: 'react-native',
66
roots: ['<rootDir>'],
7-
setupFiles: ['../../core/src/__tests__/__helpers__/setup.js'],
7+
setupFiles: ['../../core/src/__tests__/__helpers__/setup.ts'],
88
testPathIgnorePatterns: ['.../../core/src/__tests__/__helpers__/'],
99
modulePathIgnorePatterns: ['/lib/'],
1010
transform: {

packages/plugins/plugin-destination-filters/jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { compilerOptions } = require('./tsconfig');
44
module.exports = {
55
preset: 'react-native',
66
roots: ['<rootDir>'],
7-
setupFiles: ['../../core/src/__tests__/__helpers__/setup.js'],
7+
setupFiles: ['../../core/src/__tests__/__helpers__/setup.ts'],
88
testPathIgnorePatterns: ['.../../core/src/__tests__/__helpers__/'],
99
modulePathIgnorePatterns: ['/lib/'],
1010
transform: {

packages/plugins/plugin-facebook-app-events/jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { compilerOptions } = require('./tsconfig');
44
module.exports = {
55
preset: 'react-native',
66
roots: ['<rootDir>'],
7-
setupFiles: ['../../core/src/__tests__/__helpers__/setup.js'],
7+
setupFiles: ['../../core/src/__tests__/__helpers__/setup.ts'],
88
testPathIgnorePatterns: ['.../../core/src/__tests__/__helpers__/'],
99
modulePathIgnorePatterns: ['/lib/'],
1010
transform: {

packages/plugins/plugin-firebase/jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { compilerOptions } = require('./tsconfig');
44
module.exports = {
55
preset: 'react-native',
66
roots: ['<rootDir>'],
7-
setupFiles: ['../../core/src/__tests__/__helpers__/setup.js'],
7+
setupFiles: ['../../core/src/__tests__/__helpers__/setup.ts'],
88
testPathIgnorePatterns: ['.../../core/src/__tests__/__helpers__/'],
99
modulePathIgnorePatterns: ['/lib/'],
1010
transform: {

packages/plugins/plugin-idfa/jest.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { compilerOptions } = require('./tsconfig');
44
module.exports = {
55
preset: 'react-native',
66
roots: ['<rootDir>'],
7-
setupFiles: ['../../core/src/__tests__/__helpers__/setup.js'],
7+
setupFiles: ['../../core/src/__tests__/__helpers__/setup.ts'],
88
testPathIgnorePatterns: ['.../../core/src/__tests__/__helpers__/'],
99
modulePathIgnorePatterns: ['/lib/'],
1010
transform: {

packages/sovran/package.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,16 @@
8888
]
8989
},
9090
"peerDependencies": {
91+
"@react-native-async-storage/async-storage": "1.x",
9192
"react": "*",
9293
"react-native": "*"
9394
},
95+
"peerDependenciesMeta": {
96+
"@react-native-async-storage/async-storage": {
97+
"optional": true
98+
}
99+
},
94100
"dependencies": {
95-
"@react-native-async-storage/async-storage": "^1.15.15",
96101
"ansi-regex": "5.0.1",
97102
"deepmerge": "^4.2.2",
98103
"shell-quote": "1.7.3"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export default from '@react-native-async-storage/async-storage/jest/async-storage-mock';
1+
export default '@react-native-async-storage/async-storage/jest/async-storage-mock';

packages/sovran/src/index.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ if (Sovran) {
2020
} else {
2121
console.warn(LINKING_ERROR);
2222
}
23-
export { createStore, Store, Notify, Unsubscribe } from './store';
23+
export {
24+
createStore,
25+
type Store,
26+
type Notify,
27+
type Unsubscribe,
28+
} from './store';
2429
export { registerBridgeStore } from './bridge';
2530
export * from './persistor';

packages/sovran/src/persistor/async-storage-persistor.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
1-
import AsyncStorage from '@react-native-async-storage/async-storage';
21
import type { Persistor } from './persistor';
32

3+
let AsyncStorage: {
4+
getItem: (key: string) => Promise<string | null>;
5+
setItem: (key: string, value: string) => Promise<void>;
6+
} | null;
7+
8+
try {
9+
AsyncStorage = require('@react-native-async-storage/async-storage');
10+
} catch (error) {
11+
AsyncStorage = null;
12+
}
13+
414
/**
515
* Persistor implementation using AsyncStorage
616
*/
717
export const AsyncStoragePersistor: Persistor = {
818
get: async <T>(key: string): Promise<T | undefined> => {
919
try {
10-
const persistedStateJSON = await AsyncStorage.getItem(key);
20+
const persistedStateJSON = await AsyncStorage?.getItem?.(key);
1121
if (persistedStateJSON !== null && persistedStateJSON !== undefined) {
1222
return JSON.parse(persistedStateJSON);
1323
}
@@ -20,7 +30,7 @@ export const AsyncStoragePersistor: Persistor = {
2030

2131
set: async <T>(key: string, state: T): Promise<void> => {
2232
try {
23-
await AsyncStorage.setItem(key, JSON.stringify(state));
33+
await AsyncStorage?.setItem?.(key, JSON.stringify(state));
2434
} catch (e) {
2535
console.error(e);
2636
}

yarn.lock

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2261,7 +2261,7 @@
22612261
dependencies:
22622262
"@octokit/openapi-types" "^12.10.0"
22632263

2264-
"@react-native-async-storage/async-storage@^1.15.15", "@react-native-async-storage/async-storage@^1.15.17":
2264+
"@react-native-async-storage/async-storage@^1.15.15":
22652265
version "1.17.7"
22662266
resolved "https://registry.yarnpkg.com/@react-native-async-storage/async-storage/-/async-storage-1.17.7.tgz#f9213e8cc6202a8c29df5a4da1db588f3b0fa8a9"
22672267
integrity sha512-mDtWhCcpxzrZhA95f6zi0pnBsjBEZW1LKZWfxVXG0UfaWpPxDBCKowNk2xjRTytckZeVhjmPJPtBU+8QNQcR0A==
@@ -6130,11 +6130,6 @@ extract-zip@^1.6.6:
61306130
mkdirp "^0.5.4"
61316131
yauzl "^2.10.0"
61326132

6133-
fast-base64-decode@^1.0.0:
6134-
version "1.0.0"
6135-
resolved "https://registry.yarnpkg.com/fast-base64-decode/-/fast-base64-decode-1.0.0.tgz#b434a0dd7d92b12b43f26819300d2dafb83ee418"
6136-
integrity sha512-qwaScUgUGBYeDNRnbc/KyllVU88Jk1pRHPStuF/lO7B0/RTRLj7U0lkdTAutlBblY08rwZDff6tNU9cjv6j//Q==
6137-
61386133
fast-deep-equal@^3.1.1, fast-deep-equal@^3.1.3:
61396134
version "3.1.3"
61406135
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz#3a7d56b559d6cbc3eb512325244e619a65c6c525"
@@ -10624,13 +10619,6 @@ react-native-fbsdk-next@^10.1.0:
1062410619
"@expo/config-plugins" "^4.1.5"
1062510620
xml2js "^0.4.23"
1062610621

10627-
react-native-get-random-values@^1.8.0:
10628-
version "1.8.0"
10629-
resolved "https://registry.yarnpkg.com/react-native-get-random-values/-/react-native-get-random-values-1.8.0.tgz#1cb4bd4bd3966a356e59697b8f372999fe97cb16"
10630-
integrity sha512-H/zghhun0T+UIJLmig3+ZuBCvF66rdbiWUfRSNS6kv5oDSpa1ZiVyvRWtuPesQpT8dXj+Bv7WJRQOUP+5TB1sA==
10631-
dependencies:
10632-
fast-base64-decode "^1.0.0"
10633-
1063410622
react-native@^0.67.2:
1063510623
version "0.67.4"
1063610624
resolved "https://registry.yarnpkg.com/react-native/-/react-native-0.67.4.tgz#57a1503daa81f66a61f521ff67b259f126d87f9c"

0 commit comments

Comments
 (0)