@leanstacks/lambda-utils 0.2.0-alpha.2 → 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 CHANGED
@@ -1,2 +1,2 @@
1
1
  export { Logger, LoggerConfig, withRequestTracking } from './logging/logger';
2
- export { createResponse, ok, created, noContent, badRequest, notFound, internalServerError, jsonHeaders, corsHeaders, Headers, } from './utils/apigateway-response';
2
+ export { createResponse, ok, created, noContent, badRequest, notFound, internalServerError, httpHeaders, Headers, } from './utils/apigateway-response';
package/dist/index.esm.js CHANGED
@@ -1,2 +1,2 @@
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={"Content-Type":"application/json"},a=(e="*")=>({"Access-Control-Allow-Origin":e}),g=(e,n,o={})=>({statusCode:e,headers:{...o},body:JSON.stringify(n)}),c=(e,n={})=>g(200,e,n),f=(e,n={})=>g(201,e,n),m=(e={})=>g(204,{},e),h=(e="Bad Request",n={})=>g(400,{message:e},n),d=(e="Not Found",n={})=>g(404,{message:e},n),_=(e="Internal Server Error",n={})=>g(500,{message:e},n);export{i as Logger,h as badRequest,a as corsHeaders,g as createResponse,f as created,_ as internalServerError,l as jsonHeaders,m as noContent,d as notFound,c as ok,r as withRequestTracking};
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 r=t.lambdaRequestTracker();const o=(e,t,r={})=>({statusCode:e,headers:{...r},body:JSON.stringify(t)});exports.Logger=class{constructor(r){this._loggerConfig={enabled:!0,level:"info",format:"json"},this._instance=null,this._createLogger=()=>{const r="json"===this._loggerConfig.format?new t.StructuredLogFormatter:new t.CloudwatchLogFormatter,o=t.pinoLambdaDestination({formatter:r});return e({enabled:this._loggerConfig.enabled,level:this._loggerConfig.level},o)},r&&(this._loggerConfig={enabled:r.enabled??!0,level:r.level??"info",format:r.format??"json"})}get instance(){return null===this._instance&&(this._instance=this._createLogger()),this._instance}},exports.badRequest=(e="Bad Request",t={})=>o(400,{message:e},t),exports.corsHeaders=(e="*")=>({"Access-Control-Allow-Origin":e}),exports.createResponse=o,exports.created=(e,t={})=>o(201,e,t),exports.internalServerError=(e="Internal Server Error",t={})=>o(500,{message:e},t),exports.jsonHeaders={"Content-Type":"application/json"},exports.noContent=(e={})=>o(204,{},e),exports.notFound=(e="Not Found",t={})=>o(404,{message:e},t),exports.ok=(e,t={})=>o(200,e,t),exports.withRequestTracking=r;
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
@@ -4,15 +4,22 @@ import { APIGatewayProxyResult } from 'aws-lambda';
4
4
  */
5
5
  export type Headers = Record<string, string | number | boolean>;
6
6
  /**
7
- * Represents the headers for a JSON API Gateway response
7
+ * Commonly used headers for API Gateway responses
8
8
  */
9
- export declare const jsonHeaders: Headers;
10
- /**
11
- * Generates the headers required for CORS (Cross-Origin Resource Sharing) requests
12
- * @param origin The origin to allow for CORS requests
13
- * @returns Headers containing the CORS configuration
14
- */
15
- export declare const corsHeaders: (origin?: string) => Headers;
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
+ };
16
23
  /**
17
24
  * Creates a standardized API Gateway response.
18
25
  * @param statusCode The HTTP status code of the response
@@ -22,7 +29,7 @@ export declare const corsHeaders: (origin?: string) => Headers;
22
29
  *
23
30
  * @example
24
31
  * ```ts
25
- * const response = createResponse(200, { message: 'Success' }, jsonHeaders);
32
+ * const response = createResponse(200, { message: 'Success' }, httpHeaders.json);
26
33
  * ```
27
34
  */
28
35
  export declare const createResponse: (statusCode: number, body: unknown, headers?: Headers) => APIGatewayProxyResult;
@@ -33,7 +40,7 @@ export declare const createResponse: (statusCode: number, body: unknown, headers
33
40
  * @returns An API Gateway proxy result
34
41
  * @example
35
42
  * ```ts
36
- * const response = ok({ message: 'Success' }, jsonHeaders);
43
+ * const response = ok({ message: 'Success' }, httpHeaders.json);
37
44
  * ```
38
45
  */
39
46
  export declare const ok: (body: unknown, headers?: Headers) => APIGatewayProxyResult;
@@ -44,7 +51,7 @@ export declare const ok: (body: unknown, headers?: Headers) => APIGatewayProxyRe
44
51
  * @returns An API Gateway proxy result
45
52
  * @example
46
53
  * ```ts
47
- * const response = created({ message: 'Resource created' }, jsonHeaders);
54
+ * const response = created({ message: 'Resource created' }, httpHeaders.json);
48
55
  * ```
49
56
  */
50
57
  export declare const created: (body: unknown, headers?: Headers) => APIGatewayProxyResult;
@@ -54,7 +61,7 @@ export declare const created: (body: unknown, headers?: Headers) => APIGatewayPr
54
61
  * @returns An API Gateway proxy result
55
62
  * @example
56
63
  * ```ts
57
- * const response = noContent(corsHeaders());
64
+ * const response = noContent(httpHeaders.cors());
58
65
  * ```
59
66
  */
60
67
  export declare const noContent: (headers?: Headers) => APIGatewayProxyResult;
@@ -65,7 +72,7 @@ export declare const noContent: (headers?: Headers) => APIGatewayProxyResult;
65
72
  * @returns An API Gateway proxy result
66
73
  * @example
67
74
  * ```ts
68
- * const response = badRequest('Invalid input', jsonHeaders);
75
+ * const response = badRequest('Invalid input', httpHeaders.json);
69
76
  * ```
70
77
  */
71
78
  export declare const badRequest: (message?: string, headers?: Headers) => APIGatewayProxyResult;
@@ -76,7 +83,7 @@ export declare const badRequest: (message?: string, headers?: Headers) => APIGat
76
83
  * @returns An API Gateway proxy result
77
84
  * @example
78
85
  * ```ts
79
- * const response = notFound('Resource not found', jsonHeaders);
86
+ * const response = notFound('Resource not found', httpHeaders.json);
80
87
  * ```
81
88
  */
82
89
  export declare const notFound: (message?: string, headers?: Headers) => APIGatewayProxyResult;
@@ -87,7 +94,7 @@ export declare const notFound: (message?: string, headers?: Headers) => APIGatew
87
94
  * @returns An API Gateway proxy result
88
95
  * @example
89
96
  * ```ts
90
- * const response = internalServerError('Something went wrong', jsonHeaders);
97
+ * const response = internalServerError('Something went wrong', httpHeaders.json);
91
98
  * ```
92
99
  */
93
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.2.0-alpha.2",
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"