forked from checkout/frames-react-native
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogger.tsx
More file actions
75 lines (71 loc) · 1.96 KB
/
logger.tsx
File metadata and controls
75 lines (71 loc) · 1.96 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
import { Platform } from "react-native";
import {
LIVE_LOGGER,
SANDBOX_LOGGER,
MBC_LIVE_PUBLIC_KEY_REGEX,
NAS_LIVE_PUBLIC_KEY_REGEX,
} from "./constants";
import { FramesConfig } from "../types/types";
export const log = async (
loglevel: string,
type: string,
config: FramesConfig,
extra?: object
) => {
try {
const pjson = require("../../package.json");
const date = new Date();
await fetch(getEnvironment(config.publicKey), {
method: "POST",
headers: {
Accept: "application/json",
"Content-Type": "application/cloudevents+json",
},
body: JSON.stringify({
specversion: "1.0",
id: generateUUID(),
type,
source: "frames-react-native-sdk",
time: date.toISOString(),
data: {
...extra,
os: Platform.OS,
osVersion: Platform.Version,
sdkVersion: pjson.version,
environment:
MBC_LIVE_PUBLIC_KEY_REGEX.test(config.publicKey) ||
NAS_LIVE_PUBLIC_KEY_REGEX.test(config.publicKey)
? `production`
: `sandbox`,
},
cko: {
ddTags: "source:frames-react-native-sdk",
loglevel,
},
}),
});
} catch (error) {
throw error;
}
};
const generateUUID = () => {
var d = new Date().getTime(); //Timestamp
var d2 = 0;
return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
var r = Math.random() * 16; //random number between 0 and 16
if (d > 0) {
//Use timestamp until depleted
r = (d + r) % 16 | 0;
d = Math.floor(d / 16);
} else {
//Use microseconds since page-load if supported
r = (d2 + r) % 16 | 0;
d2 = Math.floor(d2 / 16);
}
return (c === "x" ? r : (r & 0x3) | 0x8).toString(16);
});
};
export const getEnvironment = (key: string) =>
MBC_LIVE_PUBLIC_KEY_REGEX.test(key) || NAS_LIVE_PUBLIC_KEY_REGEX.test(key)
? LIVE_LOGGER
: SANDBOX_LOGGER;