-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ deploy github user contribution endpoint to cloudflare
- Loading branch information
Showing
9 changed files
with
257 additions
and
287 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 |
---|---|---|
|
@@ -4,3 +4,5 @@ dist | |
!svg-only/dist | ||
build | ||
.env | ||
.wrangler | ||
.dev.vars |
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 |
---|---|---|
@@ -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 | ||
|
||
``` |
33 changes: 0 additions & 33 deletions
33
packages/github-user-contribution-service/api/github-user-contribution/[userName].ts
This file was deleted.
Oops, something went wrong.
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,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", | ||
}, | ||
}); | ||
}), | ||
}; |
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
This file was deleted.
Oops, something went wrong.
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,9 @@ | ||
name = "github-user-contribution" | ||
main = "index.ts" | ||
compatibility_date = "2024-09-02" | ||
|
||
account_id = "56268cde636c288343cb0767952ecf2e" | ||
workers_dev = true | ||
|
||
[observability] | ||
enabled = true |
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 |
---|---|---|
|
@@ -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; | ||
|