From b31a6a2f76d6e327e305ad25afb5ecba36f43b0e Mon Sep 17 00:00:00 2001 From: ram3ez Date: Mon, 15 Dec 2025 20:10:00 +0400 Subject: [PATCH 1/2] added typescript definitions (intellisense) to nodejs starter --- node/starter/src/main.js | 34 +++++++-- node/starter/src/types.d.ts | 147 ++++++++++++++++++++++++++++++++++++ 2 files changed, 173 insertions(+), 8 deletions(-) create mode 100644 node/starter/src/types.d.ts diff --git a/node/starter/src/main.js b/node/starter/src/main.js index 0539b5ef..3f82f84e 100644 --- a/node/starter/src/main.js +++ b/node/starter/src/main.js @@ -1,5 +1,23 @@ +/// + +/* + 'req' variable has: + 'headers' - object with request headers + 'payload' - request body data as a string + 'variables' - object with function variables + + 'res' variable has: + 'send(text, status)' - function to return text response. Status code defaults to 200 + 'json(obj, status)' - function to return JSON response. Status code defaults to 200 + + 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 +33,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..7e412837 --- /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: Request; + + /** + * The response object allows you to return a response to the execution. + */ + res: Response; + + /** + * 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 Request { + /** + * 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 Response { + /** + * 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 {}; From 12cbfc63d4981f9ea5421785c9c2fadee954d56d Mon Sep 17 00:00:00 2001 From: ram3ez Date: Mon, 15 Dec 2025 20:32:08 +0400 Subject: [PATCH 2/2] improved nitpicks --- node/starter/src/main.js | 14 ++------------ node/starter/src/types.d.ts | 8 ++++---- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/node/starter/src/main.js b/node/starter/src/main.js index 3f82f84e..c08f5b0d 100644 --- a/node/starter/src/main.js +++ b/node/starter/src/main.js @@ -1,17 +1,7 @@ /// -/* - 'req' variable has: - 'headers' - object with request headers - 'payload' - request body data as a string - 'variables' - object with function variables - - 'res' variable has: - 'send(text, status)' - function to return text response. Status code defaults to 200 - 'json(obj, status)' - function to return JSON response. Status code defaults to 200 - - If an error is thrown, a response with code 500 will be returned. -*/ +// 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'; diff --git a/node/starter/src/types.d.ts b/node/starter/src/types.d.ts index 7e412837..8d504b1b 100644 --- a/node/starter/src/types.d.ts +++ b/node/starter/src/types.d.ts @@ -6,12 +6,12 @@ declare global { /** * The request object contains all data regarding the function execution request. */ - req: Request; + req: AppwriteRequest; /** * The response object allows you to return a response to the execution. */ - res: Response; + res: AppwriteResponse; /** * Log a message to the Appwrite Console. @@ -26,7 +26,7 @@ declare global { error: (message: any) => void; } - interface Request { + interface AppwriteRequest { /** * Raw request body text. */ @@ -88,7 +88,7 @@ declare global { url: string; } - interface Response { + interface AppwriteResponse { /** * Returns a text response. * @param text Content to return.