Skip to content

Commit

Permalink
Refactor/rpc (BewlyBewly#700)
Browse files Browse the repository at this point in the history
* refactor(rpc): improve type, and type safe (BewlyBewly#673)

* refactor(rpc): improve type, and type safe

* revert `pnpm-lock.yaml` file

* improve api merge

* fix: type annotation in background/index.ts

* fix: type annotation in background/index.ts

---------

Co-authored-by: star knight <[email protected]>
  • Loading branch information
hakadao and starknt committed May 1, 2024
1 parent 79f11cb commit dd24072
Show file tree
Hide file tree
Showing 20 changed files with 70 additions and 137 deletions.
2 changes: 1 addition & 1 deletion src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ function isExtensionUri(url: string) {
// eslint-disable-next-line node/prefer-global/process
if (process.env.FIREFOX) {
browser.webRequest.onBeforeSendHeaders.addListener(
(details) => {
(details: any) => {
const requestHeaders: browser.WebRequest.HttpHeaders = []
if (details.documentUrl) {
const url = new URL(details.documentUrl)
Expand Down
6 changes: 3 additions & 3 deletions src/background/messageListeners/anime.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { APIMAP } from '../utils'
import { AHS } from '../utils'

const API_ANIME: APIMAP = {
const API_ANIME = {
// https://github.com/SocialSisterYi/bilibili-API-collect/blob/36e250090800793b41b223b55eefdcbb9391b53e/user/space.md#%E6%9F%A5%E8%AF%A2%E7%94%A8%E6%88%B7%E8%BF%BD%E7%95%AA%E8%BF%BD%E5%89%A7%E6%98%8E%E7%BB%86
getPopularAnimeList: {
url: 'https://api.bilibili.com/pgc/web/rank/list',
Expand Down Expand Up @@ -35,7 +35,7 @@ const API_ANIME: APIMAP = {
method: 'get',
},
params: {
coursor: '',
coursor: 0,
name: 'anime',
},
afterHandle: AHS.J_D,
Expand Down Expand Up @@ -63,6 +63,6 @@ const API_ANIME: APIMAP = {
},
afterHandle: AHS.J_D,
},
}
} satisfies APIMAP

export default API_ANIME
4 changes: 2 additions & 2 deletions src/background/messageListeners/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import type { APIMAP } from '../utils'
import { AHS } from '../utils'

const API_AUTH: APIMAP = {
const API_AUTH = {
// biliJct 似乎没有使用
logout: {
url: 'https://passport.bilibili.com/login/exit/v2',
Expand Down Expand Up @@ -54,6 +54,6 @@ const API_AUTH: APIMAP = {
},
afterHandle: AHS.J_S,
},
}
} satisfies APIMAP

export default API_AUTH
8 changes: 4 additions & 4 deletions src/background/messageListeners/favorite.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { APIMAP } from '../utils'
import { AHS } from '../utils'

const API_FAVORITE: APIMAP = {
const API_FAVORITE = {
// https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/docs/fav/info.md#%E8%8E%B7%E5%8F%96%E6%8C%87%E5%AE%9A%E7%94%A8%E6%88%B7%E5%88%9B%E5%BB%BA%E7%9A%84%E6%89%80%E6%9C%89%E6%94%B6%E8%97%8F%E5%A4%B9%E4%BF%A1%E6%81%AF
getFavoriteCategories: {
url: 'https://api.bilibili.com/x/v3/fav/folder/created/list-all',
Expand All @@ -20,7 +20,7 @@ const API_FAVORITE: APIMAP = {
method: 'get',
},
params: {
media_id: '',
media_id: -1,
pn: 1,
ps: 20,
keyword: '',
Expand All @@ -39,11 +39,11 @@ const API_FAVORITE: APIMAP = {
},
params: {
resources: '',
media_id: '',
media_id: 0,
csrf: '',
},
afterHandle: AHS.J_D,
},
}
} satisfies APIMAP

export default API_FAVORITE
10 changes: 5 additions & 5 deletions src/background/messageListeners/history.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { APIMAP } from '../utils'
import { AHS } from '../utils'

const API_HISTORY: APIMAP = {
const API_HISTORY = {
// https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/docs/history&toview/history.md#%E8%8E%B7%E5%8F%96%E5%8E%86%E5%8F%B2%E8%AE%B0%E5%BD%95%E5%88%97%E8%A1%A8_web%E7%AB%AF
getHistoryList: {
url: 'https://api.bilibili.com/x/web-interface/history/cursor',
Expand All @@ -11,7 +11,7 @@ const API_HISTORY: APIMAP = {
params: {
ps: 20,
type: '',
view_at: '',
view_at: 0,
},
afterHandle: AHS.J_D,
},
Expand All @@ -36,7 +36,7 @@ const API_HISTORY: APIMAP = {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
},
body: {
aid: '',
kid: '',
csrf: '',
},
},
Expand Down Expand Up @@ -74,12 +74,12 @@ const API_HISTORY: APIMAP = {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
},
body: {
switch: '',
switch: false,
csrf: '',
},
},
afterHandle: AHS.J_D,
},
}
} satisfies APIMAP

export default API_HISTORY
20 changes: 19 additions & 1 deletion src/background/messageListeners/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,26 @@ import API_USER from './user'
import API_VIDEO from './video'
import API_WATCHLATER from './watchLater'

export const API_COLLECTION = {
AUTH: API_AUTH,
ANIME: API_ANIME,
HISTORY: API_HISTORY,
FAVORITE: API_FAVORITE,
MOMENT: API_MOMENT,
NOTIFICATION: API_NOTIFICATION,
RANKING: API_RANKING,
SEARCH: API_SEARCH,
USER: API_USER,
VIDEO: API_VIDEO,
WATCHLATER: API_WATCHLATER,

[Symbol.iterator]() {
return Object.values(this).values()
},
}

// Merge all API objects into one
const FullAPI = Object.assign({}, API_AUTH, API_ANIME, API_HISTORY, API_FAVORITE, API_MOMENT, API_NOTIFICATION, API_RANKING, API_SEARCH, API_USER, API_VIDEO, API_WATCHLATER)
const FullAPI = Object.assign({}, ...API_COLLECTION)
// Create a message listener for each API
const handleMessage = apiListenerFactory(FullAPI)

Expand Down
9 changes: 4 additions & 5 deletions src/background/messageListeners/moment.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { APIMAP } from '../utils'
import { AHS } from '../utils'

const API_MOMENT: APIMAP = {
const API_MOMENT = {
getTopBarNewMomentsCount: {
url: 'https://api.bilibili.com/x/web-interface/dynamic/entrance',
_fetch: {
Expand Down Expand Up @@ -39,13 +39,12 @@ const API_MOMENT: APIMAP = {
method: 'get',
},
params: {
type: 268435455,
type: 'all',
offset: 0,
update_baseline: 0,
update_baseline: '',
},
afterHandle: AHS.J_D,
},

}
} satisfies APIMAP

export default API_MOMENT
4 changes: 2 additions & 2 deletions src/background/messageListeners/notification.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { APIMAP } from '../utils'
import { AHS } from '../utils'

const API_NOTIFICATION: APIMAP = {
const API_NOTIFICATION = {
getUnreadMsg: {
url: 'https://api.bilibili.com/x/msgfeed/unread',
_fetch: {
Expand All @@ -25,6 +25,6 @@ const API_NOTIFICATION: APIMAP = {
},
afterHandle: AHS.J_D,
},
}
} satisfies APIMAP

export default API_NOTIFICATION
4 changes: 2 additions & 2 deletions src/background/messageListeners/ranking.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { APIMAP } from '../utils'
import { AHS } from '../utils'

const API_RANKING: APIMAP = {
const API_RANKING = {
// https://github.com/SocialSisterYi/bilibili-API-collect/blob/7873a79022a5606e2391d93b411a05576a0df111/docs/video_ranking/ranking.md#%E8%8E%B7%E5%8F%96%E5%88%86%E5%8C%BA%E8%A7%86%E9%A2%91%E6%8E%92%E8%A1%8C%E6%A6%9C%E5%88%97%E8%A1%A8
getRankingVideos: {
url: 'https://api.bilibili.com/x/web-interface/ranking/v2',
Expand All @@ -25,6 +25,6 @@ const API_RANKING: APIMAP = {
},
afterHandle: AHS.J_D,
},
}
} satisfies APIMAP

export default API_RANKING
4 changes: 2 additions & 2 deletions src/background/messageListeners/search.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { APIMAP } from '../utils'
import { AHS } from '../utils'

const API_SEARCH: APIMAP = {
const API_SEARCH = {
getSearchSuggestion: {
url: 'https://s.search.bilibili.com/main/suggest',
_fetch: {
Expand All @@ -13,6 +13,6 @@ const API_SEARCH: APIMAP = {
},
afterHandle: AHS.J_D,
},
}
} satisfies APIMAP

export default API_SEARCH
8 changes: 3 additions & 5 deletions src/background/messageListeners/user.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import type { APIMAP } from '../utils'
import { AHS } from '../utils'
import API from '../msg.define'

const API_USER: APIMAP = {
const API_USER = {
// https://github.com/SocialSisterYi/bilibili-API-collect/blob/e379d904c2753fa30e9083f59016f07e89d19467/docs/login/login_info.md#%E5%AF%BC%E8%88%AA%E6%A0%8F%E7%94%A8%E6%88%B7%E4%BF%A1%E6%81%AF
getUserInfo: {
url: 'https://api.bilibili.com/x/web-interface/nav',
Expand All @@ -19,7 +18,7 @@ const API_USER: APIMAP = {
afterHandle: AHS.J_D,
},
// https://github.com/SocialSisterYi/bilibili-API-collect/blob/ed9ac01b6769430aa3f12ad02c2ed337a96924eb/docs/user/relation.md#操作用户关系
[API.USER.RELATION_MODIFY]: {
relationModify: {
url: 'https://api.bilibili.com/x/relation/modify',
_fetch: {
method: 'post',
Expand All @@ -32,7 +31,6 @@ const API_USER: APIMAP = {
},
afterHandle: AHS.J_D,
},

}
} satisfies APIMAP

export default API_USER
10 changes: 5 additions & 5 deletions src/background/messageListeners/video.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { APIMAP } from '../utils'
import { AHS } from '../utils'

const API_VIDEO: APIMAP = {
const API_VIDEO = {
getRecommendVideos: {
url: 'https://api.bilibili.com/x/web-interface/index/top/feed/rcmd',
_fetch: {
Expand Down Expand Up @@ -43,7 +43,7 @@ const API_VIDEO: APIMAP = {
access_key: '',
goto: '',
id: 0,
idx: 0,
idx: '',
reason_id: 1,
device: '',
mobi_app: '',
Expand Down Expand Up @@ -94,7 +94,7 @@ const API_VIDEO: APIMAP = {
params: {
csrf: '',
type: 1,
oid: '',
oid: 0,
sort: 0,
nohot: 0,
pn: 1,
Expand Down Expand Up @@ -125,10 +125,10 @@ const API_VIDEO: APIMAP = {
fnver: 0,
fnval: 1,
bvid: '',
cid: '',
cid: 0,
},
afterHandle: AHS.J_D,
},
}
} satisfies APIMAP

export default API_VIDEO
10 changes: 5 additions & 5 deletions src/background/messageListeners/watchLater.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { APIMAP } from '../utils'
import { AHS } from '../utils'

const API_WATCHLATER: APIMAP = {
const API_WATCHLATER = {
// https://github.com/SocialSisterYi/bilibili-API-collect/blob/master/docs/history&toview/toview.md#%E8%A7%86%E9%A2%91%E6%B7%BB%E5%8A%A0%E7%A8%8D%E5%90%8E%E5%86%8D%E7%9C%8B
saveToWatchLater: {
url: 'https://api.bilibili.com/x/v2/history/toview/add',
Expand All @@ -11,7 +11,7 @@ const API_WATCHLATER: APIMAP = {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
},
body: {
aid: '',
aid: 0,
csrf: '',
},
},
Expand All @@ -26,12 +26,12 @@ const API_WATCHLATER: APIMAP = {
'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
},
body: {
viewed: '',
viewed: false,
csrf: '',
},
},
params: {
aid: '',
aid: 0,
},
afterHandle: AHS.J_D,
},
Expand All @@ -57,6 +57,6 @@ const API_WATCHLATER: APIMAP = {
},
afterHandle: AHS.J_D,
},
}
} satisfies APIMAP

export default API_WATCHLATER
Loading

0 comments on commit dd24072

Please sign in to comment.