♻️ 类型迁移&重构

This commit is contained in:
目棃
2025-03-07 17:59:35 +08:00
parent ce5a88954a
commit d5aaecbf72
39 changed files with 514 additions and 567 deletions

View File

@@ -31,14 +31,14 @@ async function getEmoticonSet(): Promise<Record<string, string> | TGApp.BBS.Resp
* @description 根据 cookie 获取用户信息
* @since Beta v0.5.0
* @param {TGApp.App.Account.Cookie} cookie - 账户 cookie
* @returns {Promise<TGApp.BBS.Response.Base | TGApp.Plugins.Mys.User.Info>}
* @returns {Promise<TGApp.BBS.Response.Base | TGApp.BBS.User.Info>}
*/
async function getUserFullInfo(
cookie: TGApp.App.Account.Cookie,
): Promise<TGApp.BBS.Response.Base | TGApp.Plugins.Mys.User.Info> {
): Promise<TGApp.BBS.Response.Base | TGApp.BBS.User.Info> {
const ck = { cookie_token: cookie.cookie_token, account_id: cookie.account_id };
const params = { gids: "2" };
const resp = await TGHttp<TGApp.Plugins.Mys.User.HomeResponse | TGApp.BBS.Response.Base>(
const resp = await TGHttp<TGApp.BBS.User.InfoResp>(
"https://bbs-api.miyoushe.com/user/wapi/getUserFullInfo",
{ method: "GET", headers: getRequestHeader(ck, "GET", params, "X4", true), query: params },
);
@@ -51,14 +51,14 @@ async function getUserFullInfo(
* @since Beta v0.7.1
* @param {number} gid - gid
* @param {string} userId - 用户 id
* @returns {Promise<TGApp.BBS.Response.Base | TGApp.Plugins.Mys.User.Info>}
* @returns {Promise<TGApp.BBS.Response.Base | TGApp.BBS.User.Info>}
*/
async function getOtherUserInfo(
gid: number,
userId: string,
): Promise<TGApp.BBS.Response.Base | TGApp.Plugins.Mys.User.Info> {
): Promise<TGApp.BBS.Response.Base | TGApp.BBS.User.Info> {
const params = { gids: gid.toString(), uid: userId };
const resp = await TGHttp<TGApp.Plugins.Mys.User.HomeResponse | TGApp.BBS.Response.Base>(
const resp = await TGHttp<TGApp.BBS.User.InfoResp>(
"https://bbs-api.miyoushe.com/user/wapi/getUserFullInfo",
{ method: "GET", headers: getRequestHeader({}, "GET", params, "X4", true), query: params },
);

View File

@@ -15,16 +15,16 @@ const bapBu: Readonly<string> = "https://bbs-api.miyoushe.com/painter/wapi/";
* @param {string} newsType 咨讯类型: 1 为公告2 为活动3 为咨讯
* @param {number} pageSize 返回数量
* @param {number} lastId 上一次请求的最后一条数据的 id
* @return {Promise<TGApp.BBS.News.Res>}
* @return {Promise<TGApp.BBS.Post.NewsRes>}
*/
async function getNewsList(
gid: string = "2",
newsType: string = "1",
pageSize: number = 20,
lastId: number = 0,
): Promise<TGApp.BBS.News.Res> {
): Promise<TGApp.BBS.Post.NewsRes> {
return (
await TGHttp<TGApp.BBS.News.Resp>(`${bapBu}getNewsList`, {
await TGHttp<TGApp.BBS.Post.NewsResp>(`${bapBu}getNewsList`, {
method: "GET",
headers: { "Content-Type": "application/json" },
query: { gids: gid, page_size: pageSize, type: newsType, last_id: lastId },

View File

@@ -15,12 +15,12 @@ const Referer: Readonly<string> = "https://bbs.mihoyo.com/";
* @since Beta v0.7.1
* @param {number|string} postId 帖子 ID
* @param {Record<string, string>} cookie Cookie
* @return {Promise<TGApp.Plugins.Mys.Post.FullData|TGApp.BBS.Response.Base>}
* @return {Promise<TGApp.BBS.Post.FullData | TGApp.BBS.Response.Base>}
*/
async function getPostFull(
postId: number | string,
cookie?: Record<string, string>,
): Promise<TGApp.Plugins.Mys.Post.FullData | TGApp.BBS.Response.Base> {
): Promise<TGApp.BBS.Post.FullData | TGApp.BBS.Response.Base> {
const param = { post_id: postId, read: 1 };
let header;
if (cookie) {
@@ -29,7 +29,7 @@ async function getPostFull(
"x-rpc-client_type": "2",
};
} else header = { referer: Referer };
const resp = await TGHttp<TGApp.Plugins.Mys.Post.Response>(`${bapBu}getPostFull`, {
const resp = await TGHttp<TGApp.BBS.Post.FullResp>(`${bapBu}getPostFull`, {
method: "GET",
headers: header,
query: param,
@@ -42,11 +42,11 @@ async function getPostFull(
* @description 获取合集帖子
* @since Beta v0.7.1
* @param {string} collectionId 合集 ID
* @returns {Promise<TGApp.Plugins.Mys.Post.FullData[]>}
* @returns {Promise<Array<TGApp.BBS.Post.FullData>>}
*/
async function getPostFullInCollection(
collectionId: string,
): Promise<Array<TGApp.Plugins.Mys.Post.FullData>> {
): Promise<Array<TGApp.BBS.Post.FullData>> {
return (
await TGHttp<TGApp.BBS.Collection.PostsResp>(`${bapBu}getPostFullInCollection`, {
method: "GET",
@@ -177,15 +177,15 @@ async function getTopicPostList(
* @param {string} gid 游戏分区 ID
* @param {string} keyword 关键词
* @param {string} lastId 最后一条帖子 ID
* @return {Promise<TGApp.BBS.Search.PostsRes>} 返回帖子列表
* @return {Promise<TGApp.BBS.Post.SearchRes>} 返回帖子列表
*/
async function searchPosts(
gid: string = "2",
keyword: string,
lastId: string,
): Promise<TGApp.BBS.Search.PostsRes> {
): Promise<TGApp.BBS.Post.SearchRes> {
return (
await TGHttp<TGApp.BBS.Search.PostsResp>(`${bapBu}searchPosts`, {
await TGHttp<TGApp.BBS.Post.SearchResp>(`${bapBu}searchPosts`, {
method: "GET",
headers: { "Content-Type": "application/json" },
query: { gids: gid, keyword, last_id: lastId, size: 20 },

View File

@@ -129,15 +129,14 @@ async function genAuthKey2(
* @description 通过cookie获取游戏账号
* @since Beta v0.7.2
* @param {TGApp.App.Account.Cookie} cookie cookie
* @returns {Promise<TGApp.BBS.Account.GameAccount[]|TGApp.BBS.Response.Base>}
* @returns {Promise<Array<TGApp.BBS.Game.Account>|TGApp.BBS.Response.Base>}
*/
async function getUserGameRolesByCookie(
cookie: TGApp.App.Account.Cookie,
): Promise<TGApp.BBS.Account.GameAccount[] | TGApp.BBS.Response.Base> {
): Promise<Array<TGApp.BBS.Game.Account> | TGApp.BBS.Response.Base> {
const ck = { account_id: cookie.account_id, cookie_token: cookie.cookie_token };
const params = { game_biz: "hk4e_cn" };
type ResType = { list: Array<TGApp.BBS.Account.GameAccount> };
const resp = await TGHttp<TGApp.BBS.Response.BaseWithData<ResType>>(
const resp = await TGHttp<TGApp.BBS.Game.AccountResp>(
`${taBu}binding/api/getUserGameRolesByCookie`,
{
method: "GET",

View File

@@ -41,15 +41,13 @@ function handleAnnoContent(data: string): string {
* @description 解析公告内容,转换为结构化数据
* @since Beta v0.5.3
* @param {TGApp.BBS.Announcement.ContentItem} anno - 公告内容
* @returns {TGApp.Plugins.Mys.SctPost.Base[]} 结构化数据
* @returns {TGApp.BBS.SctPost.Base[]} 结构化数据
*/
function parseAnnoContent(
anno: TGApp.BBS.Announcement.ContentItem,
): Array<TGApp.Plugins.Mys.SctPost.Base> {
function parseAnnoContent(anno: TGApp.BBS.Announcement.ContentItem): Array<TGApp.BBS.SctPost.Base> {
const parser = new DOMParser();
const first = handleAnnoContent(anno.content);
const doc = parser.parseFromString(first, "text/html");
const children: Array<TGApp.Plugins.Mys.SctPost.Base> = [];
const children: Array<TGApp.BBS.SctPost.Base> = [];
if (anno.banner !== "") children.push({ insert: { image: anno.banner } });
doc.body.childNodes.forEach((child) => children.push(...parseAnnoNode(child)));
return children;
@@ -60,13 +58,10 @@ function parseAnnoContent(
* @since Beta v0.7.0
* @param {Node} node - 节点
* @param {Record<string, string>} attr - 属性
* @returns {TGApp.Plugins.Mys.SctPost.Base} 结构化数据
* @returns {TGApp.BBS.SctPost.Base} 结构化数据
*/
function parseAnnoNode(
node: Node,
attr?: Record<string, string>,
): Array<TGApp.Plugins.Mys.SctPost.Base> {
let defaultRes: TGApp.Plugins.Mys.SctPost.Base = {
function parseAnnoNode(node: Node, attr?: Record<string, string>): Array<TGApp.BBS.SctPost.Base> {
let defaultRes: TGApp.BBS.SctPost.Base = {
insert: {
tag: node.nodeName,
text: node.textContent,
@@ -95,7 +90,7 @@ function parseAnnoNode(
return [parseAnnoParagraph(element, attr)];
}
if (element.tagName === "OL" || element.tagName === "LI" || element.tagName === "UL") {
const res: Array<TGApp.Plugins.Mys.SctPost.Base> = [];
const res: Array<TGApp.BBS.SctPost.Base> = [];
Array.from(element.children).forEach((child) => res.push(...parseAnnoNode(child, attr)));
for (const comp of res) {
if (comp.children) {
@@ -112,7 +107,7 @@ function parseAnnoNode(
if (element.tagName === "TABLE") return [parseAnnoTable(element)];
if (element.tagName === "DIV") {
if (element.childNodes.length > 1) {
const res: Array<TGApp.Plugins.Mys.SctPost.Base> = [];
const res: Array<TGApp.BBS.SctPost.Base> = [];
element.childNodes.forEach((child) => res.push(...parseAnnoNode(child, attr)));
return res;
}
@@ -128,12 +123,12 @@ function parseAnnoNode(
}
if (element.tagName === "SPAN") return [parseAnnoSpan(element, attr)];
if (element.tagName === "STRONG") {
const res: Array<TGApp.Plugins.Mys.SctPost.Base> = [];
const res: Array<TGApp.BBS.SctPost.Base> = [];
element.childNodes.forEach((child) => res.push(...parseAnnoNode(child, { bold: "true" })));
return res;
}
if (element.tagName === "T") {
const res: Array<TGApp.Plugins.Mys.SctPost.Base> = [];
const res: Array<TGApp.BBS.SctPost.Base> = [];
element.childNodes.forEach((child) => res.push(...parseAnnoNode(child, attr)));
return res;
}
@@ -145,12 +140,9 @@ function parseAnnoNode(
* @since Beta v0.7.0
* @param {HTMLElement} p - 段落元素
* @param {Record<string, string>} attr - 属性
* @returns {TGApp.Plugins.Mys.SctPost.Base} 结构化数据
* @returns {TGApp.BBS.SctPost.Base} 结构化数据
*/
function parseAnnoParagraph(
p: HTMLElement,
attr?: Record<string, string>,
): TGApp.Plugins.Mys.SctPost.Base {
function parseAnnoParagraph(p: HTMLElement, attr?: Record<string, string>): TGApp.BBS.SctPost.Base {
const defaultRes = {
insert: {
tag: p.tagName,
@@ -193,9 +185,9 @@ function parseAnnoParagraph(
if (child.tagName === "T") return { insert: "", children: parseAnnoNode(child) };
return defaultRes;
}
const res: TGApp.Plugins.Mys.SctPost.Base = { insert: "", children: [] };
const res: TGApp.BBS.SctPost.Base = { insert: "", children: [] };
p.childNodes.forEach((child) => {
let childRes: TGApp.Plugins.Mys.SctPost.Base;
let childRes: TGApp.BBS.SctPost.Base;
if (child.nodeType === Node.TEXT_NODE) {
childRes = { insert: child.textContent ?? "" };
} else if (child.nodeType === Node.ELEMENT_NODE) {
@@ -227,12 +219,9 @@ function parseAnnoParagraph(
* @since Beta v0.7.0
* @param {HTMLElement} span - span 元素
* @param {Record<string, string>} attr - 属性
* @returns {TGApp.Plugins.Mys.SctPost.Base} 结构化数据
* @returns {TGApp.BBS.SctPost.Base} 结构化数据
*/
function parseAnnoSpan(
span: HTMLElement,
attr?: Record<string, string>,
): TGApp.Plugins.Mys.SctPost.Base {
function parseAnnoSpan(span: HTMLElement, attr?: Record<string, string>): TGApp.BBS.SctPost.Base {
const defaultRes = {
insert: {
tag: span.tagName,
@@ -281,9 +270,9 @@ function parseAnnoSpan(
* @description 解析公告图片
* @since Beta v0.7.0
* @param {HTMLElement} img - 图片元素
* @returns {TGApp.Plugins.Mys.SctPost.Base} 结构化数据
* @returns {TGApp.BBS.SctPost.Base} 结构化数据
*/
function parseAnnoImage(img: HTMLElement): TGApp.Plugins.Mys.SctPost.Base {
function parseAnnoImage(img: HTMLElement): TGApp.BBS.SctPost.Base {
if (img.tagName !== "IMG") {
return {
insert: {
@@ -304,9 +293,9 @@ function parseAnnoImage(img: HTMLElement): TGApp.Plugins.Mys.SctPost.Base {
* @description 解析公告锚点
* @since Beta v0.7.0
* @param {HTMLElement} a - 锚点元素
* @returns {TGApp.Plugins.Mys.SctPost.Base} 结构化数据
* @returns {TGApp.BBS.SctPost.Base} 结构化数据
*/
function parseAnnoAnchor(a: HTMLElement): TGApp.Plugins.Mys.SctPost.Base {
function parseAnnoAnchor(a: HTMLElement): TGApp.BBS.SctPost.Base {
if (a.tagName !== "A") {
return {
insert: {
@@ -333,9 +322,9 @@ function parseAnnoAnchor(a: HTMLElement): TGApp.Plugins.Mys.SctPost.Base {
* @description 解析公告详情
* @since Beta v0.7.0
* @param {HTMLElement} details - 详情元素
* @returns {TGApp.Plugins.Mys.SctPost.Base} 结构化数据
* @returns {TGApp.BBS.SctPost.Base} 结构化数据
*/
function parseAnnoDetails(details: HTMLElement): TGApp.Plugins.Mys.SctPost.Base {
function parseAnnoDetails(details: HTMLElement): TGApp.BBS.SctPost.Base {
const defaultRes = {
insert: {
tag: details.tagName,
@@ -368,9 +357,9 @@ function parseAnnoDetails(details: HTMLElement): TGApp.Plugins.Mys.SctPost.Base
* @description 解析公告表格
* @since Beta v0.7.0
* @param {HTMLElement} table - 表格元素
* @returns {TGApp.Plugins.Mys.SctPost.Base} 结构化数据
* @returns {TGApp.BBS.SctPost.Base} 结构化数据
*/
function parseAnnoTable(table: HTMLElement): TGApp.Plugins.Mys.SctPost.Base {
function parseAnnoTable(table: HTMLElement): TGApp.BBS.SctPost.Base {
const defaultRes = {
insert: {
tag: table.tagName,