@leanstacks/lambda-utils 0.1.0 → 0.2.0-alpha.2

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 +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';
package/dist/index.esm.js CHANGED
@@ -1,2 +1,2 @@
1
- import e from"pino";import{lambdaRequestTracker as n,StructuredLogFormatter as t,CloudwatchLogFormatter as o,pinoLambdaDestination as i}from"pino-lambda";const l=n();class r{constructor(n){this._loggerConfig={enabled:!0,level:"info",format:"json"},this._instance=null,this._createLogger=()=>{const n="json"===this._loggerConfig.format?new t:new o,l=i({formatter:n});return e({enabled:this._loggerConfig.enabled,level:this._loggerConfig.level},l)},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}}export{r as Logger,l 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={"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};
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 n=t.lambdaRequestTracker();exports.Logger=class{constructor(n){this._loggerConfig={enabled:!0,level:"info",format:"json"},this._instance=null,this._createLogger=()=>{const n="json"===this._loggerConfig.format?new t.StructuredLogFormatter:new t.CloudwatchLogFormatter,r=t.pinoLambdaDestination({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}},exports.withRequestTracking=n;
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;
2
2
  //# sourceMappingURL=index.js.map
@@ -0,0 +1,93 @@
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
+ * Represents the headers for a JSON API Gateway response
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;
16
+ /**
17
+ * Creates a standardized API Gateway response.
18
+ * @param statusCode The HTTP status code of the response
19
+ * @param body The body of the response
20
+ * @param headers Optional headers to include in the response
21
+ * @returns An API Gateway proxy result
22
+ *
23
+ * @example
24
+ * ```ts
25
+ * const response = createResponse(200, { message: 'Success' }, jsonHeaders);
26
+ * ```
27
+ */
28
+ export declare const createResponse: (statusCode: number, body: unknown, headers?: Headers) => APIGatewayProxyResult;
29
+ /**
30
+ * Creates a 200 OK API Gateway response
31
+ * @param body The body of the response
32
+ * @param headers Optional headers to include in the response
33
+ * @returns An API Gateway proxy result
34
+ * @example
35
+ * ```ts
36
+ * const response = ok({ message: 'Success' }, jsonHeaders);
37
+ * ```
38
+ */
39
+ export declare const ok: (body: unknown, headers?: Headers) => APIGatewayProxyResult;
40
+ /**
41
+ * Creates a 201 Created API Gateway response
42
+ * @param body The body of the response
43
+ * @param headers Optional headers to include in the response
44
+ * @returns An API Gateway proxy result
45
+ * @example
46
+ * ```ts
47
+ * const response = created({ message: 'Resource created' }, jsonHeaders);
48
+ * ```
49
+ */
50
+ export declare const created: (body: unknown, headers?: Headers) => APIGatewayProxyResult;
51
+ /**
52
+ * Creates a 204 No Content API Gateway response
53
+ * @param headers Optional headers to include in the response
54
+ * @returns An API Gateway proxy result
55
+ * @example
56
+ * ```ts
57
+ * const response = noContent(corsHeaders());
58
+ * ```
59
+ */
60
+ export declare const noContent: (headers?: Headers) => APIGatewayProxyResult;
61
+ /**
62
+ * Creates a 400 Bad Request API Gateway response
63
+ * @param message The error message to include in the response
64
+ * @param headers Optional headers to include in the response
65
+ * @returns An API Gateway proxy result
66
+ * @example
67
+ * ```ts
68
+ * const response = badRequest('Invalid input', jsonHeaders);
69
+ * ```
70
+ */
71
+ export declare const badRequest: (message?: string, headers?: Headers) => APIGatewayProxyResult;
72
+ /**
73
+ * Creates a 404 Not Found API Gateway response
74
+ * @param message The error message to include in the response
75
+ * @param headers Optional headers to include in the response
76
+ * @returns An API Gateway proxy result
77
+ * @example
78
+ * ```ts
79
+ * const response = notFound('Resource not found', jsonHeaders);
80
+ * ```
81
+ */
82
+ export declare const notFound: (message?: string, headers?: Headers) => APIGatewayProxyResult;
83
+ /**
84
+ * Creates a 500 Internal Server Error API Gateway response
85
+ * @param message The error message to include in the response
86
+ * @param headers Optional headers to include in the response
87
+ * @returns An API Gateway proxy result
88
+ * @example
89
+ * ```ts
90
+ * const response = internalServerError('Something went wrong', jsonHeaders);
91
+ * ```
92
+ */
93
+ 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.1.0",
3
+ "version": "0.2.0-alpha.2",
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",