diff --git a/packages/xhs/src/libs/client-base.js b/packages/xhs/src/libs/client-base.ts similarity index 91% rename from packages/xhs/src/libs/client-base.js rename to packages/xhs/src/libs/client-base.ts index dc108e5..a8776a1 100644 --- a/packages/xhs/src/libs/client-base.js +++ b/packages/xhs/src/libs/client-base.ts @@ -26,6 +26,18 @@ const transformJsonKeys = (jsonData) => { }; class XhsClient { + proxies: string | null; + timeout: number; + _host: string; + _creatorHost: string; + _customerHost: string; + home: string; + userAgent: string; + _cookie: string; + _headers: Record; + signConfig?: Record = { + signUrl: 'http://localhost:5005/sign', + }; /** * Constructor for XhsClient * @param {Object} options - Configuration options @@ -108,7 +120,7 @@ class XhsClient { const cookieDict = this.getCookieMap(); const newCookieDict = { ...cookieDict, ...data }; const cookieStr = Object.entries(newCookieDict) - .map(([key, value]) => { + .map(([key, value]: [string, string]) => { const trimmedKey = key.trim(); const trimmedValue = value ? value.trim() : ''; return `${trimmedKey}=${trimmedValue}`; @@ -119,12 +131,12 @@ class XhsClient { this.cookie = cookieStr; } - async request(method, url, config = {}) { + async request(method, url, config: any = {}) { try { delete config.sign; const headers = { ...this._headers, ...(config.headers || {}) }; - const fetchOptions = { + const fetchOptions: any = { method: method, headers: headers, }; @@ -145,7 +157,7 @@ class XhsClient { if (response.status === 471 || response.status === 461) { const verifyType = response.headers.get('verifytype'); const verifyUuid = response.headers.get('verifyuuid'); - throw new NeedVerifyError(`出现验证码,请求失败,Verifytype: ${verifyType},Verifyuuid: ${verifyUuid}`, response, verifyType, verifyUuid); + throw new NeedVerifyError(`出现验证码,请求失败,Verifytype: ${verifyType},Verifyuuid: ${verifyUuid}`, response, verifyType); } let data; @@ -197,7 +209,7 @@ class XhsClient { * @param {*} config * @returns */ - async requestSign(uri, data = null, config = {}, method = 'GET') { + async requestSign(uri, data = null, config: any = {}, method = 'GET') { const needSign = config.needSign ?? true; if (needSign && config.sign) { await config.sign(uri, data, config, method); @@ -240,7 +252,7 @@ class XhsClient { this.printResult('get', { uri, params, config }); const endpoint = this.getEndpoint(config).endpoint; config = await this.requestSign(uri, null, config, 'GET'); - + return this.request('GET', `${endpoint}${uri}`, config); } /** @@ -324,7 +336,7 @@ class XhsClient { if (stateMatch) { const state = stateMatch[1].replace(/undefined/g, '""'); if (state !== '{}') { - const noteDict = transformJsonKeys(JSON.parse(state)); + const noteDict = transformJsonKeys(JSON.parse(state)) as any; return { code: 0, data: noteDict.note.note_detail_map[noteId].note }; } } @@ -375,32 +387,6 @@ class XhsClient { return this.get(uri, params); } - /** - * - * @uri /api/sns/web/v1/search/notes - * @param {string} keyword 关键词 - * @param {number} page 页码 - * @param {number} pageSize 分页查询的数量 - * @param {string} sort 搜索的类型,分为: general, popularity_descending, time_descending - * @param {number} noteType 笔记类型 - * @returns - */ - async getNoteByKeyword(keyword, page = 1, pageSize = 20, sort = SearchSortType.GENERAL, noteType = SearchNoteType.ALL) { - const uri = '/api/sns/web/v1/search/notes'; - const data = { - keyword: keyword, - page: page, - page_size: pageSize, - search_id: getSearchId(), - sort: sort.value, - note_type: noteType.value, - image_formats: ['jpg', 'webp', 'avif'], - ext_flags: [], - }; - - return this.post(uri, data); - } - /** * 获取笔记评论 * @uri /api/sns/web/v2/comment/page @@ -504,8 +490,9 @@ class XhsClient { if (stateMatch) { const state = stateMatch[1].replace(/"undefined"/g, '"_"').replace(/\bundefined\b/g, '""'); if (state !== '{}') { - const parsedState = JSON.parse(state); - const userBasicInfo = transformJsonKeys(parsedState).user.user_page_data.basic_info; + const parsedState = JSON.parse(state) as any; + const data = transformJsonKeys(parsedState) as any + const userBasicInfo =data.user.user_page_data.basic_info; return userBasicInfo; } } diff --git a/packages/xhs/src/libs/xhs.ts b/packages/xhs/src/libs/xhs.ts index 798c7d1..16dcbcc 100644 --- a/packages/xhs/src/libs/xhs.ts +++ b/packages/xhs/src/libs/xhs.ts @@ -1,7 +1,7 @@ import { getApiInfo } from './xhs-api/api.ts'; import { XhsClient as XhsClientBase } from './client-base.js'; import { Mention, CommonentInfo, ResponseMession } from './xhs-type/mention.ts'; -import { pick } from 'lodash-es'; +import { pick } from 'lodash-es'; import { getNote } from './modules/get-note.ts'; export type Result = { code: number; // 0: success @@ -62,7 +62,7 @@ type XhsSign = { signUrl?: string; }; export class XhsClient extends XhsClientBase { - signConfig?: XhsSign; + declare signConfig?: XhsSign; constructor(opts: XhsOptions) { super(opts as any); } @@ -92,7 +92,7 @@ export class XhsClient extends XhsClientBase { console.log('sign==>', data); break; case 'post': - console.log('post==>', data); + console.log('post==>', data); break; case 'error': console.log('error==>', data); @@ -181,7 +181,7 @@ export class XhsClient extends XhsClientBase { needSign: true, }, ); - return response; + return response as any; } async sign(uri: string, data: any, config: any, method?: 'GET' | 'POST') { let headers = config?.headers || {}; diff --git a/packages/xhs/tsconfig.json b/packages/xhs/tsconfig.json index 463d887..5ce308e 100644 --- a/packages/xhs/tsconfig.json +++ b/packages/xhs/tsconfig.json @@ -12,7 +12,7 @@ } }, "include": [ - "src/**/*.ts", "src/libs/client-base.js", + "src/**/*.ts", "src/libs/client-base.ts", ], "exclude": [], } \ No newline at end of file