@lucaapp/service-utils 2.0.8 → 3.0.0
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.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/lib/datetime/dateConversion.d.ts +10 -0
- package/dist/lib/datetime/dateConversion.js +34 -0
- package/dist/lib/datetime/index.d.ts +1 -0
- package/dist/lib/datetime/index.js +1 -0
- package/dist/lib/proxy/index.d.ts +1 -0
- package/dist/lib/proxy/index.js +17 -0
- package/dist/lib/proxy/proxy.d.ts +37 -0
- package/dist/lib/proxy/proxy.js +98 -0
- package/package.json +7 -4
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -27,4 +27,5 @@ __exportStar(require("./lib/wsEvent"), exports);
|
|
|
27
27
|
__exportStar(require("./lib/lifecycle"), exports);
|
|
28
28
|
__exportStar(require("./lib/logger"), exports);
|
|
29
29
|
__exportStar(require("./lib/datetime"), exports);
|
|
30
|
+
__exportStar(require("./lib/proxy"), exports);
|
|
30
31
|
__exportStar(require("./types"), exports);
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Converts a Date object or string to ISO date format (YYYY-MM-DD)
|
|
3
|
+
* Returns undefined if the input is null, undefined, or invalid
|
|
4
|
+
*/
|
|
5
|
+
export declare const toISODateString: (date: Date | string | null | undefined) => string | undefined;
|
|
6
|
+
/**
|
|
7
|
+
* Converts an ISO date string to a Date object
|
|
8
|
+
* Returns undefined if the input is null, undefined, or invalid
|
|
9
|
+
*/
|
|
10
|
+
export declare const toDateObject: (date: string | null | undefined) => Date | undefined;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.toDateObject = exports.toISODateString = void 0;
|
|
7
|
+
const moment_1 = __importDefault(require("moment"));
|
|
8
|
+
const DATE_FORMAT_ISO = 'YYYY-MM-DD';
|
|
9
|
+
/**
|
|
10
|
+
* Converts a Date object or string to ISO date format (YYYY-MM-DD)
|
|
11
|
+
* Returns undefined if the input is null, undefined, or invalid
|
|
12
|
+
*/
|
|
13
|
+
const toISODateString = (date) => {
|
|
14
|
+
if (!date)
|
|
15
|
+
return undefined;
|
|
16
|
+
const momentDate = (0, moment_1.default)(date);
|
|
17
|
+
if (!momentDate.isValid())
|
|
18
|
+
return undefined;
|
|
19
|
+
return momentDate.format(DATE_FORMAT_ISO);
|
|
20
|
+
};
|
|
21
|
+
exports.toISODateString = toISODateString;
|
|
22
|
+
/**
|
|
23
|
+
* Converts an ISO date string to a Date object
|
|
24
|
+
* Returns undefined if the input is null, undefined, or invalid
|
|
25
|
+
*/
|
|
26
|
+
const toDateObject = (date) => {
|
|
27
|
+
if (!date)
|
|
28
|
+
return undefined;
|
|
29
|
+
const momentDate = (0, moment_1.default)(date);
|
|
30
|
+
if (!momentDate.isValid())
|
|
31
|
+
return undefined;
|
|
32
|
+
return momentDate.toDate();
|
|
33
|
+
};
|
|
34
|
+
exports.toDateObject = toDateObject;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './proxy';
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./proxy"), exports);
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { AxiosError, AxiosInstance, AxiosRequestConfig } from 'axios';
|
|
2
|
+
import { HttpsProxyAgent } from 'https-proxy-agent';
|
|
3
|
+
import https from 'https';
|
|
4
|
+
export interface ProxyConfig {
|
|
5
|
+
https?: string | null;
|
|
6
|
+
http?: string | null;
|
|
7
|
+
retries?: number;
|
|
8
|
+
timeout?: number;
|
|
9
|
+
}
|
|
10
|
+
export interface ProxyClientOptions {
|
|
11
|
+
proxy?: string | null;
|
|
12
|
+
timeout?: number;
|
|
13
|
+
throttleRequestsPerSecond?: number;
|
|
14
|
+
rejectUnauthorized?: boolean;
|
|
15
|
+
retryCondition?: (error: AxiosError) => boolean;
|
|
16
|
+
onRetry?: (retryCount: number, error: AxiosError, requestConfig: AxiosRequestConfig) => void;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Creates an HTTPS proxy agent for the given proxy URL
|
|
20
|
+
*/
|
|
21
|
+
export declare const createHttpsProxyAgent: (proxy: string) => HttpsProxyAgent<string>;
|
|
22
|
+
/**
|
|
23
|
+
* Creates a proxy-aware Axios client with configurable options
|
|
24
|
+
*/
|
|
25
|
+
export declare const createProxyClient: ({ proxy, timeout, throttleRequestsPerSecond, rejectUnauthorized, retryCondition, onRetry, }?: ProxyClientOptions) => AxiosInstance;
|
|
26
|
+
/**
|
|
27
|
+
* Parses proxy configuration from URL string
|
|
28
|
+
*/
|
|
29
|
+
export declare const getProxyConfig: (proxyUrl?: string | null) => {
|
|
30
|
+
host: string;
|
|
31
|
+
port: number;
|
|
32
|
+
} | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* Creates a proxy agent based on configuration
|
|
35
|
+
* Falls back to regular HTTPS agent if no proxy is configured
|
|
36
|
+
*/
|
|
37
|
+
export declare const createProxyAgent: (proxyUrl?: string | null, rejectUnauthorized?: boolean) => HttpsProxyAgent<string> | https.Agent;
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.createProxyAgent = exports.getProxyConfig = exports.createProxyClient = exports.createHttpsProxyAgent = void 0;
|
|
7
|
+
const axios_1 = __importDefault(require("axios"));
|
|
8
|
+
const axios_rate_limit_1 = __importDefault(require("axios-rate-limit"));
|
|
9
|
+
const axios_retry_1 = __importDefault(require("axios-retry"));
|
|
10
|
+
const https_proxy_agent_1 = require("https-proxy-agent");
|
|
11
|
+
const https_1 = __importDefault(require("https"));
|
|
12
|
+
/**
|
|
13
|
+
* Creates an HTTPS proxy agent for the given proxy URL
|
|
14
|
+
*/
|
|
15
|
+
const createHttpsProxyAgent = (proxy) => new https_proxy_agent_1.HttpsProxyAgent(proxy);
|
|
16
|
+
exports.createHttpsProxyAgent = createHttpsProxyAgent;
|
|
17
|
+
/**
|
|
18
|
+
* Default retry condition that excludes 429 Too Many Requests from retries
|
|
19
|
+
*/
|
|
20
|
+
const defaultRetryCondition = (error) => {
|
|
21
|
+
// Do not retry on 429 Too Many Requests
|
|
22
|
+
if (error.response && error.response.status === 429) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
return axios_retry_1.default.isNetworkOrIdempotentRequestError(error);
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Creates a proxy-aware Axios client with configurable options
|
|
29
|
+
*/
|
|
30
|
+
const createProxyClient = ({ proxy, timeout = 120000, throttleRequestsPerSecond, rejectUnauthorized = true, retryCondition = defaultRetryCondition, onRetry, } = {}) => {
|
|
31
|
+
const retryConfig = {
|
|
32
|
+
retries: 3,
|
|
33
|
+
retryDelay: axios_retry_1.default.exponentialDelay,
|
|
34
|
+
shouldResetTimeout: true,
|
|
35
|
+
retryCondition,
|
|
36
|
+
onRetry,
|
|
37
|
+
};
|
|
38
|
+
let client;
|
|
39
|
+
if (!proxy) {
|
|
40
|
+
// No proxy - create regular client
|
|
41
|
+
client = axios_1.default.create({
|
|
42
|
+
proxy: false,
|
|
43
|
+
timeout,
|
|
44
|
+
httpsAgent: new https_1.default.Agent({
|
|
45
|
+
rejectUnauthorized,
|
|
46
|
+
}),
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
else {
|
|
50
|
+
// With proxy - create proxy-aware client
|
|
51
|
+
client = axios_1.default.create({
|
|
52
|
+
httpsAgent: (0, exports.createHttpsProxyAgent)(proxy),
|
|
53
|
+
proxy: false,
|
|
54
|
+
timeout,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
// Apply retry configuration
|
|
58
|
+
(0, axios_retry_1.default)(client, retryConfig);
|
|
59
|
+
// Apply rate limiting if specified
|
|
60
|
+
if (throttleRequestsPerSecond) {
|
|
61
|
+
(0, axios_rate_limit_1.default)(client, { maxRPS: throttleRequestsPerSecond });
|
|
62
|
+
}
|
|
63
|
+
return client;
|
|
64
|
+
};
|
|
65
|
+
exports.createProxyClient = createProxyClient;
|
|
66
|
+
/**
|
|
67
|
+
* Parses proxy configuration from URL string
|
|
68
|
+
*/
|
|
69
|
+
const getProxyConfig = (proxyUrl) => {
|
|
70
|
+
if (!proxyUrl) {
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
const { hostname, port } = new URL(proxyUrl);
|
|
75
|
+
return {
|
|
76
|
+
host: hostname,
|
|
77
|
+
port: Number(port) || 8080,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
catch (error) {
|
|
81
|
+
console.error('Error parsing proxy URL:', error);
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
exports.getProxyConfig = getProxyConfig;
|
|
86
|
+
/**
|
|
87
|
+
* Creates a proxy agent based on configuration
|
|
88
|
+
* Falls back to regular HTTPS agent if no proxy is configured
|
|
89
|
+
*/
|
|
90
|
+
const createProxyAgent = (proxyUrl, rejectUnauthorized = true) => {
|
|
91
|
+
if (proxyUrl) {
|
|
92
|
+
return (0, exports.createHttpsProxyAgent)(proxyUrl);
|
|
93
|
+
}
|
|
94
|
+
return new https_1.default.Agent({
|
|
95
|
+
rejectUnauthorized,
|
|
96
|
+
});
|
|
97
|
+
};
|
|
98
|
+
exports.createProxyAgent = createProxyAgent;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lucaapp/service-utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"files": [
|
|
@@ -28,12 +28,15 @@
|
|
|
28
28
|
"@types/node": "22.13.11",
|
|
29
29
|
"@types/response-time": "^2.3.9",
|
|
30
30
|
"@types/swagger-ui-express": "4.1.3",
|
|
31
|
-
"axios": "^1.
|
|
31
|
+
"axios": "^1.11.0",
|
|
32
|
+
"axios-rate-limit": "^1.4.0",
|
|
33
|
+
"axios-retry": "^4.5.0",
|
|
32
34
|
"body-parser": "^2.2.0",
|
|
33
35
|
"busboy": "^1.6.0",
|
|
34
36
|
"cls-rtracer": "^2.6.3",
|
|
35
37
|
"express": "4.21.2",
|
|
36
38
|
"express-async-errors": "3.1.1",
|
|
39
|
+
"https-proxy-agent": "^7.0.6",
|
|
37
40
|
"jose": "4.15.9",
|
|
38
41
|
"kafkajs": "2.1.0",
|
|
39
42
|
"libphonenumber-js": "1.9.44",
|
|
@@ -84,8 +87,8 @@
|
|
|
84
87
|
"cookie": ">= 1.0.2",
|
|
85
88
|
"string-width": "4.2.3",
|
|
86
89
|
"@types/express": "4.17.23",
|
|
87
|
-
"esbuild": "^0.25.
|
|
88
|
-
"axios": "^1.
|
|
90
|
+
"esbuild": "^0.25.7",
|
|
91
|
+
"axios": "^1.11.0",
|
|
89
92
|
"vite": "6.2.5"
|
|
90
93
|
}
|
|
91
94
|
}
|