api boilerplate

This commit is contained in:
not-nullptr 2024-03-10 01:37:10 +00:00
parent 7b9a62cc5b
commit b91e72626f
10 changed files with 627 additions and 49 deletions

View file

@ -0,0 +1,23 @@
import type { CreateAccountRequest, CreateAccountResponse } from "$types/api";
const apiUsers = {
async createAccount(body: CreateAccountRequest): Promise<CreateAccountResponse> {
return await SuyuAPI.req("POST", "/api/user/create-account", body);
},
} as const;
export class SuyuAPI {
static users = apiUsers;
static async req(method: string, path: string, body?: any) {
const res = await fetch(path, {
method,
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(body),
});
return await res.json();
}
}

View file

@ -0,0 +1,6 @@
import { DataSource } from "typeorm";
export const db = new DataSource({
type: "better-sqlite3",
database: "db.sqlite",
});

View file

@ -0,0 +1,20 @@
import type { Role } from "$types/db";
import { Column, Entity, PrimaryColumn } from "typeorm";
@Entity()
export class SuyuUser {
@PrimaryColumn()
id: string;
@Column()
username: string;
@Column()
displayName: string;
@Column()
avatarUrl: string;
@Column()
roles: Role[];
}

20
src/types/api.d.ts vendored Normal file
View file

@ -0,0 +1,20 @@
/* api types */
import type { SuyuUser } from "$lib/server/schema";
export interface CreateAccountRequest {
username: string;
}
export interface CreateAccountResponseSuccess {
success: true;
user: SuyuUser;
token: string;
}
export interface CreateAccountResponseFailure {
success: false;
error: string;
}
export type CreateAccountResponse = CreateAccountResponseSuccess | CreateAccountResponseFailure;

1
src/types/db.d.ts vendored Normal file
View file

@ -0,0 +1 @@
export type Role = "user" | "moderator";