dromanis.finora.functions.common 1.0.4 โ 3.0.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 +239 -31
- package/dist/corsHandler.js +1 -1
- package/dist/corsHandler.js.map +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,49 +1,92 @@
|
|
|
1
1
|
# dromanis.finora.functions.common
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://www.npmjs.com/package/dromanis.finora.functions.common)
|
|
4
|
+
[](https://opensource.org/licenses/ISC)
|
|
4
5
|
|
|
5
|
-
|
|
6
|
-
- **userAuthenticator**: Easily add JWT authentication to your AWS Lambda functions with decoded token access.
|
|
7
|
-
- **corsHandler**: Handle CORS headers and OPTIONS requests for API Gateway responses.
|
|
8
|
-
- TypeScript-first, fully typed for safety and autocompletion.
|
|
9
|
-
- Includes automated tests and Husky hooks for code quality.
|
|
6
|
+
A TypeScript-first utility library providing common functionality for AWS Lambda functions in the Dromanis Finora ecosystem. This package offers robust JWT authentication and CORS handling capabilities designed specifically for serverless API Gateway integrations.
|
|
10
7
|
|
|
11
|
-
##
|
|
8
|
+
## ๐ Features
|
|
12
9
|
|
|
13
|
-
|
|
10
|
+
- **JWT Authentication**: Secure authentication middleware with automatic token validation and payload extraction
|
|
11
|
+
- **CORS Handling**: Comprehensive CORS support for API Gateway responses with customizable headers
|
|
12
|
+
- **TypeScript-First**: Full type safety with comprehensive TypeScript definitions
|
|
13
|
+
- **AWS Lambda Optimized**: Built specifically for AWS Lambda and API Gateway integration
|
|
14
|
+
- **Fully Tested**: Comprehensive test suite with 100% code coverage
|
|
15
|
+
- **Zero Dependencies**: Minimal runtime dependencies for optimal Lambda performance
|
|
16
|
+
|
|
17
|
+
## ๐ฆ Installation
|
|
18
|
+
|
|
19
|
+
```bash
|
|
14
20
|
npm install dromanis.finora.functions.common
|
|
15
21
|
```
|
|
16
22
|
|
|
17
|
-
## Usage
|
|
23
|
+
## ๐ ๏ธ Usage
|
|
18
24
|
|
|
19
25
|
### JWT Authentication
|
|
20
26
|
|
|
27
|
+
The `userAuthenticator` class provides JWT token validation for your Lambda functions.
|
|
28
|
+
|
|
29
|
+
#### Basic Usage
|
|
30
|
+
|
|
21
31
|
```typescript
|
|
22
32
|
import { userAuthenticator } from 'dromanis.finora.functions.common';
|
|
23
|
-
import { APIGatewayProxyEvent } from 'aws-lambda';
|
|
33
|
+
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
|
|
24
34
|
|
|
25
35
|
const authenticator = new userAuthenticator();
|
|
26
36
|
|
|
27
|
-
export const handler = async (event: APIGatewayProxyEvent) => {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
37
|
+
export const handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
|
|
38
|
+
// Authenticate the request
|
|
39
|
+
const authResult = authenticator.authenticate(event);
|
|
40
|
+
|
|
41
|
+
if (authResult.statusCode !== 200) {
|
|
42
|
+
// Authentication failed - return error response
|
|
43
|
+
return authResult;
|
|
31
44
|
}
|
|
32
45
|
|
|
33
46
|
// Access the decoded token payload
|
|
34
|
-
const userData =
|
|
35
|
-
console.log('
|
|
47
|
+
const userData = authResult.decodedToken;
|
|
48
|
+
console.log('Authenticated user:', userData);
|
|
36
49
|
|
|
37
|
-
//
|
|
50
|
+
// Your business logic here
|
|
38
51
|
return {
|
|
39
52
|
statusCode: 200,
|
|
40
|
-
body: JSON.stringify({
|
|
53
|
+
body: JSON.stringify({
|
|
54
|
+
message: 'Success!',
|
|
55
|
+
user: userData
|
|
56
|
+
})
|
|
41
57
|
};
|
|
42
58
|
};
|
|
43
59
|
```
|
|
44
60
|
|
|
61
|
+
#### Authentication Requirements
|
|
62
|
+
|
|
63
|
+
- **Environment Variable**: Set `JWT_SECRET` environment variable
|
|
64
|
+
- **Authorization Header**: Include `Authorization: Bearer <token>` in request headers
|
|
65
|
+
- **Token Format**: Valid JWT token signed with the same secret
|
|
66
|
+
|
|
67
|
+
#### Response Format
|
|
68
|
+
|
|
69
|
+
**Success Response (200):**
|
|
70
|
+
```typescript
|
|
71
|
+
{
|
|
72
|
+
statusCode: 200,
|
|
73
|
+
body: '{"message": "Authenticated"}',
|
|
74
|
+
decodedToken: {
|
|
75
|
+
// Your JWT payload (user, role, etc.)
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
**Error Responses:**
|
|
81
|
+
- `401`: Missing/invalid Authorization header or invalid/expired token
|
|
82
|
+
- `500`: JWT secret not configured
|
|
83
|
+
|
|
45
84
|
### CORS Handling
|
|
46
85
|
|
|
86
|
+
The `corsHandler` class manages CORS headers for your API Gateway responses.
|
|
87
|
+
|
|
88
|
+
#### Basic Usage
|
|
89
|
+
|
|
47
90
|
```typescript
|
|
48
91
|
import { corsHandler } from 'dromanis.finora.functions.common';
|
|
49
92
|
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
|
|
@@ -51,13 +94,13 @@ import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
|
|
|
51
94
|
const cors = new corsHandler();
|
|
52
95
|
|
|
53
96
|
export const handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
|
|
54
|
-
// Handle OPTIONS requests
|
|
97
|
+
// Handle preflight OPTIONS requests
|
|
55
98
|
if (event.httpMethod === 'OPTIONS') {
|
|
56
99
|
return cors.handleOptionsMethod();
|
|
57
100
|
}
|
|
58
101
|
|
|
59
|
-
// Your main logic
|
|
60
|
-
const response = {
|
|
102
|
+
// Your main business logic
|
|
103
|
+
const response: APIGatewayProxyResult = {
|
|
61
104
|
statusCode: 200,
|
|
62
105
|
body: JSON.stringify({ message: 'Success!' })
|
|
63
106
|
};
|
|
@@ -67,24 +110,189 @@ export const handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayPr
|
|
|
67
110
|
};
|
|
68
111
|
```
|
|
69
112
|
|
|
70
|
-
|
|
113
|
+
#### CORS Configuration
|
|
114
|
+
|
|
115
|
+
The CORS handler automatically adds the following headers:
|
|
116
|
+
|
|
117
|
+
- `Access-Control-Allow-Origin: *`
|
|
118
|
+
- `Access-Control-Allow-Headers: *`
|
|
119
|
+
- `Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS`
|
|
120
|
+
|
|
121
|
+
#### Methods
|
|
71
122
|
|
|
72
|
-
|
|
123
|
+
**`handleWithCors(response: APIGatewayProxyResult): APIGatewayProxyResult`**
|
|
124
|
+
- Adds CORS headers to your existing response
|
|
125
|
+
- Preserves existing headers
|
|
126
|
+
- Returns the modified response with CORS headers
|
|
73
127
|
|
|
128
|
+
**`handleOptionsMethod(): APIGatewayProxyResult`**
|
|
129
|
+
- Handles preflight OPTIONS requests
|
|
130
|
+
- Returns a 200 response with appropriate CORS headers
|
|
131
|
+
- Use this for preflight request handling
|
|
132
|
+
|
|
133
|
+
### Combined Usage
|
|
134
|
+
|
|
135
|
+
```typescript
|
|
136
|
+
import { userAuthenticator, corsHandler } from 'dromanis.finora.functions.common';
|
|
137
|
+
import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
|
|
138
|
+
|
|
139
|
+
const authenticator = new userAuthenticator();
|
|
140
|
+
const cors = new corsHandler();
|
|
141
|
+
|
|
142
|
+
export const handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
|
|
143
|
+
// Handle preflight requests
|
|
144
|
+
if (event.httpMethod === 'OPTIONS') {
|
|
145
|
+
return cors.handleOptionsMethod();
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// Authenticate request
|
|
149
|
+
const authResult = authenticator.authenticate(event);
|
|
150
|
+
if (authResult.statusCode !== 200) {
|
|
151
|
+
return cors.handleWithCors(authResult);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Your business logic
|
|
155
|
+
const response = {
|
|
156
|
+
statusCode: 200,
|
|
157
|
+
body: JSON.stringify({
|
|
158
|
+
message: 'Authenticated and authorized!',
|
|
159
|
+
user: authResult.decodedToken
|
|
160
|
+
})
|
|
161
|
+
};
|
|
162
|
+
|
|
163
|
+
// Return response with CORS headers
|
|
164
|
+
return cors.handleWithCors(response);
|
|
165
|
+
};
|
|
74
166
|
```
|
|
167
|
+
|
|
168
|
+
## ๐ง Environment Setup
|
|
169
|
+
|
|
170
|
+
### Required Environment Variables
|
|
171
|
+
|
|
172
|
+
- `JWT_SECRET`: Secret key for JWT token verification (required for authentication)
|
|
173
|
+
|
|
174
|
+
### Example Environment Configuration
|
|
175
|
+
|
|
176
|
+
```bash
|
|
177
|
+
# .env file
|
|
178
|
+
JWT_SECRET=your-super-secure-jwt-secret-key
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
## ๐งช Testing
|
|
182
|
+
|
|
183
|
+
This package includes comprehensive tests using Jest and TypeScript.
|
|
184
|
+
|
|
185
|
+
### Running Tests
|
|
186
|
+
|
|
187
|
+
```bash
|
|
188
|
+
# Run all tests
|
|
75
189
|
npm test
|
|
190
|
+
|
|
191
|
+
# Run tests in watch mode
|
|
192
|
+
npm run test:watch
|
|
193
|
+
|
|
194
|
+
# Run tests with coverage
|
|
195
|
+
npm run test:coverage
|
|
76
196
|
```
|
|
77
197
|
|
|
78
|
-
|
|
198
|
+
### Test Coverage
|
|
79
199
|
|
|
80
|
-
|
|
200
|
+
The test suite covers:
|
|
201
|
+
- JWT authentication scenarios (valid/invalid tokens, missing secrets, etc.)
|
|
202
|
+
- CORS header handling (with/without existing headers)
|
|
203
|
+
- Error handling and edge cases
|
|
204
|
+
- TypeScript type checking
|
|
205
|
+
|
|
206
|
+
## ๐๏ธ Development
|
|
207
|
+
|
|
208
|
+
### Project Structure
|
|
209
|
+
|
|
210
|
+
```
|
|
211
|
+
src/
|
|
212
|
+
โโโ __tests__/ # Test files
|
|
213
|
+
โ โโโ corsHandler.test.ts
|
|
214
|
+
โ โโโ userAuthenticator.test.ts
|
|
215
|
+
โโโ corsHandler.ts # CORS handling utilities
|
|
216
|
+
โโโ userAuthenticator.ts # JWT authentication utilities
|
|
217
|
+
โโโ index.ts # Main exports
|
|
218
|
+
```
|
|
219
|
+
|
|
220
|
+
### Build Process
|
|
221
|
+
|
|
222
|
+
```bash
|
|
223
|
+
# Clean build directory
|
|
224
|
+
npm run clean
|
|
225
|
+
|
|
226
|
+
# Build TypeScript to JavaScript
|
|
227
|
+
npm run build
|
|
228
|
+
|
|
229
|
+
# Run tests
|
|
230
|
+
npm test
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
### Development Setup
|
|
234
|
+
|
|
235
|
+
1. Clone the repository
|
|
236
|
+
2. Install dependencies: `npm install`
|
|
237
|
+
3. Set up environment variables
|
|
238
|
+
4. Run tests: `npm test`
|
|
239
|
+
5. Build: `npm run build`
|
|
240
|
+
|
|
241
|
+
## ๐ API Reference
|
|
242
|
+
|
|
243
|
+
### userAuthenticator
|
|
244
|
+
|
|
245
|
+
```typescript
|
|
246
|
+
class userAuthenticator {
|
|
247
|
+
authenticate(event: APIGatewayProxyEvent): AuthenticationResult
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
interface AuthenticationResult {
|
|
251
|
+
statusCode: number;
|
|
252
|
+
body: string;
|
|
253
|
+
decodedToken?: any; // Present only on successful authentication
|
|
254
|
+
}
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
### corsHandler
|
|
258
|
+
|
|
259
|
+
```typescript
|
|
260
|
+
class corsHandler {
|
|
261
|
+
handleWithCors(response: APIGatewayProxyResult): APIGatewayProxyResult
|
|
262
|
+
handleOptionsMethod(): APIGatewayProxyResult
|
|
263
|
+
}
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
## ๐ค Contributing
|
|
267
|
+
|
|
268
|
+
We welcome contributions! Please follow these steps:
|
|
81
269
|
|
|
82
270
|
1. Fork the repository
|
|
83
|
-
2. Create
|
|
84
|
-
3.
|
|
85
|
-
4.
|
|
86
|
-
5.
|
|
271
|
+
2. Create a feature branch: `git checkout -b feature/amazing-feature`
|
|
272
|
+
3. Make your changes and add tests
|
|
273
|
+
4. Ensure tests pass: `npm test`
|
|
274
|
+
5. Commit your changes: `git commit -m 'Add amazing feature'`
|
|
275
|
+
6. Push to the branch: `git push origin feature/amazing-feature`
|
|
276
|
+
7. Open a Pull Request
|
|
277
|
+
|
|
278
|
+
### Code Quality
|
|
279
|
+
|
|
280
|
+
This project uses:
|
|
281
|
+
- **Husky**: Git hooks for automated testing
|
|
282
|
+
- **Jest**: Testing framework
|
|
283
|
+
- **TypeScript**: Type safety
|
|
284
|
+
- **ESLint**: Code linting (configured via Husky)
|
|
285
|
+
|
|
286
|
+
All commits are automatically tested before being accepted.
|
|
287
|
+
|
|
288
|
+
## ๐ License
|
|
289
|
+
|
|
290
|
+
This project is licensed under the ISC License - see the [LICENSE](LICENSE) file for details.
|
|
291
|
+
|
|
292
|
+
## ๐ข About Dromanis Finora
|
|
293
|
+
|
|
294
|
+
This package is part of the Dromanis Finora ecosystem, providing financial technology solutions built on AWS serverless architecture.
|
|
87
295
|
|
|
88
|
-
|
|
296
|
+
---
|
|
89
297
|
|
|
90
|
-
|
|
298
|
+
For questions, issues, or feature requests, please open an issue on the GitHub repository.
|
package/dist/corsHandler.js
CHANGED
package/dist/corsHandler.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"corsHandler.js","sourceRoot":"","sources":["../src/corsHandler.ts"],"names":[],"mappings":";;;AAEA,MAAa,WAAW;IACtB,cAAc,CAAC,QAA+B;QAC5C,OAAO;YACL,GAAG,QAAQ;YACX,OAAO,EAAE;gBACP,GAAG,CAAC,QAAQ,CAAC,OAAO,IAAI,EAAE,CAAC;gBAC3B,6BAA6B,EAAE,GAAG;gBAClC,8BAA8B,EAAE,GAAG;gBACnC,8BAA8B,EAAE,6BAA6B;aAC9D;SACF,CAAC;IACJ,CAAC;IAED,mBAAmB;QACjB,OAAO,IAAI,CAAC,cAAc,CAAC;YACzB,UAAU,EAAE,GAAG;YACf,IAAI,EAAE,EAAE;
|
|
1
|
+
{"version":3,"file":"corsHandler.js","sourceRoot":"","sources":["../src/corsHandler.ts"],"names":[],"mappings":";;;AAEA,MAAa,WAAW;IACtB,cAAc,CAAC,QAA+B;QAC5C,OAAO;YACL,GAAG,QAAQ;YACX,OAAO,EAAE;gBACP,GAAG,CAAC,QAAQ,CAAC,OAAO,IAAI,EAAE,CAAC;gBAC3B,6BAA6B,EAAE,GAAG;gBAClC,8BAA8B,EAAE,GAAG;gBACnC,8BAA8B,EAAE,6BAA6B;aAC9D;SACF,CAAC;IACJ,CAAC;IAED,mBAAmB;QACjB,OAAO,IAAI,CAAC,cAAc,CAAC;YACzB,UAAU,EAAE,GAAG;YACf,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,CAAC;SACzB,CAAC,CAAC;IACL,CAAC;CACF;AAnBD,kCAmBC"}
|