Skip to content

Commit

Permalink
types: perfect type definition
Browse files Browse the repository at this point in the history
  • Loading branch information
justorez committed Jan 31, 2024
1 parent ef8e5a3 commit fa122fa
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 40 deletions.
45 changes: 23 additions & 22 deletions src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ import {
Comic,
Episode,
DInfo,
Picture,
PicturePage,
SearchPage
PagePicture,
PageSearch,
PageEpisode,
Favorites
} from './types'

export class Pica {
Expand Down Expand Up @@ -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']
Expand Down Expand Up @@ -105,13 +106,14 @@ export class Pica {
}

async login() {
const res = await this.request('post', 'auth/sign-in', {
const res = await this.request<string>('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 错误')
}
Expand Down Expand Up @@ -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<PageEpisode>('get', url)
return res.eps
}

Expand All @@ -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)
Expand All @@ -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<PagePicture>('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
}

/**
Expand All @@ -217,7 +219,6 @@ export class Pica {
}

async download(url: string, info: DInfo): Promise<void> {

// 使用单独的 axios 请求
// 哔咔的某些文件服务器安全证书不可用,我真服了!
// 把图片 https 全部换成 http
Expand All @@ -231,7 +232,7 @@ export class Pica {
const res = await this.api.get<Buffer>(url, {
responseType: 'arraybuffer',
maxRedirects: 0,
validateStatus: (status) => (status >= 200 && status < 304)
validateStatus: (status) => status >= 200 && status < 304
})

// 由于 axio 的自动重定向过程无法修改,只好手动处理
Expand All @@ -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<PageSearch>('post', url, data)
return res.comics
}

async searchAll(keyword: string) {
Expand Down Expand Up @@ -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<Favorites>('get', url)
return res.comics.docs
}

/**
Expand All @@ -302,11 +303,11 @@ export class Pica {
return this.request('post', url)
}

request(
request<T>(
method: string,
url: string,
data?: object
): Promise<Record<string, any>> {
): Promise<Record<string, T>> {
return this.api.request({
url,
method,
Expand Down
45 changes: 27 additions & 18 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
type Page<T> = {
export type LoginResult = {
token: string
}

/**
* 收藏夹
*/
export type Favorites = {
docs: Comic[]
}

/**
* 分页
*/
export type Page<T> = {
total: number
limit: number
page: number
pages: number
docs: T[]
}

/**
* 搜索分页
*/
export type SearchPage = Page<Comic>

export interface Comic {
export type Comic = {
updated_at: string
author: string
description: string
Expand All @@ -26,28 +35,26 @@ export interface Comic {
_id: string
}

export type PageSearch = Page<Comic>

/**
* 章节图片分页
*/
export type PicturePage = Page<MPicture>

export interface Picture {
id: string
media: {
originalName: string
path: string
fileServer: string
}
}
export type PagePicture = Page<Picture>

// 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 {
Expand All @@ -57,6 +64,8 @@ export interface Episode {
updated_at: string
}

export type PageEpisode = Page<Episode>

export interface DInfo {
title: string // 漫画标题
epTitle: string // 章节标题
Expand Down

0 comments on commit fa122fa

Please sign in to comment.