|
|
@@ -0,0 +1,40 @@
|
|
|
+/**
|
|
|
+ * CORS Configuration Helper
|
|
|
+ *
|
|
|
+ * Provides secure CORS headers based on environment configuration.
|
|
|
+ * Uses EDGE_FUNCTION_ALLOWED_ORIGINS environment variable to restrict origins.
|
|
|
+ */
|
|
|
+
|
|
|
+/**
|
|
|
+ * Get CORS headers with allowed origins from environment
|
|
|
+ *
|
|
|
+ * @param additionalMethods - Additional HTTP methods to allow (default: GET, POST, OPTIONS)
|
|
|
+ * @returns CORS headers object
|
|
|
+ */
|
|
|
+export function getCorsHeaders(additionalMethods: string[] = []): Record<string, string> {
|
|
|
+ // Get allowed origins from environment variable
|
|
|
+ // Format: comma-separated list, e.g., "https://shopcall.ai,http://192.168.2.112:8081"
|
|
|
+ const allowedOriginsEnv = Deno.env.get('EDGE_FUNCTION_ALLOWED_ORIGINS') || '*';
|
|
|
+
|
|
|
+ // Default methods
|
|
|
+ const defaultMethods = ['GET', 'POST', 'OPTIONS'];
|
|
|
+ const allMethods = [...new Set([...defaultMethods, ...additionalMethods])];
|
|
|
+
|
|
|
+ return {
|
|
|
+ 'Access-Control-Allow-Origin': allowedOriginsEnv,
|
|
|
+ 'Access-Control-Allow-Headers': 'authorization, x-client-info, apikey, content-type',
|
|
|
+ 'Access-Control-Allow-Methods': allMethods.join(', '),
|
|
|
+ };
|
|
|
+}
|
|
|
+
|
|
|
+/**
|
|
|
+ * Handle CORS preflight request
|
|
|
+ *
|
|
|
+ * @param additionalMethods - Additional HTTP methods to allow
|
|
|
+ * @returns Response for OPTIONS request
|
|
|
+ */
|
|
|
+export function handleCorsPreflightRequest(additionalMethods: string[] = []): Response {
|
|
|
+ return new Response(null, {
|
|
|
+ headers: getCorsHeaders(additionalMethods)
|
|
|
+ });
|
|
|
+}
|