a few API routes
This commit is contained in:
11564
package-lock.json
generated
11564
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -47,5 +47,9 @@
|
||||
"typescript": "^5.9.3",
|
||||
"typescript-eslint": "^8.46.1",
|
||||
"vite": "^7.1.10"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "^1.13.2",
|
||||
"consola": "^3.4.2"
|
||||
}
|
||||
}
|
||||
|
||||
48
src/lib/api.ts
Normal file
48
src/lib/api.ts
Normal file
@@ -0,0 +1,48 @@
|
||||
import { Axios } from 'axios';
|
||||
import { consola } from 'consola/browser';
|
||||
import type { User } from './models';
|
||||
|
||||
|
||||
interface RequestError
|
||||
{
|
||||
message: string;
|
||||
}
|
||||
|
||||
class HeaptraderAPI
|
||||
{
|
||||
private _axios: Axios;
|
||||
private _user?: User;
|
||||
|
||||
constructor(baseURL: string = "http://localhost")
|
||||
{
|
||||
this._axios = new Axios({
|
||||
baseURL,
|
||||
withCredentials: true,
|
||||
withXSRFToken: true,
|
||||
});
|
||||
}
|
||||
|
||||
async csrfCookie()
|
||||
{
|
||||
await this._axios.get("/sanctum/csrf-cookie");
|
||||
}
|
||||
|
||||
async login(email: string, password: string): Promise<boolean>
|
||||
{
|
||||
await this.csrfCookie();
|
||||
const response = await this._axios.post<User | RequestError>("/login", { email, password });
|
||||
if (response.status === 200) {
|
||||
this._user = response.data as User;
|
||||
consola.info("Logged in as " + this._user.name);
|
||||
return true;
|
||||
} else {
|
||||
consola.error("Failed to log in: " + (response.data as RequestError).message);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
user(): User | null
|
||||
{
|
||||
return this._user ?? null;
|
||||
}
|
||||
}
|
||||
30
src/lib/models.ts
Normal file
30
src/lib/models.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
export interface User
|
||||
{
|
||||
id: number
|
||||
name: string
|
||||
email: string
|
||||
email_verified_at: string
|
||||
created_at: string
|
||||
}
|
||||
|
||||
export enum ItemCondition
|
||||
{
|
||||
PARTS_ONLY = "parts only",
|
||||
POOR = "poor",
|
||||
FAIR = "fair",
|
||||
GOOD = "good",
|
||||
EXCELLENT = "excellent"
|
||||
}
|
||||
|
||||
export interface Listing
|
||||
{
|
||||
id: number
|
||||
title: string
|
||||
description: string
|
||||
condition: ItemCondition
|
||||
price: number
|
||||
location: string
|
||||
updated_at: string
|
||||
created_at: string
|
||||
poster?: Partial<User>
|
||||
}
|
||||
Reference in New Issue
Block a user