diff --git a/node/starter/src/main.js b/node/starter/src/main.js index 0539b5ef..c08f5b0d 100644 --- a/node/starter/src/main.js +++ b/node/starter/src/main.js @@ -1,5 +1,13 @@ +/// + +// See types.d.ts for Request and Response type definitions. +// If an error is thrown, a response with code 500 will be returned. + import { Client, Users } from 'node-appwrite'; +/** + * @param {Context} context + */ // This Appwrite function will be executed every time your function is triggered export default async ({ req, res, log, error }) => { // You can use the Appwrite SDK to interact with other services @@ -15,21 +23,21 @@ export default async ({ req, res, log, error }) => { // Log messages and errors to the Appwrite Console // These logs won't be seen by your end users log(`Total users: ${response.total}`); - } catch(err) { - error("Could not list users: " + err.message); + } catch (err) { + error('Could not list users: ' + err.message); } // The req object contains the request data - if (req.path === "/ping") { + if (req.path === '/ping') { // Use res object to respond with text(), json(), or binary() // Don't forget to return a response! - return res.text("Pong"); + return res.text('Pong'); } 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", + 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/node/starter/src/types.d.ts b/node/starter/src/types.d.ts new file mode 100644 index 00000000..8d504b1b --- /dev/null +++ b/node/starter/src/types.d.ts @@ -0,0 +1,147 @@ +declare global { + /** + * Appwrite Function Context + */ + interface Context { + /** + * The request object contains all data regarding the function execution request. + */ + req: AppwriteRequest; + + /** + * The response object allows you to return a response to the execution. + */ + res: AppwriteResponse; + + /** + * Log a message to the Appwrite Console. + * @param message The message to log. + */ + log: (message: any) => void; + + /** + * Log an error to the Appwrite Console. + * @param message The error message to log. + */ + error: (message: any) => void; + } + + interface AppwriteRequest { + /** + * Raw request body text. + */ + bodyText: string; + + /** + * Parsed JSON request body (if valid JSON). + */ + bodyJson: Record; + + /** + * Binary request body. + */ + bodyBinary: ArrayBuffer; + + /** + * Request headers as a key-value object. + */ + headers: Record; + + /** + * Request method (GET, POST, etc.). + */ + method: string; + + /** + * Request URL path (e.g., "/v1/hooks"). + */ + path: string; + + /** + * Raw query string (e.g., "limit=10&offset=0"). + */ + queryString: string; + + /** + * Parsed query parameters. + */ + query: Record; + + /** + * Request scheme (http or https). + */ + scheme: string; + + /** + * Request host. + */ + host: string; + + /** + * Request port. + */ + port: number; + + /** + * Request URL. + */ + url: string; + } + + interface AppwriteResponse { + /** + * Returns a text response. + * @param text Content to return. + * @param statusCode HTTP status code (default 200). + * @param headers HTTP headers. + */ + text: ( + text: string, + statusCode?: number, + headers?: Record + ) => void; + + /** + * Returns a JSON response. + * @param obj Object to return as JSON. + * @param statusCode HTTP status code (default 200). + * @param headers HTTP headers. + */ + json: ( + obj: any, + statusCode?: number, + headers?: Record + ) => void; + + /** + * Returns an empty response (204 No Content). + */ + empty: () => void; + + /** + * Redirects the client. + * @param url URL to redirect to. + * @param statusCode HTTP status code (default 301). + * @param headers HTTP headers. + */ + redirect: ( + url: string, + statusCode?: number, + headers?: Record + ) => void; + + /** + * Returns a binary response. + * @param bytes Binary content. + * @param statusCode HTTP status code (default 200). + * @param headers HTTP headers. + */ + binary: ( + bytes: ArrayBuffer | Buffer, + statusCode?: number, + headers?: Record + ) => void; + } +} + +export {};