-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a CORS proxy to access giveth project info
- Loading branch information
1 parent
b40ca70
commit ee50cec
Showing
6 changed files
with
216 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(), | ||
], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters