From ca9c1cdf1ec2988f14ac4ca788edac31153f735f Mon Sep 17 00:00:00 2001 From: ivarlovlie Date: Fri, 25 Nov 2022 16:30:33 +0900 Subject: feat: WIP! Rework http calls into services --- .../lib/services/abstractions/IAccountService.ts | 53 +++++++++++++++++++++ code/app/src/lib/services/account-service.ts | 54 ++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 code/app/src/lib/services/abstractions/IAccountService.ts create mode 100644 code/app/src/lib/services/account-service.ts (limited to 'code/app/src/lib/services') diff --git a/code/app/src/lib/services/abstractions/IAccountService.ts b/code/app/src/lib/services/abstractions/IAccountService.ts new file mode 100644 index 0000000..736c3ae --- /dev/null +++ b/code/app/src/lib/services/abstractions/IAccountService.ts @@ -0,0 +1,53 @@ +import type { KnownProblem } from "$lib/models/internal/KnownProblem" + +export interface IAccountService { + session: Session, + login_async(payload: LoginPayload): Promise, + logout_async(): Promise, + create_account_async(payload: CreateAccountPayload): Promise, + delete_current_async(): Promise, + update_current_async(payload: UpdateAccountPayload): Promise, +} + +export type Session = { + profile: { + username: string, + displayName: string, + id: string, + }, + lastChecked: number, +} + +export type LoginPayload = { + username: string, + password: string, + persist: boolean +} + +export type LoginResponse = { + isLoggedIn: boolean +} + +export type CreateAccountPayload = { + username: string, + password: string, +} + +export type CreateAccountResponse = { + isCreated: boolean, + knownProblem?: KnownProblem +} + +export type DeleteAccountResponse = { + isDeleted: boolean +} + +export type UpdateAccountPayload = { + username: string, + password: string +} + +export type UpdateAccountResponse = { + isUpdated: boolean, + knownProblem?: KnownProblem +} \ No newline at end of file diff --git a/code/app/src/lib/services/account-service.ts b/code/app/src/lib/services/account-service.ts new file mode 100644 index 0000000..90af163 --- /dev/null +++ b/code/app/src/lib/services/account-service.ts @@ -0,0 +1,54 @@ +import { http_delete_async, http_get_async, http_post_async } from "$lib/api/_fetch"; +import { api_base, CookieNames } from "$lib/configuration"; +import { is_known_problem } from "$lib/models/internal/KnownProblem"; +import type { CreateAccountPayload, CreateAccountResponse, DeleteAccountResponse, IAccountService, LoginPayload, LoginResponse, Session, UpdateAccountPayload, UpdateAccountResponse } from "./abstractions/IAccountService"; + +export class AccountService implements IAccountService { + session: Session; + async login_async(payload: LoginPayload): Promise { + const response = await http_post_async(api_base("_/account/login"), payload); + return { isLoggedIn: response.ok }; + } + async logout_async(): Promise { + const response = await http_get_async(api_base("_/account/logout")); + + if (!response.ok) { + const deleteCookieResponse = await fetch("/delete-cookie?key=" + CookieNames.session); + if (!deleteCookieResponse.ok) { + throw new Error("Could neither logout nor delete session cookie."); + } + } + + return; + } + async create_account_async(payload: CreateAccountPayload): Promise { + const response = await http_post_async(api_base("_/account/create"), payload); + if (response.ok) return { isCreated: true }; + if (is_known_problem(response)) return { + isCreated: false, + knownProblem: await response.json() + } + + return { + isCreated: false + } + } + async delete_current_async(): Promise { + const response = await http_delete_async(api_base("_/account/delete")); + return { + isDeleted: response.ok + } + } + async update_current_async(payload: UpdateAccountPayload): Promise { + const response = await http_post_async(api_base("_/account/update"), payload); + if (response.ok) return { isUpdated: true }; + if (is_known_problem(response)) return { + isUpdated: false, + knownProblem: await response.json() + } + + return { + isUpdated: false + } + } +} \ No newline at end of file -- cgit v1.3