-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
140 lines (121 loc) · 3.46 KB
/
index.js
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*
`gatey-sdk-js`
Gatey JavaScript library.
Error Monitoring service.
https://gatey.florgon.com
Current SDK version:
v0.0.0
Expected API version:
v0.0.1
Source code:
https://github.com/florgon/gatey-sdk-js
API documentation:
https://gatey.florgon.com/dev/api
Homepages:
https://gatey.florgon.com/
*/
const sumObjectsByKey = (...objs) => {
const res = objs.reduce((a, b) => {
for (let k in b) {
if (b.hasOwnProperty(k)) a[k] = (a[k] || 0) + b[k];
}
return a;
}, {});
return res;
};
class Client {
HTTP_API_URL = "https://api-gatey.florgon.com/v1/";
HTTP_DEFAULT_HEADERS = {
"Content-Type": "application/json",
};
HTTP_DEFAULT_METHOD = "GET";
constructor(
projectId = undefined,
clientSecret = undefined,
serverSecret = undefined,
logDebug = false
) {
if (!projectId) {
throw Error("Project ID is required for gatey::Client!");
}
if (!clientSecret && serverSecret) {
throw Error(
"None of client and server secrets is not passed to the gatey::Client!"
);
}
this.logDebug = logDebug;
this.projectId = projectId;
this.clientSecret = clientSecret;
this.serverSecret = serverSecret;
}
captureEvent = function (message, level = undefined, tags = undefined) {
const params = {};
tags ??= {};
tags = Object.assign({}, tags, this._getDefaultTags());
if (level) {
params.level = level;
}
params.message = message;
params.tags = JSON.stringify(tags);
this.apiRequest("event.capture", params)
.then(() => {
if (this.logDebug) console.log("captured! OK!");
})
.catch((j) => {
if (this.logDebug) console.log("not captured! not OK!");
console.error(j.error);
});
};
apiRequest = function (method, params) {
if (this.logDebug) console.log(this._buildRequestURL(method, params).href);
return this._wrapSentRequestPromise(
fetch(this._buildRequestURL(method, params), {
method: this.HTTP_DEFAULT_METHOD,
headers: this._getHeaders(),
})
);
};
_buildRequestURL = function (method, params) {
/// @description Returns ready request URL for the API call.
const url = new URL(`${this.HTTP_API_URL}${method}`);
params.project_id = this.projectId;
params.server_secret = this.serverSecret;
params.client_secret = this.clientSecret;
url.search = new URLSearchParams(params);
return url;
};
_getDefaultTags = function () {
return {
"sdk.ver": "0.0.0",
"sdk.name": "gatey.js.official",
"runtime.name": "JavaScript",
"runtime.ver": "Undetected",
};
};
_getHeaders = function () {
/// @description Returns headers object for request.
let headers = this.HTTP_DEFAULT_HEADERS;
return headers;
};
_wrapSentRequestPromise = function (promise) {
return new Promise((resolve, reject) => {
promise
.then((httpResponse) => {
httpResponse
.json()
.then((jsonResponse) => {
if ("success" in jsonResponse)
resolve(jsonResponse, httpResponse);
reject(jsonResponse, httpResponse);
})
.catch(reject);
})
.catch(reject);
});
};
}
const client = new Client(3, "tbsN0wl8BbV54_QjxeesWbbmB5rI6sW4EpIzHj_Io2c");
client.captureEvent("hi", undefined, { "proprietary-tag": "some" });
export default {
Client,
};