@pinwheel/react-native-pinwheel 2.3.0 → 2.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -0
- package/lib/index.d.ts +66 -0
- package/lib/index.js +124 -0
- package/package.json +1 -1
- package/lib/package.json +0 -26
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Change Log
|
|
2
|
+
|
|
3
|
+
All notable changes to this project will be documented in this file.
|
|
4
|
+
|
|
5
|
+
## 2.3.x Releases
|
|
6
|
+
|
|
7
|
+
- `2.3.x` Releases - [2.3.4](#234)
|
|
8
|
+
|
|
9
|
+
---
|
|
10
|
+
|
|
11
|
+
### 2.3.4
|
|
12
|
+
|
|
13
|
+
#### Added
|
|
14
|
+
|
|
15
|
+
- Export `EventPayload` type.
|
|
16
|
+
- Export `PinwheelError` type.
|
|
17
|
+
- Export `PinwheelErrorType` type.
|
|
18
|
+
- Export `EmptyPayloadObject` type as `Record<string, never>`.
|
|
19
|
+
|
|
20
|
+
##### Updated
|
|
21
|
+
|
|
22
|
+
- `EventPayload` is no longer a union containing `{}`. It now contains `Record<string, never>` instead due to [this behavior](https://github.com/Microsoft/TypeScript/wiki/FAQ#why-are-all-types-assignable-to-empty-interfaces).
|
|
23
|
+
- Mark exported `Error` type as **@deprecated** because it collides with the built-in javascript `Error` object. Replaced with `PinwheelError`.
|
|
24
|
+
- Mark exported `ErrorType` type as **@deprecated** because the new naming convention is prefixing with "`PinwheelError`". Replaced with `PinwheelErrorType`.
|
|
25
|
+
- Update `onExit` type to be `(error: PinwheelError | Record<string, never>)` to be accurate with current functionality.
|
package/lib/index.d.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
export declare const PINWHEEL_MESSAGE_TYPES: {
|
|
3
|
+
PINWHEEL_EXIT: string;
|
|
4
|
+
PINWHEEL_MODAL_CLOSE: string;
|
|
5
|
+
PINWHEEL_LOAD_COMPLETE: string;
|
|
6
|
+
PINWHEEL_SUCCESS: string;
|
|
7
|
+
PINWHEEL_EVENT: string;
|
|
8
|
+
};
|
|
9
|
+
export declare type LinkResult = {
|
|
10
|
+
accountId: string;
|
|
11
|
+
platformId: string;
|
|
12
|
+
job: string;
|
|
13
|
+
params: {
|
|
14
|
+
amount?: {
|
|
15
|
+
value: number;
|
|
16
|
+
unit: '%' | '$';
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* @deprecated This type will be removed in version 2.4. Use the renamed type `PinwheelErrorType`
|
|
22
|
+
* instead.
|
|
23
|
+
*/
|
|
24
|
+
export declare type ErrorType = 'clientError' | 'systemError' | 'userActionRequired' | 'platformError' | 'invalidAccountsConfiguration' | 'invalidUserInput' | 'invalidLinkToken';
|
|
25
|
+
export declare type PinwheelErrorType = ErrorType;
|
|
26
|
+
/**
|
|
27
|
+
* @deprecated The type should not be used as it clashes with the native JS `Error` object.
|
|
28
|
+
* You should use `PinwheelError` instead. `Error` will be removed in version 2.4
|
|
29
|
+
*/
|
|
30
|
+
export declare type Error = {
|
|
31
|
+
type: PinwheelErrorType;
|
|
32
|
+
code: string;
|
|
33
|
+
message: string;
|
|
34
|
+
pendingRetry: boolean;
|
|
35
|
+
};
|
|
36
|
+
export declare type PinwheelError = Error;
|
|
37
|
+
export declare type EmptyPayloadObject = Record<string, never>;
|
|
38
|
+
export declare type EventPayload = {
|
|
39
|
+
selectedEmployerId: string;
|
|
40
|
+
selectedEmployerName: string;
|
|
41
|
+
} | {
|
|
42
|
+
selectedPlatformId: string;
|
|
43
|
+
selectedPlatformName: string;
|
|
44
|
+
} | {
|
|
45
|
+
value: number;
|
|
46
|
+
unit: '%' | '$';
|
|
47
|
+
} | LinkResult | {
|
|
48
|
+
accountId: string;
|
|
49
|
+
platformId: string;
|
|
50
|
+
} | PinwheelError | EmptyPayloadObject | undefined;
|
|
51
|
+
declare type PinwheelProps = {
|
|
52
|
+
linkToken: string;
|
|
53
|
+
onLogin?: (result: {
|
|
54
|
+
accountId: string;
|
|
55
|
+
platformId: string;
|
|
56
|
+
}) => void;
|
|
57
|
+
onLoginAttempt?: (result: {
|
|
58
|
+
platformId: string;
|
|
59
|
+
}) => void;
|
|
60
|
+
onSuccess?: (result: LinkResult) => void;
|
|
61
|
+
onError?: (error: PinwheelError) => void;
|
|
62
|
+
onExit?: (error: PinwheelError | EmptyPayloadObject) => void;
|
|
63
|
+
onEvent?: (eventName: string, payload: EventPayload) => void;
|
|
64
|
+
};
|
|
65
|
+
declare const _default: ({ linkToken, onLogin, onLoginAttempt, onSuccess, onError, onExit, onEvent }: PinwheelProps) => JSX.Element;
|
|
66
|
+
export default _default;
|
package/lib/index.js
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { WebView } from 'react-native-webview';
|
|
3
|
+
import { Linking, Platform, SafeAreaView, StyleSheet } from 'react-native';
|
|
4
|
+
const version = '2.3.4';
|
|
5
|
+
const styles = StyleSheet.create({
|
|
6
|
+
container: {
|
|
7
|
+
flex: 1,
|
|
8
|
+
},
|
|
9
|
+
});
|
|
10
|
+
export const PINWHEEL_MESSAGE_TYPES = {
|
|
11
|
+
PINWHEEL_EXIT: 'PINWHEEL_EXIT',
|
|
12
|
+
PINWHEEL_MODAL_CLOSE: 'PINWHEEL_MODAL_CLOSE',
|
|
13
|
+
PINWHEEL_LOAD_COMPLETE: 'PINWHEEL_LOAD_COMPLETE',
|
|
14
|
+
PINWHEEL_SUCCESS: 'PINWHEEL_SUCCESS',
|
|
15
|
+
PINWHEEL_EVENT: 'PINWHEEL_EVENT',
|
|
16
|
+
};
|
|
17
|
+
const LINK_PAGE_URL = 'https://cdn.getpinwheel.com/link-v2.3.0.html';
|
|
18
|
+
const PINWHEEL_DOMAIN = 'getpinwheel.com';
|
|
19
|
+
export default ({ linkToken, onLogin, onLoginAttempt, onSuccess, onError, onExit, onEvent }) => {
|
|
20
|
+
const handleEvent = (event) => {
|
|
21
|
+
if (!event) {
|
|
22
|
+
// first event is always an empty string
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
let eventData;
|
|
26
|
+
try {
|
|
27
|
+
eventData = JSON.parse(event.nativeEvent.data);
|
|
28
|
+
}
|
|
29
|
+
catch (_error) {
|
|
30
|
+
let error = _error;
|
|
31
|
+
console.error(error);
|
|
32
|
+
onExit && onExit(error);
|
|
33
|
+
onError && onError(error);
|
|
34
|
+
onEvent && onEvent('error', error);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
const { type, eventName, payload } = eventData;
|
|
38
|
+
if (type === 'PINWHEEL_EVENT') {
|
|
39
|
+
onEvent && onEvent(eventName, payload);
|
|
40
|
+
switch (eventName) {
|
|
41
|
+
case 'exit':
|
|
42
|
+
onExit && onExit(payload);
|
|
43
|
+
break;
|
|
44
|
+
case 'success':
|
|
45
|
+
onSuccess && onSuccess(payload);
|
|
46
|
+
break;
|
|
47
|
+
case 'login':
|
|
48
|
+
onLogin && onLogin(payload);
|
|
49
|
+
break;
|
|
50
|
+
case 'login_attempt':
|
|
51
|
+
onLoginAttempt && onLoginAttempt(payload);
|
|
52
|
+
break;
|
|
53
|
+
case 'error':
|
|
54
|
+
onError && onError(payload);
|
|
55
|
+
break;
|
|
56
|
+
default:
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
const now = Date.now();
|
|
61
|
+
const [major, minor, patch] = version.split('.').map(x => Number(x));
|
|
62
|
+
const runFirst = `
|
|
63
|
+
const uuidKey = 'pinwheel-uuid';
|
|
64
|
+
const localStorage = window.localStorage;
|
|
65
|
+
let uuid = localStorage.getItem(uuidKey);
|
|
66
|
+
if(!uuid) {
|
|
67
|
+
uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
|
|
68
|
+
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
|
|
69
|
+
return v.toString(16);
|
|
70
|
+
});
|
|
71
|
+
localStorage.setItem(uuidKey, uuid);
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
window.addEventListener('message', event => {
|
|
75
|
+
window.ReactNativeWebView.postMessage(JSON.stringify(event.data))
|
|
76
|
+
});
|
|
77
|
+
window.postMessage(
|
|
78
|
+
{
|
|
79
|
+
type: 'PINWHEEL_INIT',
|
|
80
|
+
payload: {
|
|
81
|
+
platform: "${Platform.OS}",
|
|
82
|
+
sdk: 'react native',
|
|
83
|
+
version: {
|
|
84
|
+
major: ${major},
|
|
85
|
+
minor: ${minor},
|
|
86
|
+
patch: ${patch}
|
|
87
|
+
},
|
|
88
|
+
initializationOptions: {
|
|
89
|
+
hasOnSuccess: ${!!onSuccess},
|
|
90
|
+
hasOnEvent: ${!!onEvent},
|
|
91
|
+
hasOnExit: ${!!onExit},
|
|
92
|
+
hasOnError: ${!!onError},
|
|
93
|
+
hasOnLogin: ${!!onLogin},
|
|
94
|
+
},
|
|
95
|
+
linkToken: '${linkToken}',
|
|
96
|
+
uniqueUserId: uuid,
|
|
97
|
+
initializationTimestamp: ${now}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
);
|
|
101
|
+
} catch (err) {
|
|
102
|
+
console.error(err);
|
|
103
|
+
}
|
|
104
|
+
true;
|
|
105
|
+
`;
|
|
106
|
+
return (<SafeAreaView style={styles.container}>
|
|
107
|
+
<WebView source={{ uri: LINK_PAGE_URL }} onMessage={handleEvent} injectedJavaScript={runFirst} onShouldStartLoadWithRequest={(request) => {
|
|
108
|
+
const targetURL = request.url;
|
|
109
|
+
const isLinkPage = targetURL.includes(PINWHEEL_DOMAIN);
|
|
110
|
+
if (!isLinkPage) {
|
|
111
|
+
Linking.canOpenURL(targetURL).then(supported => {
|
|
112
|
+
if (supported) {
|
|
113
|
+
Linking.openURL(targetURL).then(() => { });
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
console.warn('Don\'t know how to open URL: ' + targetURL);
|
|
117
|
+
}
|
|
118
|
+
return false;
|
|
119
|
+
}).catch(err => console.error('An error occurred ', err));
|
|
120
|
+
}
|
|
121
|
+
return isLinkPage;
|
|
122
|
+
}}/>
|
|
123
|
+
</SafeAreaView>);
|
|
124
|
+
};
|
package/package.json
CHANGED
package/lib/package.json
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "@pinwheel/react-native-pinwheel",
|
|
3
|
-
"version": "2.0.5",
|
|
4
|
-
"type": "module",
|
|
5
|
-
"description": "Pinwheel React Native SDK",
|
|
6
|
-
"main": "lib/src/index.js",
|
|
7
|
-
"types": "lib/src/index.d.ts",
|
|
8
|
-
"files": [
|
|
9
|
-
"lib/**/*"
|
|
10
|
-
],
|
|
11
|
-
"scripts": {
|
|
12
|
-
"build": "tsc"
|
|
13
|
-
},
|
|
14
|
-
"author": "Pinwheel",
|
|
15
|
-
"license": "MIT",
|
|
16
|
-
"peerDependencies": {
|
|
17
|
-
"react": "16.13.1",
|
|
18
|
-
"react-native": "0.63.3",
|
|
19
|
-
"react-native-webview": "^10.10.0"
|
|
20
|
-
},
|
|
21
|
-
"devDependencies": {
|
|
22
|
-
"@types/react-native": "^0.63.29",
|
|
23
|
-
"react-native-webview": "^10.9.2",
|
|
24
|
-
"typescript": "^4.0.3"
|
|
25
|
-
}
|
|
26
|
-
}
|