-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
117 lines (101 loc) · 3.45 KB
/
Copy pathindex.ts
File metadata and controls
117 lines (101 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import {NativeModules} from "react-native";
import {OneError} from "./src/error";
import {ONECore, interfaceMethodNames, GlobalSettings} from "./src/core";
import * as ubiqu from "./src/ubiqu";
export * from "./src/core";
export * from "./src/error";
export * from "./src/nfc";
/** @hidden */
export const Ubiqu = ubiqu;
/** @hidden */
export declare namespace Ubiqu {
export type PinEventType = ubiqu.PinEventType;
export type PinFlowType = ubiqu.PinFlowType;
export type PinFlowStage = ubiqu.PinFlowStage;
}
const ONE = NativeModules.ProcivisOneCoreModule;
// New Architecture bridgeless issue workaround: see https://github.com/facebook/react-native/issues/43221
interfaceMethodNames.forEach((method) => ONE?.[method]);
// Config entities are exposed as serialized JSON, here conversion to structs
const originalGetConfig: () => Promise<
Record<
string /* entity type */,
| Record<string /* entity identifier */, string /* json */>
> & { "globalSettings": GlobalSettings }
> = ONE?.getConfig;
if (originalGetConfig) {
ONE.getConfig = () =>
originalGetConfig().then((config) => {
const {globalSettings, ...providers} = config;
const parsed = objectMap(providers, (entityValue) => objectMap(entityValue, (json) => JSON.parse(json)));
return {
globalSettings,
...parsed
}
});
}
/**
* Initialize ONE Core
* @note Beware that only one instance can be initialized at a time, repeated calls will fail
* @param config one-core configuration
* @returns ONE Core instance
*/
export async function initializeCore(
config: Record<string, unknown> = {},
): Promise<ONECore> {
await wrapFn(ONE.initialize, "initializeCore")(JSON.stringify(config));
return wrapObj(ONE);
}
// UTILS
// returns a new object with the values at each key mapped using fn(value)
const objectMap = <Source, Result>(
obj: Record<string, Source>,
fn: (value: Source) => Result,
): Record<string, Result> =>
Object.fromEntries(Object.entries(obj).map(([k, v]) => [k, fn(v)]));
// Function call arguments/Error transformation
// for devs: Beware to not declare function parameters as optional, otherwise automatic conversion to null will not be performed
function wrapFn<Fn extends (...args: any[]) => Promise<any>>(
fn: Fn,
operation: string,
) {
return (...args: any[]) => {
const errHandler = (e: unknown) => {
if (e instanceof Error && "code" in e) {
const userInfo =
"userInfo" in e && typeof e.userInfo === "object"
? e.userInfo
: undefined;
const cause =
userInfo && "cause" in userInfo && typeof userInfo.cause === "string"
? userInfo.cause
: undefined;
throw new OneError({
operation,
code: e.code as string,
message: e.message,
cause,
originalError: e,
});
// iOS additional fields: "nativeStackIOS"
// android additional fields: "nativeStackAndroid"
} else {
throw e;
}
};
// set name on the err handler to display the original function name in callstack
Object.defineProperty(errHandler, "name", {value: operation});
return fn(...args).catch(errHandler) as unknown as Fn;
};
}
function wrapObj<T extends Record<string, (...args: any[]) => Promise<any>>>(
obj: T,
): T {
return Object.entries(obj).reduce(
(aggr, [key, fn]) => ({
...aggr,
[key]: typeof fn === "function" ? wrapFn(fn, key) : fn,
}),
{} as T,
);
}