qwik icon indicating copy to clipboard operation
qwik copied to clipboard

fix: correct a type error occurring when the `requestEvent.fail()` is returned in the `actionQrl` or `loaderQrl`

Open wtlin1228 opened this issue 2 years ago • 1 comments

Overview

What is it?

  • [ ] Feature / enhancement
  • [ ] Bug
  • [x] Docs / tests / types / typos

Description

routeAction$, globalAction$ and routeLoader$ have incorrect type when the requestEvent.fail(...) is returned in the actionQrl or loaderQrl.

ex:

export const useRouteActionWithFail = routeAction$((data, requestEvent) => {
  if (Math.random() > 1) {
    return requestEvent.fail(500, {
      sad: 'sad',
      errorMessage: 'why',
    });
  } 

  return {
    happy: 'happy',
    day: 'day',
  };
});

Use cases and why

Actual

截圖 2023-04-30 上午3 29 09

Expected behavior

The type errors should not appear.

To facilitate the reproduction of this bug, here is my testing code.

export const useRouteAction = routeAction$((data, requestEvent) => {
  return {
    happy: 'happy',
    day: 'day',
  };
});

export const useRouteActionWithFail = routeAction$((data, requestEvent) => {
  if (Math.random() > 1) {
    return requestEvent.fail(500, {
      sad: 'sad',
      errorMessage: 'why',
    });
  }

  return {
    happy: 'happy',
    day: 'day',
  };
});

export const useRouteActionWithFailWithValidation = routeAction$((data, requestEvent) => {
  if (Math.random() > 1) {
    return requestEvent.fail(500, {
      sad: 'sad',
      errorMessage: 'why',
    });
  }

  return {
    happy: 'happy',
    day: 'day',
  };
}, zod$({}));

export const useRouteLoader = routeLoader$((requestEvent) => {
  return {
    happy: 'happy',
    day: 'day',
  };
});

export const useRouteLoaderWithFail = routeLoader$((requestEvent) => {
  if (Math.random() > 1) {
    return requestEvent.fail(500, {
      sad: 'sad',
      errorMessage: 'why',
    });
  }

  return {
    happy: 'happy',
    day: 'day',
  };
});

export const useRouteLoaderWithFailWithValidation = routeLoader$((requestEvent) => {
  if (Math.random() > 1) {
    return requestEvent.fail(500, {
      sad: 'sad',
      errorMessage: 'why',
    });
  }

  return {
    happy: 'happy',
    day: 'day',
  };
}, zod$({}));

export default component$(() => {
  const routeAction = useRouteAction();
  routeAction.value?.happy;
  routeAction.value?.day;

  const routeActionWithFail = useRouteActionWithFail();
  routeActionWithFail.value?.happy; // ❌ Property 'happy' does not exist on type 'FailReturn<...>'
  routeActionWithFail.value?.day; // ❌ Property 'day' does not exist on type 'FailReturn<...>'
  routeActionWithFail.value?.sad; // ❌ Property 'sad' does not exist on type 'FailReturn<...>'
  routeActionWithFail.value?.errorMessage; // ❌ Property 'errorMessage' does not exist on type 

  const routeActionWithFailWithValidation = useRouteActionWithFailWithValidation();
  routeActionWithFailWithValidation.value?.happy; 
  routeActionWithFailWithValidation.value?.day;
  routeActionWithFailWithValidation.value?.sad;
  routeActionWithFailWithValidation.value?.errorMessage;'FailReturn<...>'

  const routeLoader = useRouteLoader();
  routeLoader.value.happy;
  routeLoader.value.day;

  const routeLoaderWithFail = useRouteLoaderWithFail();
  routeLoaderWithFail.value.happy; // ❌ Property 'happy' does not exist on type 'FailReturn<...>'
  routeLoaderWithFail.value.day; // ❌ Property 'day' does not exist on type 'FailReturn<...>'
  routeLoaderWithFail.value.sad; // ❌ Property 'sad' does not exist on type 'FailReturn<...>'
  routeLoaderWithFail.value.errorMessage;  // ❌ Property 'errorMessage' does not exist on type 'FailReturn<...>'

  const routeLoaderWithFailValidation = useRouteLoaderWithFailWithValidation();
  routeLoaderWithFailValidation.value.happy;
  routeLoaderWithFailValidation.value.day;
  routeLoaderWithFailValidation.value.sad;
  routeLoaderWithFailValidation.value.errorMessage;

  return <></>;
});

Checklist:

  • [x] My code follows the developer guidelines of this project
  • [x] I have performed a self-review of my own code
  • [ ] I have made corresponding changes to the documentation
  • [ ] Added new tests to cover the fix / functionality

wtlin1228 avatar Apr 29 '23 19:04 wtlin1228

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Are you still working on this or is it abandoned?

gioboa avatar Aug 24 '23 09:08 gioboa

@gioboa Oh, I totally forget this PR. I am willing to work on it if the related issue is still open.

wtlin1228 avatar Aug 24 '23 11:08 wtlin1228

The issue is still open, please check if with the current release the issue is there too

gioboa avatar Aug 24 '23 12:08 gioboa

@gioboa I found there are still some type errors for actions and loaders, I fixed the action type in this PR and I will try to fix loader type in later PR. So, I think this PR can be closed.

wtlin1228 avatar Aug 26 '23 17:08 wtlin1228

Thanks for investigating in it. 😊

gioboa avatar Aug 26 '23 18:08 gioboa