· 4 years ago · Sep 10, 2021, 02:18 PM
1import { Account, AppwriteException, Avatars, Client, Database, Functions, Health, Locale, Storage, Teams, Users } from 'node-appwrite';
2
3// uncomment the lines below to use them in your code
4const {
5 // APPWRITE_FUNCTION_ID, // Your function's unique ID.
6 // APPWRITE_FUNCTION_NAME, // Your function's name.
7 // APPWRITE_FUNCTION_TAG, // Your function's code tag unique ID.
8 // APPWRITE_FUNCTION_TRIGGER, // The name of the event that triggered that executed the function. This value can be any of Appwrite system events, 'api' if your function was triggered manually or by an API call, or 'schedule' if your CRON schedule triggered it.
9 // APPWRITE_FUNCTION_RUNTIME_NAME, // Your function runtime name. Can be any of Appwrite supported execution runtimes.
10 // APPWRITE_FUNCTION_RUNTIME_VERSION, // Your function runtime version.
11 // APPWRITE_FUNCTION_EVENT, // Your function event name. This value is available only when your function trigger is 'event.' This variable value can be any of Appwrite system events.
12 APPWRITE_FUNCTION_EVENT_DATA, // Your function event payload. This value is available only when your function trigger is 'event'. This variable value contains a string in JSON format with your specific event data.
13 APPWRITE_FUNCTION_DATA, // Your function's custom execution data. This variable's value contains a string in any format. If the custom data is in JSON FORMAT, it must be parsed inside the function code. Note that this variable can be set only when triggering a function using the SDK or HTTP API and the Appwrite Dashboard.
14 APPWRITE_FUNCTION_PROJECT_ID, // Your function's project ID available by default.
15 // APPWRITE_FUNCTION_USER_ID, // The userId of the user that triggered your function's execution. Executions triggered in the Appwrite console will be prepended with "admin-".
16 // APPWRITE_FUNCTION_JWT, // A JSON Web Token generated for the user that executes your function.
17} = process.env;
18
19if (!process.env.APPWRITE_ENDPOINT) {
20 throw new Error('Missing APPWRITE_ENDPOINT variable. Define APPWRITE_ENDPOINT under function settings');
21}
22if (!process.env.APPWRITE_API_KEY) {
23 throw new Error('Missing APPWRITE_API_KEY variable. Define APPWRITE_API_KEY under function settings');
24}
25
26const client = new Client();
27client
28 .setEndpoint(process.env.APPWRITE_ENDPOINT) // Your API Endpoint
29 .setProject(APPWRITE_FUNCTION_PROJECT_ID) // Your project ID available by default
30 .setKey(process.env.APPWRITE_API_KEY); // Your secret API key
31
32const account = new Account(client);
33const avatars = new Avatars(client);
34const database = new Database(client);
35const functions = new Functions(client);
36const health = new Health(client);
37const locale = new Locale(client);
38const storage = new Storage(client);
39const teams = new Teams(client);
40const users = new Users(client);
41
42
43(async () => {
44 try {
45 const functionData: any = JSON.parse(APPWRITE_FUNCTION_EVENT_DATA || APPWRITE_FUNCTION_DATA);
46 console.log('Hello from ' + process.env.APPWRITE_FUNCTION_EVENT);
47 console.log(functionData)
48 } catch (e) {
49 if (e instanceof AppwriteException) {
50 console.error(e);
51 } else {
52 console.error(e);
53 }
54 }
55})();
56