Skip to content

fix: async-storage as optional peer dependency #779

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

Merged
merged 1 commit into from
Mar 10, 2023
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
18 changes: 14 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,24 @@ The hassle-free way to add Segment analytics to your React-Native app.

## Installation

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):
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):

```sh
yarn add @segment/analytics-react-native @segment/sovran-react-native @react-native-async-storage/async-storage
yarn add @segment/analytics-react-native @segment/sovran-react-native react-native-get-random-values
# or
npm install --save @segment/analytics-react-native @segment/sovran-react-native @react-native-async-storage/async-storage
npm install --save @segment/analytics-react-native @segment/sovran-react-native react-native-get-random-values
```

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).

```sh
yarn add @react-native-async-storage/async-storage
# or
npm install --save @react-native-async-storage/async-storage
```

*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)*

For iOS, install native modules with:

```sh
Expand Down Expand Up @@ -119,7 +129,7 @@ You must pass at least the `writeKey`. Additional configuration options are list
| `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)) |
| `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) |
| `autoAddSegmentDestination`| true | Set to false to skip adding the SegmentDestination plugin |
| `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).|
| `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).|
| `proxy` | undefined | `proxy` is a batch url to post to instead of 'https://api.segment.io/v1/b'. |
| `errorHandler` | undefined | Create custom actions when errors happen, see [Handling errors](#handling-errors) |

Expand Down
2 changes: 1 addition & 1 deletion jesttemplate
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { compilerOptions } = require('./tsconfig');
module.exports = {
preset: 'react-native',
roots: ['<rootDir>'],
setupFiles: ['../../core/src/__tests__/__helpers__/setup.js'],
setupFiles: ['../../core/src/__tests__/__helpers__/setup.ts'],
testPathIgnorePatterns: ['.../../core/src/__tests__/__helpers__/'],
modulePathIgnorePatterns: ['/lib/'],
transform: {
Expand Down
1 change: 1 addition & 0 deletions packages/core/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ module.exports = {
preset: 'react-native',
testPathIgnorePatterns: ['<rootDir>/src/__tests__/__helpers__/'],
modulePathIgnorePatterns: ['/lib/'],
setupFiles: ['<rootDir>/src/__tests__/__helpers__/setup.ts'],
};
9 changes: 7 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,22 @@
},
"homepage": "https://github.com/segmentio/analytics-react-native#readme",
"dependencies": {
"@react-native-async-storage/async-storage": "^1.15.17",
"@segment/sovran-react-native": "^0.4.5",
"deepmerge": "^4.2.2",
"js-base64": "^3.7.2",
"react-native-get-random-values": "^1.8.0",
"uuid": "^9.0.0"
},
"peerDependencies": {
"react-native-get-random-values": "1.x",
"@react-native-async-storage/async-storage": "1.x",
"react": "*",
"react-native": "*"
},
"peerDependenciesMeta": {
"@react-native-async-storage/async-storage": {
"optional": true
}
},
"devDependencies": {
"@babel/core": "^7.12.9",
"@babel/runtime": "^7.12.5",
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
jest.fn();
8 changes: 8 additions & 0 deletions packages/core/src/__tests__/__helpers__/setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// This is our basic setup for all JS Tests

jest.mock('react-native');
jest.mock('uuid');
jest.mock('react-native-get-random-values');
jest.mock('@react-native-async-storage/async-storage', () =>
require('@react-native-async-storage/async-storage/jest/async-storage-mock')
);
1 change: 1 addition & 0 deletions packages/core/src/storage/__tests__/sovranStorage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { createCallbackManager as mockCreateCallbackManager } from '../../__test
import { SovranStorage } from '../sovranStorage';

jest.mock('uuid');
jest.mock('react-native-get-random-values');

jest.mock('@segment/sovran-react-native', () => ({
registerBridgeStore: jest.fn(),
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/plugin-adjust/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { compilerOptions } = require('./tsconfig');
module.exports = {
preset: 'react-native',
roots: ['<rootDir>'],
setupFiles: ['../../core/src/__tests__/__helpers__/setup.js'],
setupFiles: ['../../core/src/__tests__/__helpers__/setup.ts'],
testPathIgnorePatterns: ['.../../core/src/__tests__/__helpers__/'],
modulePathIgnorePatterns: ['/lib/'],
transform: {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/plugin-advertising-id/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { compilerOptions } = require('./tsconfig');
module.exports = {
preset: 'react-native',
roots: ['<rootDir>'],
setupFiles: ['../../core/src/__tests__/__helpers__/setup.js'],
setupFiles: ['../../core/src/__tests__/__helpers__/setup.ts'],
testPathIgnorePatterns: ['.../../core/src/__tests__/__helpers__/'],
modulePathIgnorePatterns: ['/lib/'],
transform: {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/plugin-amplitudeSession/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { compilerOptions } = require('./tsconfig');
module.exports = {
preset: 'react-native',
roots: ['<rootDir>'],
setupFiles: ['../../core/src/__tests__/__helpers__/setup.js'],
setupFiles: ['../../core/src/__tests__/__helpers__/setup.ts'],
testPathIgnorePatterns: ['.../../core/src/__tests__/__helpers__/'],
modulePathIgnorePatterns: ['/lib/'],
transform: {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/plugin-appsflyer/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { compilerOptions } = require('./tsconfig');
module.exports = {
preset: 'react-native',
roots: ['<rootDir>'],
setupFiles: ['../../core/src/__tests__/__helpers__/setup.js'],
setupFiles: ['../../core/src/__tests__/__helpers__/setup.ts'],
testPathIgnorePatterns: ['.../../core/src/__tests__/__helpers__/'],
modulePathIgnorePatterns: ['/lib/'],
transform: {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/plugin-branch/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { compilerOptions } = require('./tsconfig');
module.exports = {
preset: 'react-native',
roots: ['<rootDir>'],
setupFiles: ['../../core/src/__tests__/__helpers__/setup.js'],
setupFiles: ['../../core/src/__tests__/__helpers__/setup.ts'],
testPathIgnorePatterns: ['.../../core/src/__tests__/__helpers__/'],
modulePathIgnorePatterns: ['/lib/'],
transform: {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/plugin-braze-middleware/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { compilerOptions } = require('./tsconfig');
module.exports = {
preset: 'react-native',
roots: ['<rootDir>'],
setupFiles: ['../../core/src/__tests__/__helpers__/setup.js'],
setupFiles: ['../../core/src/__tests__/__helpers__/setup.ts'],
testPathIgnorePatterns: ['.../../core/src/__tests__/__helpers__/'],
modulePathIgnorePatterns: ['/lib/'],
transform: {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/plugin-braze/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { compilerOptions } = require('./tsconfig');
module.exports = {
preset: 'react-native',
roots: ['<rootDir>'],
setupFiles: ['../../core/src/__tests__/__helpers__/setup.js'],
setupFiles: ['../../core/src/__tests__/__helpers__/setup.ts'],
testPathIgnorePatterns: ['.../../core/src/__tests__/__helpers__/'],
modulePathIgnorePatterns: ['/lib/'],
transform: {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/plugin-destination-filters/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { compilerOptions } = require('./tsconfig');
module.exports = {
preset: 'react-native',
roots: ['<rootDir>'],
setupFiles: ['../../core/src/__tests__/__helpers__/setup.js'],
setupFiles: ['../../core/src/__tests__/__helpers__/setup.ts'],
testPathIgnorePatterns: ['.../../core/src/__tests__/__helpers__/'],
modulePathIgnorePatterns: ['/lib/'],
transform: {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/plugin-facebook-app-events/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { compilerOptions } = require('./tsconfig');
module.exports = {
preset: 'react-native',
roots: ['<rootDir>'],
setupFiles: ['../../core/src/__tests__/__helpers__/setup.js'],
setupFiles: ['../../core/src/__tests__/__helpers__/setup.ts'],
testPathIgnorePatterns: ['.../../core/src/__tests__/__helpers__/'],
modulePathIgnorePatterns: ['/lib/'],
transform: {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/plugin-firebase/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { compilerOptions } = require('./tsconfig');
module.exports = {
preset: 'react-native',
roots: ['<rootDir>'],
setupFiles: ['../../core/src/__tests__/__helpers__/setup.js'],
setupFiles: ['../../core/src/__tests__/__helpers__/setup.ts'],
testPathIgnorePatterns: ['.../../core/src/__tests__/__helpers__/'],
modulePathIgnorePatterns: ['/lib/'],
transform: {
Expand Down
2 changes: 1 addition & 1 deletion packages/plugins/plugin-idfa/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const { compilerOptions } = require('./tsconfig');
module.exports = {
preset: 'react-native',
roots: ['<rootDir>'],
setupFiles: ['../../core/src/__tests__/__helpers__/setup.js'],
setupFiles: ['../../core/src/__tests__/__helpers__/setup.ts'],
testPathIgnorePatterns: ['.../../core/src/__tests__/__helpers__/'],
modulePathIgnorePatterns: ['/lib/'],
transform: {
Expand Down
7 changes: 6 additions & 1 deletion packages/sovran/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,16 @@
]
},
"peerDependencies": {
"@react-native-async-storage/async-storage": "1.x",
"react": "*",
"react-native": "*"
},
"peerDependenciesMeta": {
"@react-native-async-storage/async-storage": {
"optional": true
}
},
"dependencies": {
"@react-native-async-storage/async-storage": "^1.15.15",
"ansi-regex": "5.0.1",
"deepmerge": "^4.2.2",
"shell-quote": "1.7.3"
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export default from '@react-native-async-storage/async-storage/jest/async-storage-mock';
export default '@react-native-async-storage/async-storage/jest/async-storage-mock';
7 changes: 6 additions & 1 deletion packages/sovran/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ if (Sovran) {
} else {
console.warn(LINKING_ERROR);
}
export { createStore, Store, Notify, Unsubscribe } from './store';
export {
createStore,
type Store,
type Notify,
type Unsubscribe,
} from './store';
export { registerBridgeStore } from './bridge';
export * from './persistor';
16 changes: 13 additions & 3 deletions packages/sovran/src/persistor/async-storage-persistor.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import AsyncStorage from '@react-native-async-storage/async-storage';
import type { Persistor } from './persistor';

let AsyncStorage: {
getItem: (key: string) => Promise<string | null>;
setItem: (key: string, value: string) => Promise<void>;
} | null;

try {
AsyncStorage = require('@react-native-async-storage/async-storage');
} catch (error) {
AsyncStorage = null;
}

/**
* Persistor implementation using AsyncStorage
*/
export const AsyncStoragePersistor: Persistor = {
get: async <T>(key: string): Promise<T | undefined> => {
try {
const persistedStateJSON = await AsyncStorage.getItem(key);
const persistedStateJSON = await AsyncStorage?.getItem?.(key);
if (persistedStateJSON !== null && persistedStateJSON !== undefined) {
return JSON.parse(persistedStateJSON);
}
Expand All @@ -20,7 +30,7 @@ export const AsyncStoragePersistor: Persistor = {

set: async <T>(key: string, state: T): Promise<void> => {
try {
await AsyncStorage.setItem(key, JSON.stringify(state));
await AsyncStorage?.setItem?.(key, JSON.stringify(state));
} catch (e) {
console.error(e);
}
Expand Down
14 changes: 1 addition & 13 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2261,7 +2261,7 @@
dependencies:
"@octokit/openapi-types" "^12.10.0"

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

fast-base64-decode@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/fast-base64-decode/-/fast-base64-decode-1.0.0.tgz#b434a0dd7d92b12b43f26819300d2dafb83ee418"
integrity sha512-qwaScUgUGBYeDNRnbc/KyllVU88Jk1pRHPStuF/lO7B0/RTRLj7U0lkdTAutlBblY08rwZDff6tNU9cjv6j//Q==

fast-deep-equal@^3.1.1, fast-deep-equal@^3.1.3:
version "3.1.3"
resolved "https://registry.yarnpkg.com/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz#3a7d56b559d6cbc3eb512325244e619a65c6c525"
Expand Down Expand Up @@ -10624,13 +10619,6 @@ react-native-fbsdk-next@^10.1.0:
"@expo/config-plugins" "^4.1.5"
xml2js "^0.4.23"

react-native-get-random-values@^1.8.0:
version "1.8.0"
resolved "https://registry.yarnpkg.com/react-native-get-random-values/-/react-native-get-random-values-1.8.0.tgz#1cb4bd4bd3966a356e59697b8f372999fe97cb16"
integrity sha512-H/zghhun0T+UIJLmig3+ZuBCvF66rdbiWUfRSNS6kv5oDSpa1ZiVyvRWtuPesQpT8dXj+Bv7WJRQOUP+5TB1sA==
dependencies:
fast-base64-decode "^1.0.0"

react-native@^0.67.2:
version "0.67.4"
resolved "https://registry.yarnpkg.com/react-native/-/react-native-0.67.4.tgz#57a1503daa81f66a61f521ff67b259f126d87f9c"
Expand Down