@@ -14,6 +14,13 @@ import { Hono } from "hono";
1414import type { MeshContext } from "../../core/mesh-context" ;
1515import { getUserId } from "../../core/mesh-context" ;
1616import { fetchToolsFromMCP } from "../../tools/connection/fetch-tools" ;
17+ import {
18+ ADMIN_MCP ,
19+ getSupabaseConfig ,
20+ supabaseGet ,
21+ resolveProfileId ,
22+ getOrCreateDecoApiKey ,
23+ } from "./deco-supabase" ;
1724
1825type Variables = { meshContext : MeshContext } ;
1926
@@ -25,104 +32,6 @@ interface SupabaseSite {
2532 thumb_url : string | null ;
2633}
2734
28- async function supabaseGet < T > (
29- supabaseUrl : string ,
30- serviceKey : string ,
31- path : string ,
32- ) : Promise < T [ ] > {
33- const res = await fetch ( `${ supabaseUrl } /rest/v1/${ path } ` , {
34- headers : {
35- apikey : serviceKey ,
36- Authorization : `Bearer ${ serviceKey } ` ,
37- Accept : "application/json" ,
38- } ,
39- } ) ;
40- if ( ! res . ok ) {
41- const text = await res . text ( ) . catch ( ( ) => res . statusText ) ;
42- console . error ( `[deco-sites] Supabase error (${ res . status } ): ${ text } ` ) ;
43- throw new Error ( `External service error (${ res . status } )` ) ;
44- }
45- return res . json ( ) as Promise < T [ ] > ;
46- }
47-
48- async function supabasePost < T > (
49- supabaseUrl : string ,
50- serviceKey : string ,
51- table : string ,
52- body : Record < string , unknown > ,
53- ) : Promise < T > {
54- const res = await fetch ( `${ supabaseUrl } /rest/v1/${ table } ` , {
55- method : "POST" ,
56- headers : {
57- apikey : serviceKey ,
58- Authorization : `Bearer ${ serviceKey } ` ,
59- "Content-Type" : "application/json" ,
60- Accept : "application/json" ,
61- Prefer : "return=representation" ,
62- } ,
63- body : JSON . stringify ( body ) ,
64- } ) ;
65- if ( ! res . ok ) {
66- const text = await res . text ( ) . catch ( ( ) => res . statusText ) ;
67- console . error ( `[deco-sites] Supabase POST error (${ res . status } ): ${ text } ` ) ;
68- throw new Error ( `External service error (${ res . status } )` ) ;
69- }
70- const rows = ( await res . json ( ) ) as T [ ] ;
71- if ( ! rows [ 0 ] ) {
72- throw new Error ( "Supabase POST returned no rows" ) ;
73- }
74- return rows [ 0 ] ;
75- }
76-
77- import { getSettings } from "../../settings" ;
78-
79- function getSupabaseConfig ( ) : {
80- supabaseUrl : string ;
81- serviceKey : string ;
82- } | null {
83- const settings = getSettings ( ) ;
84- const supabaseUrl = settings . decoSupabaseUrl ;
85- const serviceKey = settings . decoSupabaseServiceKey ;
86- if ( ! supabaseUrl || ! serviceKey ) return null ;
87- return { supabaseUrl, serviceKey } ;
88- }
89-
90- async function resolveProfileId (
91- supabaseUrl : string ,
92- serviceKey : string ,
93- email : string ,
94- ) : Promise < string | null > {
95- const profiles = await supabaseGet < { user_id : string } > (
96- supabaseUrl ,
97- serviceKey ,
98- `profiles?email=eq.${ encodeURIComponent ( email ) } &select=user_id` ,
99- ) ;
100- return profiles [ 0 ] ?. user_id ?? null ;
101- }
102-
103- async function getOrCreateDecoApiKey (
104- supabaseUrl : string ,
105- serviceKey : string ,
106- profileId : string ,
107- ) : Promise < string > {
108- const existing = await supabaseGet < { id : string } > (
109- supabaseUrl ,
110- serviceKey ,
111- `api_key?user_id=eq.${ encodeURIComponent ( profileId ) } &select=id&limit=1` ,
112- ) ;
113- if ( existing [ 0 ] ?. id ) {
114- return existing [ 0 ] . id ;
115- }
116-
117- const created = await supabasePost < { id : string } > (
118- supabaseUrl ,
119- serviceKey ,
120- "api_key" ,
121- { user_id : profileId } ,
122- ) ;
123- return created . id ;
124- }
125-
12635// Require an authenticated user on every handler in this router.
12736app . use ( "*" , async ( c , next ) => {
12837 const ctx = c . get ( "meshContext" ) ;
@@ -212,8 +121,6 @@ app.get("/", async (c) => {
212121 }
213122} ) ;
214123
215- const ADMIN_MCP = "https://sites-admin-mcp.decocache.com/api/mcp" ;
216-
217124async function fetchFaviconAsDataUrl ( domain : string ) : Promise < string | null > {
218125 try {
219126 const res = await fetch ( `https://${ domain } /favicon.ico` , {
0 commit comments