@leanstacks/lambda-utils 0.1.0 → 0.2.0-alpha.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/index.d.ts +1 -0
- package/dist/index.esm.js +1 -1
- package/dist/index.js +1 -1
- package/dist/utils/apigateway-response.d.ts +100 -0
- package/package.json +2 -1
package/dist/index.d.ts
CHANGED
package/dist/index.esm.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import e from"pino";import{lambdaRequestTracker as n,StructuredLogFormatter as
|
|
1
|
+
import e from"pino";import{lambdaRequestTracker as n,StructuredLogFormatter as o,CloudwatchLogFormatter as t,pinoLambdaDestination as s}from"pino-lambda";const r=n();class i{constructor(n){this._loggerConfig={enabled:!0,level:"info",format:"json"},this._instance=null,this._createLogger=()=>{const n="json"===this._loggerConfig.format?new o:new t,r=s({formatter:n});return e({enabled:this._loggerConfig.enabled,level:this._loggerConfig.level},r)},n&&(this._loggerConfig={enabled:n.enabled??!0,level:n.level??"info",format:n.format??"json"})}get instance(){return null===this._instance&&(this._instance=this._createLogger()),this._instance}}const l={contentType:e=>({"Content-Type":e}),json:{"Content-Type":"application/json"},cors:(e="*")=>({"Access-Control-Allow-Origin":e})},a=(e,n,o={})=>({statusCode:e,headers:{...o},body:JSON.stringify(n)}),g=(e,n={})=>a(200,e,n),c=(e,n={})=>a(201,e,n),f=(e={})=>a(204,{},e),m=(e="Bad Request",n={})=>a(400,{message:e},n),h=(e="Not Found",n={})=>a(404,{message:e},n),d=(e="Internal Server Error",n={})=>a(500,{message:e},n);export{i as Logger,m as badRequest,a as createResponse,c as created,l as httpHeaders,d as internalServerError,f as noContent,h as notFound,g as ok,r as withRequestTracking};
|
|
2
2
|
//# sourceMappingURL=index.esm.js.map
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var e=require("pino"),t=require("pino-lambda");const
|
|
1
|
+
"use strict";var e=require("pino"),t=require("pino-lambda");const o=t.lambdaRequestTracker();const r=(e,t,o={})=>({statusCode:e,headers:{...o},body:JSON.stringify(t)});exports.Logger=class{constructor(o){this._loggerConfig={enabled:!0,level:"info",format:"json"},this._instance=null,this._createLogger=()=>{const o="json"===this._loggerConfig.format?new t.StructuredLogFormatter:new t.CloudwatchLogFormatter,r=t.pinoLambdaDestination({formatter:o});return e({enabled:this._loggerConfig.enabled,level:this._loggerConfig.level},r)},o&&(this._loggerConfig={enabled:o.enabled??!0,level:o.level??"info",format:o.format??"json"})}get instance(){return null===this._instance&&(this._instance=this._createLogger()),this._instance}},exports.badRequest=(e="Bad Request",t={})=>r(400,{message:e},t),exports.createResponse=r,exports.created=(e,t={})=>r(201,e,t),exports.httpHeaders={contentType:e=>({"Content-Type":e}),json:{"Content-Type":"application/json"},cors:(e="*")=>({"Access-Control-Allow-Origin":e})},exports.internalServerError=(e="Internal Server Error",t={})=>r(500,{message:e},t),exports.noContent=(e={})=>r(204,{},e),exports.notFound=(e="Not Found",t={})=>r(404,{message:e},t),exports.ok=(e,t={})=>r(200,e,t),exports.withRequestTracking=o;
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { APIGatewayProxyResult } from 'aws-lambda';
|
|
2
|
+
/**
|
|
3
|
+
* Represents the headers for an API Gateway response
|
|
4
|
+
*/
|
|
5
|
+
export type Headers = Record<string, string | number | boolean>;
|
|
6
|
+
/**
|
|
7
|
+
* Commonly used headers for API Gateway responses
|
|
8
|
+
*/
|
|
9
|
+
export declare const httpHeaders: {
|
|
10
|
+
/** Content-Type: <type> */
|
|
11
|
+
contentType: (type: string) => {
|
|
12
|
+
'Content-Type': string;
|
|
13
|
+
};
|
|
14
|
+
/** Content-Type: application/json */
|
|
15
|
+
json: {
|
|
16
|
+
'Content-Type': string;
|
|
17
|
+
};
|
|
18
|
+
/** Access-Control-Allow-Origin: <origin> */
|
|
19
|
+
cors: (origin?: string) => {
|
|
20
|
+
'Access-Control-Allow-Origin': string;
|
|
21
|
+
};
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Creates a standardized API Gateway response.
|
|
25
|
+
* @param statusCode The HTTP status code of the response
|
|
26
|
+
* @param body The body of the response
|
|
27
|
+
* @param headers Optional headers to include in the response
|
|
28
|
+
* @returns An API Gateway proxy result
|
|
29
|
+
*
|
|
30
|
+
* @example
|
|
31
|
+
* ```ts
|
|
32
|
+
* const response = createResponse(200, { message: 'Success' }, httpHeaders.json);
|
|
33
|
+
* ```
|
|
34
|
+
*/
|
|
35
|
+
export declare const createResponse: (statusCode: number, body: unknown, headers?: Headers) => APIGatewayProxyResult;
|
|
36
|
+
/**
|
|
37
|
+
* Creates a 200 OK API Gateway response
|
|
38
|
+
* @param body The body of the response
|
|
39
|
+
* @param headers Optional headers to include in the response
|
|
40
|
+
* @returns An API Gateway proxy result
|
|
41
|
+
* @example
|
|
42
|
+
* ```ts
|
|
43
|
+
* const response = ok({ message: 'Success' }, httpHeaders.json);
|
|
44
|
+
* ```
|
|
45
|
+
*/
|
|
46
|
+
export declare const ok: (body: unknown, headers?: Headers) => APIGatewayProxyResult;
|
|
47
|
+
/**
|
|
48
|
+
* Creates a 201 Created API Gateway response
|
|
49
|
+
* @param body The body of the response
|
|
50
|
+
* @param headers Optional headers to include in the response
|
|
51
|
+
* @returns An API Gateway proxy result
|
|
52
|
+
* @example
|
|
53
|
+
* ```ts
|
|
54
|
+
* const response = created({ message: 'Resource created' }, httpHeaders.json);
|
|
55
|
+
* ```
|
|
56
|
+
*/
|
|
57
|
+
export declare const created: (body: unknown, headers?: Headers) => APIGatewayProxyResult;
|
|
58
|
+
/**
|
|
59
|
+
* Creates a 204 No Content API Gateway response
|
|
60
|
+
* @param headers Optional headers to include in the response
|
|
61
|
+
* @returns An API Gateway proxy result
|
|
62
|
+
* @example
|
|
63
|
+
* ```ts
|
|
64
|
+
* const response = noContent(httpHeaders.cors());
|
|
65
|
+
* ```
|
|
66
|
+
*/
|
|
67
|
+
export declare const noContent: (headers?: Headers) => APIGatewayProxyResult;
|
|
68
|
+
/**
|
|
69
|
+
* Creates a 400 Bad Request API Gateway response
|
|
70
|
+
* @param message The error message to include in the response
|
|
71
|
+
* @param headers Optional headers to include in the response
|
|
72
|
+
* @returns An API Gateway proxy result
|
|
73
|
+
* @example
|
|
74
|
+
* ```ts
|
|
75
|
+
* const response = badRequest('Invalid input', httpHeaders.json);
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export declare const badRequest: (message?: string, headers?: Headers) => APIGatewayProxyResult;
|
|
79
|
+
/**
|
|
80
|
+
* Creates a 404 Not Found API Gateway response
|
|
81
|
+
* @param message The error message to include in the response
|
|
82
|
+
* @param headers Optional headers to include in the response
|
|
83
|
+
* @returns An API Gateway proxy result
|
|
84
|
+
* @example
|
|
85
|
+
* ```ts
|
|
86
|
+
* const response = notFound('Resource not found', httpHeaders.json);
|
|
87
|
+
* ```
|
|
88
|
+
*/
|
|
89
|
+
export declare const notFound: (message?: string, headers?: Headers) => APIGatewayProxyResult;
|
|
90
|
+
/**
|
|
91
|
+
* Creates a 500 Internal Server Error API Gateway response
|
|
92
|
+
* @param message The error message to include in the response
|
|
93
|
+
* @param headers Optional headers to include in the response
|
|
94
|
+
* @returns An API Gateway proxy result
|
|
95
|
+
* @example
|
|
96
|
+
* ```ts
|
|
97
|
+
* const response = internalServerError('Something went wrong', httpHeaders.json);
|
|
98
|
+
* ```
|
|
99
|
+
*/
|
|
100
|
+
export declare const internalServerError: (message?: string, headers?: Headers) => APIGatewayProxyResult;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leanstacks/lambda-utils",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.0-alpha.3",
|
|
4
4
|
"description": "A collection of utilities and helper functions designed to streamline the development of AWS Lambda functions using TypeScript.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.esm.js",
|
|
@@ -35,6 +35,7 @@
|
|
|
35
35
|
"lint": "eslint src",
|
|
36
36
|
"lint:fix": "eslint src --fix",
|
|
37
37
|
"prepare": "husky",
|
|
38
|
+
"prepublish": "npm run clean && npm run build",
|
|
38
39
|
"test": "jest",
|
|
39
40
|
"test:watch": "jest --watch",
|
|
40
41
|
"test:coverage": "jest --coverage"
|