Frontend: Initial BE API client integration

This commit is contained in:
Natty 2023-11-05 15:28:55 +01:00
parent 6908a2f350
commit 0ad015cd68
Signed by: natty
GPG Key ID: BF6CB659ADEE60EC
19 changed files with 491 additions and 165 deletions

View File

@ -15,7 +15,7 @@
"devDependencies": { "devDependencies": {
"@swc/cli": "^0.1.62", "@swc/cli": "^0.1.62",
"@swc/core": "^1.3.62", "@swc/core": "^1.3.62",
"@types/node": "20.3.1", "@types/node": "20.8.10",
"ts-node": "10.4.0", "ts-node": "10.4.0",
"tsd": "^0.28.1", "tsd": "^0.28.1",
"typescript": "5.1.3" "typescript": "5.1.3"

View File

@ -15,6 +15,8 @@ export type UserLite = {
avatarUrl: string; avatarUrl: string;
avatarBlurhash: string; avatarBlurhash: string;
alsoKnownAs: string[]; alsoKnownAs: string[];
isCat?: boolean;
isBot?: boolean;
movedToUri: any; movedToUri: any;
emojis: { emojis: {
name: string; name: string;

View File

@ -13,9 +13,12 @@ import { MenuItem } from "@/types/menu";
import { $i } from "@/account"; import { $i } from "@/account";
import { i18n } from "./i18n"; import { i18n } from "./i18n";
import { import {
BackendApiEndpoint,
FrontendApiEndpoint, FrontendApiEndpoint,
FrontendApiEndpoints, FrontendApiEndpoints,
} from "magnetar-common/built/fe-api"; MagApiClient,
Method,
} from "magnetar-common";
export const pendingApiRequestsCount = ref(0); export const pendingApiRequestsCount = ref(0);
@ -23,6 +26,39 @@ const apiClient = new Misskey.api.APIClient({
origin: url, origin: url,
}); });
const magnetarApiClient = new MagApiClient(url);
export async function magApi<
T extends BackendApiEndpoint<
T["method"] & Method,
T["pathParams"] & string[],
T["request"],
T["response"]
>
>(
endpoint: T,
data: T["request"],
pathParams: {
[K in keyof T["pathParams"] as T["pathParams"][K] & string]:
| string
| number;
},
token?: string | null | undefined
): Promise<T["response"]> {
pendingApiRequestsCount.value++;
try {
return await magnetarApiClient.call(
endpoint,
data,
pathParams,
token || $i?.token
);
} finally {
pendingApiRequestsCount.value--;
}
}
export async function feApi<T extends keyof FrontendApiEndpoints & string>( export async function feApi<T extends keyof FrontendApiEndpoints & string>(
endpointDef: FrontendApiEndpoint< endpointDef: FrontendApiEndpoint<
FrontendApiEndpoints[T]["method"], FrontendApiEndpoints[T]["method"],

View File

@ -1,7 +1,8 @@
{ {
"name": "magnetar-common", "name": "magnetar-common",
"version": "0.0.1", "version": "0.0.1",
"main": "index.js", "main": "./built/index.js",
"types": "./built/index.d.ts",
"scripts": { "scripts": {
"build": "tsc" "build": "tsc"
}, },

View File

@ -1,6 +1,4 @@
import { GetNoteById } from "./types/endpoints/GetNoteById"; export type Method = "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
type Method = "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
export interface BackendApiEndpoint< export interface BackendApiEndpoint<
M extends Method, M extends Method,
@ -46,7 +44,7 @@ function nestedUrlSearchParams(data: any, topLevel: boolean = true): string {
} }
} }
type MagApiErrorCode = "Client:GenericApiError" | string; export type MagApiErrorCode = "Client:GenericApiError" | string;
export interface MagApiError { export interface MagApiError {
status: number; status: number;
@ -125,6 +123,3 @@ export class MagApiClient {
}); });
} }
} }
const a = new MagApiClient("https://aaa");
const result = await a.call(GetNoteById, { attachments: true }, { id: "aaaa" });

View File

@ -0,0 +1,5 @@
export { GetNoteById } from "./types/endpoints/GetNoteById";
export { GetTimeline } from "./types/endpoints/GetTimeline";
export { GetUserById } from "./types/endpoints/GetUserById";
export { GetUserByAcct } from "./types/endpoints/GetUserByAcct";
export { GetUserSelf } from "./types/endpoints/GetUserSelf";

View File

@ -0,0 +1,28 @@
import {
BackendApiEndpoint,
MagApiClient,
MagApiError,
MagApiErrorCode,
Method,
} from "./be-api";
import {
feEndpoints,
FrontendApiEndpoint,
FrontendApiEndpoints,
} from "./fe-api";
export * as types from "./types";
export * as packed from "./packed";
export * as endpoints from "./endpoints";
export {
Method,
BackendApiEndpoint,
MagApiError,
MagApiClient,
MagApiErrorCode,
feEndpoints,
FrontendApiEndpoint,
FrontendApiEndpoints,
};

View File

@ -0,0 +1,16 @@
export { PackDriveFileBase } from "./types/packed/PackDriveFileBase";
export { PackDriveFileFull } from "./types/packed/PackDriveFileFull";
export { PackDriveFileWithFolder } from "./types/packed/PackDriveFileWithFolder";
export { PackDriveFileWithUser } from "./types/packed/PackDriveFileWithUser";
export { PackDriveFolderBase } from "./types/packed/PackDriveFolderBase";
export { PackEmojiBase } from "./types/packed/PackEmojiBase";
export { PackDriveFolderWithParent } from "./types/packed/PackDriveFolderWithParent";
export { PackNoteBase } from "./types/packed/PackNoteBase";
export { PackNoteMaybeFull } from "./types/packed/PackNoteMaybeFull";
export { PackPollBase } from "./types/packed/PackPollBase";
export { PackNoteMaybeAttachments } from "./types/packed/PackNoteMaybeAttachments";
export { PackSecurityKeyBase } from "./types/packed/PackSecurityKeyBase";
export { PackUserBase } from "./types/packed/PackUserBase";
export { PackUserMaybeAll } from "./types/packed/PackUserMaybeAll";
export { PackUserSelf } from "./types/packed/PackUserSelf";
export { PackUserSelfMaybeAll } from "./types/packed/PackUserSelfMaybeAll";

View File

@ -0,0 +1,40 @@
export { EmojiContext } from "./types/EmojiContext";
export { FollowVisibility } from "./types/FollowVisibility";
export { Id } from "./types/Id";
export { NotificationSettings } from "./types/NotificationSettings";
export { EmojiBase } from "./types/EmojiBase";
export { MmXml } from "./types/MmXml";
export { NotificationType } from "./types/NotificationType";
export { NoteBase } from "./types/NoteBase";
export { NoteAttachmentExt } from "./types/NoteAttachmentExt";
export { NoteDetailExt } from "./types/NoteDetailExt";
export { NoteListFilter } from "./types/NoteListFilter";
export { NoteSelfContextExt } from "./types/NoteSelfContextExt";
export { NoteVisibility } from "./types/NoteVisibility";
export { PollBase } from "./types/PollBase";
export { PollChoice } from "./types/PollChoice";
export { Reaction } from "./types/Reaction";
export { ReactionPair } from "./types/ReactionPair";
export { AvatarDecoration } from "./types/AvatarDecoration";
export { ProfileField } from "./types/ProfileField";
export { SecurityKeyBase } from "./types/SecurityKeyBase";
export { SpeechTransform } from "./types/SpeechTransform";
export { UserAuthOverviewExt } from "./types/UserAuthOverviewExt";
export { UserBase } from "./types/UserBase";
export { UserDetailExt } from "./types/UserDetailExt";
export { UserProfileExt } from "./types/UserProfileExt";
export { UserProfilePinsEx } from "./types/UserProfilePinsEx";
export { UserSecretsExt } from "./types/UserSecretsExt";
export { UserRelationExt } from "./types/UserRelationExt";
export { UserSelfExt } from "./types/UserSelfExt";
export { NoteByIdReq } from "./types/NoteByIdReq";
export { GetTimelineReq } from "./types/GetTimelineReq";
export { UserByIdReq } from "./types/UserByIdReq";
export { UserSelfReq } from "./types/UserSelfReq";
export { DriveFileBase } from "./types/DriveFileBase";
export { DriveFileFolderExt } from "./types/DriveFileFolderExt";
export { DriveFileUserExt } from "./types/DriveFileUserExt";
export { DriveFolderBase } from "./types/DriveFolderBase";
export { DriveFolderParentExt } from "./types/DriveFolderParentExt";
export { ImageMeta } from "./types/ImageMeta";
export { InstanceTicker } from "./types/InstanceTicker";

View File

@ -1,4 +1,4 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. // This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { ImageMeta } from "./ImageMeta"; import type { ImageMeta } from "./ImageMeta";
export interface DriveFileBase { name: string, created_at: string, size: bigint, hash: string | null, mime_type: string, media_metadata: ImageMeta, url: string | null, thumbnail_url: string | null, sensitive: boolean, comment: string | null, folder_id: string | null, user_id: string | null, } export interface DriveFileBase { name: string, created_at: string, size: bigint, hash: string | null, mime_type: string, media_metadata: ImageMeta, url: string | null, source_url: string, thumbnail_url: string | null, blurhash: string | null, sensitive: boolean, comment: string | null, folder_id: string | null, user_id: string | null, }

View File

@ -0,0 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
export interface InstanceTicker { name: string | null, software_name: string | null, software_version: string | null, icon_url: string | null, favicon_url: string | null, theme_color: string | null, }

View File

@ -5,4 +5,4 @@ import type { NoteVisibility } from "./NoteVisibility";
import type { PackUserBase } from "./packed/PackUserBase"; import type { PackUserBase } from "./packed/PackUserBase";
import type { ReactionPair } from "./ReactionPair"; import type { ReactionPair } from "./ReactionPair";
export interface NoteBase { created_at: string, cw: string | null, cw_mm: MmXml | null, uri: string | null, url: string | null, text: string, text_mm: MmXml | null, visibility: NoteVisibility, user: PackUserBase, parent_note_id: string | null, renoted_note_id: string | null, reply_count: bigint, renote_count: bigint, mentions: Array<string>, hashtags: Array<string>, reactions: Array<ReactionPair>, local_only: boolean, has_poll: boolean, file_ids: Array<string>, emojis: EmojiContext, } export interface NoteBase { created_at: string, updated_at: string | null, cw: string | null, cw_mm: MmXml | null, uri: string | null, url: string | null, text: string, text_mm: MmXml | null, visibility: NoteVisibility, user: PackUserBase, parent_note_id: string | null, renoted_note_id: string | null, reply_count: bigint, renote_count: bigint, mentions: Array<string>, visible_user_ids: Array<string> | null, hashtags: Array<string>, reactions: Array<ReactionPair>, local_only: boolean, has_poll: boolean, file_ids: Array<string>, emojis: EmojiContext, }

View File

@ -1,7 +1,8 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. // This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
import type { AvatarDecoration } from "./AvatarDecoration"; import type { AvatarDecoration } from "./AvatarDecoration";
import type { EmojiContext } from "./EmojiContext"; import type { EmojiContext } from "./EmojiContext";
import type { InstanceTicker } from "./InstanceTicker";
import type { MmXml } from "./MmXml"; import type { MmXml } from "./MmXml";
import type { SpeechTransform } from "./SpeechTransform"; import type { SpeechTransform } from "./SpeechTransform";
export interface UserBase { acct: string, username: string, display_name: string, display_name_mm: MmXml | null, host: string | null, speech_transform: SpeechTransform, created_at: string, avatar_url: string | null, avatar_blurhash: string | null, avatar_color: string | null, avatar_decoration: AvatarDecoration, is_admin: boolean, is_moderator: boolean, is_bot: boolean, emojis: EmojiContext, } export interface UserBase { acct: string, username: string, display_name: string, display_name_mm: MmXml | null, host: string | null, speech_transform: SpeechTransform, created_at: string, avatar_url: string, avatar_blurhash: string | null, avatar_decoration: AvatarDecoration, is_admin: boolean, is_moderator: boolean, is_bot: boolean, emojis: EmojiContext, instance: InstanceTicker | null, }

View File

@ -2,4 +2,4 @@
import type { MmXml } from "./MmXml"; import type { MmXml } from "./MmXml";
import type { ProfileField } from "./ProfileField"; import type { ProfileField } from "./ProfileField";
export interface UserProfileExt { is_locked: boolean, is_silenced: boolean, is_suspended: boolean, description: string | null, description_mm: MmXml | null, location: string | null, birthday: string | null, fields: Array<ProfileField>, follower_count: bigint | null, following_count: bigint | null, note_count: bigint | null, url: string | null, moved_to_uri: string | null, also_known_as: string | null, banner_url: string | null, banner_color: string | null, banner_blurhash: string | null, has_public_reactions: boolean, } export interface UserProfileExt { is_locked: boolean, is_silenced: boolean, is_suspended: boolean, description: string | null, description_mm: MmXml | null, location: string | null, birthday: string | null, fields: Array<ProfileField>, follower_count: bigint | null, following_count: bigint | null, note_count: bigint | null, url: string | null, moved_to_uri: string | null, also_known_as: string | null, banner_url: string | null, banner_blurhash: string | null, has_public_reactions: boolean, }

View File

@ -1,3 +1,3 @@
// This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually. // This file was generated by [ts-rs](https://github.com/Aleph-Alpha/ts-rs). Do not edit this file manually.
export interface UserRelationExt { follows_you: boolean, you_follow: boolean, blocks_you: boolean, you_block: boolean, mute: boolean, mute_renotes: boolean, } export interface UserRelationExt { follows_you: boolean, you_follow: boolean, you_request_follow: boolean, they_request_follow: boolean, blocks_you: boolean, you_block: boolean, mute: boolean, mute_renotes: boolean, }

View File

@ -7,5 +7,6 @@ export const GetTimeline = {
pathParams: [] as [], pathParams: [] as [],
method: "GET" as "GET" | "POST" | "PUT" | "DELETE" | "PATCH", method: "GET" as "GET" | "POST" | "PUT" | "DELETE" | "PATCH",
request: undefined as unknown as GetTimelineReq, request: undefined as unknown as GetTimelineReq,
response: undefined as unknown as Array<PackNoteMaybeFull>, response: undefined as unknown as Array<PackNoteMaybeFull>
}; }

View File

@ -19,4 +19,4 @@
"node_modules", "node_modules",
"built", "built",
] ]
} }

View File

@ -26,7 +26,6 @@
"dependencies": { "dependencies": {
"@bull-board/api": "5.2.0", "@bull-board/api": "5.2.0",
"@bull-board/ui": "5.2.0", "@bull-board/ui": "5.2.0",
"@napi-rs/cli": "^2.16.1",
"js-yaml": "4.1.0", "js-yaml": "4.1.0",
"seedrandom": "^3.0.5" "seedrandom": "^3.0.5"
}, },

File diff suppressed because it is too large Load Diff