mirror of
https://github.com/LukeHagar/appwrite-starter-function.git
synced 2025-12-09 04:19:20 +00:00
split funcs
This commit is contained in:
@@ -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",
|
||||
});
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user