Skip to content

Commit 104ce2e

Browse files
committed
init
0 parents  commit 104ce2e

File tree

6 files changed

+102
-0
lines changed

6 files changed

+102
-0
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# palm-netlify-proxy

netlify.toml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[[headers]]
2+
for = "/*"
3+
[headers.values]
4+
Access-Control-Allow-Origin = "*"
5+
Access-Control-Allow-Headers = "*"
6+
Access-Control-Allow-Methods = "*"
7+
[[redirects]]
8+
from = "/*"
9+
to = "/.netlify/functions/proxy"
10+
status = 200
11+
force = true

netlify/functions/proxy.ts

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { Context } from "@netlify/edge-functions";
2+
3+
const pickHeaders = (headers: Headers, keys: (string | RegExp)[]): Headers => {
4+
const picked = new Headers();
5+
for (const key of headers.keys()) {
6+
if (keys.some((k) => (typeof k === "string" ? k === key : k.test(key)))) {
7+
const value = headers.get(key);
8+
if (typeof value === "string") {
9+
picked.set(key, value);
10+
}
11+
}
12+
}
13+
return picked;
14+
};
15+
16+
const CORS_HEADERS: Record<string, string> = {
17+
"access-control-allow-origin": "*",
18+
"access-control-allow-methods": "*",
19+
"access-control-allow-headers": "*",
20+
};
21+
22+
export default async (request: Request, context: Context) => {
23+
24+
console.log("111111111", request.url)
25+
26+
if (request.method === "OPTIONS") {
27+
return new Response(null, {
28+
headers: CORS_HEADERS,
29+
});
30+
}
31+
32+
const { pathname, searchParams } = new URL(request.url);
33+
34+
const url = new URL(pathname, "https://generativelanguage.googleapis.com");
35+
searchParams.delete("_path");
36+
37+
searchParams.forEach((value, key) => {
38+
url.searchParams.append(key, value);
39+
});
40+
41+
const headers = pickHeaders(request.headers, ["content-type", "x-goog-api-client", "x-goog-api-key"]);
42+
43+
const response = await fetch(url, {
44+
body: request.body,
45+
method: request.method,
46+
duplex: 'half',
47+
headers,
48+
});
49+
50+
const responseHeaders = {
51+
...CORS_HEADERS,
52+
...Object.fromEntries(response.headers)
53+
};
54+
55+
return new Response(response.body, {
56+
headers: responseHeaders,
57+
status: response.status
58+
});
59+
};

package-lock.json

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "palm-netlify-proxy",
3+
"version": "1.0.0",
4+
"scripts": {},
5+
"dependencies": {},
6+
"devDependencies": {
7+
"@netlify/edge-functions": "2.2.0"
8+
}
9+
}

0 commit comments

Comments
 (0)