mirror of
https://git.suyu.dev/suyu/website.git
synced 2026-01-05 14:08:08 +01:00
api boilerplate
This commit is contained in:
parent
7b9a62cc5b
commit
b91e72626f
10 changed files with 627 additions and 49 deletions
23
src/lib/client/api/index.ts
Normal file
23
src/lib/client/api/index.ts
Normal 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();
|
||||
}
|
||||
}
|
||||
6
src/lib/server/schema/db/index.ts
Normal file
6
src/lib/server/schema/db/index.ts
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
import { DataSource } from "typeorm";
|
||||
|
||||
export const db = new DataSource({
|
||||
type: "better-sqlite3",
|
||||
database: "db.sqlite",
|
||||
});
|
||||
20
src/lib/server/schema/index.ts
Normal file
20
src/lib/server/schema/index.ts
Normal 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
20
src/types/api.d.ts
vendored
Normal 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
1
src/types/db.d.ts
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
export type Role = "user" | "moderator";
|
||||
Loading…
Add table
Add a link
Reference in a new issue