@leanstacks/lambda-utils 0.1.0-alpha.3 → 0.1.0-alpha.5

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.
@@ -0,0 +1,47 @@
1
+ ---
2
+ name: Bug report
3
+ about: Create a report to help us improve
4
+ title: ''
5
+ labels: bug
6
+ assignees: ''
7
+ ---
8
+
9
+ ## Describe the bug
10
+
11
+ _Provide a clear and concise description of what the bug is._
12
+
13
+ ## Steps to reproduce
14
+
15
+ Steps to reproduce the behavior:
16
+
17
+ 1. Go to '...'
18
+ 2. Click on '....'
19
+ 3. Scroll down to '....'
20
+ 4. See error
21
+
22
+ ## Expected behavior
23
+
24
+ _Provide a clear and concise description of what you expected to happen._
25
+
26
+ ## Screenshots
27
+
28
+ _If applicable, add screenshots to help explain your problem._
29
+
30
+ ## Environment
31
+
32
+ **Desktop (please complete the following information):**
33
+
34
+ - OS: [e.g. iOS]
35
+ - Browser [e.g. chrome, safari]
36
+ - Version [e.g. 22]
37
+
38
+ **Smartphone (please complete the following information):**
39
+
40
+ - Device: [e.g. iPhone6]
41
+ - OS: [e.g. iOS8.1]
42
+ - Browser [e.g. stock browser, safari]
43
+ - Version [e.g. 22]
44
+
45
+ ## Additional context
46
+
47
+ _Add any other context about the problem here._
@@ -0,0 +1,25 @@
1
+ ---
2
+ name: Story
3
+ about: New feature or improvement request
4
+ title: ''
5
+ labels: enhancement
6
+ assignees: ''
7
+ ---
8
+
9
+ ## Describe the story
10
+
11
+ _Provide a clear description of the new feature or improvement to existing functionality._
12
+
13
+ ## Acceptance criteria
14
+
15
+ _Provide clear acceptance criteria to validate the story is complete._
16
+
17
+ [Gherkin syntax](https://cucumber.io/docs/gherkin/reference) example:
18
+
19
+ > Given the 'PERSONA' has 'DONE SOMETHING'
20
+ > When the 'PERSONA' does 'ONE THING'
21
+ > Then the 'PERSONA' must do 'ANOTHER THING'
22
+
23
+ ## Additional context
24
+
25
+ _Add any other context about the story here._
@@ -0,0 +1,15 @@
1
+ ---
2
+ name: Task
3
+ about: A chore unrelated to features or problems
4
+ title: ''
5
+ labels: task
6
+ assignees: ''
7
+ ---
8
+
9
+ ## Describe the task
10
+
11
+ _Provide a clear description of the task._
12
+
13
+ ## Additional context
14
+
15
+ _Add any other context about the task here._
@@ -0,0 +1,39 @@
1
+ :loudspeaker: **Instructions**
2
+
3
+ - Begin with a **DRAFT** pull request.
4
+ - Follow _italicized instructions_ to add detail to assist the reviewers.
5
+ - After completing all checklist items, change the pull request to **READY**.
6
+
7
+ ---
8
+
9
+ ### :wrench: Change Summary
10
+
11
+ _Describe the changes included in this pull request. Link to the associated [GitHub](https://docs.github.com/en/issues/tracking-your-work-with-issues/using-issues/linking-a-pull-request-to-an-issue#linking-a-pull-request-to-an-issue-using-a-keyword) or Jira issue(s)._
12
+
13
+ - see #1234
14
+ - Added the [...]
15
+ - Updated the [...]
16
+ - Fixed the [...]
17
+
18
+ ### :memo: Checklist
19
+
20
+ _Pull request authors must complete the following tasks before marking the PR as ready to review._
21
+
22
+ - [ ] Complete a self-review of changes
23
+ - [ ] Unit tests have been created or updated
24
+ - [ ] The code is free of [new] lint errors and warnings
25
+ - [ ] Update project documentation as needed: README, /docs, JSDoc, etc.
26
+
27
+ ### :test_tube: Steps to Test
28
+
29
+ _Describe the process to test the changes in this pull request._
30
+
31
+ 1. Go to [...]
32
+ 2. Click on [...]
33
+ 3. Verify that [...]
34
+
35
+ ### :link: Additional Information
36
+
37
+ _Optionally, provide additional details, screenshots, or URLs that may assist the reviewer._
38
+
39
+ - [...]
@@ -0,0 +1,43 @@
1
+ name: Continuous Integration
2
+
3
+ on:
4
+ pull_request:
5
+ branches:
6
+ - main
7
+ workflow_dispatch:
8
+
9
+ concurrency:
10
+ group: ${{ github.workflow }}-${{ github.ref }}
11
+ cancel-in-progress: true
12
+
13
+ jobs:
14
+ ci:
15
+ name: Build, Lint, and Test
16
+
17
+ runs-on: ubuntu-latest
18
+ timeout-minutes: 5
19
+
20
+ steps:
21
+ - name: Checkout repository
22
+ uses: actions/checkout@v5
23
+
24
+ - name: Setup Node.js
25
+ uses: actions/setup-node@v5
26
+ with:
27
+ node-version-file: '.nvmrc'
28
+ cache: 'npm'
29
+
30
+ - name: Install dependencies
31
+ run: npm ci
32
+
33
+ - name: Lint code
34
+ run: npm run lint
35
+
36
+ - name: Check code formatting
37
+ run: npm run format:check
38
+
39
+ - name: Build
40
+ run: npm run build
41
+
42
+ - name: Run tests with coverage
43
+ run: npm run test:coverage
@@ -0,0 +1,51 @@
1
+ name: Publish
2
+
3
+ on:
4
+ release:
5
+ types:
6
+ - published
7
+ workflow_dispatch:
8
+
9
+ concurrency:
10
+ group: ${{ github.workflow }}
11
+ cancel-in-progress: false
12
+
13
+ permissions:
14
+ id-token: write
15
+ contents: read
16
+
17
+ jobs:
18
+ build:
19
+ name: Build and Publish
20
+
21
+ runs-on: ubuntu-latest
22
+ timeout-minutes: 10
23
+
24
+ steps:
25
+ - name: Checkout repository
26
+ uses: actions/checkout@v5
27
+
28
+ - name: Setup Node.js
29
+ uses: actions/setup-node@v5
30
+ with:
31
+ node-version-file: '.nvmrc'
32
+ cache: 'npm'
33
+ registry-url: 'https://registry.npmjs.org'
34
+
35
+ - name: Install dependencies
36
+ run: npm ci
37
+
38
+ - name: Lint code
39
+ run: npm run lint
40
+
41
+ - name: Check code formatting
42
+ run: npm run format:check
43
+
44
+ - name: Run tests
45
+ run: npm run test
46
+
47
+ - name: Build
48
+ run: npm run build
49
+
50
+ - name: Publish
51
+ run: npm publish
@@ -0,0 +1,3 @@
1
+ npm test
2
+ npm run format:check
3
+ npm run lint
package/README.md CHANGED
@@ -1,3 +1,180 @@
1
1
  # Lambda Utilities
2
2
 
3
- Lambda utilities
3
+ [![npm version](https://badge.fury.io/js/@leanstacks%2Flambda-utils.svg)](https://badge.fury.io/js/@leanstacks%2Flambda-utils)
4
+ [![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
5
+
6
+ A comprehensive TypeScript utility library for AWS Lambda functions. Provides pre-configured logging, API response formatting, configuration validation, and AWS SDK clients—reducing boilerplate and promoting best practices.
7
+
8
+ ## Table of Contents
9
+
10
+ - [Installation](#installation)
11
+ - [Quick Start](#quick-start)
12
+ - [Features](#features)
13
+ - [Documentation](#documentation)
14
+ - [Contributing](#contributing)
15
+ - [License](#license)
16
+ - [Support](#support)
17
+
18
+ ## Installation
19
+
20
+ ```bash
21
+ npm install @leanstacks/lambda-utils
22
+ ```
23
+
24
+ ### Requirements
25
+
26
+ - Node.js 24.x or higher
27
+ - TypeScript 5.0 or higher
28
+
29
+ ## Quick Start
30
+
31
+ ### Logging Example
32
+
33
+ ```typescript
34
+ import { Logger, withRequestTracking } from '@leanstacks/lambda-utils';
35
+
36
+ const logger = new Logger().instance;
37
+
38
+ export const handler = async (event: any, context: any) => {
39
+ withRequestTracking(event, context);
40
+
41
+ logger.info('Processing request');
42
+
43
+ // Your Lambda handler logic here
44
+
45
+ return { statusCode: 200, body: 'Success' };
46
+ };
47
+ ```
48
+
49
+ ### API Response Example
50
+
51
+ ```typescript
52
+ import { success, badRequest } from '@leanstacks/lambda-utils';
53
+
54
+ export const handler = async (event: any) => {
55
+ if (!event.body) {
56
+ return badRequest({ message: 'Body is required' });
57
+ }
58
+
59
+ // Process request
60
+
61
+ return success({ message: 'Request processed successfully' });
62
+ };
63
+ ```
64
+
65
+ ## Features
66
+
67
+ - **📝 Structured Logging** – Pino logger pre-configured for Lambda with automatic AWS request context enrichment
68
+ - **📤 API Response Helpers** – Standard response formatting for API Gateway with proper HTTP status codes
69
+ - **⚙️ Configuration Validation** – Environment variable validation with Zod schema support
70
+ - **🔌 AWS SDK Clients** – Pre-configured AWS SDK v3 clients for DynamoDB, Lambda, and more
71
+ - **🔒 Full TypeScript Support** – Complete type definitions and IDE autocomplete
72
+ - **⚡ Lambda Optimized** – Designed for performance in serverless environments
73
+
74
+ ## Documentation
75
+
76
+ Comprehensive guides and examples are available in the `docs` directory:
77
+
78
+ | Guide | Description |
79
+ | ------------------------------------------------------------ | ---------------------------------------------------------------------- |
80
+ | **[Logging Guide](./docs/LOGGING.md)** | Configure and use structured logging with automatic AWS Lambda context |
81
+ | **[API Gateway Responses](./docs/API_GATEWAY_RESPONSES.md)** | Format responses for API Gateway with standard HTTP patterns |
82
+ | **[Configuration](./docs/CONFIGURATION.md)** | Validate and manage environment variables with type safety |
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 |
85
+
86
+ ## Usage
87
+
88
+ ### Logging
89
+
90
+ The Logger utility provides structured logging configured specifically for AWS Lambda:
91
+
92
+ ```typescript
93
+ import { Logger } from '@leanstacks/lambda-utils';
94
+
95
+ const logger = new Logger({
96
+ level: 'info', // debug, info, warn, error
97
+ format: 'json', // json or text
98
+ }).instance;
99
+
100
+ logger.info({ message: 'User authenticated', userId: '12345' });
101
+ logger.error({ message: 'Operation failed', error: err.message });
102
+ ```
103
+
104
+ **→ See [Logging Guide](./docs/LOGGING.md) for detailed configuration and best practices**
105
+
106
+ ### API Responses
107
+
108
+ Generate properly formatted responses for API Gateway:
109
+
110
+ ```typescript
111
+ import { success, error, created, badRequest } from '@leanstacks/lambda-utils';
112
+
113
+ export const handler = async (event: any) => {
114
+ return success({
115
+ data: { id: '123', name: 'Example' },
116
+ });
117
+ };
118
+ ```
119
+
120
+ **→ See [API Gateway Responses](./docs/API_GATEWAY_RESPONSES.md) for all response types**
121
+
122
+ ### Configuration Validation
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
+ ```
138
+
139
+ **→ See [Configuration](./docs/CONFIGURATION.md) for validation patterns**
140
+
141
+ ### AWS Clients
142
+
143
+ Use pre-configured AWS SDK v3 clients:
144
+
145
+ ```typescript
146
+ import { getDynamoDBClient, getLambdaClient } from '@leanstacks/lambda-utils';
147
+
148
+ const dynamoDB = getDynamoDBClient();
149
+ const lambda = getLambdaClient();
150
+
151
+ // Use clients for API calls
152
+ ```
153
+
154
+ **→ See [AWS Clients](./docs/CLIENTS.md) for available clients and examples**
155
+
156
+ ## Examples
157
+
158
+ Example Lambda functions using Lambda Utilities are available in the repository:
159
+
160
+ - API Gateway with logging and response formatting
161
+ - Configuration validation and DynamoDB integration
162
+ - Error handling and structured logging
163
+
164
+ ## Reporting Issues
165
+
166
+ If you encounter a bug or have a feature request, please report it on [GitHub Issues](https://github.com/leanstacks/lambda-utils/issues). Include as much detail as possible to help us investigate and resolve the issue quickly.
167
+
168
+ ## License
169
+
170
+ This project is licensed under the MIT License - see [LICENSE](./LICENSE) file for details.
171
+
172
+ ## Support
173
+
174
+ - **Issues & Questions:** [GitHub Issues](https://github.com/leanstacks/lambda-utils/issues)
175
+ - **Documentation:** [docs](./docs/README.md)
176
+ - **NPM Package:** [@leanstacks/lambda-utils](https://www.npmjs.com/package/@leanstacks/lambda-utils)
177
+
178
+ ## Changelog
179
+
180
+ See the project [releases](https://github.com/leanstacks/lambda-utils/releases) for version history and updates.
@@ -0,0 +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};
2
+ //# sourceMappingURL=index.esm.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.esm.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
package/dist/index.js CHANGED
@@ -1,78 +1,2 @@
1
- import pino from 'pino';
2
- import { lambdaRequestTracker, StructuredLogFormatter, CloudwatchLogFormatter, pinoLambdaDestination } from 'pino-lambda';
3
-
4
- /**
5
- * Logger middleware which adds AWS Lambda attributes to log messages.
6
- *
7
- * @example
8
- * ```typescript
9
- * import { withRequestTracking } from '@leanstacks/lambda-utils';
10
- *
11
- * export const handler = async (event, context) => {
12
- * withRequestTracking(event, context);
13
- *
14
- * // Your Lambda handler logic here
15
- * };
16
- * ```
17
- */
18
- const withRequestTracking = lambdaRequestTracker();
19
- /**
20
- * Logger class which provides a Pino logger instance with AWS Lambda attributes.
21
- *
22
- * @example
23
- * ```typescript
24
- * import { Logger } from '@leanstacks/lambda-utils';
25
- * const logger = new Logger().instance;
26
- *
27
- * logger.info('Hello, world!');
28
- * ```
29
- */
30
- class Logger {
31
- constructor(config) {
32
- this._loggerConfig = {
33
- enabled: true,
34
- level: 'info',
35
- format: 'json',
36
- };
37
- this._instance = null;
38
- /**
39
- * Creates a new, fully configured Pino logger instance.
40
- */
41
- this._createLogger = () => {
42
- const formatter = this._loggerConfig.format === 'json' ? new StructuredLogFormatter() : new CloudwatchLogFormatter();
43
- const lambdaDestination = pinoLambdaDestination({
44
- formatter,
45
- });
46
- return pino({
47
- enabled: this._loggerConfig.enabled,
48
- level: this._loggerConfig.level,
49
- }, lambdaDestination);
50
- };
51
- if (config) {
52
- this._loggerConfig = {
53
- enabled: config.enabled ?? true,
54
- level: config.level ?? 'info',
55
- format: config.format ?? 'json',
56
- };
57
- }
58
- }
59
- /**
60
- * Get the logger instance.
61
- *
62
- * @example
63
- * ```typescript
64
- * import { Logger } from '@leanstacks/lambda-utils';
65
- * const logger = new Logger().instance;
66
- *
67
- * logger.info('Hello, world!');
68
- * ```
69
- */
70
- get instance() {
71
- if (this._instance === null) {
72
- this._instance = this._createLogger();
73
- }
74
- return this._instance;
75
- }
76
- }
77
-
78
- export { Logger, withRequestTracking };
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;
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}