lambder 1.0.68 → 1.0.70

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 +17 -8
  2. package/package.json +1 -1
package/Readme.md CHANGED
@@ -35,8 +35,10 @@ const lambder = new Lambder({
35
35
  });
36
36
 
37
37
  // Define a simple api
38
- lambder.addApi("fetchData", async (ctx, res) => {
39
- const data = await fetchDataSomehow();
38
+ lambder.addApi("getCompanyPage", async (ctx, res) => {
39
+ const { host, path, get, post, cookie, headers } = ctx;
40
+ const companyName = post.payload.companyName;
41
+ const data = await fetchDataSomehow(companyName);
40
42
  return res.api({ payload: data });
41
43
  });
42
44
 
@@ -45,6 +47,14 @@ lambder.addRoute("/hello-world", (ctx, res) => {
45
47
  return res.json({ message: "Hello World" });
46
48
  });
47
49
 
50
+ // Route with parameters
51
+ lambder.addRoute("/user/:userId", async (ctx, res) => {
52
+ const user = await getUser(ctx.pathParams.userId);
53
+ if(!user) return res.status404("Not found");
54
+
55
+ return res.json({ message: `Hello ${user.name}` });
56
+ });
57
+
48
58
  // Match all other paths and serve static files from publicPath, if not found, serve index.html
49
59
  lambder.addRoute("*", (ctx, res)=>{
50
60
  return res.file(ctx.path, {}, "index.html");
@@ -73,9 +83,6 @@ export const handler = (event, context) => {
73
83
  // Before render hook example
74
84
  lambder.addHook("beforeRender", async (ctx, res) => {
75
85
  // Perform actions before rendering
76
- if(ctx.method === "POST") {
77
- // Validate CSRF token, for example
78
- }
79
86
  return ctx; // Return modified context or throw an Error
80
87
  });
81
88
  ```
@@ -98,11 +105,13 @@ import LambderCaller from "lambder/dist/LambderCaller";
98
105
  const lambderCaller = new LambderCaller({
99
106
  isCorsEnabled: false,
100
107
  apiPath: "/secure", // Your Lambder API endpoint, must be the same as in your backend
101
- fetchStartedHandler: ({ activeFetchList }) => {
108
+ fetchStartedHandler: ({ fetchParams, activeFetchList }) => {
102
109
  // When any api call starts
110
+ console.log("API Called:", fetchParams.apiName);
103
111
  },
104
- fetchEndedHandler: ({ activeFetchList }) => {
112
+ fetchEndedHandler: ({ fetchParams, fetchResult, activeFetchList }) => {
105
113
  // When any api call ends
114
+ console.log("Ongoing api call count:", activeFetchList.length);
106
115
  },
107
116
  errorMessageHandler: (message) => {
108
117
  console.error("LambderCaller:", message); // Handle error messages
@@ -113,7 +122,7 @@ const lambderCaller = new LambderCaller({
113
122
  const loadPageData = async () => {
114
123
  try {
115
124
  const response = await lambderCaller.api("getCompanyPage", {
116
- subdomain: "example", // Pass necessary parameters for your API call
125
+ companyName: "example", // Pass necessary parameters for your API call
117
126
  });
118
127
  } catch (error) {
119
128
  console.error("Failed to fetch page data:", error);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lambder",
3
- "version": "1.0.68",
3
+ "version": "1.0.70",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",