Skip to content

Conversation

@mynetfan
Copy link
Collaborator

@mynetfan mynetfan commented Mar 29, 2025

Description

在网络请求中,针对数组参数的处理预置几种参数序列化方式,使得各个应用能更方便地对接不同后台

fixed: #5702

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update
  • Please, don't make changes to pnpm-lock.yaml unless you introduce a new test example.

Checklist

ℹ️ Check all checkboxes - this will indicate that you have done everything in accordance with the rules in CONTRIBUTING.

  • If you introduce new functionality, document it. You can run documentation with pnpm run docs:dev command.
  • Run the tests with pnpm test.
  • Changes in changelog are generated from PR name. Please, make sure that it explains your changes in an understandable manner. Please, prefix changeset messages with feat:, fix:, perf:, docs:, or chore:.
  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Summary by CodeRabbit

  • New Features

    • Enhanced HTTP request configuration, allowing users to customize how parameters are serialized (using options like brackets, comma, indices, and repeat) and how response data is managed.
    • Introduced an interactive demo page that lets users experiment with and visualize different request parameter serialization methods.
  • Documentation

    • Added a new guide section detailing the extended configuration options.
  • Chores

    • Updated dependencies to improve query string handling and type support.

@mynetfan mynetfan requested review from a team, anncwb, jinmao88 and vince292007 as code owners March 29, 2025 09:47
@changeset-bot
Copy link

changeset-bot bot commented Mar 29, 2025

⚠️ No Changeset found

Latest commit: 47b974b

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 29, 2025

Walkthrough

The changes add extended configuration options for Axios-based requests by integrating qs-based parameter serialization and custom response handling. A new generic TypeScript type, ExtendOptions<T>, has been introduced along with updates to the RequestClient to process a new paramsSerializer option via a helper function. Dependency updates include adding the qs library and its type definitions. A new demo route and Vue component have also been added to showcase the functionality, accompanied by localization updates.

Changes

File(s) Change Summary
docs/src/guide/…/server.md
packages/effects/request/src/request-client/types.ts
Introduced and updated ExtendOptions<T> with paramsSerializer and responseReturn for flexible request configurations.
packages/effects/request/src/request-client/request-client.ts Added getParamsSerializer function and integrated it within RequestClient to handle qs-based parameter serialization.
packages/effects/request/package.json
pnpm-workspace.yaml
Added qs as a dependency and @types/qs as a dev dependency for query string handling.
playground/src/api/examples/params.ts
playground/src/locales/langs/zh-CN/demos.json
playground/src/router/routes/modules/demos.ts
playground/src/views/demos/features/request-params-serializer/index.vue
Added a demo for request parameter serialization, including an API example, route, localization entry, and a Vue component to interact with the new feature.

Sequence Diagram(s)

sequenceDiagram
    participant UI as Demo UI
    participant API as getParamsData
    participant RC as RequestClient
    participant QS as qs.stringify

    UI->>API: User selects a serialization method
    API->>RC: Make GET request with params & serializer option
    RC->>QS: Call getParamsSerializer(serializer)
    QS-->>RC: Return serialized parameters
    RC-->>API: Process request and return response
    API-->>UI: Update UI with received response
Loading

Assessment against linked issues

Objective (Issue Ref) Addressed Explanation
Integrate qs-based parameter serialization into RequestClient for array parameters (#5702)

Possibly related PRs

  • vbenjs/vue-vben-admin#5436: Implements the responseReturn option, which aligns with the new custom response handling.
  • vbenjs/vue-vben-admin#5024: Introduces enhanced TypeScript types and parameter handling, closely related to qs integration.
  • vbenjs/vue-vben-admin#5456: Updates response handling in RequestClient, paralleling the responseReturn feature.

Suggested labels

feature

Suggested reviewers

  • anncwb
  • vince292007

Poem

I'm a little rabbit, leaping in code delight,
Hopping through changes that shine so bright.
QS strings now dance in a neat array,
Extended configs lead the way.
With joyful hops, my cheeks do glow—
For new features that help our projects grow!

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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 generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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
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: 1

🧹 Nitpick comments (3)
playground/src/api/examples/params.ts (1)

5-17: Consider enhancing documentation for serialization types

The function implementation is clean and well-typed, but would benefit from more detailed documentation explaining the differences between the serialization types ('brackets', 'comma', 'indices', 'repeat') and when to use each one.

 /**
  * 发起数组请求
+ * 
+ * @param params - The parameters to send with the request
+ * @param type - The serialization method to use:
+ *   - 'brackets': Serializes arrays using bracket notation, e.g., 'colors[]=blue&colors[]=green'
+ *   - 'comma': Serializes arrays as comma-separated values, e.g., 'colors=blue,green'
+ *   - 'indices': Serializes arrays with indices, e.g., 'colors[0]=blue&colors[1]=green'
+ *   - 'repeat': Serializes arrays by repeating the key, e.g., 'colors=blue&colors=green'
  */
playground/src/views/demos/features/request-params-serializer/index.vue (1)

1-26: Good implementation of parameter serialization demonstration.

The component demonstrates the parameter serialization feature effectively. A few suggestions for improvement:

  1. The comment on line 16 is unnecessary and can be removed
  2. Add error handling to the API call
  3. Consider using a more reliable method to get the serialized URL parameters instead of relying on responseURL

Here's a suggested improvement:

- watchEffect(() => {
-   getParamsData(params, paramsSerializer.value).then((res) => {
-     response.value = res.request.responseURL;
-   });
- });
+ watchEffect(() => {
+   getParamsData(params, paramsSerializer.value)
+     .then((res) => {
+       response.value = res.request.responseURL;
+     })
+     .catch((error) => {
+       console.error('Failed to get params data:', error);
+       response.value = '';
+     });
+ });
packages/effects/request/src/request-client/request-client.ts (1)

14-36: Suggestion: Add unit tests for parameter serialization

This new functionality introduces several serialization formats, which should be covered by unit tests to ensure correct behavior and prevent regressions.

Consider adding tests for:

  1. Each predefined serialization format ('brackets', 'comma', 'indices', 'repeat')
  2. Custom serialization functions
  3. Edge cases (undefined parameters, empty arrays, nested objects)
  4. Handling of invalid formats
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between e91e4e0 and 47b974b.

⛔ Files ignored due to path filters (1)
  • pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (9)
  • docs/src/guide/essentials/server.md (1 hunks)
  • packages/effects/request/package.json (1 hunks)
  • packages/effects/request/src/request-client/request-client.ts (3 hunks)
  • packages/effects/request/src/request-client/types.ts (1 hunks)
  • playground/src/api/examples/params.ts (1 hunks)
  • playground/src/locales/langs/zh-CN/demos.json (1 hunks)
  • playground/src/router/routes/modules/demos.ts (1 hunks)
  • playground/src/views/demos/features/request-params-serializer/index.vue (1 hunks)
  • pnpm-workspace.yaml (2 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
packages/effects/request/src/request-client/request-client.ts (1)
packages/effects/request/src/request-client/types.ts (1)
  • RequestClientOptions (76-76)
⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Lint (windows-latest)
  • GitHub Check: post-update (ubuntu-latest)
  • GitHub Check: post-update (windows-latest)
🔇 Additional comments (13)
playground/src/locales/langs/zh-CN/demos.json (1)

53-54: Well-structured localization entry

The new localization entry for "requestParamsSerializer" follows the established pattern in the file and properly provides a Chinese translation for the parameter serialization feature.

packages/effects/request/package.json (2)

25-26: Appropriate dependencies added

The addition of the qs library is necessary for implementing parameter serialization functionality.


29-30: TypeScript typing properly supported

The inclusion of @types/qs as a dev dependency ensures proper TypeScript typing support, following best practices.

pnpm-workspace.yaml (2)

53-53: Appropriate type definition version added

The addition of @types/qs with version ^6.9.18 in the catalog section is correctly placed in alphabetical order and uses proper semantic versioning.


143-143: Appropriate library version added

The addition of qs with version ^6.14.0 in the catalog section is correctly placed in alphabetical order and uses proper semantic versioning.

docs/src/guide/essentials/server.md (1)

113-141: Documentation for new parameter serialization options looks good!

The new documentation clearly explains the extended configuration options with proper descriptions for each serialization method and response return type. This addition helps developers understand how to use the new features effectively.

playground/src/router/routes/modules/demos.ts (1)

246-257: New route for parameter serializer demo correctly configured.

The route is properly added to the features section with appropriate naming, path, and icon. The title uses the translation key for internationalization which is good practice.

packages/effects/request/src/request-client/types.ts (2)

8-29: Well-designed type enhancements for parameter serialization.

The ExtendOptions type has been properly enhanced with:

  1. A generic type parameter for better type safety
  2. The new paramsSerializer property with clear documentation
  3. Four preset options plus support for custom Axios serializer functions

The type definition is well-structured and maintains backward compatibility.


30-30: Updated RequestClientConfig to use generic ExtendOptions.

The RequestClientConfig type now correctly uses the generic version of ExtendOptions, maintaining type consistency throughout the application.

playground/src/views/demos/features/request-params-serializer/index.vue (1)

27-61: Clean and intuitive UI for demonstrating parameter serialization.

The template is well-structured with a good layout showing:

  1. The parameters to be submitted
  2. The resulting serialized URL
  3. The extracted parameter string and its decoded version

The use of Radio buttons for selecting the serialization method provides a clear and intuitive interface.

packages/effects/request/src/request-client/request-client.ts (3)

5-5: Imports look good

The addition of isString from the utils package and the qs library are appropriate for the new parameter serialization functionality.

Also applies to: 8-8


67-69: Implementation in constructor looks good

The integration of the getParamsSerializer function in the constructor ensures that the appropriate serialization method is applied for all requests based on the provided configuration.


139-141: Request method implementation is correct

The conditional inclusion of the paramsSerializer in the request configuration allows for dynamic serialization of parameters on a per-request basis. This approach provides flexibility while maintaining a clean implementation.

@mynetfan mynetfan merged commit 96d2bc5 into vbenjs:main Mar 29, 2025
14 checks passed
@mynetfan mynetfan deleted the feat/request-params branch March 29, 2025 11:21
@github-actions github-actions bot locked and limited conversation to collaborators Apr 29, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: RequestClient GET请求数组参数 报错

1 participant