lambder 1.0.82 → 1.0.84

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 +91 -6
  2. package/package.json +1 -1
package/Readme.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Lambder - Serverless NodeJS Web Framework
2
2
 
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.
3
+ Lambder is a highly opinionated 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
 
@@ -35,9 +35,8 @@ const lambder = new Lambder({
35
35
  });
36
36
 
37
37
  // Define a simple api
38
- lambder.addApi("getCompanyPage", async (ctx, res) => {
39
- const { host, path, get, post, cookie, headers } = ctx;
40
- const companyName = post.payload.companyName;
38
+ lambder.addApi("getCompanyPage", async ({ apiPayload }, res) => {
39
+ const companyName = apiPayload.companyName;
41
40
  const data = await fetchDataSomehow(companyName);
42
41
  return res.api({ payload: data });
43
42
  });
@@ -97,8 +96,11 @@ For more details on route matching, please check [path-to-regexp](https://www.np
97
96
  ```typescript
98
97
  // Define a simple api
99
98
  lambder.addApi("getCompanyPage", async (ctx, res) => {
100
- const { host, path, get, post, cookie, headers, apiName, apiPayload } = ctx;
101
- const companyName = post.payload.companyName;
99
+ const {
100
+ host, path, get, post, cookie, headers,
101
+ apiName, apiPayload
102
+ } = ctx;
103
+ const companyName = apiPayload.companyName;
102
104
  const data = await fetchDataSomehow(companyName);
103
105
  return res.api({ payload: data });
104
106
  });
@@ -130,6 +132,89 @@ lambder.addHook("beforeRender", async (ctx, res) => {
130
132
  });
131
133
  ```
132
134
 
135
+ ### Render Context (ctx) Variables
136
+ ```typescript
137
+ lambder.addApi("getCompanyName", async (ctx, res) => {
138
+ const {
139
+ host, // Request host. Exp: "www.example.com"
140
+ path, // Request path. Exp: "/index.html" or "/user/342"
141
+ get, // Get request query in JSON format. Exp: { userId: 342 }
142
+ post, // Post request body after JSON parsed. Exp: { userId: 342 }
143
+ cookie, // Cookies in an object. Exp: { "rememberMe": "true" }
144
+ headers, // Request Headers in an object. Exp: { "User-Agent": "....", ... }
145
+ apiName, // In this function it would return "getCompanyName"
146
+ apiPayload, // Same as post.payload
147
+ } = ctx;
148
+ return res.json({});
149
+ });
150
+ ```
151
+
152
+ ### Resolver Methods
153
+
154
+ ```typescript
155
+ lambder.addApi("getCompanyName", async (ctx, res) => {
156
+ res.raw(param);
157
+ // Directly sends a custom HTTP response defined by the param object.
158
+ // Useful for sending non-standard responses.
159
+ res.json(data, headers);
160
+ // Sends a JSON response with the specified data and optional headers.
161
+ // It sets the Content-Type header to application/json.
162
+
163
+ res.xml(data);
164
+ // Sends an XML response with the given data.
165
+ // Automatically encodes the response in base64 and sets
166
+ // the Content-Type header to application/xml.
167
+
168
+ res.html(data, headers);
169
+ // Sends an HTML response containing the provided data with optional headers.
170
+ // The response is base64 encoded, and the Content-Type header is set to text/html.
171
+
172
+ res.status301(url, headers);
173
+ // Redirects the client to the specified url with a 301 status code and optional headers.
174
+ // Useful for permanent redirections.
175
+
176
+ res.status404(data, headers);
177
+ // Sends a 404 Not Found response with custom data and optional headers.
178
+ // The response is base64 encoded, and the Content-Type header is set to text/html.
179
+
180
+ res.cors();
181
+ // Sends a 200 OK response with CORS headers enabled.
182
+ // This is typically used in response to a preflight request in a CORS scenario.
183
+
184
+ res.fileBase64(fileBase64, mimeType, headers);
185
+ // Sends a file response with the content provided in base64 format,
186
+ // the specified mimeType, and optional headers.
187
+
188
+ res.file(filePath, headers, fallbackFilePath);
189
+ // Serves a file from the server's public directory, with optional headers.
190
+ // If the file is not found and a fallbackFilePath is provided,
191
+ // attempts to serve the fallback file.
192
+ // Returns a JSON error if neither file is found.
193
+
194
+ res.api({ payload, versionExpired, sessionExpired, notAuthorized, message, errorMessage }, headers);
195
+ // Sends a standardized API response including the payload and status
196
+ // flags like versionExpired, sessionExpired, notAuthorized, along with
197
+ // optional messages and headers.
198
+
199
+ // res.die.*
200
+ // Acts the same as res.* but will:
201
+ // - Immediately return the value
202
+ // - Terminate the function
203
+ // - Skip the afterRender hooks
204
+
205
+ res.die.raw(param);
206
+ res.die.json(data, headers);
207
+ res.die.xml(data);
208
+ res.die.html(data, headers);
209
+ res.die.status301(url, headers);
210
+ res.die.status404(data, headers);
211
+ res.die.cors();
212
+ res.die.fileBase64(fileBase64, mimeType, headers);
213
+ res.die.file(filePath, headers, fallbackFilePath);
214
+ res.die.api({ payload, versionExpired, sessionExpired, notAuthorized, message, errorMessage }, headers);
215
+ });
216
+ ```
217
+
133
218
  ## Frontend Usage with LambderCaller
134
219
 
135
220
  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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lambder",
3
- "version": "1.0.82",
3
+ "version": "1.0.84",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",