lambder 1.0.83 → 1.0.85

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 +89 -5
  2. package/package.json +1 -1
package/Readme.md CHANGED
@@ -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,88 @@ 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
+ return res.raw(param);
157
+ // Sends a custom HTTP response defined by the param object.
158
+ // Useful for sending non-standard responses.
159
+ return 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
+ return 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
+ return 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
+ return 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
+ return 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
+ return 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
+ return 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
+ return 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
+ return 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
+ // - Skip the afterRender hooks.
203
+
204
+ res.die.raw(param);
205
+ res.die.json(data, headers);
206
+ res.die.xml(data);
207
+ res.die.html(data, headers);
208
+ res.die.status301(url, headers);
209
+ res.die.status404(data, headers);
210
+ res.die.cors();
211
+ res.die.fileBase64(fileBase64, mimeType, headers);
212
+ res.die.file(filePath, headers, fallbackFilePath);
213
+ res.die.api({ payload, versionExpired, sessionExpired, notAuthorized, message, errorMessage }, headers);
214
+ });
215
+ ```
216
+
133
217
  ## Frontend Usage with LambderCaller
134
218
 
135
219
  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.83",
3
+ "version": "1.0.85",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",