mirror of
https://github.com/LukeHagar/appwrite-starter-function.git
synced 2025-12-06 04:19:14 +00:00
split funcs
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
node_modules
|
||||||
@@ -1,33 +1,38 @@
|
|||||||
import { Client } from 'node-appwrite';
|
import { Client , Databases } from 'node-appwrite';
|
||||||
|
|
||||||
// This is your Appwrite function
|
// This is your Appwrite function
|
||||||
// It's executed each time we get a request
|
// It's executed each time we get a request
|
||||||
export default async ({ req, res, log, error }: any) => {
|
export default async ({ req, res, log, error }: any) => {
|
||||||
// Why not try the Appwrite SDK?
|
log(req.bodyRaw); // Raw request body, contains request data
|
||||||
//
|
log(JSON.stringify(req.body)); // Object from parsed JSON request body, otherwise string
|
||||||
// const client = new Client()
|
log(JSON.stringify(req.headers)); // String key-value pairs of all request headers, keys are lowercase
|
||||||
// .setEndpoint('https://cloud.appwrite.io/v1')
|
log(req.scheme); // Value of the x-forwarded-proto header, usually http or https
|
||||||
// .setProject(Bun.env["APPWRITE_FUNCTION_PROJECT_ID"])
|
log(req.method); // Request method, such as GET, POST, PUT, DELETE, PATCH, etc.
|
||||||
// .setKey(Bun.env["APPWRITE_API_KEY"]);
|
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
|
const client = new Client()
|
||||||
log("Hello, Logs!");
|
.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
|
const databases = new Databases(client);
|
||||||
error("Hello, Errors!");
|
|
||||||
|
|
||||||
// The `req` object contains the request data
|
// The `req` object contains the request data
|
||||||
if (req.method === "GET") {
|
if (req.method === "GET") {
|
||||||
// Send a response with the res object helpers
|
const doc = await databases.getDocument(
|
||||||
// `res.send()` dispatches a string back to the client
|
"metrics",
|
||||||
return res.send("Hello, World!");
|
"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",
|
|
||||||
});
|
|
||||||
};
|
};
|
||||||
|
|||||||
38
functions/node/src/main.js
Normal file
38
functions/node/src/main.js
Normal file
@@ -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.`);
|
||||||
|
}
|
||||||
|
};
|
||||||
33
src/main.js
33
src/main.js
@@ -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',
|
|
||||||
});
|
|
||||||
};
|
|
||||||
Reference in New Issue
Block a user