Skip to content

Instantly share code, notes, and snippets.

@dmslabsbr
Created May 23, 2025 18:50
Show Gist options
  • Save dmslabsbr/9d08b5baf06f534795ec4e4be0f76cd2 to your computer and use it in GitHub Desktop.
Save dmslabsbr/9d08b5baf06f534795ec4e4be0f76cd2 to your computer and use it in GitHub Desktop.
Supabase Edge Function API Proxy
import { serve } from "https://deno.land/[email protected]/http/server.ts";
serve(async (req)=>{
// 1) Coloque aqui exatamente o host e porta da sua API, sem typo:
const TARGET = "http://001.002.003.004:8080";
// 2) Se você quer forçar TODO proxy a ir em /extrair-dados,
// não precisa nem olhar o req.url.pathname — basta:
const dest = `${TARGET}/extrair-dados`;
console.log("Proxying to:", dest);
// 3) Preserva headers hop-by-hop, content-type, etc.
const outHeaders = new Headers();
for (const [k, v] of req.headers){
const lk = k.toLowerCase();
if ([
"connection",
"keep-alive",
"proxy-authenticate",
"proxy-authorization",
"te",
"trailers",
"transfer-encoding",
"upgrade"
].includes(lk)) continue;
outHeaders.set(k, v);
}
// (Opcional) CORS
outHeaders.set("Access-Control-Allow-Origin", "*");
// 4) Encaminha método, headers e body cru (multipart incluso)
const resp = await fetch(dest, {
method: req.method,
headers: outHeaders,
body: [
"GET",
"HEAD"
].includes(req.method) ? undefined : req.body
});
console.log("← External API replied:", resp.status);
// 5) Retorna resposta ao client
const clientHeaders = new Headers(resp.headers);
clientHeaders.set("Access-Control-Allow-Origin", "*");
return new Response(resp.body, {
status: resp.status,
headers: clientHeaders
});
});
@dmslabsbr
Copy link
Author

A lightweight Deno Edge Function for Supabase that turns your function endpoint into a transparent HTTP proxy. Incoming requests (any method, including multipart POST) are forwarded to a fixed backend URL (/extrair-dados), preserving all headers except hop-by-hop ones. CORS is enabled (Access-Control-Allow-Origin: *) so you can safely call this from a browser. Just update the TARGET constant to point at your own API host and port—no other changes required.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment