File size: 2,270 Bytes
391c43e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/**
 * Edge Function Runtime Types
 *
 * These types define the interface for user-defined edge functions
 * and their execution context.
 */

/**
 * The request object passed to edge functions
 */
export interface FunctionRequest {
  /** HTTP method (GET, POST, PUT, DELETE) */
  method: string;

  /** Request headers */
  headers: Record<string, string>;

  /** Parsed request body (JSON) */
  body: unknown;

  /** URL path parameters */
  params: Record<string, string>;

  /** Query string parameters */
  query: Record<string, string>;

  /** The full request path after the function name */
  path: string;
}

/**
 * The response object that functions should return
 */
export interface FunctionResponse {
  /** HTTP status code */
  status: number;

  /** Response headers */
  headers: Record<string, string>;

  /** Response body (string or object for JSON) */
  body: string | object;
}

/**
 * Database API available to edge functions
 * Provides sandboxed access to the site's SQLite database
 */
export interface DatabaseAPI {
  /**
   * Execute a SELECT query and return results
   * @param sql SQL query string with ? placeholders
   * @param params Parameter values for placeholders
   */
  query<T = Record<string, unknown>>(sql: string, params?: unknown[]): T[];

  /**
   * Execute an INSERT, UPDATE, or DELETE query
   * @param sql SQL statement with ? placeholders
   * @param params Parameter values for placeholders
   */
  run(sql: string, params?: unknown[]): { changes: number; lastInsertRowid: number | bigint };

  /**
   * Alias for query() - returns all matching rows
   */
  all<T = Record<string, unknown>>(sql: string, params?: unknown[]): T[];
}

/**
 * Result of executing an edge function
 */
export interface ExecutionResult {
  /** The function's response */
  response: FunctionResponse;

  /** Console logs captured during execution */
  logs: string[];

  /** Execution time in milliseconds */
  durationMs: number;

  /** Error message if execution failed */
  error?: string;
}

/**
 * Options for database API creation
 */
export interface DatabaseAPIOptions {
  /** If true, only SELECT queries are allowed */
  readOnly?: boolean;

  /** Maximum number of queries per execution (default: 100) */
  maxQueries?: number;
}