From fa122fa7a94285a8de5ccb69fb7dbe9239a8e7ba Mon Sep 17 00:00:00 2001 From: justorez Date: Wed, 31 Jan 2024 17:34:37 +0800 Subject: [PATCH] types: perfect type definition --- src/sdk.ts | 45 +++++++++++++++++++++++---------------------- src/types.ts | 45 +++++++++++++++++++++++++++------------------ 2 files changed, 50 insertions(+), 40 deletions(-) diff --git a/src/sdk.ts b/src/sdk.ts index 5555581..9727c08 100644 --- a/src/sdk.ts +++ b/src/sdk.ts @@ -10,9 +10,10 @@ import { Comic, Episode, DInfo, - Picture, - PicturePage, - SearchPage + PagePicture, + PageSearch, + PageEpisode, + Favorites } from './types' export class Pica { @@ -48,8 +49,8 @@ export class Pica { }) ) this.api.interceptors.request.use((config) => { - let { url, method } = config - url = url?.replace(/^\/|\/$/g, '') // url 首尾不能有 "/" + const method = config.method + const url = config.url?.replace(/^\/|\/$/g, '') // url 首尾不能有 "/" const timestamp = String(Date.now()).slice(0, -3) const raw = url + timestamp + headers.nonce + method + headers['api-key'] @@ -105,13 +106,14 @@ export class Pica { } async login() { - const res = await this.request('post', 'auth/sign-in', { + const res = await this.request('post', 'auth/sign-in', { email: process.env.PICA_ACCOUNT, password: process.env.PICA_PASSWORD }).catch((err) => { debug('\n登录异常 %s', err) throw new Error('登录失败,请检查账号/密码/网络环境') }) + if (!res.token) { throw new Error('PICA_SECRET_KEY 错误') } @@ -155,7 +157,7 @@ export class Pica { */ async episodes(bookId: string, page = 1) { const url = `comics/${bookId}/eps?page=${page}` - const res = await this.request('get', url) + const res = await this.request('get', url) return res.eps } @@ -164,9 +166,9 @@ export class Pica { */ async episodesAll(bookId: string) { const firstPage = await this.episodes(bookId) - let pages = firstPage.pages // 总页数 - let total = firstPage.total // 总章节数 - const episodes = firstPage.docs as Episode[] + const pages = firstPage.pages // 总页数 + const total = firstPage.total // 总章节数 + const episodes = firstPage.docs for (let i = 2; i <= pages; i++) { const res = await this.episodes(bookId, i) episodes.push(...res.docs) @@ -183,18 +185,18 @@ export class Pica { */ async pictures(bookId: string, orderId: string | number, page = 1) { const url = `comics/${bookId}/order/${orderId}/pages?page=${page}` - const res = await this.request('get', url) - res.pages.docs = res.pages.docs.map((doc: Picture) => { + const res = await this.request('get', url) + res.pages.docs = res.pages.docs.map((doc) => { const fileServer = process.env.PICA_FILE_SERVER || doc.media.fileServer return { - id: doc.id, + ...doc, ...doc.media, name: doc.media.originalName, url: `${fileServer}/static/${doc.media.path}` } }) - return res.pages as PicturePage + return res.pages } /** @@ -217,7 +219,6 @@ export class Pica { } async download(url: string, info: DInfo): Promise { - // 使用单独的 axios 请求 // 哔咔的某些文件服务器安全证书不可用,我真服了! // 把图片 https 全部换成 http @@ -231,7 +232,7 @@ export class Pica { const res = await this.api.get(url, { responseType: 'arraybuffer', maxRedirects: 0, - validateStatus: (status) => (status >= 200 && status < 304) + validateStatus: (status) => status >= 200 && status < 304 }) // 由于 axio 的自动重定向过程无法修改,只好手动处理 @@ -254,8 +255,8 @@ export class Pica { async search(keyword: string, page = 1, sort = this.Order.latest) { const url = `comics/advanced-search?page=${page}` const data = { keyword, sort } - const res = await this.request('post', url, data) - return res.comics as SearchPage + const res = await this.request('post', url, data) + return res.comics } async searchAll(keyword: string) { @@ -290,8 +291,8 @@ export class Pica { */ async favorites() { const url = 'users/favourite' - const res = await this.request('get', url) - return res.comics.docs as Comic[] + const res = await this.request('get', url) + return res.comics.docs } /** @@ -302,11 +303,11 @@ export class Pica { return this.request('post', url) } - request( + request( method: string, url: string, data?: object - ): Promise> { + ): Promise> { return this.api.request({ url, method, diff --git a/src/types.ts b/src/types.ts index f54addf..3569698 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,18 @@ -type Page = { +export type LoginResult = { + token: string +} + +/** + * 收藏夹 + */ +export type Favorites = { + docs: Comic[] +} + +/** + * 分页 + */ +export type Page = { total: number limit: number page: number @@ -6,12 +20,7 @@ type Page = { docs: T[] } -/** - * 搜索分页 - */ -export type SearchPage = Page - -export interface Comic { +export type Comic = { updated_at: string author: string description: string @@ -26,28 +35,26 @@ export interface Comic { _id: string } +export type PageSearch = Page + /** * 章节图片分页 */ -export type PicturePage = Page - -export interface Picture { - id: string - media: { - originalName: string - path: string - fileServer: string - } -} +export type PagePicture = Page // my picture -export interface MPicture { +export interface Picture { id: string name: string // originalName path: string fileServer: string url: string epTitle: string // 章节标题 + media: { + originalName: string + path: string + fileServer: string + } } export interface Episode { @@ -57,6 +64,8 @@ export interface Episode { updated_at: string } +export type PageEpisode = Page + export interface DInfo { title: string // 漫画标题 epTitle: string // 章节标题