Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions node/starter/src/main.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
/// <reference path="./types.d.ts" />

// 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
Expand All @@ -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',
});
};
147 changes: 147 additions & 0 deletions node/starter/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -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<string, any>;

/**
* Binary request body.
*/
bodyBinary: ArrayBuffer;

/**
* Request headers as a key-value object.
*/
headers: Record<string, string>;

/**
* 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<string, string>;

/**
* 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<string, string>
) => 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<string, string>
) => 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<string, string>
) => 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<string, string>
) => void;
}
}

export {};