lambder 1.0.66 → 1.0.67
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 +102 -25
- package/package.json +1 -1
package/Readme.md
CHANGED
|
@@ -1,58 +1,135 @@
|
|
|
1
1
|
# Lambder
|
|
2
2
|
|
|
3
|
-
Lambder
|
|
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
|
-
-
|
|
8
|
-
-
|
|
9
|
-
-
|
|
10
|
-
-
|
|
11
|
-
-
|
|
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
|
-
##
|
|
23
|
+
## Backend Usage
|
|
24
|
+
|
|
25
|
+
### Basic Setup
|
|
21
26
|
|
|
22
27
|
```typescript
|
|
23
28
|
import Lambder from 'lambder';
|
|
24
|
-
import
|
|
29
|
+
import * as path from 'path';
|
|
25
30
|
|
|
26
|
-
const
|
|
27
|
-
apiPath:
|
|
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
|
-
//
|
|
33
|
-
|
|
34
|
-
|
|
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, designed to simplify making API requests to your Lambder backend services.
|
|
36
86
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
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
|
-
##
|
|
125
|
+
## Advanced Configuration
|
|
49
126
|
|
|
50
|
-
|
|
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!
|
|
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).
|