feat: bundle frontend with API executable
This commit is contained in:
		
							parent
							
								
									57c7a0f4de
								
							
						
					
					
						commit
						6c9ebf1d08
					
				
					 13 changed files with 105 additions and 16 deletions
				
			
		| 
						 | 
				
			
			@ -7,7 +7,7 @@ import Logo from "./logo";
 | 
			
		|||
import { useRecoilState } from "recoil";
 | 
			
		||||
import { userState } from "./store";
 | 
			
		||||
import fetchAPI from "./fetch";
 | 
			
		||||
import { MeUser } from "./types";
 | 
			
		||||
import { APIError, ErrorCode, MeUser } from "./types";
 | 
			
		||||
 | 
			
		||||
function Navigation() {
 | 
			
		||||
  const [user, setUser] = useRecoilState(userState);
 | 
			
		||||
| 
						 | 
				
			
			@ -17,7 +17,15 @@ function Navigation() {
 | 
			
		|||
 | 
			
		||||
    fetchAPI<MeUser>("/users/@me").then(
 | 
			
		||||
      (res) => setUser(res),
 | 
			
		||||
      (err) => console.log("fetching /users/@me", err)
 | 
			
		||||
      (err) => {
 | 
			
		||||
        console.log("fetching /users/@me", err);
 | 
			
		||||
        if (
 | 
			
		||||
          (err as APIError).code == ErrorCode.InvalidToken ||
 | 
			
		||||
          (err as APIError).code == ErrorCode.Forbidden
 | 
			
		||||
        ) {
 | 
			
		||||
          localStorage.removeItem("pronouns-token");
 | 
			
		||||
        }
 | 
			
		||||
      }
 | 
			
		||||
    );
 | 
			
		||||
  }, []);
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -45,7 +53,7 @@ function Navigation() {
 | 
			
		|||
 | 
			
		||||
  const nav = user ? (
 | 
			
		||||
    <>
 | 
			
		||||
      <NavItem to={`/@${user.username}`}>@{user.username}</NavItem>
 | 
			
		||||
      <NavItem to={`/u/${user.username}`}>@{user.username}</NavItem>
 | 
			
		||||
      <NavItem to="/settings">Settings</NavItem>
 | 
			
		||||
      <NavItem to="/logout">Log out</NavItem>
 | 
			
		||||
    </>
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -6,10 +6,18 @@ export default async function fetchAPI<T>(
 | 
			
		|||
  method = "GET",
 | 
			
		||||
  body = null
 | 
			
		||||
) {
 | 
			
		||||
  let headers = {};
 | 
			
		||||
  const token = localStorage.getItem("pronouns-token");
 | 
			
		||||
  if (token) {
 | 
			
		||||
    headers = {
 | 
			
		||||
      Authorization: token,
 | 
			
		||||
    };
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  const resp = await fetch(`/api/v1${path}`, {
 | 
			
		||||
    method,
 | 
			
		||||
    headers: {
 | 
			
		||||
      Authorization: localStorage.getItem("pronouns-token"),
 | 
			
		||||
      ...headers,
 | 
			
		||||
      "Content-Type": "application/json",
 | 
			
		||||
    },
 | 
			
		||||
    body: body ? JSON.stringify(body) : null,
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -15,7 +15,10 @@ async function getCurrentUser() {
 | 
			
		|||
  try {
 | 
			
		||||
    return await fetchAPI<MeUser>("/users/@me");
 | 
			
		||||
  } catch (e) {
 | 
			
		||||
    if ((e as APIError).code === ErrorCode.Forbidden) {
 | 
			
		||||
    if (
 | 
			
		||||
      (e as APIError).code === ErrorCode.Forbidden ||
 | 
			
		||||
      (e as APIError).code === ErrorCode.InvalidToken
 | 
			
		||||
    ) {
 | 
			
		||||
      localStorage.removeItem("pronouns-token");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -43,6 +43,7 @@ export enum ErrorCode {
 | 
			
		|||
 | 
			
		||||
  InvalidState = 1001,
 | 
			
		||||
  InvalidOAuthCode = 1002,
 | 
			
		||||
  InvalidToken = 1003,
 | 
			
		||||
 | 
			
		||||
  UserNotFound = 2001,
 | 
			
		||||
}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue