lambder 1.0.66 → 1.0.68

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.
Files changed (2) hide show
  1. package/Readme.md +102 -25
  2. package/package.json +1 -1
package/Readme.md CHANGED
@@ -1,58 +1,135 @@
1
1
  # Lambder
2
2
 
3
- Lambder provides a simplified way to handle AWS Lambda functions with HTTP triggers, offering a clear structure for managing API endpoints, static file serving, hooks, and error handling.
3
+ Lambder is a dynamic serverless framework designed to facilitate the management and implementation of routes and APIs within AWS Lambda functions, specifically tailored for TypeScript projects. It provides a streamlined approach to handling HTTP requests, managing sessions, and defining API routes, making serverless application development more intuitive and structured.
4
4
 
5
5
  ## Features
6
6
 
7
- - Easy setup of API paths with conditions and actions.
8
- - Support for hooks at different stages of the request lifecycle.
9
- - Integrated cookie and query string parsing.
10
- - Customizable error handling and fallback mechanisms.
11
- - CORS support and easy configuration for public paths.
12
- - Response helpers for JSON, HTML, XML, files, and more.
7
+ - **Simple API & Route Declaration**: Define your APIs and routes using concise and expressive syntax.
8
+ - **Session Management**: Built-in session management to secure and personalize user experiences.
9
+ - **Flexible Hooks System**: Employ hooks to execute code at different stages of the request lifecycle, enabling fine-grained control over the application flow.
10
+ - **Error Handling**: Comprehensive error handling capabilities, including global error handlers and route-specific fallbacks.
11
+ - **Seamless Integration**: Designed to work effortlessly with AWS Lambda and API Gateway, providing a straightforward path to deploy serverless applications.
13
12
 
14
13
  ## Installation
15
14
 
15
+ To include Lambder in your TypeScript project, you can install it using npm or yarn. First, ensure that you have TypeScript set up in your project.
16
+
16
17
  ```bash
17
18
  npm install lambder
19
+ # or
20
+ yarn add lambder
18
21
  ```
19
22
 
20
- ## Quick Start
23
+ ## Backend Usage
24
+
25
+ ### Basic Setup
21
26
 
22
27
  ```typescript
23
28
  import Lambder from 'lambder';
24
- import { APIGatewayProxyEvent, Context } from 'aws-lambda';
29
+ import * as path from 'path';
25
30
 
26
- const app = new Lambder({
27
- apiPath: '/api',
28
- publicPath: '/public',
31
+ const lambder = new Lambder({
32
+ apiPath: "/secure",
29
33
  isCorsEnabled: true,
34
+ publicPath: path.resolve(`./public`),
35
+ });
36
+
37
+ // Define a simple api
38
+ lambder.addApi("fetchData", async (ctx, res) => {
39
+ const data = await fetchDataSomehow();
40
+ return res.api({ payload: data });
41
+ });
42
+
43
+ // Define a simple route
44
+ lambder.addRoute("/hello-world", (ctx, res) => {
45
+ return res.json({ message: "Hello World" });
46
+ });
47
+
48
+ // Match all other paths and serve static files from publicPath, if not found, serve index.html
49
+ lambder.addRoute("*", (ctx, res)=>{
50
+ return res.file(ctx.path, {}, "index.html");
51
+ });
52
+
53
+
54
+ // Set a fallback handler for unmatched routes
55
+ lambder.setRouteFallbackHandler((ctx, res) => {
56
+ return res.status404("Not Found");
30
57
  });
31
58
 
32
- // Define a simple API endpoint
33
- app.addApi('/hello', async (ctx) => {
34
- return ctx.resolver.json({ message: 'Hello, World!' });
59
+ // Global error handler
60
+ lambder.setGlobalErrorHandler((err, ctx, res) => {
61
+ console.error("Error:", err);
62
+ return res.status500("Internal Server Error");
63
+ });
64
+
65
+ export const handler = (event, context) => {
66
+ return lambder.render(event, context);
67
+ };
68
+ ```
69
+
70
+ ### Adding Hooks
71
+
72
+ ```typescript
73
+ // Before render hook example
74
+ lambder.addHook("beforeRender", async (ctx, res) => {
75
+ // Perform actions before rendering
76
+ if(ctx.method === "POST") {
77
+ // Validate CSRF token, for example
78
+ }
79
+ return ctx; // Return modified context or throw an Error
35
80
  });
81
+ ```
82
+
83
+ ## Frontend Usage with LambderCaller
84
+
85
+ LambderCaller is a frontend companion library for Lambder, it is only 2kb compressed, and designed to simplify making API requests to your Lambder backend services.
36
86
 
37
- // Handle 404 Not Found
38
- app.setGlobalErrorHandler((err: Error) => {
39
- return { statusCode: 404, body: 'Not Found' };
87
+ ### Installing LambderCaller
88
+
89
+ LambderCaller is included in the same `lambder` package. Ensure you have `lambder` available in your frontend project.
90
+
91
+ ### Basic Setup
92
+
93
+ Begin by initializing LambderCaller with your API configuration. This setup assumes your project structure accommodates a place for initiating and configuring API handlers, possibly within a dedicated JavaScript module or directly in your main application file.
94
+
95
+ ```javascript
96
+ import LambderCaller from "lambder/dist/LambderCaller";
97
+
98
+ const lambderCaller = new LambderCaller({
99
+ isCorsEnabled: false,
100
+ apiPath: "/secure", // Your Lambder API endpoint, must be the same as in your backend
101
+ fetchStartedHandler: ({ activeFetchList }) => {
102
+ // When any api call starts
103
+ },
104
+ fetchEndedHandler: ({ activeFetchList }) => {
105
+ // When any api call ends
106
+ },
107
+ errorMessageHandler: (message) => {
108
+ console.error("LambderCaller:", message); // Handle error messages
109
+ },
40
110
  });
41
111
 
42
- // Lambda handler
43
- exports.handler = async (event: APIGatewayProxyEvent, context: Context) => {
44
- return app.render(event, context);
112
+
113
+ const loadPageData = async () => {
114
+ try {
115
+ const response = await lambderCaller.api("getCompanyPage", {
116
+ subdomain: "example", // Pass necessary parameters for your API call
117
+ });
118
+ } catch (error) {
119
+ console.error("Failed to fetch page data:", error);
120
+ }
45
121
  };
122
+
46
123
  ```
47
124
 
48
- ## Documentation
125
+ ## Advanced Configuration
49
126
 
50
- For more details on how to use Lambder, including the full API reference, visit [Lambder Documentation](#).
127
+ Lambder is designed to be flexible and extensible, allowing for customized behaviors through hooks and custom error handling mechanisms.
51
128
 
52
129
  ## Contributing
53
130
 
54
- Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.
131
+ Contributions are welcome! Especially for documentation. If you have an idea for an improvement or have found a bug, please open an issue or submit a pull request.
55
132
 
56
133
  ## License
57
134
 
58
- MIT License
135
+ This project is licensed under the [MIT License](LICENSE).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lambder",
3
- "version": "1.0.66",
3
+ "version": "1.0.68",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",