From 630fe83de5b41aa828050dad976520e6fd7883e2 Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Tue, 30 Aug 2022 18:11:36 -0800 Subject: [PATCH] cleanup --- frontend/.prettierrc | 9 +- frontend/src/api/user.ts | 20 ++-- frontend/src/lib/requests/requests.ts | 148 +++++++++++++------------- frontend/src/pages/home.vue | 108 +++++++++++-------- frontend/src/store/auth.ts | 45 +++++--- 5 files changed, 184 insertions(+), 146 deletions(-) diff --git a/frontend/.prettierrc b/frontend/.prettierrc index 4b09075..b12bbd4 100644 --- a/frontend/.prettierrc +++ b/frontend/.prettierrc @@ -1,9 +1,10 @@ { "arrowParens": "avoid", "semi": true, - "tabWidth": 4, - "useTabs": true, + "tabWidth": 2, + "useTabs": false, "vueIndentScriptAndStyle": true, "singleQuote": true, - "trailingComma": "es5" -} + "trailingComma": "es5", + "printWidth": 120 +} \ No newline at end of file diff --git a/frontend/src/api/user.ts b/frontend/src/api/user.ts index a468737..fb93a07 100644 --- a/frontend/src/api/user.ts +++ b/frontend/src/api/user.ts @@ -1,18 +1,22 @@ import { BaseAPI, UrlBuilder } from './base'; export type Result = { - item: T; + item: T; }; export type User = { - name: string; - email: string; - isSuperuser: boolean; - id: number; + name: string; + email: string; + isSuperuser: boolean; + id: number; }; export class UserApi extends BaseAPI { - public self() { - return this.http.get>(UrlBuilder('/users/self')); - } + public self() { + return this.http.get>(UrlBuilder('/users/self')); + } + + public logout() { + return this.http.post(UrlBuilder('/users/logout'), {}); + } } diff --git a/frontend/src/lib/requests/requests.ts b/frontend/src/lib/requests/requests.ts index c0c83be..7ee3f71 100644 --- a/frontend/src/lib/requests/requests.ts +++ b/frontend/src/lib/requests/requests.ts @@ -1,95 +1,97 @@ export enum Method { - GET = 'GET', - POST = 'POST', - PUT = 'PUT', - DELETE = 'DELETE', + GET = 'GET', + POST = 'POST', + PUT = 'PUT', + DELETE = 'DELETE', } export interface TResponse { - status: number; - error: boolean; - data: T; - response: Response; + status: number; + error: boolean; + data: T; + response: Response; } export class Requests { - private baseUrl: string; - private token: () => string; - private headers: Record = {}; - private logger?: (response: Response) => void; + private baseUrl: string; + private token: () => string; + private headers: Record = {}; + private logger?: (response: Response) => void; - private url(rest: string): string { - return this.baseUrl + rest; - } + private url(rest: string): string { + return this.baseUrl + rest; + } - constructor( - baseUrl: string, - token: string | (() => string) = '', - headers: Record = {}, - logger?: (response: Response) => void - ) { - this.baseUrl = baseUrl; - this.token = typeof token === 'string' ? () => token : token; - this.headers = headers; - this.logger = logger; - } + constructor( + baseUrl: string, + token: string | (() => string) = '', + headers: Record = {}, + logger?: (response: Response) => void + ) { + this.baseUrl = baseUrl; + this.token = typeof token === 'string' ? () => token : token; + this.headers = headers; + this.logger = logger; + } - public get(url: string): Promise> { - return this.do(Method.GET, url); - } + public get(url: string): Promise> { + return this.do(Method.GET, url); + } - public post(url: string, payload: T): Promise> { - return this.do(Method.POST, url, payload); - } + public post(url: string, payload: T): Promise> { + return this.do(Method.POST, url, payload); + } - public put(url: string, payload: T): Promise> { - return this.do(Method.PUT, url, payload); - } + public put(url: string, payload: T): Promise> { + return this.do(Method.PUT, url, payload); + } - public delete(url: string): Promise> { - return this.do(Method.DELETE, url); - } + public delete(url: string): Promise> { + return this.do(Method.DELETE, url); + } - private methodSupportsBody(method: Method): boolean { - return method === Method.POST || method === Method.PUT; - } + private methodSupportsBody(method: Method): boolean { + return method === Method.POST || method === Method.PUT; + } - private async do( - method: Method, - url: string, - payload: Object = {} - ): Promise> { - const args: RequestInit = { - method, - headers: { - 'Content-Type': 'application/json', - ...this.headers, - }, - }; + private async do(method: Method, url: string, payload: Object = {}): Promise> { + const args: RequestInit = { + method, + headers: { + 'Content-Type': 'application/json', + ...this.headers, + }, + }; - const token = this.token(); - if (token !== '' && args.headers !== undefined) { - // @ts-expect-error -- headers is always defined at this point - args.headers['Authorization'] = token; - } + const token = this.token(); + if (token !== '' && args.headers !== undefined) { + // @ts-expect-error -- headers is always defined at this point + args.headers['Authorization'] = token; + } - if (this.methodSupportsBody(method)) { - args.body = JSON.stringify(payload); - } + if (this.methodSupportsBody(method)) { + args.body = JSON.stringify(payload); + } - const response = await fetch(this.url(url), args); + const response = await fetch(this.url(url), args); - if (this.logger) { - this.logger(response); - } + if (this.logger) { + this.logger(response); + } - const data = await response.json(); + const data: T = await (async () => { + try { + return await response.json(); + } catch (e) { + return {} as T; + } + })(); - return { - status: response.status, - error: !response.ok, - data, - response, - }; - } + return { + status: response.status, + error: !response.ok, + data, + response, + }; + } } diff --git a/frontend/src/pages/home.vue b/frontend/src/pages/home.vue index f159a2c..5109a92 100644 --- a/frontend/src/pages/home.vue +++ b/frontend/src/pages/home.vue @@ -1,57 +1,73 @@ diff --git a/frontend/src/store/auth.ts b/frontend/src/store/auth.ts index 3e3e6b4..092d1fb 100644 --- a/frontend/src/store/auth.ts +++ b/frontend/src/store/auth.ts @@ -1,21 +1,36 @@ +import { UserApi } from '@/api/user'; import { defineStore } from 'pinia'; export const useAuthStore = defineStore('auth', { - state: () => ({ - token: useLocalStorage('pinia/auth/token', ''), - expires: useLocalStorage('pinia/auth/expires', ''), - }), - getters: { - isTokenExpired: state => { - if (!state.expires) { - return true; - } + state: () => ({ + token: useLocalStorage('pinia/auth/token', ''), + expires: useLocalStorage('pinia/auth/expires', ''), + }), + getters: { + isTokenExpired: state => { + if (!state.expires) { + return true; + } - if (typeof state.expires === 'string') { - return new Date(state.expires) < new Date(); - } + if (typeof state.expires === 'string') { + return new Date(state.expires) < new Date(); + } - return state.expires < new Date(); - }, - }, + return state.expires < new Date(); + }, + }, + actions: { + async logout(api: UserApi) { + const result = await api.logout(); + + if (result.error) { + return result; + } + + this.token = ''; + this.expires = ''; + + return result; + }, + }, });