Skip to content

Commit ee50cec

Browse files
committed
Use a CORS proxy to access giveth project info
1 parent b40ca70 commit ee50cec

File tree

6 files changed

+216
-12
lines changed

6 files changed

+216
-12
lines changed

packages/cors-proxy/README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# CORS Proxy
2+
3+
## Installation and Usage
4+
5+
### Installation
6+
7+
First, install the required dependencies using:
8+
9+
```sh
10+
bun i
11+
```
12+
13+
### Building the Project
14+
15+
To build the project, use the following command:
16+
17+
```sh
18+
bun run build
19+
```
20+
21+
This will create a `./dist/main.js` bundle that includes all tree-shaken dependencies.
22+
23+
### Deployment
24+
25+
To deploy the project, follow these steps:
26+
27+
1. Create a function:
28+
29+
```sh
30+
bun create-func $NAME
31+
```
32+
33+
2. Deploy the function:
34+
35+
```sh
36+
bun deploy-func $NAME
37+
```
38+
39+
Replace `$NAME` with the desired name of your function.

packages/cors-proxy/biome.json

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"$schema": "https://biomejs.dev/schemas/1.8.3/schema.json",
3+
"vcs": {
4+
"enabled": true,
5+
"clientKind": "git",
6+
"useIgnoreFile": true
7+
},
8+
"organizeImports": {
9+
"enabled": false
10+
},
11+
"linter": {
12+
"enabled": true,
13+
"rules": {
14+
"recommended": true
15+
}
16+
},
17+
"formatter": {
18+
"enabled": true,
19+
"indentWidth": 2,
20+
"indentStyle": "space"
21+
}
22+
}

packages/cors-proxy/package.json

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "evmcrispr-cors-proxy",
3+
"private": true,
4+
"version": "0.0.0",
5+
"type": "module",
6+
"main": "./src/main.ts",
7+
"scripts": {
8+
"build": "tsc src/main.ts && rollup -c",
9+
"create-func": "fleek functions create --name",
10+
"deploy-func": "fleek functions deploy --noBundle --path dist/main.js --name",
11+
"lint": "biome check --write && biome format --write && biome lint --write"
12+
},
13+
"devDependencies": {
14+
"@biomejs/biome": "^1.8.3",
15+
"@rollup/plugin-node-resolve": "^13.0.6",
16+
"@rollup/plugin-typescript": "^11.1.6",
17+
"rollup": "^2.60.0",
18+
"typescript": "^5.2.2"
19+
},
20+
"license": "MIT"
21+
}

packages/cors-proxy/rollup.config.js

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { nodeResolve } from "@rollup/plugin-node-resolve";
2+
import typescript from "@rollup/plugin-typescript";
3+
4+
export default {
5+
input: "src/main.ts",
6+
output: {
7+
dir: "dist",
8+
format: "es",
9+
inlineDynamicImports: true,
10+
banner: 'import { Buffer } from "node:buffer";',
11+
},
12+
plugins: [
13+
nodeResolve(), // Needed to bundle the assets from node_modules
14+
typescript(),
15+
],
16+
};

packages/cors-proxy/src/main.ts

+103
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
type RequestObject = {
2+
method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "OPTIONS" | "HEAD";
3+
headers?: {
4+
[key: string]: string;
5+
} | null;
6+
path: string;
7+
query?: {
8+
[key: string]: string | string[];
9+
} | null;
10+
body?: string | null;
11+
};
12+
13+
type ResponseObject =
14+
| {
15+
status: number;
16+
headers?: {
17+
[key: string]: string;
18+
} | null;
19+
body?: string;
20+
}
21+
| string
22+
| ArrayBuffer;
23+
24+
export async function main(params: RequestObject): Promise<ResponseObject> {
25+
try {
26+
const { url } = processParams(params);
27+
28+
const response = await fetch(url, {
29+
method: params.method,
30+
headers: params.headers ?? {},
31+
body: JSON.stringify(params.body),
32+
});
33+
const body = await response.text();
34+
const headers = Object.fromEntries((response.headers as any).entries());
35+
headers["Access-Control-Allow-Origin"] = "*";
36+
const status = response.status;
37+
38+
return {
39+
status,
40+
headers,
41+
body,
42+
};
43+
} catch (error) {
44+
if (isErrorWithStatusAndBody(error)) {
45+
return error;
46+
}
47+
return {
48+
status: 500,
49+
body: "Internal Server Error: " + error.message + error.stack,
50+
};
51+
}
52+
}
53+
54+
// const slug = "evmcrispr";
55+
// main({
56+
// method: "POST",
57+
// path: "/v0/https://mainnet.serve.giveth.io/graphql",
58+
// headers: {
59+
// "Content-Type": "application/json",
60+
// },
61+
// body: JSON.stringify({
62+
// query: `
63+
// query GetLearnWithJasonEpisodes($slug: String!) {
64+
// projectBySlug(slug: $slug) {
65+
// id
66+
// addresses {
67+
// address
68+
// networkId
69+
// }
70+
// }
71+
// }
72+
// `,
73+
// variables: {
74+
// slug,
75+
// },
76+
// }),
77+
// }).then((res) => {
78+
// console.log(res);
79+
// });
80+
81+
function isErrorWithStatusAndBody(
82+
error: unknown,
83+
): error is { status: number; body: string } {
84+
const err = error as { [key: string]: unknown };
85+
return (
86+
err &&
87+
typeof err === "object" &&
88+
typeof err.status === "number" &&
89+
typeof err.body === "string"
90+
);
91+
}
92+
93+
function processParams(params: RequestObject): { url: string } {
94+
const { path } = params;
95+
if (!path.startsWith("/v0/")) {
96+
throw {
97+
status: 400,
98+
body: "Invalid URL",
99+
};
100+
}
101+
const url = path.replace("/v0/", "");
102+
return { url };
103+
}

packages/evmcrispr/src/modules/giveth/helpers/projectAddr.ts

+15-12
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,15 @@ export const _projectAddr = async (
1010
slug: string,
1111
): Promise<[string, number]> => {
1212
const chainId = await module.getChainId();
13-
const result = await fetch("https://mainnet.serve.giveth.io/graphql", {
14-
method: "POST",
15-
headers: {
16-
"Content-Type": "application/json",
17-
},
18-
body: JSON.stringify({
19-
query: `
13+
const result = await fetch(
14+
"https://cors-proxy.functions.on-fleek.app/v0/https://mainnet.serve.giveth.io/graphql",
15+
{
16+
method: "POST",
17+
headers: {
18+
"Content-Type": "application/json",
19+
},
20+
body: JSON.stringify({
21+
query: `
2022
query GetLearnWithJasonEpisodes($slug: String!) {
2123
projectBySlug(slug: $slug) {
2224
id
@@ -27,11 +29,12 @@ export const _projectAddr = async (
2729
}
2830
}
2931
`,
30-
variables: {
31-
slug,
32-
},
33-
}),
34-
})
32+
variables: {
33+
slug,
34+
},
35+
}),
36+
},
37+
)
3538
.then((res) => res.json())
3639
.then((res) => [
3740
res.data.projectBySlug.addresses.find((x: any) => x.networkId === chainId)

0 commit comments

Comments
 (0)