@usageflow/express 0.2.0 → 0.4.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 +17 -78
- package/dist/plugin.d.ts +3 -3
- package/dist/plugin.js +8 -11
- package/dist/plugin.js.map +1 -1
- package/package.json +2 -2
- package/src/plugin.ts +9 -11
- package/test/plugin.test.ts +1 -4
package/README.md
CHANGED
|
@@ -20,24 +20,11 @@ const { ExpressUsageFlowAPI } = require('@usageflow/express');
|
|
|
20
20
|
const app = express();
|
|
21
21
|
app.use(express.json());
|
|
22
22
|
|
|
23
|
-
// Initialize UsageFlow with API key
|
|
24
|
-
const usageFlow = new ExpressUsageFlowAPI(
|
|
25
|
-
apiKey: 'YOUR_API_KEY',
|
|
26
|
-
poolSize: 5, // Optional: Number of WebSocket connections (default: 5)
|
|
27
|
-
});
|
|
28
|
-
|
|
29
|
-
// Create middleware
|
|
30
|
-
const middleware = usageFlow.createMiddleware(
|
|
31
|
-
[
|
|
32
|
-
{ method: '*', url: '*' }, // Track all routes
|
|
33
|
-
],
|
|
34
|
-
[
|
|
35
|
-
{ method: 'GET', url: '/api/health' }, // Whitelist health check
|
|
36
|
-
]
|
|
37
|
-
);
|
|
23
|
+
// Initialize UsageFlow with API key
|
|
24
|
+
const usageFlow = new ExpressUsageFlowAPI('YOUR_API_KEY');
|
|
38
25
|
|
|
39
26
|
// Apply middleware
|
|
40
|
-
app.use(
|
|
27
|
+
app.use(usageFlow.createMiddleware());
|
|
41
28
|
|
|
42
29
|
// Your routes
|
|
43
30
|
app.get('/api/users', (req, res) => {
|
|
@@ -58,24 +45,11 @@ import { ExpressUsageFlowAPI } from '@usageflow/express';
|
|
|
58
45
|
const app = express();
|
|
59
46
|
app.use(express.json());
|
|
60
47
|
|
|
61
|
-
// Initialize UsageFlow with API key
|
|
62
|
-
const usageFlow = new ExpressUsageFlowAPI(
|
|
63
|
-
apiKey: 'YOUR_API_KEY',
|
|
64
|
-
poolSize: 5, // Optional: Number of WebSocket connections (default: 5)
|
|
65
|
-
});
|
|
66
|
-
|
|
67
|
-
// Create middleware
|
|
68
|
-
const middleware = usageFlow.createMiddleware(
|
|
69
|
-
[
|
|
70
|
-
{ method: '*', url: '*' }, // Track all routes
|
|
71
|
-
],
|
|
72
|
-
[
|
|
73
|
-
{ method: 'GET', url: '/api/health' }, // Whitelist health check
|
|
74
|
-
]
|
|
75
|
-
);
|
|
48
|
+
// Initialize UsageFlow with API key
|
|
49
|
+
const usageFlow = new ExpressUsageFlowAPI('YOUR_API_KEY');
|
|
76
50
|
|
|
77
51
|
// Apply middleware
|
|
78
|
-
app.use(
|
|
52
|
+
app.use(usageFlow.createMiddleware());
|
|
79
53
|
|
|
80
54
|
// Your routes
|
|
81
55
|
app.get('/api/users', (req, res) => {
|
|
@@ -91,66 +65,31 @@ app.listen(3000, () => {
|
|
|
91
65
|
|
|
92
66
|
### ExpressUsageFlowAPI
|
|
93
67
|
|
|
94
|
-
#### Constructor
|
|
68
|
+
#### Constructor
|
|
95
69
|
|
|
96
70
|
```typescript
|
|
97
|
-
|
|
98
|
-
apiKey: string; // Your UsageFlow API key (required)
|
|
99
|
-
poolSize?: number; // Number of WebSocket connections (default: 5)
|
|
100
|
-
}
|
|
71
|
+
constructor(apiKey: string)
|
|
101
72
|
```
|
|
102
73
|
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
Creates Express middleware for tracking API usage.
|
|
106
|
-
|
|
107
|
-
```typescript
|
|
108
|
-
createMiddleware(
|
|
109
|
-
routes: Route[], // Routes to track
|
|
110
|
-
whitelistRoutes?: Route[] // Routes to exclude from tracking
|
|
111
|
-
): (req: Request, res: Response, next: NextFunction) => Promise<void>
|
|
112
|
-
```
|
|
74
|
+
- `apiKey`: Your UsageFlow API key (required)
|
|
113
75
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
```typescript
|
|
117
|
-
interface Route {
|
|
118
|
-
method: string; // HTTP method ('GET', 'POST', 'PUT', 'DELETE', etc.) or '*' for all methods
|
|
119
|
-
url: string; // URL pattern or '*' for all URLs
|
|
120
|
-
}
|
|
121
|
-
```
|
|
122
|
-
|
|
123
|
-
#### Examples
|
|
76
|
+
### createMiddleware
|
|
124
77
|
|
|
125
|
-
|
|
78
|
+
Creates Express middleware for tracking API usage. The middleware automatically tracks all routes and handles route configuration through the UsageFlow dashboard.
|
|
126
79
|
|
|
127
80
|
```typescript
|
|
128
|
-
|
|
81
|
+
createMiddleware(): (req: Request, res: Response, next: NextFunction) => Promise<void>
|
|
129
82
|
```
|
|
130
83
|
|
|
131
|
-
|
|
84
|
+
#### Usage
|
|
132
85
|
|
|
133
86
|
```typescript
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
{ method: 'PUT', url: '/api/users/:id' },
|
|
138
|
-
]);
|
|
87
|
+
// Simple usage - tracks all routes automatically
|
|
88
|
+
const usageFlow = new ExpressUsageFlowAPI('YOUR_API_KEY');
|
|
89
|
+
app.use(usageFlow.createMiddleware());
|
|
139
90
|
```
|
|
140
91
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
```typescript
|
|
144
|
-
const middleware = usageFlow.createMiddleware(
|
|
145
|
-
[
|
|
146
|
-
{ method: '*', url: '*' }, // Track all routes
|
|
147
|
-
],
|
|
148
|
-
[
|
|
149
|
-
{ method: 'GET', url: '/api/health' }, // Exclude health check
|
|
150
|
-
{ method: 'GET', url: '/api/metrics' }, // Exclude metrics
|
|
151
|
-
]
|
|
152
|
-
);
|
|
153
|
-
```
|
|
92
|
+
Route configuration (which routes to track, whitelist, etc.) is managed through the UsageFlow dashboard, not through code parameters.
|
|
154
93
|
|
|
155
94
|
## Features
|
|
156
95
|
|
package/dist/plugin.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Request, Response, NextFunction } from "express";
|
|
2
|
-
import { UsageFlowAPI
|
|
2
|
+
import { UsageFlowAPI } from "@usageflow/core";
|
|
3
3
|
export declare class ExpressUsageFlowAPI extends UsageFlowAPI {
|
|
4
|
-
constructor(
|
|
4
|
+
constructor(apiKey: string);
|
|
5
5
|
/**
|
|
6
6
|
* Get the route pattern (e.g., /express/users/:id) from the request
|
|
7
7
|
* Tries multiple methods to get the route pattern since request.route
|
|
@@ -9,5 +9,5 @@ export declare class ExpressUsageFlowAPI extends UsageFlowAPI {
|
|
|
9
9
|
*/
|
|
10
10
|
private collectRequestMetadata;
|
|
11
11
|
private executeRequestWithMetadata;
|
|
12
|
-
createMiddleware(
|
|
12
|
+
createMiddleware(): (request: Request, response: Response, next: NextFunction) => Promise<void>;
|
|
13
13
|
}
|
package/dist/plugin.js
CHANGED
|
@@ -3,8 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.ExpressUsageFlowAPI = void 0;
|
|
4
4
|
const core_1 = require("@usageflow/core");
|
|
5
5
|
class ExpressUsageFlowAPI extends core_1.UsageFlowAPI {
|
|
6
|
-
constructor(
|
|
7
|
-
super(
|
|
6
|
+
constructor(apiKey) {
|
|
7
|
+
super({ apiKey });
|
|
8
8
|
}
|
|
9
9
|
/**
|
|
10
10
|
* Get the route pattern (e.g., /express/users/:id) from the request
|
|
@@ -54,11 +54,10 @@ class ExpressUsageFlowAPI extends core_1.UsageFlowAPI {
|
|
|
54
54
|
metadata,
|
|
55
55
|
duration: 1000
|
|
56
56
|
};
|
|
57
|
+
console.log("executeRequestWithMetadata", payload);
|
|
57
58
|
await this.allocationRequest(request, payload, metadata, hasLimit);
|
|
58
59
|
}
|
|
59
|
-
createMiddleware(
|
|
60
|
-
const routesMap = this.createRoutesMap(routes);
|
|
61
|
-
const whitelistMap = this.createRoutesMap(whitelistRoutes);
|
|
60
|
+
createMiddleware() {
|
|
62
61
|
const self = this;
|
|
63
62
|
return async (request, response, next) => {
|
|
64
63
|
const method = request.method;
|
|
@@ -67,17 +66,17 @@ class ExpressUsageFlowAPI extends core_1.UsageFlowAPI {
|
|
|
67
66
|
request.usageflow = {
|
|
68
67
|
startTime: Date.now(),
|
|
69
68
|
};
|
|
70
|
-
if (this.shouldSkipRoute(method, url || "", whitelistMap)) {
|
|
69
|
+
if (this.shouldSkipRoute(method, url || "", this.whitelistMap)) {
|
|
71
70
|
return next();
|
|
72
71
|
}
|
|
73
|
-
if (!this.shouldMonitorRoute(method, url || "",
|
|
72
|
+
if (!this.shouldMonitorRoute(method, url || "", this.monitorMap)) {
|
|
74
73
|
return next();
|
|
75
74
|
}
|
|
76
75
|
const metadata = await this.collectRequestMetadata(request);
|
|
77
76
|
metadata.url = url;
|
|
78
77
|
const { ledgerId, hasLimit } = this.guessLedgerId(request);
|
|
79
78
|
try {
|
|
80
|
-
if (this.
|
|
79
|
+
if (this.getBlockedEndpoints().has(ledgerId)) {
|
|
81
80
|
throw new core_1.UsageFlowError(`Endpoint is been blocked by the administrator`);
|
|
82
81
|
}
|
|
83
82
|
await this.executeRequestWithMetadata(ledgerId, metadata, request, response, hasLimit);
|
|
@@ -136,9 +135,7 @@ class ExpressUsageFlowAPI extends core_1.UsageFlowAPI {
|
|
|
136
135
|
allocationId: request.usageflow?.eventId,
|
|
137
136
|
metadata: metadata,
|
|
138
137
|
};
|
|
139
|
-
self.useAllocationRequest(payload)
|
|
140
|
-
throw err;
|
|
141
|
-
});
|
|
138
|
+
self.useAllocationRequest(payload);
|
|
142
139
|
// Send via WebSocket if connected
|
|
143
140
|
// Handle the different overloadsx
|
|
144
141
|
if (typeof chunk === "function") {
|
package/dist/plugin.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":";;;AACA,0CAAmJ;AAInJ,MAAa,mBAAoB,SAAQ,mBAAY;IACjD,YAAY,
|
|
1
|
+
{"version":3,"file":"plugin.js","sourceRoot":"","sources":["../src/plugin.ts"],"names":[],"mappings":";;;AACA,0CAAmJ;AAInJ,MAAa,mBAAoB,SAAQ,mBAAY;IACjD,YAAY,MAAc;Q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
|
"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.
|
|
17
|
+
"@usageflow/core": "^0.4.1",
|
|
18
18
|
"@usageflow/logger": "^0.1.1",
|
|
19
19
|
"express": "^4.18.0",
|
|
20
20
|
"ws": "^8.16.0"
|
package/src/plugin.ts
CHANGED
|
@@ -4,8 +4,8 @@ import { UsageFlowAPI, Route, RequestMetadata, RequestForAllocation, UsageFlowRe
|
|
|
4
4
|
|
|
5
5
|
|
|
6
6
|
export class ExpressUsageFlowAPI extends UsageFlowAPI {
|
|
7
|
-
constructor(
|
|
8
|
-
super(
|
|
7
|
+
constructor(apiKey: string) {
|
|
8
|
+
super({ apiKey });
|
|
9
9
|
}
|
|
10
10
|
/**
|
|
11
11
|
* Get the route pattern (e.g., /express/users/:id) from the request
|
|
@@ -74,12 +74,12 @@ export class ExpressUsageFlowAPI extends UsageFlowAPI {
|
|
|
74
74
|
duration: 1000
|
|
75
75
|
};
|
|
76
76
|
|
|
77
|
+
console.log("executeRequestWithMetadata", payload);
|
|
78
|
+
|
|
77
79
|
await this.allocationRequest(request as unknown as UsageFlowRequest, payload, metadata, hasLimit);
|
|
78
80
|
}
|
|
79
81
|
|
|
80
|
-
public createMiddleware(
|
|
81
|
-
const routesMap = this.createRoutesMap(routes);
|
|
82
|
-
const whitelistMap = this.createRoutesMap(whitelistRoutes);
|
|
82
|
+
public createMiddleware() {
|
|
83
83
|
const self = this;
|
|
84
84
|
|
|
85
85
|
|
|
@@ -92,11 +92,11 @@ export class ExpressUsageFlowAPI extends UsageFlowAPI {
|
|
|
92
92
|
startTime: Date.now(),
|
|
93
93
|
};
|
|
94
94
|
|
|
95
|
-
if (this.shouldSkipRoute(method, url || "", whitelistMap)) {
|
|
95
|
+
if (this.shouldSkipRoute(method, url || "", this.whitelistMap)) {
|
|
96
96
|
return next();
|
|
97
97
|
}
|
|
98
98
|
|
|
99
|
-
if (!this.shouldMonitorRoute(method, url || "",
|
|
99
|
+
if (!this.shouldMonitorRoute(method, url || "", this.monitorMap)) {
|
|
100
100
|
return next();
|
|
101
101
|
}
|
|
102
102
|
|
|
@@ -106,7 +106,7 @@ export class ExpressUsageFlowAPI extends UsageFlowAPI {
|
|
|
106
106
|
|
|
107
107
|
|
|
108
108
|
try {
|
|
109
|
-
if (this.
|
|
109
|
+
if (this.getBlockedEndpoints().has(ledgerId)) {
|
|
110
110
|
throw new UsageFlowError(`Endpoint is been blocked by the administrator`);
|
|
111
111
|
}
|
|
112
112
|
|
|
@@ -180,9 +180,7 @@ export class ExpressUsageFlowAPI extends UsageFlowAPI {
|
|
|
180
180
|
metadata: metadata as RequestMetadata,
|
|
181
181
|
};
|
|
182
182
|
|
|
183
|
-
self.useAllocationRequest(payload)
|
|
184
|
-
throw err;
|
|
185
|
-
});
|
|
183
|
+
self.useAllocationRequest(payload)
|
|
186
184
|
// Send via WebSocket if connected
|
|
187
185
|
|
|
188
186
|
|
package/test/plugin.test.ts
CHANGED
|
@@ -7,10 +7,7 @@ describe('ExpressUsageFlowAPI', () => {
|
|
|
7
7
|
let api: ExpressUsageFlowAPI;
|
|
8
8
|
|
|
9
9
|
beforeEach(() => {
|
|
10
|
-
api = new ExpressUsageFlowAPI(
|
|
11
|
-
apiKey: 'test-api-key',
|
|
12
|
-
poolSize: 1
|
|
13
|
-
});
|
|
10
|
+
api = new ExpressUsageFlowAPI('test-api-key');
|
|
14
11
|
// Mock socket manager
|
|
15
12
|
api.socketManager = {
|
|
16
13
|
connected: false,
|