Skip to content

Conversation

@wangcch
Copy link
Contributor

@wangcch wangcch commented Jul 17, 2024

const [res, req] = useResource(() => request<Users>({ url: `/user/list` }), []);

// () => Canceler;
req();
const [res, req, refresh, cancel] = useResource(
  () => request<Users>({ url: `/user/list` }),
  [],
  // If you want the request function to return the response
  { asyncReq: true }
);

// () => Promise<[Users | undefined, AxiosResponse<Users>]>;
req();

@wangcch wangcch changed the base branch from main to dev July 25, 2024 11:44
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a new asyncReq option to the useResource hook, allowing developers to control whether the request function returns a Canceler (default behavior) or a Promise that resolves with the response data.

Key Changes:

  • Introduces asyncReq boolean option to useResource to toggle between synchronous (Canceler-returning) and asynchronous (Promise-returning) request patterns
  • Adds new RequestAsyncFunc<T> type definition for async request functions that return Promise<readonly [Payload<T, true>, Payload<T>]>
  • Implements dual-mode request dispatcher with internal _reqAsync and _reqDispatcher functions

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/request.ts Adds RequestAsyncFunc type definition for the async request function signature
src/useResource.ts Implements the core asyncReq functionality with conditional request dispatcher logic and updated type signatures
tests/useResource.test.ts Adds comprehensive test coverage for the new async request function feature, including success and error scenarios
tests/context.test.ts Adds test case for setUseRequestConfig with empty options parameter
README.md Documents the new asyncReq option in the options table and type definitions
package.json Bumps version to 0.3.0-alpha.1 for the new feature release

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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