@c-rex/core 0.0.6 → 0.0.7
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/dist/index.d.mts +26 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +119 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +119 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +17 -9
- package/src/__tests__/logger.test.ts +0 -7
- package/src/__tests__/requests.test.ts +0 -91
- package/src/__tests__/sdk.test.ts +0 -82
- package/src/cli.ts +0 -189
- package/src/index.ts +0 -3
- package/src/logger.ts +0 -24
- package/src/logs/__tests__/server.test.ts +0 -97
- package/src/logs/server.ts +0 -38
- package/src/requests.ts +0 -62
- package/src/sdk.ts +0 -29
- package/src/transports/__tests__/graylog.test.ts +0 -63
- package/src/transports/__tests__/matomo.test.ts +0 -64
- package/src/transports/graylog.ts +0 -34
- package/src/transports/matomo.ts +0 -27
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Method } from 'axios';
|
|
2
|
+
import { ConfigInterface } from '@c-rex/interfaces';
|
|
3
|
+
|
|
4
|
+
interface CallParams {
|
|
5
|
+
url: string;
|
|
6
|
+
method: Method;
|
|
7
|
+
body?: any;
|
|
8
|
+
headers?: any;
|
|
9
|
+
params?: any;
|
|
10
|
+
}
|
|
11
|
+
declare class CrexApi {
|
|
12
|
+
private apiClient;
|
|
13
|
+
private logger;
|
|
14
|
+
constructor(baseUrl: string, logger: any);
|
|
15
|
+
execute<T>({ url, method, params, body, headers, }: CallParams): Promise<any>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
declare class CrexSDK {
|
|
19
|
+
customerConfig: ConfigInterface;
|
|
20
|
+
logger: any;
|
|
21
|
+
api: CrexApi;
|
|
22
|
+
constructor();
|
|
23
|
+
static setConfig(config: ConfigInterface): void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export { CrexApi, CrexSDK };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { Method } from 'axios';
|
|
2
|
+
import { ConfigInterface } from '@c-rex/interfaces';
|
|
3
|
+
|
|
4
|
+
interface CallParams {
|
|
5
|
+
url: string;
|
|
6
|
+
method: Method;
|
|
7
|
+
body?: any;
|
|
8
|
+
headers?: any;
|
|
9
|
+
params?: any;
|
|
10
|
+
}
|
|
11
|
+
declare class CrexApi {
|
|
12
|
+
private apiClient;
|
|
13
|
+
private logger;
|
|
14
|
+
constructor(baseUrl: string, logger: any);
|
|
15
|
+
execute<T>({ url, method, params, body, headers, }: CallParams): Promise<any>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
declare class CrexSDK {
|
|
19
|
+
customerConfig: ConfigInterface;
|
|
20
|
+
logger: any;
|
|
21
|
+
api: CrexApi;
|
|
22
|
+
constructor();
|
|
23
|
+
static setConfig(config: ConfigInterface): void;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export { CrexApi, CrexSDK };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }// src/requests.ts
|
|
2
|
+
var _axios = require('axios'); var _axios2 = _interopRequireDefault(_axios);
|
|
3
|
+
var _constants = require('@c-rex/constants');
|
|
4
|
+
var CrexApi = class {
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
constructor(baseUrl, logger) {
|
|
8
|
+
this.apiClient = _axios2.default.create({
|
|
9
|
+
baseURL: baseUrl,
|
|
10
|
+
headers: {
|
|
11
|
+
"content-Type": "application/json"
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
this.logger = logger;
|
|
15
|
+
}
|
|
16
|
+
async execute({
|
|
17
|
+
url,
|
|
18
|
+
method,
|
|
19
|
+
params,
|
|
20
|
+
body,
|
|
21
|
+
headers
|
|
22
|
+
}) {
|
|
23
|
+
let response = void 0;
|
|
24
|
+
for (let retry = 0; retry < _constants.API.MAX_RETRY; retry++) {
|
|
25
|
+
try {
|
|
26
|
+
response = await this.apiClient.request({
|
|
27
|
+
url,
|
|
28
|
+
method,
|
|
29
|
+
data: body,
|
|
30
|
+
params,
|
|
31
|
+
headers
|
|
32
|
+
});
|
|
33
|
+
} catch (error) {
|
|
34
|
+
this.logger.log("error", `API.execute error when request ${url}. Error: ${error}`);
|
|
35
|
+
throw error;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (response) {
|
|
39
|
+
return response.data;
|
|
40
|
+
}
|
|
41
|
+
throw new Error("API.execute error: Failed to retrieve a valid response");
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// src/cookies.ts
|
|
46
|
+
var GLOBAL_KEY = "__CREX_INITIAL_CONFIG__";
|
|
47
|
+
var CONFIG_COOKIE_KEY = "crex_config";
|
|
48
|
+
function isBrowser() {
|
|
49
|
+
return typeof window !== "undefined" && typeof document !== "undefined";
|
|
50
|
+
}
|
|
51
|
+
function parseCookies() {
|
|
52
|
+
const cookies = {};
|
|
53
|
+
if (typeof document === "undefined") {
|
|
54
|
+
return cookies;
|
|
55
|
+
}
|
|
56
|
+
document.cookie.split(";").forEach((cookie) => {
|
|
57
|
+
const [key, value] = cookie.split("=");
|
|
58
|
+
if (key && value) {
|
|
59
|
+
cookies[key.trim()] = decodeURIComponent(value.trim());
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
return cookies;
|
|
63
|
+
}
|
|
64
|
+
function setCookie(name, value, days = 30) {
|
|
65
|
+
if (typeof document === "undefined") return;
|
|
66
|
+
const expires = new Date(Date.now() + days * 86400 * 1e3).toUTCString();
|
|
67
|
+
document.cookie = `${name}=${encodeURIComponent(value)}; path=/; expires=${expires}; SameSite=Lax`;
|
|
68
|
+
}
|
|
69
|
+
function setConfig(config) {
|
|
70
|
+
if (isBrowser()) {
|
|
71
|
+
setCookie(CONFIG_COOKIE_KEY, JSON.stringify(config));
|
|
72
|
+
} else {
|
|
73
|
+
if (typeof global !== "undefined" && !(GLOBAL_KEY in global)) {
|
|
74
|
+
global[GLOBAL_KEY] = null;
|
|
75
|
+
}
|
|
76
|
+
const globalConfig = global[GLOBAL_KEY];
|
|
77
|
+
if (globalConfig === null) {
|
|
78
|
+
global[GLOBAL_KEY] = config;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function getConfig() {
|
|
83
|
+
let returnValue;
|
|
84
|
+
if (isBrowser()) {
|
|
85
|
+
const cookies = parseCookies();
|
|
86
|
+
const configStr = cookies[CONFIG_COOKIE_KEY];
|
|
87
|
+
if (configStr) {
|
|
88
|
+
try {
|
|
89
|
+
return JSON.parse(configStr);
|
|
90
|
+
} catch (e) {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return null;
|
|
95
|
+
} else {
|
|
96
|
+
returnValue = global[GLOBAL_KEY];
|
|
97
|
+
}
|
|
98
|
+
return returnValue;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// src/sdk.ts
|
|
102
|
+
var CrexSDK = class {
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
constructor() {
|
|
107
|
+
const config = getConfig();
|
|
108
|
+
this.customerConfig = config;
|
|
109
|
+
this.api = new CrexApi(this.customerConfig.baseUrl, null);
|
|
110
|
+
}
|
|
111
|
+
static setConfig(config) {
|
|
112
|
+
setConfig(config);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
exports.CrexApi = CrexApi; exports.CrexSDK = CrexSDK;
|
|
119
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["/Users/periotto/Desktop/workspace/c-rex.net-web-client-foundation/packages/core/dist/index.js","../src/requests.ts","../src/cookies.ts","../src/sdk.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ACX,EAAA;AAE6C,EAAA;AACJ,IAAA;AACnB,IAAA;AACuC,MAAA;AACzD,IAAA;AACH,EAAA;AAEM,EAAA;AACX;AAE2D;AAClB,EAAA;AAEkC,EAAA;AACW,EAAA;AACtF;AAEuD;AAElC,EAAA;AACsC,IAAA;AAChD,EAAA;AAC2D,IAAA;AAC5B,MAAA;AAClC,IAAA;AAE+C,IAAA;AAEpB,IAAA;AACO,MAAA;AAClC,IAAA;AACJ,EAAA;AACJ;AAEwD;AAChD,EAAA;AAEa,EAAA;AACgB,IAAA;AACc,IAAA;AAC5B,IAAA;AACP,MAAA;AAC2B,QAAA;AACvB,MAAA;AACG,QAAA;AACX,MAAA;AACJ,IAAA;AACO,IAAA;AAEJ,EAAA;AACqC,IAAA;AAC5C,EAAA;AAEO,EAAA;AACX;AF+BwF;AACA;AGhGnE;AACV,EAAA;AACA,EAAA;AACA,EAAA;AAEc,EAAA;AACQ,IAAA;AAEH,IAAA;AACkC,IAAA;AAC5D,EAAA;AAEiD,EAAA;AAC7B,IAAA;AACpB,EAAA;AACJ;AH+FwF;AACA;AACA;AACA","file":"/Users/periotto/Desktop/workspace/c-rex.net-web-client-foundation/packages/core/dist/index.js","sourcesContent":[null,"import axios, { AxiosResponse, Method, AxiosInstance } from \"axios\";\nimport { API } from \"@c-rex/constants\";\n\ninterface APIGenericResponse<T> extends AxiosResponse {\n data: T;\n statusCode: number;\n}\n\ninterface CallParams {\n url: string;\n method: Method;\n body?: any;\n headers?: any;\n params?: any;\n}\n\nexport class CrexApi {\n private apiClient: AxiosInstance;\n private logger: any;\n\n public constructor(baseUrl: string, logger: any) {\n this.apiClient = axios.create({\n baseURL: baseUrl,\n headers: {\n \"content-Type\": \"application/json\",\n },\n });\n this.logger = logger;\n }\n\n async execute<T>({\n url,\n method,\n params,\n body,\n headers,\n }: CallParams): Promise<any> {\n let response: APIGenericResponse<T> | undefined = undefined;\n\n for (let retry = 0; retry < API.MAX_RETRY; retry++) {\n try {\n response = await this.apiClient.request({\n url,\n method,\n data: body,\n params,\n headers,\n });\n } catch (error) {\n this.logger.log(\"error\", `API.execute error when request ${url}. Error: ${error}`);\n throw error;\n }\n }\n\n if (response) {\n return response.data\n }\n\n throw new Error(\"API.execute error: Failed to retrieve a valid response\");\n }\n}\n","const GLOBAL_KEY = '__CREX_INITIAL_CONFIG__';\nconst CONFIG_COOKIE_KEY = 'crex_config';\n\nfunction isBrowser() {\n return typeof window !== 'undefined' && typeof document !== 'undefined';\n}\n\nfunction parseCookies(): Record<string, string> {\n const cookies: Record<string, string> = {};\n\n if (typeof document === 'undefined') {\n return cookies;\n }\n\n document.cookie.split(';').forEach(cookie => {\n const [key, value] = cookie.split('=');\n if (key && value) {\n cookies[key.trim()] = decodeURIComponent(value.trim());\n }\n });\n\n return cookies;\n}\n\nfunction setCookie(name: string, value: string, days = 30) {\n if (typeof document === 'undefined') return;\n\n const expires = new Date(Date.now() + days * 86400 * 1000).toUTCString();\n document.cookie = `${name}=${encodeURIComponent(value)}; path=/; expires=${expires}; SameSite=Lax`;\n}\n\nexport function setConfig(config: Record<string, any>) {\n\n if (isBrowser()) {\n setCookie(CONFIG_COOKIE_KEY, JSON.stringify(config));\n } else {\n if (typeof global !== 'undefined' && !(GLOBAL_KEY in global)) {\n (global as any)[GLOBAL_KEY] = null;\n }\n\n const globalConfig = (global as any)[GLOBAL_KEY] as any;\n\n if (globalConfig === null) {\n (global as any)[GLOBAL_KEY] = config;\n }\n }\n}\n\nexport function getConfig(): Record<string, any> | null {\n let returnValue\n\n if (isBrowser()) {\n const cookies = parseCookies();\n const configStr = cookies[CONFIG_COOKIE_KEY];\n if (configStr) {\n try {\n return JSON.parse(configStr);\n } catch {\n return null;\n }\n }\n return null;\n\n } else {\n returnValue = (global as any)[GLOBAL_KEY];\n }\n\n return returnValue\n}\n","import { CrexApi } from \"./requests\";\nimport { ConfigInterface } from \"@c-rex/interfaces\";\nimport { getConfig, setConfig } from \"./cookies\";\n\nexport class CrexSDK {\n public customerConfig!: ConfigInterface;\n public logger!: any;\n public api!: CrexApi;\n\n public constructor() {\n const config = getConfig();\n\n this.customerConfig = config as ConfigInterface;\n this.api = new CrexApi(this.customerConfig.baseUrl, null);\n }\n\n public static setConfig(config: ConfigInterface) {\n setConfig(config);\n }\n}"]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
// src/requests.ts
|
|
2
|
+
import axios from "axios";
|
|
3
|
+
import { API } from "@c-rex/constants";
|
|
4
|
+
var CrexApi = class {
|
|
5
|
+
apiClient;
|
|
6
|
+
logger;
|
|
7
|
+
constructor(baseUrl, logger) {
|
|
8
|
+
this.apiClient = axios.create({
|
|
9
|
+
baseURL: baseUrl,
|
|
10
|
+
headers: {
|
|
11
|
+
"content-Type": "application/json"
|
|
12
|
+
}
|
|
13
|
+
});
|
|
14
|
+
this.logger = logger;
|
|
15
|
+
}
|
|
16
|
+
async execute({
|
|
17
|
+
url,
|
|
18
|
+
method,
|
|
19
|
+
params,
|
|
20
|
+
body,
|
|
21
|
+
headers
|
|
22
|
+
}) {
|
|
23
|
+
let response = void 0;
|
|
24
|
+
for (let retry = 0; retry < API.MAX_RETRY; retry++) {
|
|
25
|
+
try {
|
|
26
|
+
response = await this.apiClient.request({
|
|
27
|
+
url,
|
|
28
|
+
method,
|
|
29
|
+
data: body,
|
|
30
|
+
params,
|
|
31
|
+
headers
|
|
32
|
+
});
|
|
33
|
+
} catch (error) {
|
|
34
|
+
this.logger.log("error", `API.execute error when request ${url}. Error: ${error}`);
|
|
35
|
+
throw error;
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
if (response) {
|
|
39
|
+
return response.data;
|
|
40
|
+
}
|
|
41
|
+
throw new Error("API.execute error: Failed to retrieve a valid response");
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// src/cookies.ts
|
|
46
|
+
var GLOBAL_KEY = "__CREX_INITIAL_CONFIG__";
|
|
47
|
+
var CONFIG_COOKIE_KEY = "crex_config";
|
|
48
|
+
function isBrowser() {
|
|
49
|
+
return typeof window !== "undefined" && typeof document !== "undefined";
|
|
50
|
+
}
|
|
51
|
+
function parseCookies() {
|
|
52
|
+
const cookies = {};
|
|
53
|
+
if (typeof document === "undefined") {
|
|
54
|
+
return cookies;
|
|
55
|
+
}
|
|
56
|
+
document.cookie.split(";").forEach((cookie) => {
|
|
57
|
+
const [key, value] = cookie.split("=");
|
|
58
|
+
if (key && value) {
|
|
59
|
+
cookies[key.trim()] = decodeURIComponent(value.trim());
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
return cookies;
|
|
63
|
+
}
|
|
64
|
+
function setCookie(name, value, days = 30) {
|
|
65
|
+
if (typeof document === "undefined") return;
|
|
66
|
+
const expires = new Date(Date.now() + days * 86400 * 1e3).toUTCString();
|
|
67
|
+
document.cookie = `${name}=${encodeURIComponent(value)}; path=/; expires=${expires}; SameSite=Lax`;
|
|
68
|
+
}
|
|
69
|
+
function setConfig(config) {
|
|
70
|
+
if (isBrowser()) {
|
|
71
|
+
setCookie(CONFIG_COOKIE_KEY, JSON.stringify(config));
|
|
72
|
+
} else {
|
|
73
|
+
if (typeof global !== "undefined" && !(GLOBAL_KEY in global)) {
|
|
74
|
+
global[GLOBAL_KEY] = null;
|
|
75
|
+
}
|
|
76
|
+
const globalConfig = global[GLOBAL_KEY];
|
|
77
|
+
if (globalConfig === null) {
|
|
78
|
+
global[GLOBAL_KEY] = config;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
function getConfig() {
|
|
83
|
+
let returnValue;
|
|
84
|
+
if (isBrowser()) {
|
|
85
|
+
const cookies = parseCookies();
|
|
86
|
+
const configStr = cookies[CONFIG_COOKIE_KEY];
|
|
87
|
+
if (configStr) {
|
|
88
|
+
try {
|
|
89
|
+
return JSON.parse(configStr);
|
|
90
|
+
} catch {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return null;
|
|
95
|
+
} else {
|
|
96
|
+
returnValue = global[GLOBAL_KEY];
|
|
97
|
+
}
|
|
98
|
+
return returnValue;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// src/sdk.ts
|
|
102
|
+
var CrexSDK = class {
|
|
103
|
+
customerConfig;
|
|
104
|
+
logger;
|
|
105
|
+
api;
|
|
106
|
+
constructor() {
|
|
107
|
+
const config = getConfig();
|
|
108
|
+
this.customerConfig = config;
|
|
109
|
+
this.api = new CrexApi(this.customerConfig.baseUrl, null);
|
|
110
|
+
}
|
|
111
|
+
static setConfig(config) {
|
|
112
|
+
setConfig(config);
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
export {
|
|
116
|
+
CrexApi,
|
|
117
|
+
CrexSDK
|
|
118
|
+
};
|
|
119
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/requests.ts","../src/cookies.ts","../src/sdk.ts"],"sourcesContent":["import axios, { AxiosResponse, Method, AxiosInstance } from \"axios\";\nimport { API } from \"@c-rex/constants\";\n\ninterface APIGenericResponse<T> extends AxiosResponse {\n data: T;\n statusCode: number;\n}\n\ninterface CallParams {\n url: string;\n method: Method;\n body?: any;\n headers?: any;\n params?: any;\n}\n\nexport class CrexApi {\n private apiClient: AxiosInstance;\n private logger: any;\n\n public constructor(baseUrl: string, logger: any) {\n this.apiClient = axios.create({\n baseURL: baseUrl,\n headers: {\n \"content-Type\": \"application/json\",\n },\n });\n this.logger = logger;\n }\n\n async execute<T>({\n url,\n method,\n params,\n body,\n headers,\n }: CallParams): Promise<any> {\n let response: APIGenericResponse<T> | undefined = undefined;\n\n for (let retry = 0; retry < API.MAX_RETRY; retry++) {\n try {\n response = await this.apiClient.request({\n url,\n method,\n data: body,\n params,\n headers,\n });\n } catch (error) {\n this.logger.log(\"error\", `API.execute error when request ${url}. Error: ${error}`);\n throw error;\n }\n }\n\n if (response) {\n return response.data\n }\n\n throw new Error(\"API.execute error: Failed to retrieve a valid response\");\n }\n}\n","const GLOBAL_KEY = '__CREX_INITIAL_CONFIG__';\nconst CONFIG_COOKIE_KEY = 'crex_config';\n\nfunction isBrowser() {\n return typeof window !== 'undefined' && typeof document !== 'undefined';\n}\n\nfunction parseCookies(): Record<string, string> {\n const cookies: Record<string, string> = {};\n\n if (typeof document === 'undefined') {\n return cookies;\n }\n\n document.cookie.split(';').forEach(cookie => {\n const [key, value] = cookie.split('=');\n if (key && value) {\n cookies[key.trim()] = decodeURIComponent(value.trim());\n }\n });\n\n return cookies;\n}\n\nfunction setCookie(name: string, value: string, days = 30) {\n if (typeof document === 'undefined') return;\n\n const expires = new Date(Date.now() + days * 86400 * 1000).toUTCString();\n document.cookie = `${name}=${encodeURIComponent(value)}; path=/; expires=${expires}; SameSite=Lax`;\n}\n\nexport function setConfig(config: Record<string, any>) {\n\n if (isBrowser()) {\n setCookie(CONFIG_COOKIE_KEY, JSON.stringify(config));\n } else {\n if (typeof global !== 'undefined' && !(GLOBAL_KEY in global)) {\n (global as any)[GLOBAL_KEY] = null;\n }\n\n const globalConfig = (global as any)[GLOBAL_KEY] as any;\n\n if (globalConfig === null) {\n (global as any)[GLOBAL_KEY] = config;\n }\n }\n}\n\nexport function getConfig(): Record<string, any> | null {\n let returnValue\n\n if (isBrowser()) {\n const cookies = parseCookies();\n const configStr = cookies[CONFIG_COOKIE_KEY];\n if (configStr) {\n try {\n return JSON.parse(configStr);\n } catch {\n return null;\n }\n }\n return null;\n\n } else {\n returnValue = (global as any)[GLOBAL_KEY];\n }\n\n return returnValue\n}\n","import { CrexApi } from \"./requests\";\nimport { ConfigInterface } from \"@c-rex/interfaces\";\nimport { getConfig, setConfig } from \"./cookies\";\n\nexport class CrexSDK {\n public customerConfig!: ConfigInterface;\n public logger!: any;\n public api!: CrexApi;\n\n public constructor() {\n const config = getConfig();\n\n this.customerConfig = config as ConfigInterface;\n this.api = new CrexApi(this.customerConfig.baseUrl, null);\n }\n\n public static setConfig(config: Con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/core",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"module": "dist/index.mjs",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
4
7
|
"files": [
|
|
5
|
-
"
|
|
8
|
+
"dist"
|
|
6
9
|
],
|
|
7
10
|
"publishConfig": {
|
|
8
11
|
"access": "public"
|
|
9
12
|
},
|
|
10
13
|
"exports": {
|
|
11
14
|
".": {
|
|
12
|
-
"types": "./
|
|
13
|
-
"import": "./
|
|
14
|
-
"require": "./
|
|
15
|
-
"default": "./
|
|
15
|
+
"types": "./dist/index.d.ts",
|
|
16
|
+
"import": "./dist/index.mjs",
|
|
17
|
+
"require": "./dist/index.js",
|
|
18
|
+
"default": "./dist/index.js"
|
|
16
19
|
},
|
|
17
|
-
"./package.json": "./package.json"
|
|
20
|
+
"./package.json": "./package.json",
|
|
21
|
+
"./logger.server": {
|
|
22
|
+
"import": "./dist/logger.server.mjs",
|
|
23
|
+
"require": "./dist/logger.server.cjs"
|
|
24
|
+
}
|
|
18
25
|
},
|
|
26
|
+
"sideEffects": false,
|
|
19
27
|
"scripts": {
|
|
20
|
-
"dev": "
|
|
21
|
-
"build": "
|
|
28
|
+
"dev": "tsup src/index.ts --splitting --format cjs,esm --dts --watch",
|
|
29
|
+
"build": "tsup src/index.ts --splitting --format cjs,esm --dts",
|
|
22
30
|
"test:watch": "jest --watch",
|
|
23
31
|
"test": "jest"
|
|
24
32
|
},
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
import { CrexApi } from '../requests';
|
|
2
|
-
import { CrexLogger } from '../logger';
|
|
3
|
-
import axios from 'axios';
|
|
4
|
-
|
|
5
|
-
jest.mock('axios');
|
|
6
|
-
jest.mock('../logger');
|
|
7
|
-
|
|
8
|
-
describe('CrexApi', () => {
|
|
9
|
-
const mockBaseUrl = 'http://api.test.com';
|
|
10
|
-
const mockLogger = new CrexLogger({} as any);
|
|
11
|
-
let api: CrexApi;
|
|
12
|
-
|
|
13
|
-
beforeEach(() => {
|
|
14
|
-
jest.clearAllMocks();
|
|
15
|
-
(axios.create as jest.Mock).mockReturnValue({
|
|
16
|
-
request: jest.fn()
|
|
17
|
-
});
|
|
18
|
-
api = new CrexApi(mockBaseUrl, mockLogger);
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
describe('execute', () => {
|
|
22
|
-
it('should make a successful API request', async () => {
|
|
23
|
-
const mockResponse = { data: { id: 1 }, status: 200 };
|
|
24
|
-
const mockAxiosInstance = axios.create();
|
|
25
|
-
(mockAxiosInstance.request as jest.Mock).mockResolvedValue(mockResponse);
|
|
26
|
-
|
|
27
|
-
const result = await api.execute({
|
|
28
|
-
url: '/test',
|
|
29
|
-
method: 'GET',
|
|
30
|
-
headers: { 'X-Test': 'test' }
|
|
31
|
-
});
|
|
32
|
-
|
|
33
|
-
expect(result).toEqual(mockResponse.data);
|
|
34
|
-
expect(mockAxiosInstance.request).toHaveBeenCalledWith({
|
|
35
|
-
url: '/test',
|
|
36
|
-
method: 'GET',
|
|
37
|
-
headers: { 'X-Test': 'test' },
|
|
38
|
-
data: undefined,
|
|
39
|
-
params: undefined
|
|
40
|
-
});
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it('should handle request with body and params', async () => {
|
|
44
|
-
const mockResponse = { data: { success: true }, status: 200 };
|
|
45
|
-
const mockAxiosInstance = axios.create();
|
|
46
|
-
(mockAxiosInstance.request as jest.Mock).mockResolvedValue(mockResponse);
|
|
47
|
-
|
|
48
|
-
await api.execute({
|
|
49
|
-
url: '/test',
|
|
50
|
-
method: 'POST',
|
|
51
|
-
body: { name: 'test' },
|
|
52
|
-
params: { id: 1 }
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
expect(mockAxiosInstance.request).toHaveBeenCalledWith({
|
|
56
|
-
url: '/test',
|
|
57
|
-
method: 'POST',
|
|
58
|
-
data: { name: 'test' },
|
|
59
|
-
params: { id: 1 },
|
|
60
|
-
headers: undefined
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it('should retry failed requests', async () => {
|
|
65
|
-
const mockError = new Error('Network error');
|
|
66
|
-
const mockAxiosInstance = axios.create();
|
|
67
|
-
(mockAxiosInstance.request as jest.Mock).mockRejectedValue(mockError);
|
|
68
|
-
|
|
69
|
-
await expect(api.execute({
|
|
70
|
-
url: '/test',
|
|
71
|
-
method: 'GET'
|
|
72
|
-
})).rejects.toThrow('Network error');
|
|
73
|
-
|
|
74
|
-
expect(mockLogger.log).toHaveBeenCalledWith(
|
|
75
|
-
'error',
|
|
76
|
-
expect.stringContaining('Network error')
|
|
77
|
-
);
|
|
78
|
-
expect(mockAxiosInstance.request).toHaveBeenCalledTimes(1);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
it('should throw error when no valid response after retries', async () => {
|
|
82
|
-
const mockAxiosInstance = axios.create();
|
|
83
|
-
(mockAxiosInstance.request as jest.Mock).mockResolvedValue(undefined);
|
|
84
|
-
|
|
85
|
-
await expect(api.execute({
|
|
86
|
-
url: '/test',
|
|
87
|
-
method: 'GET'
|
|
88
|
-
})).rejects.toThrow('Failed to retrieve a valid response');
|
|
89
|
-
});
|
|
90
|
-
});
|
|
91
|
-
});
|
|
@@ -1,82 +0,0 @@
|
|
|
1
|
-
import { CrexSDK } from '../sdk';
|
|
2
|
-
import { CrexLogger } from '../logger';
|
|
3
|
-
import { CrexApi } from '../requests';
|
|
4
|
-
import { ConfigInterface } from '@c-rex/interfaces';
|
|
5
|
-
|
|
6
|
-
jest.mock('../logger');
|
|
7
|
-
jest.mock('../requests');
|
|
8
|
-
|
|
9
|
-
describe('CrexSDK', () => {
|
|
10
|
-
const mockConfig: ConfigInterface = {
|
|
11
|
-
projectName: 'test-project',
|
|
12
|
-
baseUrl: 'http://test.com',
|
|
13
|
-
search: {
|
|
14
|
-
fields: [],
|
|
15
|
-
tags: [],
|
|
16
|
-
restrict: [],
|
|
17
|
-
filter: [],
|
|
18
|
-
sparqlWhere: ''
|
|
19
|
-
},
|
|
20
|
-
logs: {
|
|
21
|
-
graylog: {
|
|
22
|
-
silent: true,
|
|
23
|
-
url: 'http://graylog.test',
|
|
24
|
-
app: 'test-app',
|
|
25
|
-
minimumLevel: 'info',
|
|
26
|
-
categoriesLevel: ['Document']
|
|
27
|
-
},
|
|
28
|
-
matomo: {
|
|
29
|
-
silent: false,
|
|
30
|
-
url: '',
|
|
31
|
-
app: '',
|
|
32
|
-
minimumLevel: 'info',
|
|
33
|
-
categoriesLevel: []
|
|
34
|
-
},
|
|
35
|
-
console: {
|
|
36
|
-
silent: false,
|
|
37
|
-
url: '',
|
|
38
|
-
app: '',
|
|
39
|
-
minimumLevel: 'info',
|
|
40
|
-
categoriesLevel: []
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
beforeEach(() => {
|
|
46
|
-
jest.clearAllMocks();
|
|
47
|
-
// Reset the singleton instance
|
|
48
|
-
(CrexSDK as any).instance = undefined;
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
describe('constructor', () => {
|
|
52
|
-
it('should create a new instance with correct configuration', () => {
|
|
53
|
-
const sdk = new CrexSDK(mockConfig);
|
|
54
|
-
|
|
55
|
-
expect(sdk.customerConfig).toBe(mockConfig);
|
|
56
|
-
expect(sdk.logger).toBeInstanceOf(CrexLogger);
|
|
57
|
-
expect(sdk.api).toBeInstanceOf(CrexApi);
|
|
58
|
-
expect(CrexLogger).toHaveBeenCalledWith(mockConfig);
|
|
59
|
-
expect(CrexApi).toHaveBeenCalledWith(mockConfig.baseUrl, expect.any(CrexLogger));
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
it('should return existing instance if already initialized', () => {
|
|
63
|
-
const firstInstance = new CrexSDK(mockConfig);
|
|
64
|
-
const secondInstance = new CrexSDK(mockConfig);
|
|
65
|
-
|
|
66
|
-
expect(secondInstance).toBe(firstInstance);
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
describe('getInstance', () => {
|
|
71
|
-
it('should return existing instance', () => {
|
|
72
|
-
const sdk = new CrexSDK(mockConfig);
|
|
73
|
-
const instance = CrexSDK.getInstance();
|
|
74
|
-
|
|
75
|
-
expect(instance).toBe(sdk);
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
it('should throw error if instance not initialized', () => {
|
|
79
|
-
expect(() => CrexSDK.getInstance()).toThrow('SDK not initialized');
|
|
80
|
-
});
|
|
81
|
-
});
|
|
82
|
-
});
|
package/src/cli.ts
DELETED
|
@@ -1,189 +0,0 @@
|
|
|
1
|
-
/*
|
|
2
|
-
import { execSync } from 'child_process';
|
|
3
|
-
import { checkbox, confirm, input, select } from '@inquirer/prompts';
|
|
4
|
-
|
|
5
|
-
import fs from 'fs';
|
|
6
|
-
import path from 'path';
|
|
7
|
-
import { FILTER_OPTIONS, LOG_CATEGORIES, LOG_LEVELS } from '../constants/log';
|
|
8
|
-
import { RESULT_VIEW_OPTIONS } from '../constants/components';
|
|
9
|
-
|
|
10
|
-
type Restriction = {
|
|
11
|
-
key: string;
|
|
12
|
-
value: string;
|
|
13
|
-
operator: string;
|
|
14
|
-
}
|
|
15
|
-
const addRestriction = async (word: string): Promise<Restriction[]> => {
|
|
16
|
-
const restrictions: any = []
|
|
17
|
-
|
|
18
|
-
let aux = await confirm({
|
|
19
|
-
message: `Do you want to add ${word}s to your search?`,
|
|
20
|
-
default: false,
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
while (aux) {
|
|
24
|
-
const restriction: Restriction = {
|
|
25
|
-
key: "",
|
|
26
|
-
value: '${value}',
|
|
27
|
-
operator: ""
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
restriction.key = await input({
|
|
31
|
-
message: `Type the ${word} key:`,
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
restriction.operator = await select({
|
|
35
|
-
message: `Type the ${word} operator:`,
|
|
36
|
-
choices: FILTER_OPTIONS.map(key => ({ value: key })),
|
|
37
|
-
default: FILTER_OPTIONS[0]
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
const byValue = await confirm({
|
|
41
|
-
message: `Do you want apply your ${word} using a fixed value?`,
|
|
42
|
-
default: false
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
if (byValue) {
|
|
46
|
-
restriction.value = await input({
|
|
47
|
-
message: `Type the ${word} value:`,
|
|
48
|
-
});
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
restrictions.push(restriction);
|
|
52
|
-
|
|
53
|
-
aux = await confirm({
|
|
54
|
-
message: `Do you want to add more ${word}s?`,
|
|
55
|
-
default: false
|
|
56
|
-
});
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
return restrictions;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
const log = async (name: string): Promise<any> => {
|
|
63
|
-
const log = {
|
|
64
|
-
silent: false,
|
|
65
|
-
logLevel: [],
|
|
66
|
-
categoriesLevel: [],
|
|
67
|
-
url: "",
|
|
68
|
-
app: "",
|
|
69
|
-
}
|
|
70
|
-
log.silent = await confirm({
|
|
71
|
-
message: `Do you want to silent ${name}?`,
|
|
72
|
-
default: false
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
if (!log.silent) {
|
|
76
|
-
log.url = await input({
|
|
77
|
-
message: `Type the ${name} URL:`,
|
|
78
|
-
});
|
|
79
|
-
log.app = await input({
|
|
80
|
-
message: `Type the ${name} app name:`,
|
|
81
|
-
});
|
|
82
|
-
const logLevel = await checkbox({
|
|
83
|
-
message: `Select the log level to ${name}:`,
|
|
84
|
-
choices: Object.keys(LOG_LEVELS).map(key => {
|
|
85
|
-
return {
|
|
86
|
-
value: key,
|
|
87
|
-
checked: true,
|
|
88
|
-
};
|
|
89
|
-
}),
|
|
90
|
-
});
|
|
91
|
-
const categoriesLevel = await checkbox({
|
|
92
|
-
message: `Select the categories level to ${name}:`,
|
|
93
|
-
choices: LOG_CATEGORIES.map(key => {
|
|
94
|
-
return {
|
|
95
|
-
value: key,
|
|
96
|
-
checked: true,
|
|
97
|
-
};
|
|
98
|
-
}),
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
log.logLevel = logLevel as any;
|
|
102
|
-
log.categoriesLevel = categoriesLevel as any;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
return log;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
async function main() {
|
|
109
|
-
try {
|
|
110
|
-
const projectName = await input({
|
|
111
|
-
message: 'Set the project name:',
|
|
112
|
-
default: "c-rex.net"
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
const baseUrl = await input({
|
|
116
|
-
message: 'Set the base URL:',
|
|
117
|
-
default: "https://c-rex.net/ids/api/iirds/v1/"
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
const resultViewStyle = await select({
|
|
121
|
-
message: 'Results should be shown as:',
|
|
122
|
-
choices: Object.keys(RESULT_VIEW_OPTIONS).map(key => {
|
|
123
|
-
return { value: key };
|
|
124
|
-
}),
|
|
125
|
-
default: RESULT_VIEW_OPTIONS.table
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
const searchFields = await input({
|
|
129
|
-
message: 'Type the fields used in search separated by comma:',
|
|
130
|
-
default: "titles,labels,languages"
|
|
131
|
-
});
|
|
132
|
-
|
|
133
|
-
const searchTags = await input({
|
|
134
|
-
message: 'Type the tags used in search separated by comma:',
|
|
135
|
-
default: ""
|
|
136
|
-
});
|
|
137
|
-
|
|
138
|
-
const restrictions = await addRestriction("restriction");
|
|
139
|
-
const filters = await addRestriction("filter");
|
|
140
|
-
|
|
141
|
-
const sparqlWhere = await input({
|
|
142
|
-
message: 'Type the sparqlWhere string:',
|
|
143
|
-
default: ""
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
const graylog = await log("Graylog");
|
|
147
|
-
const matomo = await log("Matomo");
|
|
148
|
-
|
|
149
|
-
const config = {
|
|
150
|
-
projectName: projectName,
|
|
151
|
-
baseUrl: baseUrl,
|
|
152
|
-
search: {
|
|
153
|
-
fields: searchFields.split(','),
|
|
154
|
-
tags: searchTags.split(','),
|
|
155
|
-
restrict: restrictions,
|
|
156
|
-
filter: filters,
|
|
157
|
-
sparqlWhere: sparqlWhere
|
|
158
|
-
},
|
|
159
|
-
resultViewStyle: resultViewStyle,
|
|
160
|
-
logs: {
|
|
161
|
-
graylog: graylog,
|
|
162
|
-
matomo: matomo
|
|
163
|
-
}
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
const configFilePath = path.join(process.cwd(), 'src', 'config', 'customerConfig.ts');
|
|
167
|
-
|
|
168
|
-
const configFileContent = `
|
|
169
|
-
import { ConfigInterface } from "@/interfaces/config";
|
|
170
|
-
export const CUSTOMER_CONFIG: ConfigInterface = ${JSON.stringify(config, null, 4)};
|
|
171
|
-
`;
|
|
172
|
-
|
|
173
|
-
fs.writeFileSync(configFilePath, configFileContent);
|
|
174
|
-
|
|
175
|
-
console.log('Installing dependencies...');
|
|
176
|
-
execSync('npm install', { stdio: 'inherit' });
|
|
177
|
-
|
|
178
|
-
console.log('Configuration completed successfully!');
|
|
179
|
-
} catch (error: any) {
|
|
180
|
-
if (error instanceof Error && error.name === 'ExitPromptError') {
|
|
181
|
-
console.log('👋 until next time!');
|
|
182
|
-
} else {
|
|
183
|
-
console.error('Error during configuration:', error);
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
main();
|
|
189
|
-
*/
|
package/src/index.ts
DELETED
package/src/logger.ts
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { ConfigInterface } from "@c-rex/interfaces";
|
|
2
|
-
import { LogCategoriesType, LogLevelType } from "@c-rex/types";
|
|
3
|
-
|
|
4
|
-
export class CrexLogger {
|
|
5
|
-
private customerConfig: ConfigInterface;
|
|
6
|
-
|
|
7
|
-
constructor(config: ConfigInterface) {
|
|
8
|
-
this.customerConfig = config;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
async log(level: LogLevelType, message: string, category?: LogCategoriesType) {
|
|
12
|
-
if (typeof window === "undefined") {
|
|
13
|
-
const { LoggerServer } = await import("./logs/server");
|
|
14
|
-
const serverLog = new LoggerServer(this.customerConfig);
|
|
15
|
-
serverLog.log(level, message, category);
|
|
16
|
-
} else {
|
|
17
|
-
fetch("/api/log", {
|
|
18
|
-
method: "POST",
|
|
19
|
-
headers: { "Content-Type": "application/json" },
|
|
20
|
-
body: JSON.stringify({ level, message, category }),
|
|
21
|
-
}).catch((err) => console.error("Erro ao enviar log", err));
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
}
|
|
@@ -1,97 +0,0 @@
|
|
|
1
|
-
import winston from 'winston';
|
|
2
|
-
import { LogLevelType, LogCategoriesType } from '@c-rex/types';
|
|
3
|
-
import { ConfigInterface } from '@c-rex/interfaces';
|
|
4
|
-
import { LoggerServer } from '../server';
|
|
5
|
-
import { MatomoTransport } from '../../transports/matomo';
|
|
6
|
-
import { GraylogTransport } from '../../transports/graylog';
|
|
7
|
-
|
|
8
|
-
jest.mock('winston', () => {
|
|
9
|
-
const actual = jest.requireActual('winston');
|
|
10
|
-
return {
|
|
11
|
-
...actual,
|
|
12
|
-
createLogger: jest.fn(),
|
|
13
|
-
transports: {
|
|
14
|
-
Console: jest.fn().mockImplementation(() => ({
|
|
15
|
-
name: 'ConsoleTransport',
|
|
16
|
-
})),
|
|
17
|
-
},
|
|
18
|
-
};
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
jest.mock('../../transports/matomo', () => ({
|
|
22
|
-
MatomoTransport: jest.fn().mockImplementation(() => ({
|
|
23
|
-
name: 'MatomoTransport',
|
|
24
|
-
})),
|
|
25
|
-
}));
|
|
26
|
-
|
|
27
|
-
jest.mock('../../transports/graylog', () => ({
|
|
28
|
-
GraylogTransport: jest.fn().mockImplementation(() => ({
|
|
29
|
-
name: 'GraylogTransport',
|
|
30
|
-
})),
|
|
31
|
-
}));
|
|
32
|
-
|
|
33
|
-
describe('LoggerServer', () => {
|
|
34
|
-
const mockConfig: ConfigInterface = {
|
|
35
|
-
logs: {
|
|
36
|
-
console: {
|
|
37
|
-
minimumLevel: 'info',
|
|
38
|
-
silent: false,
|
|
39
|
-
},
|
|
40
|
-
graylog: {
|
|
41
|
-
minimumLevel: 'error',
|
|
42
|
-
silent: true,
|
|
43
|
-
},
|
|
44
|
-
matomo: {
|
|
45
|
-
categoriesLevel: [],
|
|
46
|
-
},
|
|
47
|
-
},
|
|
48
|
-
customerConfig: {
|
|
49
|
-
logs: {
|
|
50
|
-
matomo: {
|
|
51
|
-
categoriesLevel: [],
|
|
52
|
-
},
|
|
53
|
-
},
|
|
54
|
-
},
|
|
55
|
-
} as unknown as ConfigInterface;
|
|
56
|
-
|
|
57
|
-
const mockLogger = {
|
|
58
|
-
log: jest.fn(),
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
beforeEach(() => {
|
|
62
|
-
(winston.createLogger as jest.Mock).mockReturnValue(mockLogger);
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
afterEach(() => {
|
|
66
|
-
jest.clearAllMocks();
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
it('deve criar o logger com os transports corretos', () => {
|
|
70
|
-
new LoggerServer(mockConfig);
|
|
71
|
-
|
|
72
|
-
expect(winston.transports.Console).toHaveBeenCalledWith({
|
|
73
|
-
level: 'info',
|
|
74
|
-
silent: false,
|
|
75
|
-
});
|
|
76
|
-
|
|
77
|
-
expect(MatomoTransport).toHaveBeenCalledWith({
|
|
78
|
-
level: 'info',
|
|
79
|
-
silent: false,
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
expect(GraylogTransport).toHaveBeenCalledWith({
|
|
83
|
-
level: 'error',
|
|
84
|
-
silent: true,
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
expect(winston.createLogger).toHaveBeenCalled();
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
it('deve chamar logger.log com os parâmetros corretos', () => {
|
|
91
|
-
const loggerServer = new LoggerServer(mockConfig);
|
|
92
|
-
|
|
93
|
-
loggerServer.log('info' as LogLevelType, 'mensagem de teste', 'analytics' as LogCategoriesType);
|
|
94
|
-
|
|
95
|
-
expect(mockLogger.log).toHaveBeenCalledWith('info', 'mensagem de teste', 'analytics');
|
|
96
|
-
});
|
|
97
|
-
});
|
package/src/logs/server.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import winston from "winston"
|
|
2
|
-
import { LOG_LEVELS } from "@c-rex/constants";
|
|
3
|
-
import { MatomoTransport } from "../transports/matomo"
|
|
4
|
-
import { GraylogTransport } from "../transports/graylog"
|
|
5
|
-
import { LogLevelType, LogCategoriesType } from "@c-rex/types";
|
|
6
|
-
import { ConfigInterface } from "@c-rex/interfaces";
|
|
7
|
-
|
|
8
|
-
const logger = (config: ConfigInterface) => {
|
|
9
|
-
return winston.createLogger({
|
|
10
|
-
levels: LOG_LEVELS,
|
|
11
|
-
transports: [
|
|
12
|
-
new winston.transports.Console({
|
|
13
|
-
level: config.logs.console.minimumLevel as string,
|
|
14
|
-
silent: config.logs.console.silent,
|
|
15
|
-
}),
|
|
16
|
-
new MatomoTransport({
|
|
17
|
-
level: config.logs.console.minimumLevel,
|
|
18
|
-
silent: config.logs.console.silent,
|
|
19
|
-
}),
|
|
20
|
-
new GraylogTransport({
|
|
21
|
-
level: config.logs.graylog.minimumLevel,
|
|
22
|
-
silent: config.logs.graylog.silent,
|
|
23
|
-
}),
|
|
24
|
-
],
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
export class LoggerServer {
|
|
29
|
-
logger: winston.Logger;
|
|
30
|
-
|
|
31
|
-
constructor(config: ConfigInterface) {
|
|
32
|
-
this.logger = logger(config);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
log(level: LogLevelType, message: string, category?: LogCategoriesType) {
|
|
36
|
-
this.logger.log(level, message, category);
|
|
37
|
-
}
|
|
38
|
-
}
|
package/src/requests.ts
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
import axios, { AxiosResponse, Method, AxiosInstance } from "axios";
|
|
2
|
-
import { API } from "@c-rex/constants";
|
|
3
|
-
import { CrexLogger } from "./logger";
|
|
4
|
-
|
|
5
|
-
interface APIGenericResponse<T> extends AxiosResponse {
|
|
6
|
-
data: T;
|
|
7
|
-
statusCode: number;
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
interface CallParams {
|
|
11
|
-
url: string;
|
|
12
|
-
method: Method;
|
|
13
|
-
body?: any;
|
|
14
|
-
headers?: any;
|
|
15
|
-
params?: any;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
export class CrexApi {
|
|
19
|
-
private apiClient: AxiosInstance;
|
|
20
|
-
private logger: CrexLogger;
|
|
21
|
-
|
|
22
|
-
public constructor(baseUrl: string, logger: CrexLogger) {
|
|
23
|
-
this.apiClient = axios.create({
|
|
24
|
-
baseURL: baseUrl,
|
|
25
|
-
headers: {
|
|
26
|
-
"content-Type": "application/json",
|
|
27
|
-
},
|
|
28
|
-
});
|
|
29
|
-
this.logger = logger;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
async execute<T>({
|
|
33
|
-
url,
|
|
34
|
-
method,
|
|
35
|
-
params,
|
|
36
|
-
body,
|
|
37
|
-
headers,
|
|
38
|
-
}: CallParams): Promise<any> {
|
|
39
|
-
let response: APIGenericResponse<T> | undefined = undefined;
|
|
40
|
-
|
|
41
|
-
for (let retry = 0; retry < API.MAX_RETRY; retry++) {
|
|
42
|
-
try {
|
|
43
|
-
response = await this.apiClient.request({
|
|
44
|
-
url,
|
|
45
|
-
method,
|
|
46
|
-
data: body,
|
|
47
|
-
params,
|
|
48
|
-
headers,
|
|
49
|
-
});
|
|
50
|
-
} catch (error) {
|
|
51
|
-
this.logger.log("error", `API.execute error when request ${url}. Error: ${error}`);
|
|
52
|
-
throw error;
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
if (response) {
|
|
57
|
-
return response.data
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
throw new Error("API.execute error: Failed to retrieve a valid response");
|
|
61
|
-
}
|
|
62
|
-
}
|
package/src/sdk.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import { CrexApi } from "./requests";
|
|
2
|
-
import { CrexLogger } from "./logger";
|
|
3
|
-
import { ConfigInterface } from "@c-rex/interfaces";
|
|
4
|
-
|
|
5
|
-
export class CrexSDK {
|
|
6
|
-
private static instance: CrexSDK;
|
|
7
|
-
public customerConfig!: ConfigInterface;
|
|
8
|
-
public logger!: CrexLogger;
|
|
9
|
-
public api!: CrexApi;
|
|
10
|
-
|
|
11
|
-
public constructor(config: ConfigInterface) {
|
|
12
|
-
if (CrexSDK.instance) {
|
|
13
|
-
return CrexSDK.instance;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
this.customerConfig = config;
|
|
17
|
-
this.logger = new CrexLogger(this.customerConfig);
|
|
18
|
-
this.api = new CrexApi(this.customerConfig.baseUrl, this.logger);
|
|
19
|
-
|
|
20
|
-
CrexSDK.instance = this;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
public static getInstance(): CrexSDK {
|
|
24
|
-
if (!CrexSDK.instance) {
|
|
25
|
-
throw new Error("SDK not initialized");
|
|
26
|
-
}
|
|
27
|
-
return CrexSDK.instance;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { GraylogTransport } from '../graylog';
|
|
2
|
-
import { CrexSDK } from '../..';
|
|
3
|
-
import Transport from 'winston-transport';
|
|
4
|
-
import { LogCategoriesType, LogLevelType } from '@c-rex/types';
|
|
5
|
-
|
|
6
|
-
jest.mock('../..', () => ({
|
|
7
|
-
CrexSDK: {
|
|
8
|
-
getInstance: jest.fn()
|
|
9
|
-
}
|
|
10
|
-
}));
|
|
11
|
-
|
|
12
|
-
describe('GraylogTransport', () => {
|
|
13
|
-
let transport: GraylogTransport;
|
|
14
|
-
let mockLogFn: jest.Mock;
|
|
15
|
-
const mockSDK = {
|
|
16
|
-
customerConfig: {
|
|
17
|
-
logs: {
|
|
18
|
-
graylog: {
|
|
19
|
-
categoriesLevel: ['Document']
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
beforeEach(() => {
|
|
26
|
-
mockLogFn = jest.fn();
|
|
27
|
-
|
|
28
|
-
jest.clearAllMocks();
|
|
29
|
-
(CrexSDK.getInstance as jest.Mock).mockReturnValue(mockSDK);
|
|
30
|
-
|
|
31
|
-
transport = new GraylogTransport({});
|
|
32
|
-
transport.graylogTransport.log = mockLogFn;
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
it('should initialize with transport options', () => {
|
|
36
|
-
const transport = new GraylogTransport({ level: 'info' });
|
|
37
|
-
expect(transport).toBeInstanceOf(Transport);
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
it('should log when category matches configured categories', () => {
|
|
41
|
-
const logInfo = {
|
|
42
|
-
level: 'info' as LogLevelType,
|
|
43
|
-
message: 'teste',
|
|
44
|
-
category: 'Document' as LogCategoriesType,
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
const callback = jest.fn();
|
|
48
|
-
transport.log(logInfo, callback);
|
|
49
|
-
expect(mockLogFn).toHaveBeenCalledWith(logInfo, callback);
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
it('should not log when category does not match', () => {
|
|
53
|
-
const logInfo = {
|
|
54
|
-
level: 'info' as LogLevelType,
|
|
55
|
-
message: 'teste',
|
|
56
|
-
category: 'UnmatchedCategory' as LogCategoriesType,
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
const callback = jest.fn();
|
|
60
|
-
transport.log(logInfo, callback);
|
|
61
|
-
expect(mockLogFn).not.toHaveBeenCalled();
|
|
62
|
-
});
|
|
63
|
-
});
|
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
import { MatomoTransport } from '../matomo';
|
|
2
|
-
import { CrexSDK } from '../..';
|
|
3
|
-
import Transport from 'winston-transport';
|
|
4
|
-
import { LogCategoriesType } from '@c-rex/types';
|
|
5
|
-
import { LogLevelType } from '@c-rex/types';
|
|
6
|
-
|
|
7
|
-
jest.mock('../..', () => ({
|
|
8
|
-
CrexSDK: {
|
|
9
|
-
getInstance: jest.fn()
|
|
10
|
-
}
|
|
11
|
-
}));
|
|
12
|
-
|
|
13
|
-
describe('MatomoTransport', () => {
|
|
14
|
-
let transport: MatomoTransport;
|
|
15
|
-
let mockLogFn: jest.Mock;
|
|
16
|
-
const mockSDK = {
|
|
17
|
-
customerConfig: {
|
|
18
|
-
logs: {
|
|
19
|
-
matomo: {
|
|
20
|
-
categoriesLevel: ['Document']
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
beforeEach(() => {
|
|
27
|
-
mockLogFn = jest.fn();
|
|
28
|
-
|
|
29
|
-
jest.clearAllMocks();
|
|
30
|
-
(CrexSDK.getInstance as jest.Mock).mockReturnValue(mockSDK);
|
|
31
|
-
|
|
32
|
-
transport = new MatomoTransport({});
|
|
33
|
-
transport.matomoTransport.log = mockLogFn;
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
it('should initialize with transport options', () => {
|
|
37
|
-
const transport = new MatomoTransport({ level: 'info' });
|
|
38
|
-
expect(transport).toBeInstanceOf(Transport);
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it('should log when category matches configured categories', () => {
|
|
42
|
-
const logInfo = {
|
|
43
|
-
level: 'info' as LogLevelType,
|
|
44
|
-
message: 'teste',
|
|
45
|
-
category: 'Document' as LogCategoriesType,
|
|
46
|
-
};
|
|
47
|
-
|
|
48
|
-
const callback = jest.fn();
|
|
49
|
-
transport.log(logInfo, callback);
|
|
50
|
-
expect(mockLogFn).toHaveBeenCalledWith(logInfo, callback);
|
|
51
|
-
});
|
|
52
|
-
|
|
53
|
-
it('should not log when category does not match', () => {
|
|
54
|
-
const logInfo = {
|
|
55
|
-
level: 'info' as LogLevelType,
|
|
56
|
-
message: 'teste',
|
|
57
|
-
category: 'UnmatchedCategory' as LogCategoriesType,
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
const callback = jest.fn();
|
|
61
|
-
transport.log(logInfo, callback);
|
|
62
|
-
expect(mockLogFn).not.toHaveBeenCalled();
|
|
63
|
-
});
|
|
64
|
-
});
|
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import Transport from "winston-transport";
|
|
2
|
-
import Graylog2Transport from "winston-graylog2";
|
|
3
|
-
import { CrexSDK } from "..";
|
|
4
|
-
import { LogCategoriesType, LogLevelType } from "@c-rex/types";
|
|
5
|
-
import { ALL } from "@c-rex/constants";
|
|
6
|
-
|
|
7
|
-
export class GraylogTransport extends Transport {
|
|
8
|
-
public graylogTransport: any;
|
|
9
|
-
|
|
10
|
-
constructor(opts: any) {
|
|
11
|
-
super(opts);
|
|
12
|
-
|
|
13
|
-
this.graylogTransport = new Graylog2Transport({
|
|
14
|
-
name: "Periotto-TEST",
|
|
15
|
-
silent: false,
|
|
16
|
-
handleExceptions: false,
|
|
17
|
-
graylog: {
|
|
18
|
-
servers: [{ host: "localhost", port: 12201 }],
|
|
19
|
-
},
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
log(
|
|
24
|
-
info: { level: LogLevelType, message: string, category: LogCategoriesType },
|
|
25
|
-
callback: () => void,
|
|
26
|
-
): void {
|
|
27
|
-
const SDK = CrexSDK.getInstance();
|
|
28
|
-
const graylogCategory = SDK.customerConfig.logs.graylog.categoriesLevel
|
|
29
|
-
|
|
30
|
-
if (graylogCategory.includes(info.category) || graylogCategory.includes(ALL)) {
|
|
31
|
-
this.graylogTransport.log(info, callback);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
}
|
package/src/transports/matomo.ts
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
import Transport from "winston-transport";
|
|
2
|
-
import { CrexSDK } from "..";
|
|
3
|
-
import { LogCategoriesType, LogLevelType } from "@c-rex/types";
|
|
4
|
-
import { ALL } from "@c-rex/constants";
|
|
5
|
-
|
|
6
|
-
export class MatomoTransport extends Transport {
|
|
7
|
-
public matomoTransport: any;
|
|
8
|
-
|
|
9
|
-
constructor(opts: any) {
|
|
10
|
-
super(opts);
|
|
11
|
-
|
|
12
|
-
this.matomoTransport = new Transport();
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
log(
|
|
16
|
-
info: { level: LogLevelType, message: string, category: LogCategoriesType },
|
|
17
|
-
callback: () => void,
|
|
18
|
-
): void {
|
|
19
|
-
const SDK = CrexSDK.getInstance();
|
|
20
|
-
const matomoCategory = SDK.customerConfig.logs.matomo.categoriesLevel
|
|
21
|
-
|
|
22
|
-
if (matomoCategory.includes(info.category) || matomoCategory.includes(ALL)) {
|
|
23
|
-
|
|
24
|
-
this.matomoTransport.log(info, callback);
|
|
25
|
-
}
|
|
26
|
-
}
|
|
27
|
-
}
|