|
| 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 | +} |
0 commit comments