mirror of
https://github.com/LukeHagar/appwrite-starter-function.git
synced 2025-12-09 20:37:46 +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",
|
||||
});
|
||||
};
|
||||
|
||||
48
functions/node/README.md
Normal file
48
functions/node/README.md
Normal file
@@ -0,0 +1,48 @@
|
||||
# ⚡ Node.js Starter Function
|
||||
|
||||
A simple starter function. Edit `src/main.js` to get started and create something awesome! 🚀
|
||||
|
||||
## 🧰 Usage
|
||||
|
||||
### GET /
|
||||
|
||||
- Returns a "Hello, World!" message.
|
||||
|
||||
**Response**
|
||||
|
||||
Sample `200` Response:
|
||||
|
||||
```text
|
||||
Hello, World!
|
||||
```
|
||||
|
||||
### POST, PUT, PATCH, DELETE /
|
||||
|
||||
- Returns a "Learn More" JSON response.
|
||||
|
||||
**Response**
|
||||
|
||||
Sample `200` Response:
|
||||
|
||||
```json
|
||||
{
|
||||
"motto": "Build like a team of hundreds_",
|
||||
"learn": "https://appwrite.io/docs",
|
||||
"connect": "https://appwrite.io/discord",
|
||||
"getInspired": "https://builtwith.appwrite.io"
|
||||
}
|
||||
```
|
||||
|
||||
## ⚙️ Configuration
|
||||
|
||||
| Setting | Value |
|
||||
|-------------------|---------------|
|
||||
| Runtime | Node (18.0) |
|
||||
| Entrypoint | `src/main.js` |
|
||||
| Build Commands | `npm install` |
|
||||
| Permissions | `any` |
|
||||
| Timeout (Seconds) | 15 |
|
||||
|
||||
## 🔒 Environment Variables
|
||||
|
||||
No environment variables required.
|
||||
132
functions/node/package-lock.json
generated
Normal file
132
functions/node/package-lock.json
generated
Normal file
@@ -0,0 +1,132 @@
|
||||
{
|
||||
"name": "starter-template",
|
||||
"version": "1.0.0",
|
||||
"lockfileVersion": 3,
|
||||
"requires": true,
|
||||
"packages": {
|
||||
"": {
|
||||
"name": "starter-template",
|
||||
"version": "1.0.0",
|
||||
"dependencies": {
|
||||
"node-appwrite": "^9.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"prettier": "^3.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/asynckit": {
|
||||
"version": "0.4.0",
|
||||
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
|
||||
"integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q=="
|
||||
},
|
||||
"node_modules/axios": {
|
||||
"version": "1.4.0",
|
||||
"resolved": "https://registry.npmjs.org/axios/-/axios-1.4.0.tgz",
|
||||
"integrity": "sha512-S4XCWMEmzvo64T9GfvQDOXgYRDJ/wsSZc7Jvdgx5u1sd0JwsuPLqb3SYmusag+edF6ziyMensPVqLTSc1PiSEA==",
|
||||
"dependencies": {
|
||||
"follow-redirects": "^1.15.0",
|
||||
"form-data": "^4.0.0",
|
||||
"proxy-from-env": "^1.1.0"
|
||||
}
|
||||
},
|
||||
"node_modules/combined-stream": {
|
||||
"version": "1.0.8",
|
||||
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
|
||||
"integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
|
||||
"dependencies": {
|
||||
"delayed-stream": "~1.0.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 0.8"
|
||||
}
|
||||
},
|
||||
"node_modules/delayed-stream": {
|
||||
"version": "1.0.0",
|
||||
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
|
||||
"integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==",
|
||||
"engines": {
|
||||
"node": ">=0.4.0"
|
||||
}
|
||||
},
|
||||
"node_modules/follow-redirects": {
|
||||
"version": "1.15.2",
|
||||
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.2.tgz",
|
||||
"integrity": "sha512-VQLG33o04KaQ8uYi2tVNbdrWp1QWxNNea+nmIB4EVM28v0hmP17z7aG1+wAkNzVq4KeXTq3221ye5qTJP91JwA==",
|
||||
"funding": [
|
||||
{
|
||||
"type": "individual",
|
||||
"url": "https://github.com/sponsors/RubenVerborgh"
|
||||
}
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=4.0"
|
||||
},
|
||||
"peerDependenciesMeta": {
|
||||
"debug": {
|
||||
"optional": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"node_modules/form-data": {
|
||||
"version": "4.0.0",
|
||||
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz",
|
||||
"integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==",
|
||||
"dependencies": {
|
||||
"asynckit": "^0.4.0",
|
||||
"combined-stream": "^1.0.8",
|
||||
"mime-types": "^2.1.12"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 6"
|
||||
}
|
||||
},
|
||||
"node_modules/mime-db": {
|
||||
"version": "1.52.0",
|
||||
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz",
|
||||
"integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==",
|
||||
"engines": {
|
||||
"node": ">= 0.6"
|
||||
}
|
||||
},
|
||||
"node_modules/mime-types": {
|
||||
"version": "2.1.35",
|
||||
"resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz",
|
||||
"integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==",
|
||||
"dependencies": {
|
||||
"mime-db": "1.52.0"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">= 0.6"
|
||||
}
|
||||
},
|
||||
"node_modules/node-appwrite": {
|
||||
"version": "9.0.0",
|
||||
"resolved": "https://registry.npmjs.org/node-appwrite/-/node-appwrite-9.0.0.tgz",
|
||||
"integrity": "sha512-iTcHbuaJfr6bP/HFkRVV+FcaumKkbINqZyypQdl+tYxv6Dx0bkB/YKUXGYfTkgP18TLPWQQB++OGQhi98dlo2w==",
|
||||
"dependencies": {
|
||||
"axios": "^1.3.6",
|
||||
"form-data": "^4.0.0"
|
||||
}
|
||||
},
|
||||
"node_modules/prettier": {
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://registry.npmjs.org/prettier/-/prettier-3.0.0.tgz",
|
||||
"integrity": "sha512-zBf5eHpwHOGPC47h0zrPyNn+eAEIdEzfywMoYn2XPi0P44Zp0tSq64rq0xAREh4auw2cJZHo9QUob+NqCQky4g==",
|
||||
"dev": true,
|
||||
"bin": {
|
||||
"prettier": "bin/prettier.cjs"
|
||||
},
|
||||
"engines": {
|
||||
"node": ">=14"
|
||||
},
|
||||
"funding": {
|
||||
"url": "https://github.com/prettier/prettier?sponsor=1"
|
||||
}
|
||||
},
|
||||
"node_modules/proxy-from-env": {
|
||||
"version": "1.1.0",
|
||||
"resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
|
||||
"integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg=="
|
||||
}
|
||||
}
|
||||
}
|
||||
16
functions/node/package.json
Normal file
16
functions/node/package.json
Normal file
@@ -0,0 +1,16 @@
|
||||
{
|
||||
"name": "starter-template",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "src/main.js",
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"format": "prettier --write ."
|
||||
},
|
||||
"dependencies": {
|
||||
"node-appwrite": "^9.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"prettier": "^3.0.0"
|
||||
}
|
||||
}
|
||||
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.`);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user