@evervault/evervault-react-native 0.2.3 → 0.4.2
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/README.md +46 -17
- package/android/build.gradle +5 -3
- package/android/gradle.properties +1 -1
- package/android/src/main/java/com/evervaultsdk/EvervaultSdkModule.kt +34 -5
- package/android/src/main/java/com/evervaultsdk/EvervaultSdkPackage.kt +1 -1
- package/dist/commonjs/components/Card/Card.js +115 -0
- package/dist/commonjs/components/Card/Card.js.map +1 -0
- package/dist/commonjs/components/Card/CardCVC.js +59 -0
- package/dist/commonjs/components/Card/CardCVC.js.map +1 -0
- package/dist/commonjs/components/Card/CardExpiry.js +50 -0
- package/dist/commonjs/components/Card/CardExpiry.js.map +1 -0
- package/dist/commonjs/components/Card/CardHolder.js +45 -0
- package/dist/commonjs/components/Card/CardHolder.js.map +1 -0
- package/dist/commonjs/components/Card/CardNumber.js +68 -0
- package/dist/commonjs/components/Card/CardNumber.js.map +1 -0
- package/dist/commonjs/components/Card/context.js +23 -0
- package/dist/commonjs/components/Card/context.js.map +1 -0
- package/dist/commonjs/components/Card/index.js +13 -0
- package/dist/commonjs/components/Card/index.js.map +1 -0
- package/dist/commonjs/components/Card/types.js +2 -0
- package/dist/commonjs/components/Card/types.js.map +1 -0
- package/dist/commonjs/components/Card/utilities.js +85 -0
- package/dist/commonjs/components/Card/utilities.js.map +1 -0
- package/dist/commonjs/components/useForm.js +116 -0
- package/dist/commonjs/components/useForm.js.map +1 -0
- package/dist/commonjs/index.js +26 -0
- package/dist/commonjs/index.js.map +1 -0
- package/{lib/commonjs/index.js → dist/commonjs/native.js} +3 -10
- package/dist/commonjs/native.js.map +1 -0
- package/dist/commonjs/sdk.js +28 -0
- package/dist/commonjs/sdk.js.map +1 -0
- package/dist/module/components/Card/Card.js +110 -0
- package/dist/module/components/Card/Card.js.map +1 -0
- package/dist/module/components/Card/CardCVC.js +50 -0
- package/dist/module/components/Card/CardCVC.js.map +1 -0
- package/dist/module/components/Card/CardExpiry.js +44 -0
- package/dist/module/components/Card/CardExpiry.js.map +1 -0
- package/dist/module/components/Card/CardHolder.js +39 -0
- package/dist/module/components/Card/CardHolder.js.map +1 -0
- package/dist/module/components/Card/CardNumber.js +62 -0
- package/dist/module/components/Card/CardNumber.js.map +1 -0
- package/dist/module/components/Card/context.js +16 -0
- package/dist/module/components/Card/context.js.map +1 -0
- package/dist/module/components/Card/index.js +2 -0
- package/dist/module/components/Card/index.js.map +1 -0
- package/dist/module/components/Card/types.js +2 -0
- package/dist/module/components/Card/types.js.map +1 -0
- package/dist/module/components/Card/utilities.js +77 -0
- package/dist/module/components/Card/utilities.js.map +1 -0
- package/dist/module/components/useForm.js +110 -0
- package/dist/module/components/useForm.js.map +1 -0
- package/dist/module/index.js +3 -0
- package/dist/module/index.js.map +1 -0
- package/{lib/module/index.js → dist/module/native.js} +2 -8
- package/dist/module/native.js.map +1 -0
- package/dist/module/sdk.js +21 -0
- package/dist/module/sdk.js.map +1 -0
- package/dist/typescript/src/components/Card/Card.d.ts +29 -0
- package/dist/typescript/src/components/Card/Card.d.ts.map +1 -0
- package/dist/typescript/src/components/Card/CardCVC.d.ts +6 -0
- package/dist/typescript/src/components/Card/CardCVC.d.ts.map +1 -0
- package/dist/typescript/src/components/Card/CardExpiry.d.ts +6 -0
- package/dist/typescript/src/components/Card/CardExpiry.d.ts.map +1 -0
- package/dist/typescript/src/components/Card/CardHolder.d.ts +7 -0
- package/dist/typescript/src/components/Card/CardHolder.d.ts.map +1 -0
- package/dist/typescript/src/components/Card/CardNumber.d.ts +8 -0
- package/dist/typescript/src/components/Card/CardNumber.d.ts.map +1 -0
- package/dist/typescript/src/components/Card/context.d.ts +12 -0
- package/dist/typescript/src/components/Card/context.d.ts.map +1 -0
- package/dist/typescript/src/components/Card/index.d.ts +2 -0
- package/dist/typescript/src/components/Card/index.d.ts.map +1 -0
- package/dist/typescript/src/components/Card/types.d.ts +35 -0
- package/dist/typescript/src/components/Card/types.d.ts.map +1 -0
- package/dist/typescript/src/components/Card/utilities.d.ts +7 -0
- package/dist/typescript/src/components/Card/utilities.d.ts.map +1 -0
- package/dist/typescript/src/components/useForm.d.ts +22 -0
- package/dist/typescript/src/components/useForm.d.ts.map +1 -0
- package/dist/typescript/src/index.d.ts +4 -0
- package/dist/typescript/src/index.d.ts.map +1 -0
- package/dist/typescript/src/native.d.ts +2 -0
- package/dist/typescript/src/native.d.ts.map +1 -0
- package/{lib/typescript/src/index.d.ts → dist/typescript/src/sdk.d.ts} +1 -1
- package/dist/typescript/src/sdk.d.ts.map +1 -0
- package/package.json +33 -33
- package/src/components/Card/Card.tsx +155 -0
- package/src/components/Card/CardCVC.tsx +53 -0
- package/src/components/Card/CardExpiry.tsx +48 -0
- package/src/components/Card/CardHolder.tsx +46 -0
- package/src/components/Card/CardNumber.tsx +69 -0
- package/src/components/Card/context.tsx +25 -0
- package/src/components/Card/index.ts +1 -0
- package/src/components/Card/types.ts +52 -0
- package/src/components/Card/utilities.ts +103 -0
- package/src/components/useForm.tsx +172 -0
- package/src/index.tsx +10 -26
- package/src/native.ts +18 -0
- package/src/sdk.ts +25 -0
- package/lib/commonjs/index.js.map +0 -1
- package/lib/module/index.js.map +0 -1
- package/lib/typescript/src/index.d.ts.map +0 -1
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
2
|
+
|
|
3
|
+
export interface UseFormReturn<T> {
|
|
4
|
+
values: T;
|
|
5
|
+
setValue: <K extends keyof T>(field: K, value: T[K]) => void;
|
|
6
|
+
setValues: React.Dispatch<React.SetStateAction<T>>;
|
|
7
|
+
errors: Partial<Record<keyof T, string>> | null;
|
|
8
|
+
setError: <K extends keyof T>(field: K, error: string | undefined) => void;
|
|
9
|
+
isValid: boolean;
|
|
10
|
+
validate: () => void;
|
|
11
|
+
register: <K extends keyof T>(
|
|
12
|
+
name: K
|
|
13
|
+
) => {
|
|
14
|
+
onChange: (value: T[K]) => void;
|
|
15
|
+
//TODO(Mark): Replace with union of React.FocusEvent<HTMLInputElement> and native event
|
|
16
|
+
onBlur: (event: any) => void;
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
interface UseFormOptions<T> {
|
|
21
|
+
initialValues: T;
|
|
22
|
+
validate: Record<keyof T, (values: T) => string | undefined>;
|
|
23
|
+
onChange?: (values: UseFormReturn<T>) => void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function useForm<T extends object>({
|
|
27
|
+
initialValues,
|
|
28
|
+
validate,
|
|
29
|
+
onChange,
|
|
30
|
+
}: UseFormOptions<T>): UseFormReturn<T> {
|
|
31
|
+
const validators = useRef(validate);
|
|
32
|
+
const triggerChange = useRef(false);
|
|
33
|
+
const [values, setValues] = useState<T>(initialValues);
|
|
34
|
+
const [errors, setErrors] = useState<UseFormReturn<T>["errors"]>(
|
|
35
|
+
{} as UseFormReturn<T>["errors"]
|
|
36
|
+
);
|
|
37
|
+
|
|
38
|
+
// keep track of latest validation functions in a ref to prevent
|
|
39
|
+
// re-renders when the function changes.
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
validators.current = validate;
|
|
42
|
+
}, [validate]);
|
|
43
|
+
|
|
44
|
+
const setError = useCallback(
|
|
45
|
+
<K extends keyof T>(field: K, error: string | undefined) => {
|
|
46
|
+
if (!error) {
|
|
47
|
+
setErrors((prev) => {
|
|
48
|
+
if (!prev) return prev;
|
|
49
|
+
return Object.keys(prev).reduce((acc, key) => {
|
|
50
|
+
if (key === field) return acc;
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
...acc,
|
|
54
|
+
[key]: prev?.[key as K],
|
|
55
|
+
};
|
|
56
|
+
}, {});
|
|
57
|
+
});
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
setErrors((prev) => ({
|
|
62
|
+
...prev,
|
|
63
|
+
[field]: error,
|
|
64
|
+
}));
|
|
65
|
+
|
|
66
|
+
triggerChange.current = true;
|
|
67
|
+
},
|
|
68
|
+
[]
|
|
69
|
+
);
|
|
70
|
+
|
|
71
|
+
const setValue = useCallback(
|
|
72
|
+
<K extends keyof T>(field: K, value: T[K]) => {
|
|
73
|
+
if (errors?.[field]) {
|
|
74
|
+
setError(field, undefined);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
setValues((previous) => ({
|
|
78
|
+
...previous,
|
|
79
|
+
[field]: value,
|
|
80
|
+
}));
|
|
81
|
+
|
|
82
|
+
triggerChange.current = true;
|
|
83
|
+
},
|
|
84
|
+
[errors, setError]
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
const isValid = useMemo(
|
|
88
|
+
() => Object.keys(errors ?? {}).length === 0,
|
|
89
|
+
[errors]
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
const validateForm = useCallback(() => {
|
|
93
|
+
const nextErrors = Object.keys(values).reduce((acc, key) => {
|
|
94
|
+
const validator = validators.current?.[key as keyof T];
|
|
95
|
+
if (validator) {
|
|
96
|
+
const error = validator(values);
|
|
97
|
+
if (error) {
|
|
98
|
+
return {
|
|
99
|
+
...acc,
|
|
100
|
+
[key]: error,
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return acc;
|
|
106
|
+
}, {} as Record<keyof T, string>);
|
|
107
|
+
|
|
108
|
+
const hasErrors = Object.keys(nextErrors).length > 0;
|
|
109
|
+
setErrors(hasErrors ? nextErrors : null);
|
|
110
|
+
|
|
111
|
+
// iterate over the errors and if any of them are different then we should
|
|
112
|
+
// trigger a change event.
|
|
113
|
+
const shouldTriggerChange = Object.keys(nextErrors).some((key) => {
|
|
114
|
+
const prevError = errors?.[key as keyof T];
|
|
115
|
+
const nextError = nextErrors[key as keyof T];
|
|
116
|
+
return prevError !== nextError;
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
triggerChange.current = shouldTriggerChange;
|
|
120
|
+
}, [errors, values]);
|
|
121
|
+
|
|
122
|
+
const validateField = useCallback(
|
|
123
|
+
<K extends keyof T>(field: K) => {
|
|
124
|
+
const validator = validators.current?.[field];
|
|
125
|
+
if (validator) {
|
|
126
|
+
const error = validator(values);
|
|
127
|
+
setError(field, error);
|
|
128
|
+
}
|
|
129
|
+
},
|
|
130
|
+
[values, setError]
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
const register = useCallback(
|
|
134
|
+
<K extends keyof T>(name: K) => {
|
|
135
|
+
const handleBlur = () => {
|
|
136
|
+
validateField(name);
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const handleChange = (value: T[K]) => {
|
|
140
|
+
setValue(name, value);
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
onBlur: handleBlur,
|
|
145
|
+
onChange: handleChange,
|
|
146
|
+
};
|
|
147
|
+
},
|
|
148
|
+
[setValue, validateField]
|
|
149
|
+
);
|
|
150
|
+
|
|
151
|
+
const form = useMemo(
|
|
152
|
+
() => ({
|
|
153
|
+
values,
|
|
154
|
+
setValue,
|
|
155
|
+
errors,
|
|
156
|
+
setError,
|
|
157
|
+
isValid,
|
|
158
|
+
register,
|
|
159
|
+
setValues,
|
|
160
|
+
validate: validateForm,
|
|
161
|
+
}),
|
|
162
|
+
[values, setValue, errors, setError, isValid, register, validateForm]
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
useEffect(() => {
|
|
166
|
+
if (!triggerChange.current) return;
|
|
167
|
+
triggerChange.current = false;
|
|
168
|
+
onChange?.(form);
|
|
169
|
+
}, [form, onChange]);
|
|
170
|
+
|
|
171
|
+
return form;
|
|
172
|
+
}
|
package/src/index.tsx
CHANGED
|
@@ -1,26 +1,10 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
: new Proxy(
|
|
12
|
-
{},
|
|
13
|
-
{
|
|
14
|
-
get() {
|
|
15
|
-
throw new Error(LINKING_ERROR);
|
|
16
|
-
},
|
|
17
|
-
}
|
|
18
|
-
);
|
|
19
|
-
|
|
20
|
-
export async function init(teamUuid: string, appUuid: string): Promise<void> {
|
|
21
|
-
return EvervaultSdk.initialize(teamUuid, appUuid);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
export async function encrypt(data: any): Promise<string> {
|
|
25
|
-
return EvervaultSdk.encrypt(data);
|
|
26
|
-
}
|
|
1
|
+
export { type CardProps, Card } from './components/Card';
|
|
2
|
+
export { init, encrypt } from './sdk';
|
|
3
|
+
export type {
|
|
4
|
+
CardPayload,
|
|
5
|
+
CardExpiry,
|
|
6
|
+
CardBrandName,
|
|
7
|
+
CardConfig,
|
|
8
|
+
CardForm,
|
|
9
|
+
CardField,
|
|
10
|
+
} from './components/Card/types';
|
package/src/native.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { NativeModules, Platform } from 'react-native';
|
|
2
|
+
|
|
3
|
+
const LINKING_ERROR =
|
|
4
|
+
`The package '@evervault/evervault-react-native' doesn't seem to be linked. Make sure: \n\n` +
|
|
5
|
+
Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
|
|
6
|
+
'- You rebuilt the app after installing the package\n' +
|
|
7
|
+
'- You are not using Expo Go\n';
|
|
8
|
+
|
|
9
|
+
export const EvervaultSdk = NativeModules.EvervaultSdk
|
|
10
|
+
? NativeModules.EvervaultSdk
|
|
11
|
+
: new Proxy(
|
|
12
|
+
{},
|
|
13
|
+
{
|
|
14
|
+
get() {
|
|
15
|
+
throw new Error(LINKING_ERROR);
|
|
16
|
+
},
|
|
17
|
+
}
|
|
18
|
+
);
|
package/src/sdk.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Platform } from 'react-native';
|
|
2
|
+
import { EvervaultSdk } from './native';
|
|
3
|
+
|
|
4
|
+
export async function init(teamUuid: string, appUuid: string): Promise<void> {
|
|
5
|
+
if (!teamUuid.startsWith('team_')) {
|
|
6
|
+
throw new Error("Invalid Evervault Team UUID");
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
if (!appUuid.startsWith('app_')) {
|
|
10
|
+
throw new Error("Invalid Evervault App UUID");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
return EvervaultSdk.initialize(teamUuid, appUuid);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export async function encrypt(data: any): Promise<string> {
|
|
17
|
+
if (Platform.OS === 'android' && typeof data !== 'string') {
|
|
18
|
+
throw new Error(`The Evervault SDK does not currently support encrypting non-string data on Android.
|
|
19
|
+
If this is required for your use case, please get in touch with us at support@evervault.com. Please do not
|
|
20
|
+
serialize other data types to strings and pass them to this method as this will result in the data
|
|
21
|
+
type being returned in it's stringified form when decryption occurs.`);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return EvervaultSdk.encrypt(data);
|
|
25
|
+
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":["_reactNative","require","LINKING_ERROR","Platform","select","ios","default","EvervaultSdk","NativeModules","Proxy","get","Error","init","teamUuid","appUuid","initialize","encrypt","data"],"sourceRoot":"../../src","sources":["index.tsx"],"mappings":";;;;;;;AAAA,IAAAA,YAAA,GAAAC,OAAA;AAEA,MAAMC,aAAa,GAChB,4FAA2F,GAC5FC,qBAAQ,CAACC,MAAM,CAAC;EAAEC,GAAG,EAAE,gCAAgC;EAAEC,OAAO,EAAE;AAAG,CAAC,CAAC,GACvE,sDAAsD,GACtD,+BAA+B;AAEjC,MAAMC,YAAY,GAAGC,0BAAa,CAACD,YAAY,GAC3CC,0BAAa,CAACD,YAAY,GAC1B,IAAIE,KAAK,CACP,CAAC,CAAC,EACF;EACEC,GAAGA,CAAA,EAAG;IACJ,MAAM,IAAIC,KAAK,CAACT,aAAa,CAAC;EAChC;AACF,CACF,CAAC;AAEE,eAAeU,IAAIA,CAACC,QAAgB,EAAEC,OAAe,EAAiB;EAC3E,OAAOP,YAAY,CAACQ,UAAU,CAACF,QAAQ,EAAEC,OAAO,CAAC;AACnD;AAEO,eAAeE,OAAOA,CAACC,IAAS,EAAmB;EACxD,OAAOV,YAAY,CAACS,OAAO,CAACC,IAAI,CAAC;AACnC"}
|
package/lib/module/index.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"names":["NativeModules","Platform","LINKING_ERROR","select","ios","default","EvervaultSdk","Proxy","get","Error","init","teamUuid","appUuid","initialize","encrypt","data"],"sourceRoot":"../../src","sources":["index.tsx"],"mappings":"AAAA,SAASA,aAAa,EAAEC,QAAQ,QAAQ,cAAc;AAEtD,MAAMC,aAAa,GAChB,4FAA2F,GAC5FD,QAAQ,CAACE,MAAM,CAAC;EAAEC,GAAG,EAAE,gCAAgC;EAAEC,OAAO,EAAE;AAAG,CAAC,CAAC,GACvE,sDAAsD,GACtD,+BAA+B;AAEjC,MAAMC,YAAY,GAAGN,aAAa,CAACM,YAAY,GAC3CN,aAAa,CAACM,YAAY,GAC1B,IAAIC,KAAK,CACP,CAAC,CAAC,EACF;EACEC,GAAGA,CAAA,EAAG;IACJ,MAAM,IAAIC,KAAK,CAACP,aAAa,CAAC;EAChC;AACF,CACF,CAAC;AAEL,OAAO,eAAeQ,IAAIA,CAACC,QAAgB,EAAEC,OAAe,EAAiB;EAC3E,OAAON,YAAY,CAACO,UAAU,CAACF,QAAQ,EAAEC,OAAO,CAAC;AACnD;AAEA,OAAO,eAAeE,OAAOA,CAACC,IAAS,EAAmB;EACxD,OAAOT,YAAY,CAACQ,OAAO,CAACC,IAAI,CAAC;AACnC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.tsx"],"names":[],"mappings":"AAmBA,wBAAsB,IAAI,CAAC,QAAQ,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAE3E;AAED,wBAAsB,OAAO,CAAC,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC,MAAM,CAAC,CAExD"}
|