From 683aa0abaca94fb12a7469367918224e6d399b19 Mon Sep 17 00:00:00 2001 From: Luke Hagar Date: Mon, 12 Feb 2024 08:47:36 -0600 Subject: [PATCH] split funcs --- .gitignore | 1 + functions/bun/src/main.ts | 49 ++++++++++--------- README.md => functions/node/README.md | 0 .../node/package-lock.json | 0 package.json => functions/node/package.json | 0 functions/node/src/main.js | 38 ++++++++++++++ src/main.js | 33 ------------- 7 files changed, 66 insertions(+), 55 deletions(-) create mode 100644 .gitignore rename README.md => functions/node/README.md (100%) rename package-lock.json => functions/node/package-lock.json (100%) rename package.json => functions/node/package.json (100%) create mode 100644 functions/node/src/main.js delete mode 100644 src/main.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b512c09 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules \ No newline at end of file diff --git a/functions/bun/src/main.ts b/functions/bun/src/main.ts index e7d02da..ba3d9b9 100644 --- a/functions/bun/src/main.ts +++ b/functions/bun/src/main.ts @@ -1,33 +1,38 @@ -import { Client } from 'node-appwrite'; +import { Client , Databases } 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"]); + 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)); - // You can log messages to the console - log("Hello, Logs!"); + const client = new Client() + .setEndpoint('https://cloud.appwrite.io/v1') + .setProject(Bun.env["APPWRITE_FUNCTION_PROJECT_ID"]) + .setKey(Bun.env["APPWRITE_API_KEY"]); - // If something goes wrong, log an error - error("Hello, Errors!"); + const databases = new Databases(client); // 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!"); + const doc = await databases.getDocument( + "metrics", + "load", + "65ca2f77931f5ebb7d19" + ); + log(doc); + // await databases.updateDocument("metrics", "load", "65ca2f77931f5ebb7d19", {doc.count + 1}) + return res.send(`Count Updated`); + } else { + return res.send(`Invalid request method. Please use GET.`); } - - // `res.json()` is a handy helper for sending JSON - return res.json({ - motto: "Build like a team of hundreds_", - learn: "https://appwrite.io/docs", - connect: "https://appwrite.io/discord", - getInspired: "https://builtwith.appwrite.io", - }); }; diff --git a/README.md b/functions/node/README.md similarity index 100% rename from README.md rename to functions/node/README.md diff --git a/package-lock.json b/functions/node/package-lock.json similarity index 100% rename from package-lock.json rename to functions/node/package-lock.json diff --git a/package.json b/functions/node/package.json similarity index 100% rename from package.json rename to functions/node/package.json diff --git a/functions/node/src/main.js b/functions/node/src/main.js new file mode 100644 index 0000000..5701b3e --- /dev/null +++ b/functions/node/src/main.js @@ -0,0 +1,38 @@ +import { Client, Databases, ID } from "node-appwrite"; + +// This is your Appwrite function +// It's executed each time we get a request +export default async ({ req, res, log, error }) => { + 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)); + + const client = new Client() + .setEndpoint("https://appwrite.plygrnd.org/v1") + .setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID) + .setKey(process.env.APPWRITE_API_KEY); + + const databases = new Databases(client); + + // The `req` object contains the request data + if (req.method === "GET") { + const doc = await databases.getDocument( + "metrics", + "load", + "65ca2e4954a2e5c286d0" + ); + log(doc); + // await databases.updateDocument("metrics", "load", "65ca2e4954a2e5c286d0", {doc.count + 1}) + return res.send(`Count Updated`); + } else { + return res.send(`Invalid request method. Please use GET.`); + } +}; diff --git a/src/main.js b/src/main.js deleted file mode 100644 index fd68025..0000000 --- a/src/main.js +++ /dev/null @@ -1,33 +0,0 @@ -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 }) => { - // Why not try the Appwrite SDK? - // - // const client = new Client() - // .setEndpoint('https://cloud.appwrite.io/v1') - // .setProject(process.env.APPWRITE_FUNCTION_PROJECT_ID) - // .setKey(process.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, Jordan!`); - } - - // `res.json()` is a handy helper for sending JSON - return res.json({ - motto: 'Build like a team of hundreds_', - learn: 'https://appwrite.io/docs', - connect: 'https://appwrite.io/discord', - getInspired: 'https://builtwith.appwrite.io', - }); -};