dromanis.finora.functions.common 1.0.1 → 1.0.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/README.md CHANGED
@@ -3,7 +3,8 @@
3
3
  A set of common utilities used across different AWS Lambda functions in the Dromanis Finora ecosystem.
4
4
 
5
5
  ## Features
6
- - **userAuthenticator**: Easily add JWT authentication to your AWS Lambda functions.
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.
7
8
  - TypeScript-first, fully typed for safety and autocompletion.
8
9
  - Includes automated tests and Husky hooks for code quality.
9
10
 
@@ -15,7 +16,9 @@ npm install dromanis.finora.functions.common
15
16
 
16
17
  ## Usage
17
18
 
18
- ````typescript
19
+ ### JWT Authentication
20
+
21
+ ```typescript
19
22
  import { userAuthenticator } from 'dromanis.finora.functions.common';
20
23
  import { APIGatewayProxyEvent } from 'aws-lambda';
21
24
 
@@ -26,13 +29,43 @@ export const handler = async (event: APIGatewayProxyEvent) => {
26
29
  if (result.statusCode !== 200) {
27
30
  return result;
28
31
  }
32
+
33
+ // Access the decoded token payload
34
+ const userData = result.decodedToken;
35
+ console.log('User:', userData.user, 'Role:', userData.role);
36
+
29
37
  // Proceed with your logic
30
38
  return {
39
+ statusCode: 200,
40
+ body: JSON.stringify({ message: 'Success!', user: userData.user })
41
+ };
42
+ };
43
+ ```
44
+
45
+ ### CORS Handling
46
+
47
+ ```typescript
48
+ import { corsHandler } from 'dromanis.finora.functions.common';
49
+ import { APIGatewayProxyEvent, APIGatewayProxyResult } from 'aws-lambda';
50
+
51
+ const cors = new corsHandler();
52
+
53
+ export const handler = async (event: APIGatewayProxyEvent): Promise<APIGatewayProxyResult> => {
54
+ // Handle OPTIONS requests
55
+ if (event.httpMethod === 'OPTIONS') {
56
+ return cors.handleOptionsMethod();
57
+ }
58
+
59
+ // Your main logic
60
+ const response = {
31
61
  statusCode: 200,
32
62
  body: JSON.stringify({ message: 'Success!' })
33
63
  };
64
+
65
+ // Add CORS headers to your response
66
+ return cors.handleWithCors(response);
34
67
  };
35
- ````
68
+ ```
36
69
 
37
70
  ## Testing
38
71
 
@@ -0,0 +1,6 @@
1
+ import { APIGatewayProxyResult } from 'aws-lambda';
2
+ export declare class corsHandler {
3
+ handleWithCors(response: APIGatewayProxyResult): APIGatewayProxyResult;
4
+ handleOptionsMethod(): APIGatewayProxyResult;
5
+ }
6
+ //# sourceMappingURL=corsHandler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"corsHandler.d.ts","sourceRoot":"","sources":["../src/corsHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,YAAY,CAAC;AAEnD,qBAAa,WAAW;IACtB,cAAc,CAAC,QAAQ,EAAE,qBAAqB,GAAG,qBAAqB;IAYtE,mBAAmB,IAAI,qBAAqB;CAM7C"}
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.corsHandler = void 0;
4
+ class corsHandler {
5
+ handleWithCors(response) {
6
+ return {
7
+ ...response,
8
+ headers: {
9
+ ...(response.headers || {}),
10
+ 'Access-Control-Allow-Origin': '*',
11
+ 'Access-Control-Allow-Headers': '*',
12
+ 'Access-Control-Allow-Methods': 'GET,POST,PUT,DELETE,OPTIONS',
13
+ },
14
+ };
15
+ }
16
+ handleOptionsMethod() {
17
+ return this.handleWithCors({
18
+ statusCode: 200,
19
+ body: ''
20
+ });
21
+ }
22
+ }
23
+ exports.corsHandler = corsHandler;
24
+ //# sourceMappingURL=corsHandler.js.map
@@ -0,0 +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;SACT,CAAC,CAAC;IACL,CAAC;CACF;AAnBD,kCAmBC"}
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './userAuthenticator';
2
+ export * from './corsHandler';
2
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,qBAAqB,CAAC;AACpC,cAAc,eAAe,CAAC"}
package/dist/index.js CHANGED
@@ -15,4 +15,5 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
17
  __exportStar(require("./userAuthenticator"), exports);
18
+ __exportStar(require("./corsHandler"), exports);
18
19
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,sDAAoC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,sDAAoC;AACpC,gDAA8B"}
@@ -1,9 +1,15 @@
1
1
  import { APIGatewayProxyEvent } from 'aws-lambda';
2
+ import jwt from 'jsonwebtoken';
2
3
  export declare class userAuthenticator {
3
4
  constructor();
4
5
  authenticate(event: APIGatewayProxyEvent): {
5
6
  statusCode: number;
6
7
  body: string;
8
+ decodedToken?: never;
9
+ } | {
10
+ statusCode: number;
11
+ decodedToken: string | jwt.JwtPayload;
12
+ body: string;
7
13
  };
8
14
  }
9
15
  //# sourceMappingURL=userAuthenticator.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"userAuthenticator.d.ts","sourceRoot":"","sources":["../src/userAuthenticator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAGlD,qBAAa,iBAAiB;;IAK1B,YAAY,CAAC,KAAK,EAAE,oBAAoB;;;;CA6B3C"}
1
+ {"version":3,"file":"userAuthenticator.d.ts","sourceRoot":"","sources":["../src/userAuthenticator.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAClD,OAAO,GAAG,MAAM,cAAc,CAAC;AAE/B,qBAAa,iBAAiB;;IAK1B,YAAY,CAAC,KAAK,EAAE,oBAAoB;;;;;;;;;CA8B3C"}
@@ -26,9 +26,10 @@ class userAuthenticator {
26
26
  };
27
27
  }
28
28
  try {
29
- jsonwebtoken_1.default.verify(token, secret);
29
+ let decodedToke = jsonwebtoken_1.default.verify(token, secret);
30
30
  return {
31
31
  statusCode: 200,
32
+ decodedToken: decodedToke,
32
33
  body: JSON.stringify({ message: 'Authenticated' })
33
34
  };
34
35
  }
@@ -1 +1 @@
1
- {"version":3,"file":"userAuthenticator.js","sourceRoot":"","sources":["../src/userAuthenticator.ts"],"names":[],"mappings":";;;;;;AACA,gEAA+B;AAE/B,MAAa,iBAAiB;IAC1B;QACI,iCAAiC;IACrC,CAAC;IAED,YAAY,CAAC,KAA2B;QACpC,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,EAAE,CAAC,eAAe,CAAC,IAAI,KAAK,CAAC,OAAO,EAAE,CAAC,eAAe,CAAC,CAAC;QACxF,IAAI,CAAC,UAAU,IAAI,CAAC,UAAU,CAAC,UAAU,CAAC,SAAS,CAAC,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,UAAU,CAAC,SAAS,CAAC,EAAE,CAAC;YACnD,OAAO;gBACH,UAAU,EAAE,GAAG;gBACf,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,KAAK,EAAE,yCAAyC,EAAE,CAAC;aAC7E,CAAC;QACN,CAAC;QACD,MAAM,KAAK,GAAG,UAAU,CAAC,OAAO,CAAC,SAAS,EAAE,EAAE,CAAC,CAAC;QAChD,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC;QACzC,IAAI,CAAC,MAAM,EAAE,CAAC;YACV,OAAO;gBACH,UAAU,EAAE,GAAG;gBACf,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,KAAK,EAAE,2BAA2B,EAAE,CAAC;aAC/D,CAAC;QACN,CAAC;QACD,IAAI,CAAC;YACD,IAAI,WAAW,GAAG,sBAAG,CAAC,MAAM,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC;YAC5C,OAAO;gBACH,UAAU,EAAE,GAAG;gBACf,YAAY,EAAE,WAAW;gBACzB,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,OAAO,EAAE,eAAe,EAAE,CAAC;aACrD,CAAC;QACN,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACX,OAAO;gBACH,UAAU,EAAE,GAAG;gBACf,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,EAAE,KAAK,EAAE,0BAA0B,EAAE,CAAC;aAC9D,CAAC;QACN,CAAC;IACL,CAAC;CACJ;AAnCD,8CAmCC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dromanis.finora.functions.common",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "main": "dist/index.js",
5
5
  "module": "dist/index.js",
6
6
  "types": "dist/index.d.ts",