diff --git a/app/views/docs/functions-develop.phtml b/app/views/docs/functions-develop.phtml index b1d11293..ff92cc21 100644 --- a/app/views/docs/functions-develop.phtml +++ b/app/views/docs/functions-develop.phtml @@ -433,6 +433,43 @@ public class Handler { } +
import { Client } from 'node-appwrite';
+
+// This is your Appwrite function
+// It's executed each time we get a request
+export default async ({ req, res, log, error }: any) => {
+ // Why not try the Appwrite SDK?
+ // const client = new Client()
+ // .setEndpoint('https://cloud.appwrite.io/v1')
+ // .setProject(Bun.env["APPWRITE_FUNCTION_PROJECT_ID"])
+ // .setKey(Bun.env["APPWRITE_API_KEY"]);
+
+ // You can log messages to the console
+ log("Hello, Logs!");
+
+ // If something goes wrong, log an error
+ error("Hello, Errors!");
+
+ // The `req` object contains the request data
+ if (req.method === "GET") {
+ // Send a response with the res object helpers
+ // `res.send()` dispatches a string back to the client
+ return res.send("Hello, World!");
+ }
+
+ // `res.json()` is a handy helper for sending JSON
+ return res.json({
+ motto: "Build Fast. Scale Big. All in One Place.",
+ learn: "https://appwrite.io/docs",
+ connect: "https://appwrite.io/discord",
+ getInspired: "https://builtwith.appwrite.io",
+ });
+};
+ If you prefer to learn through more examples like this, explore the examples page.
@@ -447,7 +484,7 @@ public class Handler {Property | +Property | Description |
error() |
Methoc to log errors to the Appwrite Console, end users will not be able to see these errors. See full examples here. | -
// before destructuring
+export default async function (context: any) {
+ context.log("This is a log!");
+ return context.res.send("This is a response!");
+}
+
// after destructuring
export default async function ({ req, res, log, error }: any) {
log("This is a log!");
@@ -756,6 +808,25 @@ public class Main {
}
export default async ({ req, res, log }: any) => {
+ log(req.bodyRaw); // Raw request body, contains request data
+ log(JSON.stringify(req.body)); // Object from parsed JSON request body, otherwise string
+ log(JSON.stringify(req.headers)); // String key-value pairs of all request headers, keys are lowercase
+ log(req.scheme); // Value of the x-forwarded-proto header, usually http or https
+ log(req.method); // Request method, such as GET, POST, PUT, DELETE, PATCH, etc.
+ log(req.url); // Full URL, for example: http://awesome.appwrite.io:8000/v1/hooks?limit=12&offset=50
+ log(req.host); // Hostname from the host header, such as awesome.appwrite.io
+ log(req.port); // Port from the host header, for example 8000
+ log(req.path); // Path part of URL, for example /v1/hooks
+ log(req.queryString); // Raw query params string. For example "limit=12&offset=50"
+ log(JSON.stringify(req.query)); // Parsed query params. For example, req.query.limit
+
+ return res.send("All the request parameters are logged to the Appwrite Console.");
+ export default async ({ req, res, log }) => {
+
+ switch (req.query.type) {
+ case 'empty':
+ return res.empty();
+ case 'json':
+ return res.json({"type": "This is a JSON response"});
+ case 'redirect':
+ return res.redirect("https://appwrite.io", 301);
+ case 'html':
+ return res.send(
+ "<h1>This is an HTML response</h1>", 200, {
+ "content-type": "text/html"
+ });
+ default:
+ return res.send("This is a text response");
+ }
}
export default async ({ res, log, error }: any) => {
+ export default async ({ req, res, log, error }: any) => {
log("This is a log, use for logging information to console");
- log(`This function was called with ${context.req.method} method`);
+ log(`This function was called with ${req.method} method`);
error("This is an error, use for logging errors to console");
return res.send("Check the Appwrite Console to see logs and errors!");
@@ -1312,6 +1406,18 @@ namespace runtime {
}
export default async ({ req, res, log, error }: any) => {
+ log("This is a log, use for logging information to console");
+ log(`This function was called with ${req.method} method`);
+ error("This is an error, use for logging errors to console");
+
+ return res.send("Check the Appwrite Console to see logs and errors!");
+};
+ You can access these logs through the following steps.
export default async ({ req, res, log }) => {
+ return res.send(Bun.env.get('MY_VAR'));
}
dotnet restore
bun install
import { Client, Databases, ID } from 'node-appwrite';
+
+export default function ({req, res, error}: any){
+ const client = new Client()
+ .setEndpoint("https://cloud.appwrite.io/v1")
+ .setProject(Bun.env.get("APPWRITE_FUNCTION_PROJECT_ID") || "")
+ .setKey(Bun.env.get("APPWRITE_API_KEY") || "");
+
+ const databases = new Databases(client);
+
+ try {
+ databases.createDocument(
+ "[DATABASE_ID]",
+ "[COLLECTION_ID]",
+ ID.unique(),
+ {}
+ );
+ } catch (e) {
+ error("Failed to create document: " + e.message);
+ return res.send("Failed to create document");
+ }
+
+ return res.send("Document created");
}
import { Client, Databases, ID } from 'node-appwrite';
+
+export default function ({req, res, error}: any){
+ const client = new Client()
+ .setEndpoint("https://cloud.appwrite.io/v1")
+ .setProject(Bun.env.get("APPWRITE_FUNCTION_PROJECT_ID") || "")
+
+ if (req.headers["x-appwrite-user-jwt"]) {
+ client.setJWT(req.headers["x-appwrite-user-jwt"]);
+ } else {
+ return res.send("Please sign in, JWT not found");
+ }
+
+ const databases = new Databases(client);
+
+ try {
+ databases.createDocument(
+ "[DATABASE_ID]",
+ "[COLLECTION_ID]",
+ ID.unique(),
+ {}
+ );
+ } catch (e) {
+ error("Failed to create document: " + e.message)
+ return res.send("Failed to create document");
+ }
+
+ return res.send("Document created");
}
// src/utils.ts
+
+export function add(a: number, b: number): number {
+ return a + b;
+}
+ // src/main.ts
+
+import { add } from './utils.ts';
+
+export default function ({res}: {res: any}) {
+ return res.send(add(1, 2));
}
Appwrite Functions received major updates in Appwrite version 1.4. If you still have functions from previous versions, they will be read-only in Appwrite 1.4. - You will have to migrate your old functions to follow new runtime syntax. + You will have to recreate your old functions to follow new runtime syntax.
@@ -2638,6 +2842,11 @@ public class Main {
req
and res
has been replaced by context
, which contains new logger methods.
diff --git a/app/views/docs/functions-runtimes.phtml b/app/views/docs/functions-runtimes.phtml
index 663d4eaf..08af0ab3 100644
--- a/app/views/docs/functions-runtimes.phtml
+++ b/app/views/docs/functions-runtimes.phtml
@@ -43,7 +43,6 @@ foreach ($runtimes as $key => $item) {