lambder 1.0.78 → 1.0.80

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 +44 -1
  2. package/package.json +1 -1
package/Readme.md CHANGED
@@ -55,8 +55,19 @@ lambder.addRoute("/user/:userId", async (ctx, res) => {
55
55
  return res.json({ message: `Hello ${user.name}` });
56
56
  });
57
57
 
58
+ // Define a regex route
59
+ lambder.addRoute(/\/hello-regex/, (ctx, res) => {
60
+ return res.json({ message: "Hello Regex" });
61
+ });
62
+
63
+ // Define a function route
64
+ lambder.addRoute((ctx)=>ctx.path === '/hello-fn-route', (ctx, res) => {
65
+ return res.json({ message: "Hello from a function route" });
66
+ });
67
+
68
+
58
69
  // Match all other paths and serve static files from publicPath, if not found, serve index.html
59
- lambder.addRoute("*", (ctx, res)=>{
70
+ lambder.addRoute("/(.*)", (ctx, res)=>{
60
71
  return res.file(ctx.path, {}, "index.html");
61
72
  });
62
73
 
@@ -77,6 +88,38 @@ export const handler = (event, context) => {
77
88
  };
78
89
  ```
79
90
 
91
+
92
+
93
+ ### Adding APIs
94
+
95
+ For more details on route matching, please check [path-to-regexp](https://www.npmjs.com/package/path-to-regexp) package.
96
+
97
+ ```typescript
98
+ // Define a simple api
99
+ lambder.addApi("getCompanyPage", async (ctx, res) => {
100
+ const { host, path, get, post, cookie, headers, apiName, apiPayload } = ctx;
101
+ const companyName = post.payload.companyName;
102
+ const data = await fetchDataSomehow(companyName);
103
+ return res.api({ payload: data });
104
+ });
105
+ ```
106
+
107
+
108
+ ### Adding Routes
109
+
110
+ For more details on route matching, please check [path-to-regexp](https://www.npmjs.com/package/path-to-regexp) package.
111
+
112
+ ```typescript
113
+ // Route with parameters
114
+ lambder.addRoute("/user/:userId", async (ctx, res) => {
115
+ const user = await getUser(ctx.pathParams.userId);
116
+ if(!user) return res.status404("Not found");
117
+
118
+ return res.html(`Hello ${user.name}`);
119
+ });
120
+ ```
121
+
122
+
80
123
  ### Adding Hooks
81
124
 
82
125
  ```typescript
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lambder",
3
- "version": "1.0.78",
3
+ "version": "1.0.80",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",