Skip to content

refactor: cms request add headers#71

Merged
consistent-k merged 1 commit intomainfrom
dev
Dec 1, 2024
Merged

refactor: cms request add headers#71
consistent-k merged 1 commit intomainfrom
dev

Conversation

@consistent-k
Copy link
Copy Markdown
Owner

@consistent-k consistent-k commented Dec 1, 2024

Summary by CodeRabbit

  • 新特性

    • 更新了项目的版本号为“0.1.0”,并增加了关键词以提升包的可发现性。
    • 新增了 CMSDetailData 接口,扩展了数据结构能力。
    • 所有相关请求现在使用 CMSDetailData 作为数据结构,简化了数据处理逻辑。
    • 请求头中添加了 User-AgentReferer,增强了请求的上下文信息。
  • 修复

    • 日志记录增强,包含更多上下文信息以帮助错误追踪。

@consistent-k
Copy link
Copy Markdown
Owner Author

@coderabbitai full review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Dec 1, 2024

✅ Actions performed

Full review triggered.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Dec 1, 2024

Walkthrough

此次更改涉及多个文件,主要更新了数据结构和请求处理方式。在 package.json 中添加了版本号和关键词。在 src/types/cms.ts 中引入了新的接口 CMSDetailData,并在相关文件中替换了对 CMSDetailList 的引用为 CMSDetailData。此外,多个文件的请求头中增加了 User-AgentReferer 字段,以提供更丰富的请求上下文。这些更改旨在简化数据处理和增强请求的可识别性。

Changes

文件路径 更改摘要
package.json 添加字段 "version": "0.1.0""keywords": ["电影", "视频", "电视剧", "综艺", "动漫"]
src/types/cms.ts 新增接口 CMSDetailData,包含属性 list: CMSDetailList[]
src/utils/cms/category/index.ts 更新导入 CMSDetailListCMSDetailData,请求处理逻辑中使用新数据结构,添加请求头。
src/utils/cms/detail/index.ts 更新导入 CMSDetailListCMSDetailData,请求返回单个 CMSDetailData 对象,添加请求头。
src/utils/cms/home/index.ts 添加导入 USER_AGENT_CHROME,请求中加入 User-AgentReferer 头。
src/utils/cms/homeVod/index.ts 更新导入 CMSDetailListCMSDetailData,请求返回单个对象,添加请求头。
src/utils/cms/search/index.ts 更新导入 CMSDetailListCMSDetailData,请求返回类型更改,添加请求头。

Poem

在月光下,兔子跳跃,
新版本来,欢声笑语。
关键词闪耀,数据更新,
请求头中,信息更全。
一起庆祝,代码更美,
兔子快乐,舞动不息! 🐇✨


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (4)
src/utils/cms/home/index.ts (1)

7-7: 请求头的添加有助于模拟真实浏览器行为!

建议考虑将请求头配置抽取到一个统一的配置文件中,以便于维护和复用。

可以考虑创建一个通用的请求头配置:

// src/constant/headers.ts
export const DEFAULT_HEADERS = {
  'User-Agent': USER_AGENT_CHROME,
  'Referer': (url: string) => `${url}/`
};

Also applies to: 19-22

src/utils/cms/search/index.ts (1)

20-28: 请求头的添加提高了请求的真实性

添加的请求头配置增强了请求的合法性,但建议考虑以下几点:

  1. 考虑将请求头配置集中管理
  2. 可以添加错误重试机制

建议重构为:

+ const DEFAULT_HEADERS = {
+   'User-Agent': USER_AGENT_CHROME,
+   'Referer': ''
+ };

  const res = await request.post<CMSDetailData>(
    `${namespace.url}/api.php/provide/vod`,
    {
      params: {
        ac: 'detail',
        wd: keyword
      },
-     headers: {
-       'User-Agent': USER_AGENT_CHROME,
-       Referer: `${namespace.url}/`
-     }
+     headers: {
+       ...DEFAULT_HEADERS,
+       Referer: `${namespace.url}/`
+     }
    }
  );
src/types/cms.ts (1)

19-21: 新增的接口类型设计合理

CMSDetailData 接口的添加优化了 API 响应数据的类型定义,使得类型系统更加完整和清晰。建议考虑添加以下内容:

  1. 为接口添加 JSDoc 文档注释
  2. 考虑添加 code 字段的类型定义

建议添加文档注释:

+ /**
+  * CMS 详情数据接口
+  * @property {CMSDetailList[]} list - 详情数据列表
+  * @property {number} code - 响应状态码
+  */
  export interface CMSDetailData {
    list: CMSDetailList[];
+   code: number;
  }
src/utils/cms/detail/index.ts (1)

Line range hint 42-77: 建议增强错误处理机制

当前的错误处理可以进行以下改进:

  1. 为网络请求添加重试机制
  2. 细化错误类型的处理
  3. 增加错误日志的详细程度

建议重构错误处理逻辑:

 try {
     const body = await ctx.req.json();
     logger.info(`${DETAIL_MESSAGE.INFO} - ${namespace.name} - ${JSON.stringify(body)}`);
 
     const { id } = body;
 
-    const res = await request.get<CMSDetailData>(...);
+    const res = await retry(
+        async () => await request.get<CMSDetailData>(...),
+        { retries: 3, minTimeout: 1000 }
+    );
     
     const { list, code } = res;
 
     if (code === 1 && list.length > 0) {
         // ... existing code ...
     }
 
-    logger.error(`${DETAIL_MESSAGE.ERROR} - ${namespace.name} - ${JSON.stringify(res)}`);
+    logger.error(`${DETAIL_MESSAGE.ERROR} - ${namespace.name} - Response: ${JSON.stringify(res)} - Request ID: ${id}`);
     return {
         code: ERROR_CODE,
         message: DETAIL_MESSAGE.ERROR,
         data: []
     };
 } catch (error) {
     ctx.res.headers.set('Cache-Control', 'no-cache');
-    logger.error(`${DETAIL_MESSAGE.ERROR} - ${namespace.name} - ${error}`);
+    logger.error(`${DETAIL_MESSAGE.ERROR} - ${namespace.name} - ${error.stack || error.message || error}`);
     return {
         code: SYSTEM_ERROR_CODE,
-        message: DETAIL_MESSAGE.ERROR,
+        message: `${DETAIL_MESSAGE.ERROR}: ${error.message || '未知错误'}`,
         data: []
     };
 }
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 717149b and b263292.

📒 Files selected for processing (7)
  • package.json (1 hunks)
  • src/types/cms.ts (1 hunks)
  • src/utils/cms/category/index.ts (2 hunks)
  • src/utils/cms/detail/index.ts (2 hunks)
  • src/utils/cms/home/index.ts (2 hunks)
  • src/utils/cms/homeVod/index.ts (1 hunks)
  • src/utils/cms/search/index.ts (2 hunks)
🔇 Additional comments (6)
package.json (1)

3-6: 版本号和关键词设置合理!

版本号遵循语义化版本规范,关键词准确反映了项目的核心功能和领域。

src/utils/cms/category/index.ts (1)

9-9: 建议验证 POST 请求使用查询参数的设计

当前实现使用 POST 请求但参数通过 query string 传递,这种混合方式可能不符合 RESTful API 设计最佳实践。建议考虑以下方案之一:

  1. 如果需要传递复杂数据,将参数移至请求体
  2. 如果是简单的数据获取,改用 GET 请求

Also applies to: 19-19

src/utils/cms/search/index.ts (1)

7-7: 导入更改符合新的数据结构要求

新增的导入声明与类型系统的重构保持一致,同时添加了用户代理常量以支持请求头的增强。

Also applies to: 9-9

src/utils/cms/homeVod/index.ts (1)

7-7: 类型导入变更保持一致性

导入声明的更改与其他文件保持一致,符合类型系统的重构要求。

Also applies to: 9-9

src/utils/cms/detail/index.ts (2)

7-7: 导入更改看起来不错!

导入的更改符合重构目标,通过添加 USER_AGENT_CHROME 来增强请求头,同时更新了 CMS 数据类型以提高类型安全性。

Also applies to: 9-9


32-39: 建议验证 Referer 的安全性并改进错误处理

请求配置的更改有以下几点建议:

  1. 建议验证 namespace.url 的格式和安全性,以防止潜在的安全风险。
  2. 考虑添加请求超时配置。

建议应用以下改进:

 const res = await request.get<CMSDetailData>(`${namespace.url}/api.php/provide/vod`, {
     params: {
         ac: 'detail',
         ids: id
     },
     headers: {
         'User-Agent': USER_AGENT_CHROME,
         Referer: `${namespace.url}/`
-    }
+    },
+    timeout: 5000
 });

运行以下脚本验证 namespace.url 的使用:

✅ Verification successful

Let me analyze the usage of namespace.url in the codebase to verify the concerns.

Let me check if there's any timeout configuration in other requests to maintain consistency.


Based on the analysis of the codebase, I can now provide the final response:

建议保持当前的 Referer 配置

经过验证,当前的实现是安全的:

  1. namespace.url 在整个代码库中被一致地用作 Referer,这是标准做法。
  2. 各个模块都有独立的 request 实例,并且都遵循相同的 Referer 设置模式。
  3. 超时配置仅在特定的播放接口中使用(4秒超时),对于详情页面的请求,当前无超时是合理的,因为这些请求通常响应更快。
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# 检查 namespace.url 的其他使用位置,确保一致性和安全性
rg "namespace\.url" -t ts

Length of output: 5414


Script:

#!/bin/bash
# Check if there's any URL validation or sanitization for namespace.url
rg -B 3 -A 3 "namespace\s*=|interface.*Namespace" -t ts

Length of output: 724


Script:

#!/bin/bash
# Check for timeout configurations in requests
rg "timeout:|timeout\s*=" -t ts

Length of output: 289


Script:

#!/bin/bash
# Check request utility implementation
fd "request.ts" --exec cat {}

Length of output: 8940

Comment on lines +27 to +29
headers: {
'User-Agent': USER_AGENT_CHROME,
Referer: `${namespace.url}/`
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

建议统一处理请求头配置

与 home/index.ts 文件类似,这里也添加了相同的请求头。建议将这些重复的请求头配置统一管理。

建议创建一个请求拦截器统一处理请求头:

// src/utils/cms/request.ts
import { USER_AGENT_CHROME } from '@/constant/userAgent';

request.interceptors.request.use((config) => {
  config.headers = {
    ...config.headers,
    'User-Agent': USER_AGENT_CHROME,
    'Referer': `${config.baseURL}/`
  };
  return config;
});

Comment on lines +16 to +24
let res = await request.get<CMSDetailData>(`${namespace.url}/api.php/provide/vod`, {
params: {
ac: 'detail',
t: '0',
pagesize: 30
},
headers: {
'User-Agent': USER_AGENT_CHROME,
Referer: `${namespace.url}/`
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

请求配置存在重复代码

与 search/index.ts 文件中的请求配置存在重复,建议将请求头配置抽取到公共模块。

建议创建一个新的工具函数:

// src/utils/request/headers.ts
export const getRequestHeaders = (baseUrl: string) => ({
  'User-Agent': USER_AGENT_CHROME,
  'Referer': `${baseUrl}/`
});

然后在请求中使用:

  const res = await request.get<CMSDetailData>(
    `${namespace.url}/api.php/provide/vod`,
    {
      params: {
        ac: 'detail',
        t: '0',
        pagesize: 30
      },
-     headers: {
-       'User-Agent': USER_AGENT_CHROME,
-       Referer: `${namespace.url}/`
-     }
+     headers: getRequestHeaders(namespace.url)
    }
  );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant