ezthrottle 1.0.0 → 1.1.1
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/README.md +262 -10
- package/dist/client.d.ts +58 -0
- package/dist/client.js +177 -0
- package/dist/errors.d.ts +10 -0
- package/dist/errors.js +25 -0
- package/dist/idempotentStrategy.d.ts +9 -0
- package/dist/idempotentStrategy.js +13 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +33 -0
- package/dist/step.d.ts +125 -0
- package/dist/step.js +411 -0
- package/dist/stepType.d.ts +9 -0
- package/dist/stepType.js +13 -0
- package/dist/types.d.ts +110 -0
- package/dist/types.js +2 -0
- package/package.json +20 -5
- package/examples/basic.js +0 -43
- package/src/client.js +0 -136
- package/src/errors.js +0 -27
- package/src/index.js +0 -11
package/package.json
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ezthrottle",
|
|
3
|
-
"version": "1.
|
|
4
|
-
"description": "Node.js SDK for EZThrottle - The
|
|
5
|
-
"main": "
|
|
3
|
+
"version": "1.1.1",
|
|
4
|
+
"description": "Node.js SDK for EZThrottle - The API Dam for rate-limited services",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
6
7
|
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"prepublishOnly": "npm run build",
|
|
7
10
|
"test": "node test/client.test.js"
|
|
8
11
|
},
|
|
9
12
|
"keywords": [
|
|
@@ -11,7 +14,8 @@
|
|
|
11
14
|
"rate-limiting",
|
|
12
15
|
"api",
|
|
13
16
|
"queue",
|
|
14
|
-
"tracktags"
|
|
17
|
+
"tracktags",
|
|
18
|
+
"typescript"
|
|
15
19
|
],
|
|
16
20
|
"author": "TrackTags",
|
|
17
21
|
"license": "MIT",
|
|
@@ -19,8 +23,19 @@
|
|
|
19
23
|
"type": "git",
|
|
20
24
|
"url": "https://github.com/rjpruitt16/ezthrottle-node"
|
|
21
25
|
},
|
|
26
|
+
"files": [
|
|
27
|
+
"dist",
|
|
28
|
+
"README.md"
|
|
29
|
+
],
|
|
22
30
|
"dependencies": {
|
|
23
|
-
"node-fetch": "^2.7.0"
|
|
31
|
+
"node-fetch": "^2.7.0",
|
|
32
|
+
"uuid": "^8.3.2"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@types/node": "^20.0.0",
|
|
36
|
+
"@types/node-fetch": "^2.6.0",
|
|
37
|
+
"@types/uuid": "^8.3.0",
|
|
38
|
+
"typescript": "^5.0.0"
|
|
24
39
|
},
|
|
25
40
|
"engines": {
|
|
26
41
|
"node": ">=14.0.0"
|
package/examples/basic.js
DELETED
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
const { EZThrottle, RateLimitError } = require('../src');
|
|
2
|
-
|
|
3
|
-
async function main() {
|
|
4
|
-
const client = new EZThrottle({
|
|
5
|
-
apiKey: process.env.TRACKTAGS_API_KEY || 'ck_live_cust_XXX_YYY',
|
|
6
|
-
});
|
|
7
|
-
|
|
8
|
-
try {
|
|
9
|
-
console.log('Queueing request...');
|
|
10
|
-
|
|
11
|
-
const result = await client.queueRequest({
|
|
12
|
-
url: 'https://api.example.com/data',
|
|
13
|
-
webhookUrl: 'https://myapp.com/webhook',
|
|
14
|
-
method: 'GET',
|
|
15
|
-
metadata: {
|
|
16
|
-
userId: '12345',
|
|
17
|
-
requestId: 'abc-123',
|
|
18
|
-
},
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
console.log('Job queued successfully:', result);
|
|
22
|
-
console.log('Job ID:', result.job_id);
|
|
23
|
-
|
|
24
|
-
} catch (error) {
|
|
25
|
-
if (error instanceof RateLimitError) {
|
|
26
|
-
console.error('Rate limited!');
|
|
27
|
-
console.error('Error:', error.message);
|
|
28
|
-
console.error('Retry at:', new Date(error.retryAt));
|
|
29
|
-
|
|
30
|
-
const waitMs = error.retryAt - Date.now();
|
|
31
|
-
console.log(`Wait ${Math.ceil(waitMs / 1000)} seconds before retrying`);
|
|
32
|
-
|
|
33
|
-
} else {
|
|
34
|
-
console.error('Error:', error.message);
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
if (require.main === module) {
|
|
40
|
-
main().catch(console.error);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
module.exports = { main };
|
package/src/client.js
DELETED
|
@@ -1,136 +0,0 @@
|
|
|
1
|
-
const fetch = require('node-fetch');
|
|
2
|
-
const { EZThrottleError, TimeoutError, RateLimitError } = require('./errors');
|
|
3
|
-
|
|
4
|
-
class EZThrottle {
|
|
5
|
-
constructor({ apiKey, tracktagsUrl, ezthrottleUrl }) {
|
|
6
|
-
if (!apiKey) {
|
|
7
|
-
throw new Error('apiKey is required');
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
this.apiKey = apiKey;
|
|
11
|
-
this.tracktagsUrl = tracktagsUrl || 'https://tracktags.fly.dev';
|
|
12
|
-
this.ezthrottleUrl = ezthrottleUrl || 'https://ezthrottle.fly.dev';
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
async queueRequest({ url, webhookUrl, method = 'GET', headers, body, metadata, retryAt }) {
|
|
16
|
-
const jobPayload = {
|
|
17
|
-
url,
|
|
18
|
-
webhook_url: webhookUrl,
|
|
19
|
-
method: method.toUpperCase(),
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
if (headers) jobPayload.headers = headers;
|
|
23
|
-
if (body) jobPayload.body = body;
|
|
24
|
-
if (metadata) jobPayload.metadata = metadata;
|
|
25
|
-
if (retryAt !== undefined) jobPayload.retry_at = retryAt;
|
|
26
|
-
|
|
27
|
-
const proxyPayload = {
|
|
28
|
-
scope: 'customer',
|
|
29
|
-
metric_name: '',
|
|
30
|
-
target_url: `${this.ezthrottleUrl}/api/v1/jobs`,
|
|
31
|
-
method: 'POST',
|
|
32
|
-
headers: {
|
|
33
|
-
'Content-Type': 'application/json',
|
|
34
|
-
},
|
|
35
|
-
body: JSON.stringify(jobPayload),
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
const response = await fetch(`${this.tracktagsUrl}/api/v1/proxy`, {
|
|
39
|
-
method: 'POST',
|
|
40
|
-
headers: {
|
|
41
|
-
'Authorization': `Bearer ${this.apiKey}`,
|
|
42
|
-
'Content-Type': 'application/json',
|
|
43
|
-
},
|
|
44
|
-
body: JSON.stringify(proxyPayload),
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
if (response.status === 429) {
|
|
48
|
-
const errorData = await response.json();
|
|
49
|
-
const retryAfterSeconds = response.headers.get('retry-after');
|
|
50
|
-
let calculatedRetryAt = errorData.retry_at;
|
|
51
|
-
|
|
52
|
-
if (retryAfterSeconds && !calculatedRetryAt) {
|
|
53
|
-
calculatedRetryAt = Date.now() + (parseInt(retryAfterSeconds) * 1000);
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
throw new RateLimitError(
|
|
57
|
-
`Rate limited: ${errorData.error || 'Unknown error'}`,
|
|
58
|
-
calculatedRetryAt
|
|
59
|
-
);
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (response.status !== 200) {
|
|
63
|
-
const text = await response.text();
|
|
64
|
-
throw new EZThrottleError(`Proxy request failed: ${text}`);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
const proxyResponse = await response.json();
|
|
68
|
-
|
|
69
|
-
if (proxyResponse.status !== 'allowed') {
|
|
70
|
-
throw new EZThrottleError(
|
|
71
|
-
`Request denied: ${proxyResponse.error || 'Unknown error'}`
|
|
72
|
-
);
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
const forwarded = proxyResponse.forwarded_response || {};
|
|
76
|
-
|
|
77
|
-
if (forwarded.status_code !== 201) {
|
|
78
|
-
throw new EZThrottleError(
|
|
79
|
-
`EZThrottle job creation failed: ${forwarded.body || 'Unknown error'}`
|
|
80
|
-
);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
const ezthrottleResponse = JSON.parse(forwarded.body || '{}');
|
|
84
|
-
return ezthrottleResponse;
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
async request({ url, method = 'GET', headers, body }) {
|
|
88
|
-
return fetch(url, {
|
|
89
|
-
method,
|
|
90
|
-
headers,
|
|
91
|
-
body,
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
async queueAndWait({
|
|
96
|
-
url,
|
|
97
|
-
webhookUrl,
|
|
98
|
-
method = 'GET',
|
|
99
|
-
headers,
|
|
100
|
-
body,
|
|
101
|
-
metadata,
|
|
102
|
-
retryAt,
|
|
103
|
-
timeout = 300000,
|
|
104
|
-
pollInterval = 2000,
|
|
105
|
-
}) {
|
|
106
|
-
const result = await this.queueRequest({
|
|
107
|
-
url,
|
|
108
|
-
webhookUrl,
|
|
109
|
-
method,
|
|
110
|
-
headers,
|
|
111
|
-
body,
|
|
112
|
-
metadata,
|
|
113
|
-
retryAt,
|
|
114
|
-
});
|
|
115
|
-
|
|
116
|
-
const jobId = result.job_id;
|
|
117
|
-
if (!jobId) {
|
|
118
|
-
throw new EZThrottleError('No job_id in response');
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
const startTime = Date.now();
|
|
122
|
-
|
|
123
|
-
return new Promise((resolve, reject) => {
|
|
124
|
-
const checkResult = async () => {
|
|
125
|
-
if (Date.now() - startTime > timeout) {
|
|
126
|
-
reject(new TimeoutError(`Timeout waiting for job ${jobId}`));
|
|
127
|
-
return;
|
|
128
|
-
}
|
|
129
|
-
setTimeout(checkResult, pollInterval);
|
|
130
|
-
};
|
|
131
|
-
checkResult();
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
module.exports = EZThrottle;
|
package/src/errors.js
DELETED
|
@@ -1,27 +0,0 @@
|
|
|
1
|
-
class EZThrottleError extends Error {
|
|
2
|
-
constructor(message, retryAt = null) {
|
|
3
|
-
super(message);
|
|
4
|
-
this.name = 'EZThrottleError';
|
|
5
|
-
this.retryAt = retryAt;
|
|
6
|
-
}
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
class TimeoutError extends Error {
|
|
10
|
-
constructor(message) {
|
|
11
|
-
super(message);
|
|
12
|
-
this.name = 'TimeoutError';
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
class RateLimitError extends EZThrottleError {
|
|
17
|
-
constructor(message, retryAt) {
|
|
18
|
-
super(message, retryAt);
|
|
19
|
-
this.name = 'RateLimitError';
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
module.exports = {
|
|
24
|
-
EZThrottleError,
|
|
25
|
-
TimeoutError,
|
|
26
|
-
RateLimitError,
|
|
27
|
-
};
|
package/src/index.js
DELETED