Skip to content

Commit

Permalink
Use a CORS proxy to access giveth project info
Browse files Browse the repository at this point in the history
  • Loading branch information
sembrestels committed Jul 22, 2024
1 parent b40ca70 commit ee50cec
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 12 deletions.
39 changes: 39 additions & 0 deletions packages/cors-proxy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# CORS Proxy

## Installation and Usage

### Installation

First, install the required dependencies using:

```sh
bun i
```

### Building the Project

To build the project, use the following command:

```sh
bun run build
```

This will create a `./dist/main.js` bundle that includes all tree-shaken dependencies.

### Deployment

To deploy the project, follow these steps:

1. Create a function:

```sh
bun create-func $NAME
```

2. Deploy the function:

```sh
bun deploy-func $NAME
```

Replace `$NAME` with the desired name of your function.
22 changes: 22 additions & 0 deletions packages/cors-proxy/biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"$schema": "https://biomejs.dev/schemas/1.8.3/schema.json",
"vcs": {
"enabled": true,
"clientKind": "git",
"useIgnoreFile": true
},
"organizeImports": {
"enabled": false
},
"linter": {
"enabled": true,
"rules": {
"recommended": true
}
},
"formatter": {
"enabled": true,
"indentWidth": 2,
"indentStyle": "space"
}
}
21 changes: 21 additions & 0 deletions packages/cors-proxy/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "evmcrispr-cors-proxy",
"private": true,
"version": "0.0.0",
"type": "module",
"main": "./src/main.ts",
"scripts": {
"build": "tsc src/main.ts && rollup -c",
"create-func": "fleek functions create --name",
"deploy-func": "fleek functions deploy --noBundle --path dist/main.js --name",
"lint": "biome check --write && biome format --write && biome lint --write"
},
"devDependencies": {
"@biomejs/biome": "^1.8.3",
"@rollup/plugin-node-resolve": "^13.0.6",
"@rollup/plugin-typescript": "^11.1.6",
"rollup": "^2.60.0",
"typescript": "^5.2.2"
},
"license": "MIT"
}
16 changes: 16 additions & 0 deletions packages/cors-proxy/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { nodeResolve } from "@rollup/plugin-node-resolve";
import typescript from "@rollup/plugin-typescript";

export default {
input: "src/main.ts",
output: {
dir: "dist",
format: "es",
inlineDynamicImports: true,
banner: 'import { Buffer } from "node:buffer";',
},
plugins: [
nodeResolve(), // Needed to bundle the assets from node_modules
typescript(),
],
};
103 changes: 103 additions & 0 deletions packages/cors-proxy/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
type RequestObject = {
method: "GET" | "POST" | "PUT" | "DELETE" | "PATCH" | "OPTIONS" | "HEAD";
headers?: {
[key: string]: string;
} | null;
path: string;
query?: {
[key: string]: string | string[];
} | null;
body?: string | null;
};

type ResponseObject =
| {
status: number;
headers?: {
[key: string]: string;
} | null;
body?: string;
}
| string
| ArrayBuffer;

export async function main(params: RequestObject): Promise<ResponseObject> {
try {
const { url } = processParams(params);

const response = await fetch(url, {
method: params.method,
headers: params.headers ?? {},
body: JSON.stringify(params.body),
});
const body = await response.text();
const headers = Object.fromEntries((response.headers as any).entries());
headers["Access-Control-Allow-Origin"] = "*";
const status = response.status;

return {
status,
headers,
body,
};
} catch (error) {
if (isErrorWithStatusAndBody(error)) {
return error;
}
return {
status: 500,
body: "Internal Server Error: " + error.message + error.stack,
};
}
}

// const slug = "evmcrispr";
// main({
// method: "POST",
// path: "/v0/https://mainnet.serve.giveth.io/graphql",
// headers: {
// "Content-Type": "application/json",
// },
// body: JSON.stringify({
// query: `
// query GetLearnWithJasonEpisodes($slug: String!) {
// projectBySlug(slug: $slug) {
// id
// addresses {
// address
// networkId
// }
// }
// }
// `,
// variables: {
// slug,
// },
// }),
// }).then((res) => {
// console.log(res);
// });

function isErrorWithStatusAndBody(
error: unknown,
): error is { status: number; body: string } {
const err = error as { [key: string]: unknown };
return (
err &&
typeof err === "object" &&
typeof err.status === "number" &&
typeof err.body === "string"
);
}

function processParams(params: RequestObject): { url: string } {
const { path } = params;
if (!path.startsWith("/v0/")) {
throw {
status: 400,
body: "Invalid URL",
};
}
const url = path.replace("/v0/", "");
return { url };
}
27 changes: 15 additions & 12 deletions packages/evmcrispr/src/modules/giveth/helpers/projectAddr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ export const _projectAddr = async (
slug: string,
): Promise<[string, number]> => {
const chainId = await module.getChainId();
const result = await fetch("https://mainnet.serve.giveth.io/graphql", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
query: `
const result = await fetch(
"https://cors-proxy.functions.on-fleek.app/v0/https://mainnet.serve.giveth.io/graphql",
{
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
query: `
query GetLearnWithJasonEpisodes($slug: String!) {
projectBySlug(slug: $slug) {
id
Expand All @@ -27,11 +29,12 @@ export const _projectAddr = async (
}
}
`,
variables: {
slug,
},
}),
})
variables: {
slug,
},
}),
},
)
.then((res) => res.json())
.then((res) => [
res.data.projectBySlug.addresses.find((x: any) => x.networkId === chainId)
Expand Down

0 comments on commit ee50cec

Please sign in to comment.