rn-custom-ui 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +20 -0
- package/README.md +477 -0
- package/lib/module/assets/images/close.png +0 -0
- package/lib/module/components/CustomCheckbox/index.js +99 -0
- package/lib/module/components/CustomCheckbox/index.js.map +1 -0
- package/lib/module/components/CustomCheckbox/styles.js +213 -0
- package/lib/module/components/CustomCheckbox/styles.js.map +1 -0
- package/lib/module/components/CustomCheckbox/types.js +4 -0
- package/lib/module/components/CustomCheckbox/types.js.map +1 -0
- package/lib/module/components/closeButton/index.js +45 -0
- package/lib/module/components/closeButton/index.js.map +1 -0
- package/lib/module/components/closeButton/types.js +4 -0
- package/lib/module/components/closeButton/types.js.map +1 -0
- package/lib/module/components/customButton/index.js +176 -0
- package/lib/module/components/customButton/index.js.map +1 -0
- package/lib/module/components/customButton/styles.js +282 -0
- package/lib/module/components/customButton/styles.js.map +1 -0
- package/lib/module/components/customButton/types.js +4 -0
- package/lib/module/components/customButton/types.js.map +1 -0
- package/lib/module/components/customDropDown/DropDownBottomSheet.js +57 -0
- package/lib/module/components/customDropDown/DropDownBottomSheet.js.map +1 -0
- package/lib/module/components/customDropDown/DropDownList.js +51 -0
- package/lib/module/components/customDropDown/DropDownList.js.map +1 -0
- package/lib/module/components/customDropDown/DropDownListItem.js +38 -0
- package/lib/module/components/customDropDown/DropDownListItem.js.map +1 -0
- package/lib/module/components/customDropDown/DropDownListModal.js +63 -0
- package/lib/module/components/customDropDown/DropDownListModal.js.map +1 -0
- package/lib/module/components/customDropDown/index.js +143 -0
- package/lib/module/components/customDropDown/index.js.map +1 -0
- package/lib/module/components/customDropDown/styles.js +189 -0
- package/lib/module/components/customDropDown/styles.js.map +1 -0
- package/lib/module/components/customDropDown/types.js +4 -0
- package/lib/module/components/customDropDown/types.js.map +1 -0
- package/lib/module/components/customDropDown/utils.js +17 -0
- package/lib/module/components/customDropDown/utils.js.map +1 -0
- package/lib/module/components/customList/ListEmpty.js +33 -0
- package/lib/module/components/customList/ListEmpty.js.map +1 -0
- package/lib/module/components/customList/ListFooterLoader.js +14 -0
- package/lib/module/components/customList/ListFooterLoader.js.map +1 -0
- package/lib/module/components/customList/defaultKeyExtractor.js +9 -0
- package/lib/module/components/customList/defaultKeyExtractor.js.map +1 -0
- package/lib/module/components/customList/getFlashList.js +19 -0
- package/lib/module/components/customList/getFlashList.js.map +1 -0
- package/lib/module/components/customList/index.js +117 -0
- package/lib/module/components/customList/index.js.map +1 -0
- package/lib/module/components/customList/styles.js +45 -0
- package/lib/module/components/customList/styles.js.map +1 -0
- package/lib/module/components/customList/types.js +4 -0
- package/lib/module/components/customList/types.js.map +1 -0
- package/lib/module/components/customModal/ModalFooter.js +44 -0
- package/lib/module/components/customModal/ModalFooter.js.map +1 -0
- package/lib/module/components/customModal/index.js +132 -0
- package/lib/module/components/customModal/index.js.map +1 -0
- package/lib/module/components/customModal/styles.js +123 -0
- package/lib/module/components/customModal/styles.js.map +1 -0
- package/lib/module/components/customModal/types.js +4 -0
- package/lib/module/components/customModal/types.js.map +1 -0
- package/lib/module/components/customModal/useCustomModal.js +23 -0
- package/lib/module/components/customModal/useCustomModal.js.map +1 -0
- package/lib/module/components/customNumberPad/NumberPadKey.js +39 -0
- package/lib/module/components/customNumberPad/NumberPadKey.js.map +1 -0
- package/lib/module/components/customNumberPad/index.js +156 -0
- package/lib/module/components/customNumberPad/index.js.map +1 -0
- package/lib/module/components/customNumberPad/styles.js +131 -0
- package/lib/module/components/customNumberPad/styles.js.map +1 -0
- package/lib/module/components/customNumberPad/types.js +4 -0
- package/lib/module/components/customNumberPad/types.js.map +1 -0
- package/lib/module/components/customNumberPad/useNumberPadInput.js +44 -0
- package/lib/module/components/customNumberPad/useNumberPadInput.js.map +1 -0
- package/lib/module/components/customNumberPad/utils.js +38 -0
- package/lib/module/components/customNumberPad/utils.js.map +1 -0
- package/lib/module/components/customSlideTabs/AnimatedTabIndicator.js +66 -0
- package/lib/module/components/customSlideTabs/AnimatedTabIndicator.js.map +1 -0
- package/lib/module/components/customSlideTabs/TabBarItem.js +61 -0
- package/lib/module/components/customSlideTabs/TabBarItem.js.map +1 -0
- package/lib/module/components/customSlideTabs/TabScene.js +88 -0
- package/lib/module/components/customSlideTabs/TabScene.js.map +1 -0
- package/lib/module/components/customSlideTabs/index.js +277 -0
- package/lib/module/components/customSlideTabs/index.js.map +1 -0
- package/lib/module/components/customSlideTabs/styles.js +132 -0
- package/lib/module/components/customSlideTabs/styles.js.map +1 -0
- package/lib/module/components/customSlideTabs/tabBarScroll.js +65 -0
- package/lib/module/components/customSlideTabs/tabBarScroll.js.map +1 -0
- package/lib/module/components/customSlideTabs/types.js +4 -0
- package/lib/module/components/customSlideTabs/types.js.map +1 -0
- package/lib/module/components/loading/index.js +34 -0
- package/lib/module/components/loading/index.js.map +1 -0
- package/lib/module/customFunctions/array.js +61 -0
- package/lib/module/customFunctions/array.js.map +1 -0
- package/lib/module/customFunctions/async.js +62 -0
- package/lib/module/customFunctions/async.js.map +1 -0
- package/lib/module/customFunctions/date.js +109 -0
- package/lib/module/customFunctions/date.js.map +1 -0
- package/lib/module/customFunctions/general.js +29 -0
- package/lib/module/customFunctions/general.js.map +1 -0
- package/lib/module/customFunctions/id.js +20 -0
- package/lib/module/customFunctions/id.js.map +1 -0
- package/lib/module/customFunctions/index.js +29 -0
- package/lib/module/customFunctions/index.js.map +1 -0
- package/lib/module/customFunctions/number.js +94 -0
- package/lib/module/customFunctions/number.js.map +1 -0
- package/lib/module/customFunctions/object.js +39 -0
- package/lib/module/customFunctions/object.js.map +1 -0
- package/lib/module/customFunctions/string.js +63 -0
- package/lib/module/customFunctions/string.js.map +1 -0
- package/lib/module/customFunctions/types.js +2 -0
- package/lib/module/customFunctions/types.js.map +1 -0
- package/lib/module/customFunctions/validation.js +34 -0
- package/lib/module/customFunctions/validation.js.map +1 -0
- package/lib/module/customHooks/errors.js +61 -0
- package/lib/module/customHooks/errors.js.map +1 -0
- package/lib/module/customHooks/fetchJson.js +36 -0
- package/lib/module/customHooks/fetchJson.js.map +1 -0
- package/lib/module/customHooks/index.js +10 -0
- package/lib/module/customHooks/index.js.map +1 -0
- package/lib/module/customHooks/internal.js +49 -0
- package/lib/module/customHooks/internal.js.map +1 -0
- package/lib/module/customHooks/types.js +2 -0
- package/lib/module/customHooks/types.js.map +1 -0
- package/lib/module/customHooks/useApi.js +91 -0
- package/lib/module/customHooks/useApi.js.map +1 -0
- package/lib/module/customHooks/useLazyApi.js +80 -0
- package/lib/module/customHooks/useLazyApi.js.map +1 -0
- package/lib/module/customHooks/useMutation.js +69 -0
- package/lib/module/customHooks/useMutation.js.map +1 -0
- package/lib/module/customHooks/usePaginatedApi.js +127 -0
- package/lib/module/customHooks/usePaginatedApi.js.map +1 -0
- package/lib/module/customHooks/usePollingApi.js +37 -0
- package/lib/module/customHooks/usePollingApi.js.map +1 -0
- package/lib/module/index.js +13 -0
- package/lib/module/index.js.map +1 -0
- package/lib/module/multiply.js +6 -0
- package/lib/module/multiply.js.map +1 -0
- package/lib/module/package.json +1 -0
- package/lib/typescript/package.json +1 -0
- package/lib/typescript/src/components/CustomCheckbox/index.d.ts +5 -0
- package/lib/typescript/src/components/CustomCheckbox/index.d.ts.map +1 -0
- package/lib/typescript/src/components/CustomCheckbox/styles.d.ts +67 -0
- package/lib/typescript/src/components/CustomCheckbox/styles.d.ts.map +1 -0
- package/lib/typescript/src/components/CustomCheckbox/types.d.ts +28 -0
- package/lib/typescript/src/components/CustomCheckbox/types.d.ts.map +1 -0
- package/lib/typescript/src/components/closeButton/index.d.ts +4 -0
- package/lib/typescript/src/components/closeButton/index.d.ts.map +1 -0
- package/lib/typescript/src/components/closeButton/types.d.ts +8 -0
- package/lib/typescript/src/components/closeButton/types.d.ts.map +1 -0
- package/lib/typescript/src/components/customButton/index.d.ts +5 -0
- package/lib/typescript/src/components/customButton/index.d.ts.map +1 -0
- package/lib/typescript/src/components/customButton/styles.d.ts +231 -0
- package/lib/typescript/src/components/customButton/styles.d.ts.map +1 -0
- package/lib/typescript/src/components/customButton/types.d.ts +46 -0
- package/lib/typescript/src/components/customButton/types.d.ts.map +1 -0
- package/lib/typescript/src/components/customDropDown/DropDownBottomSheet.d.ts +18 -0
- package/lib/typescript/src/components/customDropDown/DropDownBottomSheet.d.ts.map +1 -0
- package/lib/typescript/src/components/customDropDown/DropDownList.d.ts +17 -0
- package/lib/typescript/src/components/customDropDown/DropDownList.d.ts.map +1 -0
- package/lib/typescript/src/components/customDropDown/DropDownListItem.d.ts +15 -0
- package/lib/typescript/src/components/customDropDown/DropDownListItem.d.ts.map +1 -0
- package/lib/typescript/src/components/customDropDown/DropDownListModal.d.ts +19 -0
- package/lib/typescript/src/components/customDropDown/DropDownListModal.d.ts.map +1 -0
- package/lib/typescript/src/components/customDropDown/index.d.ts +5 -0
- package/lib/typescript/src/components/customDropDown/index.d.ts.map +1 -0
- package/lib/typescript/src/components/customDropDown/styles.d.ts +190 -0
- package/lib/typescript/src/components/customDropDown/styles.d.ts.map +1 -0
- package/lib/typescript/src/components/customDropDown/types.d.ts +37 -0
- package/lib/typescript/src/components/customDropDown/types.d.ts.map +1 -0
- package/lib/typescript/src/components/customDropDown/utils.d.ts +4 -0
- package/lib/typescript/src/components/customDropDown/utils.d.ts.map +1 -0
- package/lib/typescript/src/components/customList/ListEmpty.d.ts +10 -0
- package/lib/typescript/src/components/customList/ListEmpty.d.ts.map +1 -0
- package/lib/typescript/src/components/customList/ListFooterLoader.d.ts +3 -0
- package/lib/typescript/src/components/customList/ListFooterLoader.d.ts.map +1 -0
- package/lib/typescript/src/components/customList/defaultKeyExtractor.d.ts +2 -0
- package/lib/typescript/src/components/customList/defaultKeyExtractor.d.ts.map +1 -0
- package/lib/typescript/src/components/customList/getFlashList.d.ts +6 -0
- package/lib/typescript/src/components/customList/getFlashList.d.ts.map +1 -0
- package/lib/typescript/src/components/customList/index.d.ts +11 -0
- package/lib/typescript/src/components/customList/index.d.ts.map +1 -0
- package/lib/typescript/src/components/customList/styles.d.ts +42 -0
- package/lib/typescript/src/components/customList/styles.d.ts.map +1 -0
- package/lib/typescript/src/components/customList/types.d.ts +33 -0
- package/lib/typescript/src/components/customList/types.d.ts.map +1 -0
- package/lib/typescript/src/components/customModal/ModalFooter.d.ts +12 -0
- package/lib/typescript/src/components/customModal/ModalFooter.d.ts.map +1 -0
- package/lib/typescript/src/components/customModal/index.d.ts +6 -0
- package/lib/typescript/src/components/customModal/index.d.ts.map +1 -0
- package/lib/typescript/src/components/customModal/styles.d.ts +83 -0
- package/lib/typescript/src/components/customModal/styles.d.ts.map +1 -0
- package/lib/typescript/src/components/customModal/types.d.ts +36 -0
- package/lib/typescript/src/components/customModal/types.d.ts.map +1 -0
- package/lib/typescript/src/components/customModal/useCustomModal.d.ts +8 -0
- package/lib/typescript/src/components/customModal/useCustomModal.d.ts.map +1 -0
- package/lib/typescript/src/components/customNumberPad/NumberPadKey.d.ts +23 -0
- package/lib/typescript/src/components/customNumberPad/NumberPadKey.d.ts.map +1 -0
- package/lib/typescript/src/components/customNumberPad/index.d.ts +7 -0
- package/lib/typescript/src/components/customNumberPad/index.d.ts.map +1 -0
- package/lib/typescript/src/components/customNumberPad/styles.d.ts +37 -0
- package/lib/typescript/src/components/customNumberPad/styles.d.ts.map +1 -0
- package/lib/typescript/src/components/customNumberPad/types.d.ts +47 -0
- package/lib/typescript/src/components/customNumberPad/types.d.ts.map +1 -0
- package/lib/typescript/src/components/customNumberPad/useNumberPadInput.d.ts +16 -0
- package/lib/typescript/src/components/customNumberPad/useNumberPadInput.d.ts.map +1 -0
- package/lib/typescript/src/components/customNumberPad/utils.d.ts +4 -0
- package/lib/typescript/src/components/customNumberPad/utils.d.ts.map +1 -0
- package/lib/typescript/src/components/customSlideTabs/AnimatedTabIndicator.d.ts +16 -0
- package/lib/typescript/src/components/customSlideTabs/AnimatedTabIndicator.d.ts.map +1 -0
- package/lib/typescript/src/components/customSlideTabs/TabBarItem.d.ts +23 -0
- package/lib/typescript/src/components/customSlideTabs/TabBarItem.d.ts.map +1 -0
- package/lib/typescript/src/components/customSlideTabs/TabScene.d.ts +21 -0
- package/lib/typescript/src/components/customSlideTabs/TabScene.d.ts.map +1 -0
- package/lib/typescript/src/components/customSlideTabs/index.d.ts +5 -0
- package/lib/typescript/src/components/customSlideTabs/index.d.ts.map +1 -0
- package/lib/typescript/src/components/customSlideTabs/styles.d.ts +130 -0
- package/lib/typescript/src/components/customSlideTabs/styles.d.ts.map +1 -0
- package/lib/typescript/src/components/customSlideTabs/tabBarScroll.d.ts +15 -0
- package/lib/typescript/src/components/customSlideTabs/tabBarScroll.d.ts.map +1 -0
- package/lib/typescript/src/components/customSlideTabs/types.d.ts +68 -0
- package/lib/typescript/src/components/customSlideTabs/types.d.ts.map +1 -0
- package/lib/typescript/src/components/loading/index.d.ts +7 -0
- package/lib/typescript/src/components/loading/index.d.ts.map +1 -0
- package/lib/typescript/src/customFunctions/array.d.ts +7 -0
- package/lib/typescript/src/customFunctions/array.d.ts.map +1 -0
- package/lib/typescript/src/customFunctions/async.d.ts +9 -0
- package/lib/typescript/src/customFunctions/async.d.ts.map +1 -0
- package/lib/typescript/src/customFunctions/date.d.ts +18 -0
- package/lib/typescript/src/customFunctions/date.d.ts.map +1 -0
- package/lib/typescript/src/customFunctions/general.d.ts +7 -0
- package/lib/typescript/src/customFunctions/general.d.ts.map +1 -0
- package/lib/typescript/src/customFunctions/id.d.ts +4 -0
- package/lib/typescript/src/customFunctions/id.d.ts.map +1 -0
- package/lib/typescript/src/customFunctions/index.d.ts +26 -0
- package/lib/typescript/src/customFunctions/index.d.ts.map +1 -0
- package/lib/typescript/src/customFunctions/number.d.ts +15 -0
- package/lib/typescript/src/customFunctions/number.d.ts.map +1 -0
- package/lib/typescript/src/customFunctions/object.d.ts +6 -0
- package/lib/typescript/src/customFunctions/object.d.ts.map +1 -0
- package/lib/typescript/src/customFunctions/string.d.ts +20 -0
- package/lib/typescript/src/customFunctions/string.d.ts.map +1 -0
- package/lib/typescript/src/customFunctions/types.d.ts +22 -0
- package/lib/typescript/src/customFunctions/types.d.ts.map +1 -0
- package/lib/typescript/src/customFunctions/validation.d.ts +11 -0
- package/lib/typescript/src/customFunctions/validation.d.ts.map +1 -0
- package/lib/typescript/src/customHooks/errors.d.ts +16 -0
- package/lib/typescript/src/customHooks/errors.d.ts.map +1 -0
- package/lib/typescript/src/customHooks/fetchJson.d.ts +5 -0
- package/lib/typescript/src/customHooks/fetchJson.d.ts.map +1 -0
- package/lib/typescript/src/customHooks/index.d.ts +9 -0
- package/lib/typescript/src/customHooks/index.d.ts.map +1 -0
- package/lib/typescript/src/customHooks/internal.d.ts +12 -0
- package/lib/typescript/src/customHooks/internal.d.ts.map +1 -0
- package/lib/typescript/src/customHooks/types.d.ts +49 -0
- package/lib/typescript/src/customHooks/types.d.ts.map +1 -0
- package/lib/typescript/src/customHooks/useApi.d.ts +13 -0
- package/lib/typescript/src/customHooks/useApi.d.ts.map +1 -0
- package/lib/typescript/src/customHooks/useLazyApi.d.ts +14 -0
- package/lib/typescript/src/customHooks/useLazyApi.d.ts.map +1 -0
- package/lib/typescript/src/customHooks/useMutation.d.ts +15 -0
- package/lib/typescript/src/customHooks/useMutation.d.ts.map +1 -0
- package/lib/typescript/src/customHooks/usePaginatedApi.d.ts +19 -0
- package/lib/typescript/src/customHooks/usePaginatedApi.d.ts.map +1 -0
- package/lib/typescript/src/customHooks/usePollingApi.d.ts +13 -0
- package/lib/typescript/src/customHooks/usePollingApi.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +11 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/lib/typescript/src/multiply.d.ts +2 -0
- package/lib/typescript/src/multiply.d.ts.map +1 -0
- package/package.json +181 -0
- package/src/assets/images/close.png +0 -0
- package/src/components/CustomCheckbox/index.tsx +143 -0
- package/src/components/CustomCheckbox/styles.ts +146 -0
- package/src/components/CustomCheckbox/types.ts +33 -0
- package/src/components/closeButton/index.tsx +40 -0
- package/src/components/closeButton/types.ts +13 -0
- package/src/components/customButton/index.tsx +288 -0
- package/src/components/customButton/styles.ts +247 -0
- package/src/components/customButton/types.ts +71 -0
- package/src/components/customDropDown/DropDownBottomSheet.tsx +70 -0
- package/src/components/customDropDown/DropDownList.tsx +88 -0
- package/src/components/customDropDown/DropDownListItem.tsx +74 -0
- package/src/components/customDropDown/DropDownListModal.tsx +86 -0
- package/src/components/customDropDown/index.tsx +213 -0
- package/src/components/customDropDown/styles.ts +184 -0
- package/src/components/customDropDown/types.ts +48 -0
- package/src/components/customDropDown/utils.ts +24 -0
- package/src/components/customList/ListEmpty.tsx +36 -0
- package/src/components/customList/ListFooterLoader.tsx +11 -0
- package/src/components/customList/defaultKeyExtractor.ts +7 -0
- package/src/components/customList/getFlashList.ts +26 -0
- package/src/components/customList/index.tsx +142 -0
- package/src/components/customList/styles.ts +43 -0
- package/src/components/customList/types.ts +60 -0
- package/src/components/customModal/ModalFooter.tsx +57 -0
- package/src/components/customModal/index.tsx +166 -0
- package/src/components/customModal/styles.ts +131 -0
- package/src/components/customModal/types.ts +48 -0
- package/src/components/customModal/useCustomModal.ts +25 -0
- package/src/components/customNumberPad/NumberPadKey.tsx +81 -0
- package/src/components/customNumberPad/index.tsx +204 -0
- package/src/components/customNumberPad/styles.ts +146 -0
- package/src/components/customNumberPad/types.ts +53 -0
- package/src/components/customNumberPad/useNumberPadInput.ts +56 -0
- package/src/components/customNumberPad/utils.ts +54 -0
- package/src/components/customSlideTabs/AnimatedTabIndicator.tsx +103 -0
- package/src/components/customSlideTabs/TabBarItem.tsx +118 -0
- package/src/components/customSlideTabs/TabScene.tsx +136 -0
- package/src/components/customSlideTabs/index.tsx +385 -0
- package/src/components/customSlideTabs/styles.ts +136 -0
- package/src/components/customSlideTabs/tabBarScroll.ts +92 -0
- package/src/components/customSlideTabs/types.ts +87 -0
- package/src/components/loading/index.tsx +34 -0
- package/src/customFunctions/array.ts +83 -0
- package/src/customFunctions/async.ts +85 -0
- package/src/customFunctions/date.ts +158 -0
- package/src/customFunctions/general.ts +35 -0
- package/src/customFunctions/id.ts +25 -0
- package/src/customFunctions/index.ts +91 -0
- package/src/customFunctions/number.ts +139 -0
- package/src/customFunctions/object.ts +59 -0
- package/src/customFunctions/string.ts +119 -0
- package/src/customFunctions/types.ts +25 -0
- package/src/customFunctions/validation.ts +49 -0
- package/src/customHooks/errors.ts +87 -0
- package/src/customHooks/fetchJson.ts +49 -0
- package/src/customHooks/index.ts +20 -0
- package/src/customHooks/internal.ts +67 -0
- package/src/customHooks/types.ts +71 -0
- package/src/customHooks/useApi.ts +107 -0
- package/src/customHooks/useLazyApi.ts +92 -0
- package/src/customHooks/useMutation.ts +97 -0
- package/src/customHooks/usePaginatedApi.ts +168 -0
- package/src/customHooks/usePollingApi.ts +48 -0
- package/src/index.tsx +63 -0
- package/src/multiply.tsx +3 -0
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
export const capitalizeFirst = (value: string): string => {
|
|
2
|
+
if (!value) {
|
|
3
|
+
return '';
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
return value.charAt(0).toUpperCase() + value.slice(1);
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export const getLocalizedSplitText = (
|
|
10
|
+
text: string | undefined,
|
|
11
|
+
language: string,
|
|
12
|
+
options?: { englishCode?: string; delimiter?: string }
|
|
13
|
+
): string => {
|
|
14
|
+
if (typeof text !== 'string' || !text) {
|
|
15
|
+
return '';
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const delimiter = options?.delimiter ?? '||';
|
|
19
|
+
const englishCode = options?.englishCode ?? 'en';
|
|
20
|
+
const parts = text.split(delimiter);
|
|
21
|
+
|
|
22
|
+
if (language.startsWith('fr')) {
|
|
23
|
+
return parts[1]?.trim() ?? parts[0]?.trim() ?? '';
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
if (language.startsWith(englishCode)) {
|
|
27
|
+
return parts[0]?.trim() ?? '';
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return parts[0]?.trim() ?? '';
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export const removeDuplicates = <T>(items: T[]): T[] =>
|
|
34
|
+
Array.from(new Set(items));
|
|
35
|
+
|
|
36
|
+
export const removeDuplicateStrings = (items: string[]): string[] =>
|
|
37
|
+
removeDuplicates(items);
|
|
38
|
+
|
|
39
|
+
export const findItemByProperty = <T extends Record<string, unknown>>(
|
|
40
|
+
items: T[] | null | undefined,
|
|
41
|
+
property: keyof T,
|
|
42
|
+
value: unknown
|
|
43
|
+
): T | undefined => {
|
|
44
|
+
if (!items?.length) {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return items.find((item) => item[property] === value);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export const truncate = (
|
|
52
|
+
value: string,
|
|
53
|
+
maxLength: number,
|
|
54
|
+
suffix = '...'
|
|
55
|
+
): string => {
|
|
56
|
+
if (!value || value.length <= maxLength) {
|
|
57
|
+
return value;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return `${value.slice(0, Math.max(0, maxLength - suffix.length))}${suffix}`;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export const capitalizeWords = (value: string): string =>
|
|
64
|
+
value
|
|
65
|
+
.trim()
|
|
66
|
+
.split(/\s+/)
|
|
67
|
+
.filter(Boolean)
|
|
68
|
+
.map((word) => capitalizeFirst(word.toLowerCase()))
|
|
69
|
+
.join(' ');
|
|
70
|
+
|
|
71
|
+
export const slugify = (value: string): string =>
|
|
72
|
+
value
|
|
73
|
+
.trim()
|
|
74
|
+
.toLowerCase()
|
|
75
|
+
.replace(/[^a-z0-9\s-]/g, '')
|
|
76
|
+
.replace(/\s+/g, '-')
|
|
77
|
+
.replace(/-+/g, '-');
|
|
78
|
+
|
|
79
|
+
export const maskString = (
|
|
80
|
+
value: string,
|
|
81
|
+
options: {
|
|
82
|
+
visibleStart?: number;
|
|
83
|
+
visibleEnd?: number;
|
|
84
|
+
maskChar?: string;
|
|
85
|
+
} = {}
|
|
86
|
+
): string => {
|
|
87
|
+
const { visibleStart = 0, visibleEnd = 4, maskChar = '*' } = options;
|
|
88
|
+
|
|
89
|
+
if (!value) {
|
|
90
|
+
return '';
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (value.length <= visibleStart + visibleEnd) {
|
|
94
|
+
return maskChar.repeat(value.length);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const start = value.slice(0, visibleStart);
|
|
98
|
+
const end = value.slice(-visibleEnd);
|
|
99
|
+
const masked = maskChar.repeat(value.length - visibleStart - visibleEnd);
|
|
100
|
+
|
|
101
|
+
return `${start}${masked}${end}`;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
export const normalizeWhitespace = (value: string): string =>
|
|
105
|
+
value.trim().replace(/\s+/g, ' ');
|
|
106
|
+
|
|
107
|
+
export const initialsFromName = (fullName: string, maxInitials = 2): string => {
|
|
108
|
+
const parts = normalizeWhitespace(fullName).split(' ').filter(Boolean);
|
|
109
|
+
|
|
110
|
+
return parts
|
|
111
|
+
.slice(0, maxInitials)
|
|
112
|
+
.map((part) => part.charAt(0).toUpperCase())
|
|
113
|
+
.join('');
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
export const camelToTitle = (value: string): string =>
|
|
117
|
+
capitalizeWords(
|
|
118
|
+
value.replace(/([a-z])([A-Z])/g, '$1 $2').replace(/[-_]/g, ' ')
|
|
119
|
+
);
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export type CurrencyLocale = 'en-US' | 'en-CA' | 'fr-CA' | 'fr-FR' | string;
|
|
2
|
+
|
|
3
|
+
export type FormatCurrencyOptions = {
|
|
4
|
+
locale?: CurrencyLocale;
|
|
5
|
+
currency?: string;
|
|
6
|
+
isDiscount?: boolean;
|
|
7
|
+
minimumFractionDigits?: number;
|
|
8
|
+
maximumFractionDigits?: number;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type FormatNumberOptions = {
|
|
12
|
+
locale?: string;
|
|
13
|
+
minimumFractionDigits?: number;
|
|
14
|
+
maximumFractionDigits?: number;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export type LocalizedDateTimeOptions = {
|
|
18
|
+
englishLocale?: string;
|
|
19
|
+
frenchLocale?: string;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export type LoggerPayload = {
|
|
23
|
+
msg?: string;
|
|
24
|
+
data?: unknown;
|
|
25
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
const EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/;
|
|
2
|
+
const URL_REGEX =
|
|
3
|
+
/^(https?:\/\/)?([\w-]+\.)+[\w-]{2,}(\/[\w\-._~:/?#[\]@!$&'()*+,;=]*)?$/i;
|
|
4
|
+
const PHONE_REGEX = /^\+?[\d\s().-]{7,20}$/;
|
|
5
|
+
const NUMERIC_REGEX = /^-?\d+(\.\d+)?$/;
|
|
6
|
+
|
|
7
|
+
export const isValidEmail = (email: string): boolean => EMAIL_REGEX.test(email);
|
|
8
|
+
|
|
9
|
+
export const isValidUrl = (value: string): boolean =>
|
|
10
|
+
URL_REGEX.test(value.trim());
|
|
11
|
+
|
|
12
|
+
export const isValidPhone = (value: string): boolean =>
|
|
13
|
+
PHONE_REGEX.test(value.trim());
|
|
14
|
+
|
|
15
|
+
export const isNumeric = (value: string | number): boolean =>
|
|
16
|
+
NUMERIC_REGEX.test(String(value).trim());
|
|
17
|
+
|
|
18
|
+
export const isStrongPassword = (
|
|
19
|
+
password: string,
|
|
20
|
+
options: {
|
|
21
|
+
minLength?: number;
|
|
22
|
+
requireNumber?: boolean;
|
|
23
|
+
requireLetter?: boolean;
|
|
24
|
+
} = {}
|
|
25
|
+
): boolean => {
|
|
26
|
+
const { minLength = 8, requireNumber = true, requireLetter = true } = options;
|
|
27
|
+
|
|
28
|
+
if (password.length < minLength) {
|
|
29
|
+
return false;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (requireNumber && !/\d/.test(password)) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (requireLetter && !/[a-zA-Z]/.test(password)) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return true;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const encodePlusInEmail = (email: string): string => {
|
|
44
|
+
if (!email.includes('+')) {
|
|
45
|
+
return email;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return email.replace(/\+/g, '%2B');
|
|
49
|
+
};
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { ApiErrorLike } from './types';
|
|
2
|
+
|
|
3
|
+
export class ApiError extends Error implements ApiErrorLike {
|
|
4
|
+
status?: number;
|
|
5
|
+
code?: string;
|
|
6
|
+
details?: unknown;
|
|
7
|
+
|
|
8
|
+
constructor(
|
|
9
|
+
message: string,
|
|
10
|
+
options: {
|
|
11
|
+
status?: number;
|
|
12
|
+
code?: string;
|
|
13
|
+
details?: unknown;
|
|
14
|
+
cause?: unknown;
|
|
15
|
+
} = {}
|
|
16
|
+
) {
|
|
17
|
+
super(message, options.cause ? { cause: options.cause } : undefined);
|
|
18
|
+
this.name = 'ApiError';
|
|
19
|
+
this.status = options.status;
|
|
20
|
+
this.code = options.code;
|
|
21
|
+
this.details = options.details;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export const isApiError = (error: unknown): error is ApiError =>
|
|
26
|
+
error instanceof ApiError;
|
|
27
|
+
|
|
28
|
+
export const toApiError = (error: unknown): ApiError => {
|
|
29
|
+
if (isApiError(error)) {
|
|
30
|
+
return error;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (error instanceof Error) {
|
|
34
|
+
const maybeStatus = (error as Error & { status?: number }).status;
|
|
35
|
+
const maybeCode = (error as Error & { code?: string }).code;
|
|
36
|
+
const maybeDetails = (error as Error & { details?: unknown }).details;
|
|
37
|
+
|
|
38
|
+
return new ApiError(error.message, {
|
|
39
|
+
status: maybeStatus,
|
|
40
|
+
code: maybeCode,
|
|
41
|
+
details: maybeDetails ?? error,
|
|
42
|
+
cause: error,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (typeof error === 'string') {
|
|
47
|
+
return new ApiError(error);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (error && typeof error === 'object') {
|
|
51
|
+
const record = error as Record<string, unknown>;
|
|
52
|
+
const message = String(
|
|
53
|
+
record.message ?? record.error ?? record.statusText ?? 'Unknown error'
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
return new ApiError(message, {
|
|
57
|
+
status: typeof record.status === 'number' ? record.status : undefined,
|
|
58
|
+
code: typeof record.code === 'string' ? record.code : undefined,
|
|
59
|
+
details: error,
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return new ApiError('Unknown error', { details: error });
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
export const getApiErrorMessage = (
|
|
67
|
+
error: unknown,
|
|
68
|
+
fallback = 'Something went wrong'
|
|
69
|
+
): string => {
|
|
70
|
+
if (!error) {
|
|
71
|
+
return fallback;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
if (typeof error === 'string') {
|
|
75
|
+
return error;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
if (error instanceof Error) {
|
|
79
|
+
return error.message || fallback;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (typeof error === 'object' && error !== null && 'message' in error) {
|
|
83
|
+
return String((error as { message?: unknown }).message ?? fallback);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return fallback;
|
|
87
|
+
};
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { ApiError } from './errors';
|
|
2
|
+
|
|
3
|
+
export type FetchJsonOptions = RequestInit & {
|
|
4
|
+
parseJson?: boolean;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export const fetchJson = async <TResponse>(
|
|
8
|
+
input: RequestInfo | URL,
|
|
9
|
+
options: FetchJsonOptions = {}
|
|
10
|
+
): Promise<TResponse> => {
|
|
11
|
+
const { parseJson = true, headers, ...init } = options;
|
|
12
|
+
|
|
13
|
+
const response = await fetch(input, {
|
|
14
|
+
...init,
|
|
15
|
+
headers: {
|
|
16
|
+
'Accept': 'application/json',
|
|
17
|
+
'Content-Type': 'application/json',
|
|
18
|
+
...headers,
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
const rawText = await response.text();
|
|
23
|
+
let payload: unknown = rawText;
|
|
24
|
+
|
|
25
|
+
if (parseJson && rawText) {
|
|
26
|
+
try {
|
|
27
|
+
payload = JSON.parse(rawText);
|
|
28
|
+
} catch {
|
|
29
|
+
payload = rawText;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (!response.ok) {
|
|
34
|
+
const message =
|
|
35
|
+
typeof payload === 'object' &&
|
|
36
|
+
payload !== null &&
|
|
37
|
+
'message' in payload &&
|
|
38
|
+
typeof (payload as { message?: unknown }).message === 'string'
|
|
39
|
+
? (payload as { message: string }).message
|
|
40
|
+
: response.statusText || 'Request failed';
|
|
41
|
+
|
|
42
|
+
throw new ApiError(message, {
|
|
43
|
+
status: response.status,
|
|
44
|
+
details: payload,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
return payload as TResponse;
|
|
49
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
export { ApiError, getApiErrorMessage, isApiError, toApiError } from './errors';
|
|
2
|
+
export { fetchJson, type FetchJsonOptions } from './fetchJson';
|
|
3
|
+
export { useApi } from './useApi';
|
|
4
|
+
export { useLazyApi } from './useLazyApi';
|
|
5
|
+
export { useMutation } from './useMutation';
|
|
6
|
+
export { usePaginatedApi } from './usePaginatedApi';
|
|
7
|
+
export { usePollingApi } from './usePollingApi';
|
|
8
|
+
export type {
|
|
9
|
+
ApiErrorLike,
|
|
10
|
+
ApiFetcher,
|
|
11
|
+
ApiStatus,
|
|
12
|
+
ApiStatusFlags,
|
|
13
|
+
PaginatedFetcher,
|
|
14
|
+
PaginatedResponse,
|
|
15
|
+
UseApiOptions,
|
|
16
|
+
UseLazyApiOptions,
|
|
17
|
+
UseMutationOptions,
|
|
18
|
+
UsePaginatedApiOptions,
|
|
19
|
+
UsePollingApiOptions,
|
|
20
|
+
} from './types';
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { useEffect, useRef } from 'react';
|
|
2
|
+
|
|
3
|
+
import { toApiError } from './errors';
|
|
4
|
+
import type { ApiStatus, ApiStatusFlags } from './types';
|
|
5
|
+
|
|
6
|
+
export const useDependencyKey = (deps: ReadonlyArray<unknown>) =>
|
|
7
|
+
JSON.stringify(deps);
|
|
8
|
+
|
|
9
|
+
export const getStatusFlags = (status: ApiStatus): ApiStatusFlags => ({
|
|
10
|
+
status,
|
|
11
|
+
isIdle: status === 'idle',
|
|
12
|
+
isLoading: status === 'loading',
|
|
13
|
+
isSuccess: status === 'success',
|
|
14
|
+
isError: status === 'error',
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
export const useIsMounted = () => {
|
|
18
|
+
const isMountedRef = useRef(true);
|
|
19
|
+
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
isMountedRef.current = true;
|
|
22
|
+
|
|
23
|
+
return () => {
|
|
24
|
+
isMountedRef.current = false;
|
|
25
|
+
};
|
|
26
|
+
}, []);
|
|
27
|
+
|
|
28
|
+
return isMountedRef;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
export const useAbortControllerRef = () => {
|
|
32
|
+
const abortRef = useRef<AbortController | null>(null);
|
|
33
|
+
|
|
34
|
+
const abort = () => {
|
|
35
|
+
abortRef.current?.abort();
|
|
36
|
+
abortRef.current = null;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const create = () => {
|
|
40
|
+
abort();
|
|
41
|
+
const controller = new AbortController();
|
|
42
|
+
abortRef.current = controller;
|
|
43
|
+
return controller;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
useEffect(() => abort, []);
|
|
47
|
+
|
|
48
|
+
return { abortRef, abort, create };
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export const normalizeApiError = <TError>(error: unknown): TError =>
|
|
52
|
+
toApiError(error) as TError;
|
|
53
|
+
|
|
54
|
+
export const isAbortError = (error: unknown) => {
|
|
55
|
+
if (
|
|
56
|
+
typeof DOMException !== 'undefined' &&
|
|
57
|
+
error instanceof DOMException &&
|
|
58
|
+
error.name === 'AbortError'
|
|
59
|
+
) {
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return (
|
|
64
|
+
error instanceof Error &&
|
|
65
|
+
(error.name === 'AbortError' || error.message === 'Aborted')
|
|
66
|
+
);
|
|
67
|
+
};
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
export type ApiStatus = 'idle' | 'loading' | 'success' | 'error';
|
|
2
|
+
|
|
3
|
+
export type ApiStatusFlags = {
|
|
4
|
+
status: ApiStatus;
|
|
5
|
+
isIdle: boolean;
|
|
6
|
+
isLoading: boolean;
|
|
7
|
+
isSuccess: boolean;
|
|
8
|
+
isError: boolean;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type ApiFetcher<TData> = (signal?: AbortSignal) => Promise<TData>;
|
|
12
|
+
|
|
13
|
+
export type ApiErrorLike = {
|
|
14
|
+
message: string;
|
|
15
|
+
status?: number;
|
|
16
|
+
code?: string;
|
|
17
|
+
details?: unknown;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export type UseApiOptions<TData, TError = ApiErrorLike> = {
|
|
21
|
+
enabled?: boolean;
|
|
22
|
+
initialData?: TData | null;
|
|
23
|
+
deps?: ReadonlyArray<unknown>;
|
|
24
|
+
keepPreviousData?: boolean;
|
|
25
|
+
onSuccess?: (data: TData) => void;
|
|
26
|
+
onError?: (error: TError) => void;
|
|
27
|
+
onSettled?: (data: TData | null, error: TError | null) => void;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export type UseLazyApiOptions<TData, TError = ApiErrorLike> = Omit<
|
|
31
|
+
UseApiOptions<TData, TError>,
|
|
32
|
+
'enabled'
|
|
33
|
+
>;
|
|
34
|
+
|
|
35
|
+
export type UseMutationOptions<TData, TVariables, TError = ApiErrorLike> = {
|
|
36
|
+
onSuccess?: (data: TData, variables: TVariables) => void;
|
|
37
|
+
onError?: (error: TError, variables: TVariables) => void;
|
|
38
|
+
onSettled?: (
|
|
39
|
+
data: TData | null,
|
|
40
|
+
error: TError | null,
|
|
41
|
+
variables: TVariables
|
|
42
|
+
) => void;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
export type PaginatedResponse<TItem> = {
|
|
46
|
+
items: TItem[];
|
|
47
|
+
hasMore: boolean;
|
|
48
|
+
total?: number;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export type PaginatedFetcher<TItem> = (
|
|
52
|
+
page: number,
|
|
53
|
+
signal?: AbortSignal
|
|
54
|
+
) => Promise<PaginatedResponse<TItem>>;
|
|
55
|
+
|
|
56
|
+
export type UsePaginatedApiOptions<TItem, TError = ApiErrorLike> = {
|
|
57
|
+
enabled?: boolean;
|
|
58
|
+
initialPage?: number;
|
|
59
|
+
deps?: ReadonlyArray<unknown>;
|
|
60
|
+
onSuccess?: (response: PaginatedResponse<TItem>, page: number) => void;
|
|
61
|
+
onError?: (error: TError, page: number) => void;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export type UsePollingApiOptions<TData, TError = ApiErrorLike> = UseApiOptions<
|
|
65
|
+
TData,
|
|
66
|
+
TError
|
|
67
|
+
> & {
|
|
68
|
+
intervalMs: number;
|
|
69
|
+
pollWhenHidden?: boolean;
|
|
70
|
+
pauseOnError?: boolean;
|
|
71
|
+
};
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
getStatusFlags,
|
|
5
|
+
isAbortError,
|
|
6
|
+
normalizeApiError,
|
|
7
|
+
useAbortControllerRef,
|
|
8
|
+
useDependencyKey,
|
|
9
|
+
useIsMounted,
|
|
10
|
+
} from './internal';
|
|
11
|
+
import type { ApiFetcher, ApiStatus, UseApiOptions } from './types';
|
|
12
|
+
|
|
13
|
+
export const useApi = <TData, TError = unknown>(
|
|
14
|
+
fetcher: ApiFetcher<TData>,
|
|
15
|
+
options: UseApiOptions<TData, TError> = {}
|
|
16
|
+
) => {
|
|
17
|
+
const {
|
|
18
|
+
enabled = true,
|
|
19
|
+
initialData = null,
|
|
20
|
+
deps = [],
|
|
21
|
+
keepPreviousData = false,
|
|
22
|
+
onSuccess,
|
|
23
|
+
onError,
|
|
24
|
+
onSettled,
|
|
25
|
+
} = options;
|
|
26
|
+
|
|
27
|
+
const [data, setData] = useState<TData | null>(initialData);
|
|
28
|
+
const [error, setError] = useState<TError | null>(null);
|
|
29
|
+
const [status, setStatus] = useState<ApiStatus>(enabled ? 'loading' : 'idle');
|
|
30
|
+
|
|
31
|
+
const fetcherRef = useRef(fetcher);
|
|
32
|
+
const callbacksRef = useRef({ onSuccess, onError, onSettled });
|
|
33
|
+
const isMountedRef = useIsMounted();
|
|
34
|
+
const { abort, create } = useAbortControllerRef();
|
|
35
|
+
const depsKey = useDependencyKey(deps);
|
|
36
|
+
|
|
37
|
+
fetcherRef.current = fetcher;
|
|
38
|
+
callbacksRef.current = { onSuccess, onError, onSettled };
|
|
39
|
+
|
|
40
|
+
const execute = useCallback(async () => {
|
|
41
|
+
const controller = create();
|
|
42
|
+
|
|
43
|
+
setStatus('loading');
|
|
44
|
+
setError(null);
|
|
45
|
+
|
|
46
|
+
if (!keepPreviousData) {
|
|
47
|
+
setData(initialData);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
try {
|
|
51
|
+
const result = await fetcherRef.current(controller.signal);
|
|
52
|
+
|
|
53
|
+
if (controller.signal.aborted || !isMountedRef.current) {
|
|
54
|
+
return result;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
setData(result);
|
|
58
|
+
setStatus('success');
|
|
59
|
+
callbacksRef.current.onSuccess?.(result);
|
|
60
|
+
callbacksRef.current.onSettled?.(result, null);
|
|
61
|
+
return result;
|
|
62
|
+
} catch (unknownError) {
|
|
63
|
+
if (isAbortError(unknownError)) {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const parsedError = normalizeApiError<TError>(unknownError);
|
|
68
|
+
|
|
69
|
+
if (!isMountedRef.current) {
|
|
70
|
+
throw parsedError;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
setError(parsedError);
|
|
74
|
+
setStatus('error');
|
|
75
|
+
callbacksRef.current.onError?.(parsedError);
|
|
76
|
+
callbacksRef.current.onSettled?.(null, parsedError);
|
|
77
|
+
throw parsedError;
|
|
78
|
+
}
|
|
79
|
+
}, [create, initialData, isMountedRef, keepPreviousData]);
|
|
80
|
+
|
|
81
|
+
const refetch = useCallback(async () => execute(), [execute]);
|
|
82
|
+
|
|
83
|
+
const reset = useCallback(() => {
|
|
84
|
+
abort();
|
|
85
|
+
setData(initialData);
|
|
86
|
+
setError(null);
|
|
87
|
+
setStatus('idle');
|
|
88
|
+
}, [abort, initialData]);
|
|
89
|
+
|
|
90
|
+
useEffect(() => {
|
|
91
|
+
if (!enabled) {
|
|
92
|
+
abort();
|
|
93
|
+
setStatus('idle');
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
execute().catch(() => undefined);
|
|
98
|
+
}, [abort, depsKey, enabled, execute]);
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
data,
|
|
102
|
+
error,
|
|
103
|
+
...getStatusFlags(status),
|
|
104
|
+
refetch,
|
|
105
|
+
reset,
|
|
106
|
+
};
|
|
107
|
+
};
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { useCallback, useRef, useState } from 'react';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
getStatusFlags,
|
|
5
|
+
isAbortError,
|
|
6
|
+
normalizeApiError,
|
|
7
|
+
useAbortControllerRef,
|
|
8
|
+
useIsMounted,
|
|
9
|
+
} from './internal';
|
|
10
|
+
import type { ApiFetcher, ApiStatus, UseLazyApiOptions } from './types';
|
|
11
|
+
|
|
12
|
+
export const useLazyApi = <TData, TError = unknown>(
|
|
13
|
+
fetcher: ApiFetcher<TData>,
|
|
14
|
+
options: UseLazyApiOptions<TData, TError> = {}
|
|
15
|
+
) => {
|
|
16
|
+
const {
|
|
17
|
+
initialData = null,
|
|
18
|
+
keepPreviousData = false,
|
|
19
|
+
onSuccess,
|
|
20
|
+
onError,
|
|
21
|
+
onSettled,
|
|
22
|
+
} = options;
|
|
23
|
+
|
|
24
|
+
const [data, setData] = useState<TData | null>(initialData);
|
|
25
|
+
const [error, setError] = useState<TError | null>(null);
|
|
26
|
+
const [status, setStatus] = useState<ApiStatus>('idle');
|
|
27
|
+
|
|
28
|
+
const fetcherRef = useRef(fetcher);
|
|
29
|
+
const callbacksRef = useRef({ onSuccess, onError, onSettled });
|
|
30
|
+
const isMountedRef = useIsMounted();
|
|
31
|
+
const { abort, create } = useAbortControllerRef();
|
|
32
|
+
|
|
33
|
+
fetcherRef.current = fetcher;
|
|
34
|
+
callbacksRef.current = { onSuccess, onError, onSettled };
|
|
35
|
+
|
|
36
|
+
const execute = useCallback(async () => {
|
|
37
|
+
const controller = create();
|
|
38
|
+
|
|
39
|
+
setStatus('loading');
|
|
40
|
+
setError(null);
|
|
41
|
+
|
|
42
|
+
if (!keepPreviousData) {
|
|
43
|
+
setData(initialData);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
try {
|
|
47
|
+
const result = await fetcherRef.current(controller.signal);
|
|
48
|
+
|
|
49
|
+
if (controller.signal.aborted || !isMountedRef.current) {
|
|
50
|
+
return result;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
setData(result);
|
|
54
|
+
setStatus('success');
|
|
55
|
+
callbacksRef.current.onSuccess?.(result);
|
|
56
|
+
callbacksRef.current.onSettled?.(result, null);
|
|
57
|
+
return result;
|
|
58
|
+
} catch (unknownError) {
|
|
59
|
+
if (isAbortError(unknownError)) {
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const parsedError = normalizeApiError<TError>(unknownError);
|
|
64
|
+
|
|
65
|
+
if (!isMountedRef.current) {
|
|
66
|
+
throw parsedError;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
setError(parsedError);
|
|
70
|
+
setStatus('error');
|
|
71
|
+
callbacksRef.current.onError?.(parsedError);
|
|
72
|
+
callbacksRef.current.onSettled?.(null, parsedError);
|
|
73
|
+
throw parsedError;
|
|
74
|
+
}
|
|
75
|
+
}, [create, initialData, isMountedRef, keepPreviousData]);
|
|
76
|
+
|
|
77
|
+
const reset = useCallback(() => {
|
|
78
|
+
abort();
|
|
79
|
+
setData(initialData);
|
|
80
|
+
setError(null);
|
|
81
|
+
setStatus('idle');
|
|
82
|
+
}, [abort, initialData]);
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
data,
|
|
86
|
+
error,
|
|
87
|
+
...getStatusFlags(status),
|
|
88
|
+
execute,
|
|
89
|
+
refetch: execute,
|
|
90
|
+
reset,
|
|
91
|
+
};
|
|
92
|
+
};
|