Skip to content

Commit

Permalink
✨ deploy github user contribution endpoint to cloudflare
Browse files Browse the repository at this point in the history
  • Loading branch information
Platane committed Feb 20, 2025
1 parent 10c4c3c commit 852f0ae
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 287 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ dist
!svg-only/dist
build
.env
.wrangler
.dev.vars
417 changes: 172 additions & 245 deletions bun.lock

Large diffs are not rendered by default.

13 changes: 12 additions & 1 deletion packages/github-user-contribution-service/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# @snk/github-user-contribution-service

Expose github-user-contribution as an endpoint, using vercel.sh
Expose github-user-contribution as an endpoint. hosted on cloudflare

```sh


# deploy
bunx wrangler deploy --branch=production

# change secret
bunx wrangler secret put GITHUB_TOKEN

```

This file was deleted.

52 changes: 52 additions & 0 deletions packages/github-user-contribution-service/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import { getGithubUserContribution } from "@snk/github-user-contribution";

const cors =
<
Req extends { headers: Headers },
Res extends { headers: Headers },
A extends Array<any>,
>(
f: (req: Req, ...args: A) => Res | Promise<Res>,
) =>
async (req: Req, ...args: A) => {
const res = await f(req, ...args);

const origin = req.headers.get("origin");

if (origin) {
const { host, hostname } = new URL(origin);

if (hostname === "localhost" || host === "platane.github.io")
res.headers.set("Access-Control-Allow-Origin", origin);
}

res.headers.set("Access-Control-Allow-Methods", "GET, OPTIONS");
res.headers.set("Access-Control-Allow-Headers", "Content-Type");
return res;
};

export default {
fetch: cors(async (req: Request, env: { GITHUB_TOKEN: string }) => {
const url = new URL(req.url);

const [, userName] =
url.pathname.match(/^\/github-user-contribution\/([^\/]*)\/?$/) ?? [];

if (req.method === "OPTIONS") return new Response();

if (!userName || req.method !== "GET")
return new Response("unknown route", { status: 404 });

const body = await getGithubUserContribution(userName, {
githubToken: env.GITHUB_TOKEN,
});

return new Response(JSON.stringify(body), {
status: 200,
headers: {
"Cache-Control": "max-age=21600, s-maxage=21600",
"Content-Type": "application/json",
},
});
}),
};
10 changes: 8 additions & 2 deletions packages/github-user-contribution-service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
"name": "@snk/github-user-contribution-service",
"version": "1.0.0",
"dependencies": {
"@snk/github-user-contribution": "1.0.0",
"@vercel/node": "5.1.7"
"@snk/github-user-contribution": "1.0.0"
},
"devDependencies": {
"wrangler": "3.109.2",
"@cloudflare/workers-types": "4.20250214.0"
},
"scripts": {
"deploy": "wrangler deploy"
}
}
5 changes: 0 additions & 5 deletions packages/github-user-contribution-service/vercel.json

This file was deleted.

9 changes: 9 additions & 0 deletions packages/github-user-contribution-service/wrangler.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
name = "github-user-contribution"
main = "index.ts"
compatibility_date = "2024-09-02"

account_id = "56268cde636c288343cb0767952ecf2e"
workers_dev = true

[observability]
enabled = true
3 changes: 2 additions & 1 deletion packages/github-user-contribution/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,13 @@ export const getGithubUserContribution = async (
headers: {
Authorization: `bearer ${o.githubToken}`,
"Content-Type": "application/json",
"User-Agent": "[email protected]",
},
method: "POST",
body: JSON.stringify({ variables, query }),
});

if (!res.ok) throw new Error(res.statusText);
if (!res.ok) throw new Error(await res.text().catch(() => res.statusText));

const { data, errors } = (await res.json()) as {
data: GraphQLRes;
Expand Down

0 comments on commit 852f0ae

Please sign in to comment.