@leanstacks/lambda-utils 0.3.0-alpha.2 → 0.3.0-alpha.4
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 -2
- package/dist/index.esm.js +1 -1
- package/dist/index.js +1 -1
- package/dist/validation/config.d.ts +45 -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,5 +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 {
|
|
5
|
-
export { DynamoDBDocumentClient } from '@aws-sdk/lib-dynamodb';
|
|
4
|
+
export { createConfigManager, ConfigManager } from './validation/config';
|
package/dist/index.esm.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import n from"pino";import{lambdaRequestTracker as e,StructuredLogFormatter as t,CloudwatchLogFormatter as o,pinoLambdaDestination as
|
|
1
|
+
import n from"pino";import{lambdaRequestTracker as e,StructuredLogFormatter as t,CloudwatchLogFormatter as o,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 t:new o,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,t={})=>({statusCode:n,headers:{...t},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),C=(n="Internal Server Error",e={})=>f(500,{message:n},e);let w=null,y=null;const _=(n,e,t)=>{w=new i(n||{});const o={marshallOptions:e||{},unmarshallOptions:t||{}};return y=s.from(w,o),{client:w,documentClient:y}},b=()=>{if(!w)throw new Error("DynamoDB client not initialized. Call initializeDynamoDBClients() first.");return w},D=()=>{if(!y)throw new Error("DynamoDB Document client not initialized. Call initializeDynamoDBClients() first.");return y},v=()=>{w=null,y=null},j=n=>{let e=null;const t=()=>{try{return n.parse(process.env)}catch(n){if(n instanceof l.ZodError){const e=n.issues.map(n=>`${n.path.join(".")}: ${n.message}`).join("\n");throw new Error(`Configuration validation failed:\n${e}`)}throw n}};return{get:()=>(e||(e=t()),e),refresh:()=>(e=t(),e)}};export{c as Logger,h as badRequest,j as createConfigManager,f as createResponse,d as created,b as getDynamoDBClient,D as getDynamoDBDocumentClient,m as httpHeaders,_ as initializeDynamoDBClients,C as internalServerError,u as noContent,p as notFound,g as ok,v as resetDynamoDBClients,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.createConfigManager=e=>{let t=null;const n=()=>{try{return e.parse(process.env)}catch(e){if(e instanceof o.z.ZodError){const t=e.issues.map(e=>`${e.path.join(".")}: ${e.message}`).join("\n");throw new Error(`Configuration validation failed:\n${t}`)}throw e}};return{get:()=>(t||(t=n()),t),refresh:()=>(t=n(),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.withRequestTracking=i;
|
|
2
2
|
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
/**
|
|
3
|
+
* Interface for a configuration manager instance
|
|
4
|
+
*/
|
|
5
|
+
export interface ConfigManager<T> {
|
|
6
|
+
/**
|
|
7
|
+
* Get the validated configuration (cached after first call)
|
|
8
|
+
* @throws {Error} if validation fails
|
|
9
|
+
* @returns The validated configuration object
|
|
10
|
+
*/
|
|
11
|
+
get: () => T;
|
|
12
|
+
/**
|
|
13
|
+
* Refresh the configuration by re-validating environment variables
|
|
14
|
+
* Useful in tests when environment variables are changed
|
|
15
|
+
* @throws {Error} if validation fails
|
|
16
|
+
* @returns The newly validated configuration object
|
|
17
|
+
*/
|
|
18
|
+
refresh: () => T;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Creates a reusable configuration manager for any Lambda function
|
|
22
|
+
*
|
|
23
|
+
* @template T - The configuration type inferred from the provided Zod schema
|
|
24
|
+
* @param schema - A Zod schema that defines the structure and validation rules for environment variables
|
|
25
|
+
* @returns A ConfigManager instance with get() and refresh() methods
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* ```typescript
|
|
29
|
+
* // Define your schema
|
|
30
|
+
* const configSchema = z.object({
|
|
31
|
+
* TABLE_NAME: z.string().min(1),
|
|
32
|
+
* AWS_REGION: z.string().default('us-east-1'),
|
|
33
|
+
* });
|
|
34
|
+
*
|
|
35
|
+
* // Create config manager
|
|
36
|
+
* const configManager = createConfigManager(configSchema);
|
|
37
|
+
*
|
|
38
|
+
* // Access configuration (cached on first call)
|
|
39
|
+
* const config = configManager.get();
|
|
40
|
+
*
|
|
41
|
+
* // Type your config
|
|
42
|
+
* type Config = z.infer<typeof configSchema>;
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export declare const createConfigManager: <T extends z.ZodSchema>(schema: T) => ConfigManager<z.infer<T>>;
|
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.4",
|
|
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
|
}
|