-
Notifications
You must be signed in to change notification settings - Fork 14
Fix changes for axum rewrite and refactor request handling #488
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Eldemarkki
wants to merge
15
commits into
main
Choose a base branch
from
axum-rewrite
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
83b2717
feat: fix changes for axum rewrite and refactor request handling
Eldemarkki f36912e
feat: add password prompt to regenerating auth token
Eldemarkki 4da8652
feat: show notifications when deleting account
Eldemarkki 0eb5ebb
Merge branch 'main' into axum-rewrite
Eldemarkki 56cdf0a
update leaderboards
Eldemarkki 8600a8e
fix: fix join leaderboard errors
Eldemarkki 83a1fb3
fix: fix bug where unauthenticated user couldnt search public users
Eldemarkki 1c929b6
fix lint error
Eldemarkki 66ddcf0
Merge branch 'main' into axum-rewrite
Eldemarkki fbd0741
Merge branch 'main' into axum-rewrite
Eldemarkki 9d009e6
Merge branch 'main' into axum-rewrite
Eldemarkki 6eddddf
chore: fix package-lock.json
Eldemarkki f48dee5
fix: fix package lock again and update nextjs
Eldemarkki f226267
fix: fix lint errors
Eldemarkki da06258
fix: fix package lock part 3
Eldemarkki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,4 +1,4 @@ | ||
| { | ||
| "editor.defaultFormatter": "rvest.vs-code-prettier-eslint", | ||
| "editor.defaultFormatter": "esbenp.prettier-vscode", | ||
| "nixEnvSelector.nixFile": "${workspaceRoot}/shell.nix" | ||
| } |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 contains hidden or 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 contains hidden or 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,121 @@ | ||
| "use server"; | ||
|
|
||
| import { cookies, headers } from "next/headers"; | ||
| import { GetRequestError, PostRequestError } from "../types"; | ||
|
|
||
| export const getRequest = async <T>(path: string) => { | ||
| const token = cookies().get("token")?.value; | ||
| if (!token) { | ||
| return { | ||
| error: GetRequestError.Unauthorized as const, | ||
| }; | ||
| } | ||
|
|
||
| const ip = headers().get("client-ip") ?? "Unknown IP"; | ||
|
|
||
| const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}${path}`, { | ||
| headers: { | ||
| Authorization: `Bearer ${token}`, | ||
| "client-ip": ip, | ||
| "bypass-token": process.env.RATELIMIT_IP_FORWARD_SECRET ?? "", | ||
| }, | ||
| cache: "no-cache", | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| if (response.status === 401) { | ||
| return { | ||
| error: GetRequestError.Unauthorized as const, | ||
| response, | ||
| }; | ||
| } | ||
|
|
||
| if (response.status === 429) { | ||
| return { | ||
| error: GetRequestError.RateLimited as const, | ||
| response, | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| error: GetRequestError.UnknownError as const, | ||
| path, | ||
| response, | ||
| }; | ||
| } | ||
|
|
||
| const data = (await response.json()) as T; | ||
|
|
||
| return data; | ||
| }; | ||
|
|
||
| type WithResponseBody<R> = | ||
| | { data: R } | ||
| | { error: PostRequestError; statusCode: number }; | ||
|
|
||
| type NoResponseBody = WithResponseBody<null>; | ||
|
|
||
| async function baseFetch( | ||
| path: string, | ||
| body?: unknown, | ||
| method: string = "POST", | ||
| ) { | ||
| const tokenCookieName = "token"; | ||
| const token = cookies().get(tokenCookieName)?.value; | ||
|
|
||
| const h = new Headers({ | ||
| "Content-Type": "application/json", | ||
| "client-ip": headers().get("client-ip") ?? "Unknown IP", | ||
| "bypass-token": process.env.RATELIMIT_IP_FORWARD_SECRET ?? "", | ||
| }); | ||
|
|
||
| if (token) { | ||
| h.set("Authorization", `Bearer ${token}`); | ||
| } | ||
|
|
||
| const response = await fetch(process.env.NEXT_PUBLIC_API_URL + path, { | ||
| method, | ||
| headers: h, | ||
| cache: "no-cache", | ||
| body: body ? JSON.stringify(body) : undefined, | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| if (response.status === 429) { | ||
| return { | ||
| error: PostRequestError.RateLimited, | ||
| statusCode: response.status, | ||
| }; | ||
| } | ||
|
|
||
| return { | ||
| error: PostRequestError.UnknownError, | ||
| statusCode: response.status, | ||
| }; | ||
| } | ||
|
|
||
| return response; | ||
| } | ||
|
|
||
| export async function postRequestWithResponse<R>( | ||
| path: string, | ||
| body?: unknown, | ||
| method: string = "POST", | ||
| ): Promise<WithResponseBody<R>> { | ||
| const response = await baseFetch(path, body, method); | ||
| if ("error" in response) return response; | ||
|
|
||
| const data = (await response.json()) as R; | ||
| return { data }; | ||
| } | ||
|
|
||
| export async function postRequestWithoutResponse( | ||
| path: string, | ||
| body?: unknown, | ||
| method: string = "POST", | ||
| ): Promise<NoResponseBody> { | ||
| const response = await baseFetch(path, body, method); | ||
| if ("error" in response) return response; | ||
|
|
||
| return { data: null }; | ||
| } | ||
This file contains hidden or 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 contains hidden or 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,123 +1,43 @@ | ||
| "use server"; | ||
|
|
||
| import { cookies, headers } from "next/headers"; | ||
| import { | ||
| CreateLeaderboardError, | ||
| GetLeaderboardError, | ||
| GetLeaderboardsError, | ||
| Leaderboard, | ||
| LeaderboardData, | ||
| } from "../types"; | ||
| import { getRequest, postRequestWithResponse } from "./baseApi"; | ||
|
|
||
| export const getMyLeaderboards = async () => { | ||
| const token = cookies().get("token")?.value; | ||
| if (!token) { | ||
| return { error: GetLeaderboardsError.Unauthorized }; | ||
| } | ||
|
|
||
| try { | ||
| const response = await fetch( | ||
| process.env.NEXT_PUBLIC_API_URL + "/users/@me/leaderboards", | ||
| { | ||
| headers: { | ||
| Authorization: `Bearer ${token}`, | ||
| "client-ip": headers().get("client-ip") ?? "Unknown IP", | ||
| "bypass-token": process.env.RATELIMIT_IP_FORWARD_SECRET ?? "", | ||
| }, | ||
| cache: "no-cache", | ||
| }, | ||
| ); | ||
|
|
||
| if (!response.ok) { | ||
| if (response.status === 401) { | ||
| return { error: GetLeaderboardsError.Unauthorized }; | ||
| } else if (response.status === 429) { | ||
| return { error: GetLeaderboardsError.RateLimited }; | ||
| } | ||
|
|
||
| const errorText = await response.text(); | ||
| console.log("Unknown error when getting user's leaderboards:", errorText); | ||
|
|
||
| return { error: GetLeaderboardsError.UnknownError }; | ||
| } | ||
|
|
||
| const data = (await response.json()) as Leaderboard[]; | ||
|
|
||
| return data; | ||
| } catch (e: unknown) { | ||
| console.error("Unknown error when getting user's leaderboards:", e); | ||
| return { error: GetLeaderboardsError.UnknownError }; | ||
| } | ||
| }; | ||
| export const getMyLeaderboards = () => | ||
| getRequest<Leaderboard[]>("/users/@me/leaderboards"); | ||
|
|
||
| export const getLeaderboard = async (leaderboardName: string) => { | ||
| const token = cookies().get("token")?.value; | ||
| if (!token) { | ||
| return { error: GetLeaderboardError.Unauthorized }; | ||
| } | ||
|
|
||
| const response = await fetch( | ||
| process.env.NEXT_PUBLIC_API_URL + `/leaderboards/${leaderboardName}`, | ||
| { | ||
| headers: { | ||
| Authorization: `Bearer ${token}`, | ||
| "client-ip": headers().get("client-ip") ?? "Unknown IP", | ||
| "bypass-token": process.env.RATELIMIT_IP_FORWARD_SECRET ?? "", | ||
| }, | ||
| cache: "no-cache", | ||
| next: { | ||
| tags: [`leaderboard-${leaderboardName}`], | ||
| }, | ||
| }, | ||
| const data = await getRequest<LeaderboardData>( | ||
| `/leaderboards/${leaderboardName}`, | ||
| ); | ||
|
|
||
| if (!response.ok) { | ||
| if (response.status === 401) { | ||
| return { error: GetLeaderboardError.Unauthorized }; | ||
| } else if (response.status === 429) { | ||
| return { error: GetLeaderboardError.RateLimited }; | ||
| if ("error" in data) { | ||
| if (data.response?.status === 404) { | ||
| return { error: GetLeaderboardError.LeaderboardNotFound }; | ||
| } | ||
|
|
||
| const errorText = await response.text(); | ||
| console.log(errorText); | ||
|
|
||
| return { error: GetLeaderboardError.UnknownError }; | ||
| } | ||
|
|
||
| const data = (await response.json()) as LeaderboardData; | ||
|
|
||
| return data; | ||
| }; | ||
|
|
||
| export const createLeaderboard = async (leaderboardName: string) => { | ||
| const token = cookies().get("token")?.value; | ||
| const response = await fetch( | ||
| process.env.NEXT_PUBLIC_API_URL + "/leaderboards/create", | ||
| const data = await postRequestWithResponse<{ invite_code: string }>( | ||
| "/leaderboards/create", | ||
| { | ||
| method: "POST", | ||
| headers: { | ||
| Authorization: `Bearer ${token}`, | ||
| "Content-Type": "application/json", | ||
| "client-ip": headers().get("client-ip") ?? "Unknown IP", | ||
| "bypass-token": process.env.RATELIMIT_IP_FORWARD_SECRET ?? "", | ||
| }, | ||
| cache: "no-cache", | ||
| body: JSON.stringify({ name: leaderboardName }), | ||
| name: leaderboardName, | ||
| }, | ||
| ); | ||
|
|
||
| if (!response.ok) { | ||
| if (response.status === 409) { | ||
| return { error: CreateLeaderboardError.AlreadyExists }; | ||
| } else if (response.status === 429) { | ||
| return { error: CreateLeaderboardError.RateLimited }; | ||
| } | ||
|
|
||
| console.error("Error while creating leaderboard:", await response.text()); | ||
| return { error: CreateLeaderboardError.UnknownError }; | ||
| if ("error" in data && data.statusCode === 409) { | ||
| return { | ||
| error: CreateLeaderboardError.AlreadyExists, | ||
| }; | ||
| } | ||
|
|
||
| const data = (await response.json()) as { invite_code: string }; | ||
|
|
||
| return data; | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.