@usageflow/express 0.4.1 → 0.4.3
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/plugin.js +25 -12
- package/dist/plugin.js.map +1 -1
- package/package.json +2 -2
- package/src/plugin.ts +29 -11
- package/test/plugin.test.ts +4 -36
package/dist/plugin.js
CHANGED
|
@@ -44,18 +44,20 @@ class ExpressUsageFlowAPI extends core_1.UsageFlowAPI {
|
|
|
44
44
|
};
|
|
45
45
|
return metadata;
|
|
46
46
|
}
|
|
47
|
-
async executeRequestWithMetadata(ledgerId, metadata, request, response, hasLimit) {
|
|
47
|
+
async executeRequestWithMetadata(ledgerId, metadata, request, response, hasLimit, responseTrackingField) {
|
|
48
48
|
if (!this.apiKey) {
|
|
49
49
|
throw new Error("API key not initialized");
|
|
50
50
|
}
|
|
51
|
+
let amount = 1;
|
|
52
|
+
if (responseTrackingField) {
|
|
53
|
+
amount = 1000;
|
|
54
|
+
}
|
|
51
55
|
const payload = {
|
|
52
56
|
alias: ledgerId,
|
|
53
|
-
amount
|
|
57
|
+
amount,
|
|
54
58
|
metadata,
|
|
55
|
-
duration: 1000
|
|
56
59
|
};
|
|
57
|
-
|
|
58
|
-
await this.allocationRequest(request, payload, metadata, hasLimit);
|
|
60
|
+
await this.allocationRequest(request, payload, metadata, hasLimit, responseTrackingField);
|
|
59
61
|
}
|
|
60
62
|
createMiddleware() {
|
|
61
63
|
const self = this;
|
|
@@ -74,12 +76,12 @@ class ExpressUsageFlowAPI extends core_1.UsageFlowAPI {
|
|
|
74
76
|
}
|
|
75
77
|
const metadata = await this.collectRequestMetadata(request);
|
|
76
78
|
metadata.url = url;
|
|
77
|
-
const { ledgerId, hasLimit } = this.guessLedgerId(request);
|
|
79
|
+
const { ledgerId, hasLimit, responseTrackingField } = this.guessLedgerId(request);
|
|
78
80
|
try {
|
|
79
81
|
if (this.getBlockedEndpoints().has(ledgerId)) {
|
|
80
82
|
throw new core_1.UsageFlowError(`Endpoint is been blocked by the administrator`);
|
|
81
83
|
}
|
|
82
|
-
await this.executeRequestWithMetadata(ledgerId, metadata, request, response, hasLimit);
|
|
84
|
+
await this.executeRequestWithMetadata(ledgerId, metadata, request, response, hasLimit, responseTrackingField);
|
|
83
85
|
// Capture response data
|
|
84
86
|
const originalEnd = response.end;
|
|
85
87
|
response.end = function (chunk, encoding, cb) {
|
|
@@ -121,17 +123,28 @@ class ExpressUsageFlowAPI extends core_1.UsageFlowAPI {
|
|
|
121
123
|
catch (error) {
|
|
122
124
|
}
|
|
123
125
|
}
|
|
124
|
-
const headers = {
|
|
125
|
-
"x-usage-key": self.apiKey,
|
|
126
|
-
"Content-Type": "application/json",
|
|
127
|
-
};
|
|
128
126
|
if (request.usageflow?.startTime) {
|
|
129
127
|
metadata.requestDuration =
|
|
130
128
|
Date.now() - request.usageflow.startTime;
|
|
131
129
|
}
|
|
130
|
+
// Extract response schema from the parsed response body
|
|
131
|
+
const responseBody = metadata.body;
|
|
132
|
+
if (responseBody !== undefined) {
|
|
133
|
+
const responseSchema = self.extractSchema(responseBody);
|
|
134
|
+
metadata.responseSchema = responseSchema;
|
|
135
|
+
}
|
|
136
|
+
// Extract amount from the response body using the tracking field path
|
|
137
|
+
const amountPath = request.usageflow?.responseTrackingField;
|
|
138
|
+
let amount = 1;
|
|
139
|
+
if (amountPath && responseBody !== undefined) {
|
|
140
|
+
const amount = self.getValueByPath(responseBody, amountPath);
|
|
141
|
+
if (amount !== undefined) {
|
|
142
|
+
metadata.amount = amount;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
132
145
|
const payload = {
|
|
133
146
|
alias: ledgerId,
|
|
134
|
-
amount:
|
|
147
|
+
amount: amount,
|
|
135
148
|
allocationId: request.usageflow?.eventId,
|
|
136
149
|
metadata: metadata,
|
|
137
150
|
};
|
package/dist/plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../src/plugin.ts"],"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,kDA4NC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@usageflow/express",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.3",
|
|
4
4
|
"description": "UsageFlow plugin for Express applications",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"access": "public"
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@usageflow/core": "^0.4.
|
|
17
|
+
"@usageflow/core": "^0.4.3",
|
|
18
18
|
"@usageflow/logger": "^0.1.1",
|
|
19
19
|
"express": "^4.18.0",
|
|
20
20
|
"ws": "^8.16.0"
|
package/src/plugin.ts
CHANGED
|
@@ -62,21 +62,25 @@ export class ExpressUsageFlowAPI extends UsageFlowAPI {
|
|
|
62
62
|
request: Request,
|
|
63
63
|
response: Response,
|
|
64
64
|
hasLimit: boolean,
|
|
65
|
+
responseTrackingField?: string,
|
|
65
66
|
): Promise<void> {
|
|
66
67
|
if (!this.apiKey) {
|
|
67
68
|
throw new Error("API key not initialized");
|
|
68
69
|
}
|
|
69
70
|
|
|
71
|
+
let amount = 1;
|
|
72
|
+
if (responseTrackingField) {
|
|
73
|
+
amount = 1000
|
|
74
|
+
}
|
|
75
|
+
|
|
70
76
|
const payload = {
|
|
71
77
|
alias: ledgerId,
|
|
72
|
-
amount
|
|
78
|
+
amount,
|
|
73
79
|
metadata,
|
|
74
|
-
duration: 1000
|
|
75
80
|
};
|
|
76
81
|
|
|
77
|
-
console.log("executeRequestWithMetadata", payload);
|
|
78
82
|
|
|
79
|
-
await this.allocationRequest(request as unknown as UsageFlowRequest, payload, metadata, hasLimit);
|
|
83
|
+
await this.allocationRequest(request as unknown as UsageFlowRequest, payload, metadata, hasLimit, responseTrackingField);
|
|
80
84
|
}
|
|
81
85
|
|
|
82
86
|
public createMiddleware() {
|
|
@@ -102,7 +106,7 @@ export class ExpressUsageFlowAPI extends UsageFlowAPI {
|
|
|
102
106
|
|
|
103
107
|
const metadata = await this.collectRequestMetadata(request);
|
|
104
108
|
metadata.url = url;
|
|
105
|
-
const { ledgerId, hasLimit } = this.guessLedgerId(request);
|
|
109
|
+
const { ledgerId, hasLimit, responseTrackingField } = this.guessLedgerId(request);
|
|
106
110
|
|
|
107
111
|
|
|
108
112
|
try {
|
|
@@ -116,6 +120,7 @@ export class ExpressUsageFlowAPI extends UsageFlowAPI {
|
|
|
116
120
|
request,
|
|
117
121
|
response,
|
|
118
122
|
hasLimit,
|
|
123
|
+
responseTrackingField
|
|
119
124
|
);
|
|
120
125
|
|
|
121
126
|
// Capture response data
|
|
@@ -163,19 +168,32 @@ export class ExpressUsageFlowAPI extends UsageFlowAPI {
|
|
|
163
168
|
}
|
|
164
169
|
}
|
|
165
170
|
|
|
166
|
-
const headers = {
|
|
167
|
-
"x-usage-key": self.apiKey!,
|
|
168
|
-
"Content-Type": "application/json",
|
|
169
|
-
};
|
|
170
|
-
|
|
171
171
|
if (request.usageflow?.startTime) {
|
|
172
172
|
metadata.requestDuration =
|
|
173
173
|
Date.now() - request.usageflow.startTime;
|
|
174
174
|
}
|
|
175
175
|
|
|
176
|
+
// Extract response schema from the parsed response body
|
|
177
|
+
const responseBody = (metadata as any).body;
|
|
178
|
+
if (responseBody !== undefined) {
|
|
179
|
+
const responseSchema = self.extractSchema(responseBody);
|
|
180
|
+
metadata.responseSchema = responseSchema;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Extract amount from the response body using the tracking field path
|
|
184
|
+
const amountPath = request.usageflow?.responseTrackingField;
|
|
185
|
+
let amount = 1;
|
|
186
|
+
if (amountPath && responseBody !== undefined) {
|
|
187
|
+
const amount = self.getValueByPath(responseBody, amountPath);
|
|
188
|
+
if (amount !== undefined) {
|
|
189
|
+
(metadata as any).amount = amount;
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
|
|
176
194
|
const payload: RequestForAllocation = {
|
|
177
195
|
alias: ledgerId,
|
|
178
|
-
amount:
|
|
196
|
+
amount: amount,
|
|
179
197
|
allocationId: request.usageflow?.eventId,
|
|
180
198
|
metadata: metadata as RequestMetadata,
|
|
181
199
|
};
|
package/test/plugin.test.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { test, describe, beforeEach, afterEach } from 'node:test';
|
|
2
2
|
import assert from 'node:assert';
|
|
3
3
|
import { ExpressUsageFlowAPI } from '../src/plugin';
|
|
4
|
-
import { Route } from '@usageflow/core';
|
|
5
4
|
|
|
6
5
|
describe('ExpressUsageFlowAPI', () => {
|
|
7
6
|
let api: ExpressUsageFlowAPI;
|
|
@@ -27,36 +26,13 @@ describe('ExpressUsageFlowAPI', () => {
|
|
|
27
26
|
});
|
|
28
27
|
|
|
29
28
|
test('should create middleware', () => {
|
|
30
|
-
const
|
|
31
|
-
{ method: 'GET', url: '/users' }
|
|
32
|
-
];
|
|
33
|
-
|
|
34
|
-
const middleware = api.createMiddleware(routes);
|
|
29
|
+
const middleware = api.createMiddleware();
|
|
35
30
|
assert.strictEqual(typeof middleware, 'function');
|
|
36
31
|
assert.strictEqual(middleware.length, 3); // Express middleware signature
|
|
37
32
|
});
|
|
38
33
|
|
|
39
|
-
test('should create middleware with whitelist', () => {
|
|
40
|
-
const routes: Route[] = [
|
|
41
|
-
{ method: '*', url: '*' }
|
|
42
|
-
];
|
|
43
|
-
const whitelistRoutes: Route[] = [
|
|
44
|
-
{ method: 'GET', url: '/health' }
|
|
45
|
-
];
|
|
46
|
-
|
|
47
|
-
const middleware = api.createMiddleware(routes, whitelistRoutes);
|
|
48
|
-
assert.strictEqual(typeof middleware, 'function');
|
|
49
|
-
});
|
|
50
|
-
|
|
51
34
|
test.skip('should skip whitelisted routes', async () => {
|
|
52
|
-
const
|
|
53
|
-
{ method: '*', url: '*' }
|
|
54
|
-
];
|
|
55
|
-
const whitelistRoutes: Route[] = [
|
|
56
|
-
{ method: 'GET', url: '/health' }
|
|
57
|
-
];
|
|
58
|
-
|
|
59
|
-
const middleware = api.createMiddleware(routes, whitelistRoutes);
|
|
35
|
+
const middleware = api.createMiddleware();
|
|
60
36
|
|
|
61
37
|
const req: any = {
|
|
62
38
|
method: 'GET',
|
|
@@ -94,11 +70,7 @@ describe('ExpressUsageFlowAPI', () => {
|
|
|
94
70
|
});
|
|
95
71
|
|
|
96
72
|
test('should monitor matching routes', async () => {
|
|
97
|
-
const
|
|
98
|
-
{ method: 'GET', url: '/users' }
|
|
99
|
-
];
|
|
100
|
-
|
|
101
|
-
const middleware = api.createMiddleware(routes);
|
|
73
|
+
const middleware = api.createMiddleware();
|
|
102
74
|
|
|
103
75
|
const req: any = {
|
|
104
76
|
method: 'GET',
|
|
@@ -152,11 +124,7 @@ describe('ExpressUsageFlowAPI', () => {
|
|
|
152
124
|
destroy() { }
|
|
153
125
|
} as any;
|
|
154
126
|
|
|
155
|
-
const
|
|
156
|
-
{ method: 'GET', url: '/users' }
|
|
157
|
-
];
|
|
158
|
-
|
|
159
|
-
const middleware = api.createMiddleware(routes);
|
|
127
|
+
const middleware = api.createMiddleware();
|
|
160
128
|
|
|
161
129
|
const req: any = {
|
|
162
130
|
method: 'GET',
|