@leanstacks/lambda-utils 0.3.0-alpha.1 → 0.3.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/README.md +22 -28
- package/dist/index.d.ts +1 -0
- package/dist/index.esm.js +1 -1
- package/dist/index.js +1 -1
- package/dist/validation/zod-validator.d.ts +9 -0
- package/package.json +6 -5
package/README.md
CHANGED
|
@@ -67,7 +67,7 @@ export const handler = async (event: APIGatewayProxyEvent) => {
|
|
|
67
67
|
- **📝 Structured Logging** – Pino logger pre-configured for Lambda with automatic AWS request context enrichment
|
|
68
68
|
- **📤 API Response Helpers** – Standard response formatting for API Gateway with proper HTTP status codes
|
|
69
69
|
- **⚙️ Configuration Validation** – Environment variable validation with Zod schema support
|
|
70
|
-
- **🔌 AWS SDK Clients** – Pre-configured AWS SDK v3 clients
|
|
70
|
+
- **🔌 AWS SDK Clients** – Pre-configured AWS SDK v3 clients including DynamoDB with document client support
|
|
71
71
|
- **🔒 Full TypeScript Support** – Complete type definitions and IDE autocomplete
|
|
72
72
|
- **⚡ Lambda Optimized** – Designed for performance in serverless environments
|
|
73
73
|
|
|
@@ -79,9 +79,7 @@ Comprehensive guides and examples are available in the `docs` directory:
|
|
|
79
79
|
| ------------------------------------------------------------ | ---------------------------------------------------------------------- |
|
|
80
80
|
| **[Logging Guide](./docs/LOGGING.md)** | Configure and use structured logging with automatic AWS Lambda context |
|
|
81
81
|
| **[API Gateway Responses](./docs/API_GATEWAY_RESPONSES.md)** | Format responses for API Gateway with standard HTTP patterns |
|
|
82
|
-
| **[
|
|
83
|
-
| **[AWS Clients](./docs/CLIENTS.md)** | Use pre-configured AWS SDK v3 clients in your handlers |
|
|
84
|
-
| **[Getting Started](./docs/GETTING_STARTED.md)** | Setup and first steps guide |
|
|
82
|
+
| **[AWS Clients](./docs/README.md)** | Use pre-configured AWS SDK v3 clients in your handlers |
|
|
85
83
|
|
|
86
84
|
## Usage
|
|
87
85
|
|
|
@@ -119,39 +117,35 @@ export const handler = async (event: APIGatewayProxyEvent) => {
|
|
|
119
117
|
|
|
120
118
|
**→ See [API Gateway Responses](./docs/API_GATEWAY_RESPONSES.md) for all response types**
|
|
121
119
|
|
|
122
|
-
###
|
|
123
|
-
|
|
124
|
-
Validate your Lambda environment configuration:
|
|
125
|
-
|
|
126
|
-
```typescript
|
|
127
|
-
import { validateConfig } from '@leanstacks/lambda-utils';
|
|
128
|
-
import { z } from 'zod';
|
|
129
|
-
|
|
130
|
-
const configSchema = z.object({
|
|
131
|
-
DATABASE_URL: z.string().url(),
|
|
132
|
-
LOG_LEVEL: z.enum(['debug', 'info', 'warn', 'error']),
|
|
133
|
-
API_KEY: z.string(),
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
const config = validateConfig(configSchema);
|
|
137
|
-
```
|
|
120
|
+
### AWS Clients
|
|
138
121
|
|
|
139
|
-
|
|
122
|
+
Use pre-configured AWS SDK v3 clients. Currently available:
|
|
140
123
|
|
|
141
|
-
|
|
124
|
+
#### DynamoDB Client
|
|
142
125
|
|
|
143
|
-
|
|
126
|
+
Initialize the DynamoDB clients (base client and document client) once during handler initialization:
|
|
144
127
|
|
|
145
128
|
```typescript
|
|
146
|
-
import {
|
|
129
|
+
import { initializeDynamoDBClients, getDynamoDBDocumentClient } from '@leanstacks/lambda-utils';
|
|
147
130
|
|
|
148
|
-
const
|
|
149
|
-
|
|
131
|
+
export const handler = async (event: any, context: any) => {
|
|
132
|
+
// Initialize clients once
|
|
133
|
+
initializeDynamoDBClients({ region: 'us-east-1' });
|
|
134
|
+
|
|
135
|
+
// Use the document client for operations
|
|
136
|
+
const docClient = getDynamoDBDocumentClient();
|
|
137
|
+
const result = await docClient.get({
|
|
138
|
+
TableName: 'MyTable',
|
|
139
|
+
Key: { id: 'item-123' },
|
|
140
|
+
});
|
|
150
141
|
|
|
151
|
-
|
|
142
|
+
return { statusCode: 200, body: JSON.stringify(result) };
|
|
143
|
+
};
|
|
152
144
|
```
|
|
153
145
|
|
|
154
|
-
**→ See [
|
|
146
|
+
**→ See [DynamoDB Client Guide](./docs/DYNAMODB_CLIENT.md) for detailed configuration and examples**
|
|
147
|
+
|
|
148
|
+
Additional AWS Clients are coming soon.
|
|
155
149
|
|
|
156
150
|
## Examples
|
|
157
151
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { Logger, LoggerConfig, withRequestTracking } from './logging/logger';
|
|
2
2
|
export { createResponse, ok, created, noContent, badRequest, notFound, internalServerError, httpHeaders, Headers, } from './utils/apigateway-response';
|
|
3
3
|
export { getDynamoDBClient, getDynamoDBDocumentClient, initializeDynamoDBClients, resetDynamoDBClients, } from './clients/dynamodb-client';
|
|
4
|
+
export { validate } from './validation/zod-validator';
|
package/dist/index.esm.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import n from"pino";import{lambdaRequestTracker as e,StructuredLogFormatter as
|
|
1
|
+
import n from"pino";import{lambdaRequestTracker as e,StructuredLogFormatter as o,CloudwatchLogFormatter as t,pinoLambdaDestination as r}from"pino-lambda";import{DynamoDBClient as i}from"@aws-sdk/client-dynamodb";import{DynamoDBDocumentClient as s}from"@aws-sdk/lib-dynamodb";import{z as l}from"zod";const a=e();class c{constructor(e){this._loggerConfig={enabled:!0,level:"info",format:"json"},this._instance=null,this._createLogger=()=>{const e="json"===this._loggerConfig.format?new o:new t,i=r({formatter:e});return n({enabled:this._loggerConfig.enabled,level:this._loggerConfig.level},i)},e&&(this._loggerConfig={enabled:e.enabled??!0,level:e.level??"info",format:e.format??"json"})}get instance(){return null===this._instance&&(this._instance=this._createLogger()),this._instance}}const m={contentType:n=>({"Content-Type":n}),json:{"Content-Type":"application/json"},cors:(n="*")=>({"Access-Control-Allow-Origin":n})},f=(n,e,o={})=>({statusCode:n,headers:{...o},body:JSON.stringify(e)}),g=(n,e={})=>f(200,n,e),d=(n,e={})=>f(201,n,e),u=(n={})=>f(204,{},n),h=(n="Bad Request",e={})=>f(400,{message:n},e),p=(n="Not Found",e={})=>f(404,{message:n},e),w=(n="Internal Server Error",e={})=>f(500,{message:n},e);let C=null,y=null;const _=(n,e,o)=>{C=new i(n||{});const t={marshallOptions:e||{},unmarshallOptions:o||{}};return y=s.from(C,t),{client:C,documentClient:y}},b=()=>{if(!C)throw new Error("DynamoDB client not initialized. Call initializeDynamoDBClients() first.");return C},D=()=>{if(!y)throw new Error("DynamoDB Document client not initialized. Call initializeDynamoDBClients() first.");return y},v=()=>{C=null,y=null},E=(n,e)=>{try{return n.parse(e)}catch(n){if(n instanceof l.ZodError){const e=n.issues.map(n=>n.message).join(", ");throw new Error(`Validation failed: ${e}`)}if(n instanceof Error)throw n;{const e=String(n);throw new Error(`An unknown error occurred during validation: ${e}`)}}};export{c as Logger,h as badRequest,f as createResponse,d as created,b as getDynamoDBClient,D as getDynamoDBDocumentClient,m as httpHeaders,_ as initializeDynamoDBClients,w as internalServerError,u as noContent,p as notFound,g as ok,v as resetDynamoDBClients,E as validate,a 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"),n=require("@aws-sdk/client-dynamodb"),
|
|
1
|
+
"use strict";var e=require("pino"),t=require("pino-lambda"),n=require("@aws-sdk/client-dynamodb"),r=require("@aws-sdk/lib-dynamodb"),o=require("zod");const i=t.lambdaRequestTracker();const s=(e,t,n={})=>({statusCode:e,headers:{...n},body:JSON.stringify(t)});let a=null,l=null;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.badRequest=(e="Bad Request",t={})=>s(400,{message:e},t),exports.createResponse=s,exports.created=(e,t={})=>s(201,e,t),exports.getDynamoDBClient=()=>{if(!a)throw new Error("DynamoDB client not initialized. Call initializeDynamoDBClients() first.");return a},exports.getDynamoDBDocumentClient=()=>{if(!l)throw new Error("DynamoDB Document client not initialized. Call initializeDynamoDBClients() first.");return l},exports.httpHeaders={contentType:e=>({"Content-Type":e}),json:{"Content-Type":"application/json"},cors:(e="*")=>({"Access-Control-Allow-Origin":e})},exports.initializeDynamoDBClients=(e,t,o)=>{a=new n.DynamoDBClient(e||{});const i={marshallOptions:t||{},unmarshallOptions:o||{}};return l=r.DynamoDBDocumentClient.from(a,i),{client:a,documentClient:l}},exports.internalServerError=(e="Internal Server Error",t={})=>s(500,{message:e},t),exports.noContent=(e={})=>s(204,{},e),exports.notFound=(e="Not Found",t={})=>s(404,{message:e},t),exports.ok=(e,t={})=>s(200,e,t),exports.resetDynamoDBClients=()=>{a=null,l=null},exports.validate=(e,t)=>{try{return e.parse(t)}catch(e){if(e instanceof o.z.ZodError){const t=e.issues.map(e=>e.message).join(", ");throw new Error(`Validation failed: ${t}`)}if(e instanceof Error)throw e;{const t=String(e);throw new Error(`An unknown error occurred during validation: ${t}`)}}},exports.withRequestTracking=i;
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
/**
|
|
3
|
+
* Validates the given data against the provided Zod schema.
|
|
4
|
+
* @param schema The Zod schema to validate against.
|
|
5
|
+
* @param data The data to be validated.
|
|
6
|
+
* @returns The validated data if it passes the schema validation.
|
|
7
|
+
* @throws {Error} If the validation fails or an unknown error occurs.
|
|
8
|
+
*/
|
|
9
|
+
export declare const validate: (schema: z.ZodSchema, data: unknown) => unknown;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@leanstacks/lambda-utils",
|
|
3
|
-
"version": "0.3.0-alpha.
|
|
3
|
+
"version": "0.3.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",
|
|
@@ -63,9 +63,10 @@
|
|
|
63
63
|
"typescript": "5.9.3"
|
|
64
64
|
},
|
|
65
65
|
"dependencies": {
|
|
66
|
-
"@aws-sdk/client-dynamodb": "
|
|
67
|
-
"@aws-sdk/lib-dynamodb": "
|
|
68
|
-
"pino": "
|
|
69
|
-
"pino-lambda": "
|
|
66
|
+
"@aws-sdk/client-dynamodb": "3.955.0",
|
|
67
|
+
"@aws-sdk/lib-dynamodb": "3.955.0",
|
|
68
|
+
"pino": "10.1.0",
|
|
69
|
+
"pino-lambda": "4.4.1",
|
|
70
|
+
"zod": "4.2.1"
|
|
70
71
|
}
|
|
71
72
|
}
|