@intlayer/design-system 5.8.1 → 6.0.0-canary.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/dist/.vite/manifest.json +34 -106
- package/dist/better-auth.CMQ3rA-I-7umXOENE.js.map +1 -1
- package/dist/better-auth.CMQ3rA-I-C2nBKRMa.cjs.map +1 -1
- package/dist/components/Breadcrumb/breadcrumb.content.cjs +3 -1
- package/dist/components/Breadcrumb/breadcrumb.content.cjs.map +1 -1
- package/dist/components/Breadcrumb/breadcrumb.content.d.ts +3 -0
- package/dist/components/Breadcrumb/breadcrumb.content.d.ts.map +1 -1
- package/dist/components/Breadcrumb/breadcrumb.content.mjs +3 -1
- package/dist/components/Breadcrumb/breadcrumb.content.mjs.map +1 -1
- package/dist/components/Breadcrumb/index.cjs.map +1 -1
- package/dist/components/Breadcrumb/index.d.ts.map +1 -1
- package/dist/components/Breadcrumb/index.mjs.map +1 -1
- package/dist/components/CopyButton/CopyButton.content.cjs +3 -1
- package/dist/components/CopyButton/CopyButton.content.cjs.map +1 -1
- package/dist/components/CopyButton/CopyButton.content.d.ts +3 -0
- package/dist/components/CopyButton/CopyButton.content.d.ts.map +1 -1
- package/dist/components/CopyButton/CopyButton.content.mjs +3 -1
- package/dist/components/CopyButton/CopyButton.content.mjs.map +1 -1
- package/dist/components/DictionaryEditor/DictionaryEditor.cjs +7 -3
- package/dist/components/DictionaryEditor/DictionaryEditor.cjs.map +1 -1
- package/dist/components/DictionaryEditor/DictionaryEditor.d.ts.map +1 -1
- package/dist/components/DictionaryEditor/DictionaryEditor.mjs +7 -3
- package/dist/components/DictionaryEditor/DictionaryEditor.mjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/ContentEditor.cjs +1 -1
- package/dist/components/DictionaryFieldEditor/ContentEditor.cjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/ContentEditor.mjs +1 -1
- package/dist/components/DictionaryFieldEditor/ContentEditor.mjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/ContentEditorView/TextEditor.cjs +83 -34
- package/dist/components/DictionaryFieldEditor/ContentEditorView/TextEditor.cjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/ContentEditorView/TextEditor.d.ts.map +1 -1
- package/dist/components/DictionaryFieldEditor/ContentEditorView/TextEditor.mjs +83 -34
- package/dist/components/DictionaryFieldEditor/ContentEditorView/TextEditor.mjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/DictionaryCreationForm/DictionaryCreationForm.cjs +13 -9
- package/dist/components/DictionaryFieldEditor/DictionaryCreationForm/DictionaryCreationForm.cjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/DictionaryCreationForm/DictionaryCreationForm.d.ts.map +1 -1
- package/dist/components/DictionaryFieldEditor/DictionaryCreationForm/DictionaryCreationForm.mjs +12 -8
- package/dist/components/DictionaryFieldEditor/DictionaryCreationForm/DictionaryCreationForm.mjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/DictionaryCreationForm/dictionaryCreationForm.content.cjs +27 -13
- package/dist/components/DictionaryFieldEditor/DictionaryCreationForm/dictionaryCreationForm.content.cjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/DictionaryCreationForm/dictionaryCreationForm.content.d.ts +27 -0
- package/dist/components/DictionaryFieldEditor/DictionaryCreationForm/dictionaryCreationForm.content.d.ts.map +1 -1
- package/dist/components/DictionaryFieldEditor/DictionaryCreationForm/dictionaryCreationForm.content.mjs +27 -13
- package/dist/components/DictionaryFieldEditor/DictionaryCreationForm/dictionaryCreationForm.content.mjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/DictionaryCreationForm/useDictionaryFormSchema.content.cjs +9 -4
- package/dist/components/DictionaryFieldEditor/DictionaryCreationForm/useDictionaryFormSchema.content.cjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/DictionaryCreationForm/useDictionaryFormSchema.content.d.ts +9 -0
- package/dist/components/DictionaryFieldEditor/DictionaryCreationForm/useDictionaryFormSchema.content.d.ts.map +1 -1
- package/dist/components/DictionaryFieldEditor/DictionaryCreationForm/useDictionaryFormSchema.content.mjs +9 -4
- package/dist/components/DictionaryFieldEditor/DictionaryCreationForm/useDictionaryFormSchema.content.mjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/DictionaryDetails/DictionaryDetailsForm.cjs +29 -25
- package/dist/components/DictionaryFieldEditor/DictionaryDetails/DictionaryDetailsForm.cjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/DictionaryDetails/DictionaryDetailsForm.d.ts.map +1 -1
- package/dist/components/DictionaryFieldEditor/DictionaryDetails/DictionaryDetailsForm.mjs +27 -23
- package/dist/components/DictionaryFieldEditor/DictionaryDetails/DictionaryDetailsForm.mjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/DictionaryDetails/dictionaryDetails.content.cjs +37 -17
- package/dist/components/DictionaryFieldEditor/DictionaryDetails/dictionaryDetails.content.cjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/DictionaryDetails/dictionaryDetails.content.d.ts +64 -2
- package/dist/components/DictionaryFieldEditor/DictionaryDetails/dictionaryDetails.content.d.ts.map +1 -1
- package/dist/components/DictionaryFieldEditor/DictionaryDetails/dictionaryDetails.content.mjs +37 -17
- package/dist/components/DictionaryFieldEditor/DictionaryDetails/dictionaryDetails.content.mjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/DictionaryDetails/useDictionaryDetailsSchema.content.cjs +25 -12
- package/dist/components/DictionaryFieldEditor/DictionaryDetails/useDictionaryDetailsSchema.content.cjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/DictionaryDetails/useDictionaryDetailsSchema.content.d.ts +25 -0
- package/dist/components/DictionaryFieldEditor/DictionaryDetails/useDictionaryDetailsSchema.content.d.ts.map +1 -1
- package/dist/components/DictionaryFieldEditor/DictionaryDetails/useDictionaryDetailsSchema.content.mjs +25 -12
- package/dist/components/DictionaryFieldEditor/DictionaryDetails/useDictionaryDetailsSchema.content.mjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/DictionaryFieldEditor.cjs +4 -5
- package/dist/components/DictionaryFieldEditor/DictionaryFieldEditor.cjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/DictionaryFieldEditor.d.ts.map +1 -1
- package/dist/components/DictionaryFieldEditor/DictionaryFieldEditor.mjs +4 -5
- package/dist/components/DictionaryFieldEditor/DictionaryFieldEditor.mjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/JSONEditor.cjs +2 -2
- package/dist/components/DictionaryFieldEditor/JSONEditor.cjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/JSONEditor.d.ts.map +1 -1
- package/dist/components/DictionaryFieldEditor/JSONEditor.mjs +2 -2
- package/dist/components/DictionaryFieldEditor/JSONEditor.mjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/NavigationView/navigationViewNode.content.cjs +25 -12
- package/dist/components/DictionaryFieldEditor/NavigationView/navigationViewNode.content.cjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/NavigationView/navigationViewNode.content.d.ts +25 -0
- package/dist/components/DictionaryFieldEditor/NavigationView/navigationViewNode.content.d.ts.map +1 -1
- package/dist/components/DictionaryFieldEditor/NavigationView/navigationViewNode.content.mjs +25 -12
- package/dist/components/DictionaryFieldEditor/NavigationView/navigationViewNode.content.mjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/SaveForm/SaveForm.cjs +36 -31
- package/dist/components/DictionaryFieldEditor/SaveForm/SaveForm.cjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/SaveForm/SaveForm.d.ts.map +1 -1
- package/dist/components/DictionaryFieldEditor/SaveForm/SaveForm.mjs +36 -31
- package/dist/components/DictionaryFieldEditor/SaveForm/SaveForm.mjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/SaveForm/saveForm.content.cjs +32 -28
- package/dist/components/DictionaryFieldEditor/SaveForm/saveForm.content.cjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/SaveForm/saveForm.content.d.ts +32 -5
- package/dist/components/DictionaryFieldEditor/SaveForm/saveForm.content.d.ts.map +1 -1
- package/dist/components/DictionaryFieldEditor/SaveForm/saveForm.content.mjs +32 -28
- package/dist/components/DictionaryFieldEditor/SaveForm/saveForm.content.mjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/StructureEditor.cjs +2 -2
- package/dist/components/DictionaryFieldEditor/StructureEditor.cjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/StructureEditor.mjs +2 -2
- package/dist/components/DictionaryFieldEditor/StructureEditor.mjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/StructureView/StructureView.cjs +2 -5
- package/dist/components/DictionaryFieldEditor/StructureView/StructureView.cjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/StructureView/StructureView.mjs +2 -5
- package/dist/components/DictionaryFieldEditor/StructureView/StructureView.mjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/StructureView/structureView.content.cjs +9 -4
- package/dist/components/DictionaryFieldEditor/StructureView/structureView.content.cjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/StructureView/structureView.content.d.ts +9 -0
- package/dist/components/DictionaryFieldEditor/StructureView/structureView.content.d.ts.map +1 -1
- package/dist/components/DictionaryFieldEditor/StructureView/structureView.content.mjs +9 -4
- package/dist/components/DictionaryFieldEditor/StructureView/structureView.content.mjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/VersionSwitcherDropDown/VersionSwitcher.cjs +3 -3
- package/dist/components/DictionaryFieldEditor/VersionSwitcherDropDown/VersionSwitcher.cjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/VersionSwitcherDropDown/VersionSwitcher.mjs +3 -3
- package/dist/components/DictionaryFieldEditor/VersionSwitcherDropDown/VersionSwitcher.mjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/VersionSwitcherDropDown/VersionSwitcherContext.cjs +4 -6
- package/dist/components/DictionaryFieldEditor/VersionSwitcherDropDown/VersionSwitcherContext.cjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/VersionSwitcherDropDown/VersionSwitcherContext.d.ts +3 -3
- package/dist/components/DictionaryFieldEditor/VersionSwitcherDropDown/VersionSwitcherContext.d.ts.map +1 -1
- package/dist/components/DictionaryFieldEditor/VersionSwitcherDropDown/VersionSwitcherContext.mjs +4 -6
- package/dist/components/DictionaryFieldEditor/VersionSwitcherDropDown/VersionSwitcherContext.mjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/VersionSwitcherDropDown/versionSwitcherDropDown.content.cjs +7 -3
- package/dist/components/DictionaryFieldEditor/VersionSwitcherDropDown/versionSwitcherDropDown.content.cjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/VersionSwitcherDropDown/versionSwitcherDropDown.content.d.ts +7 -0
- package/dist/components/DictionaryFieldEditor/VersionSwitcherDropDown/versionSwitcherDropDown.content.d.ts.map +1 -1
- package/dist/components/DictionaryFieldEditor/VersionSwitcherDropDown/versionSwitcherDropDown.content.mjs +7 -3
- package/dist/components/DictionaryFieldEditor/VersionSwitcherDropDown/versionSwitcherDropDown.content.mjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/dictionaryFieldEditor.content.cjs +9 -6
- package/dist/components/DictionaryFieldEditor/dictionaryFieldEditor.content.cjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/dictionaryFieldEditor.content.d.ts +9 -4
- package/dist/components/DictionaryFieldEditor/dictionaryFieldEditor.content.d.ts.map +1 -1
- package/dist/components/DictionaryFieldEditor/dictionaryFieldEditor.content.mjs +9 -6
- package/dist/components/DictionaryFieldEditor/dictionaryFieldEditor.content.mjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/nodeTypeSelector.content.cjs +28 -14
- package/dist/components/DictionaryFieldEditor/nodeTypeSelector.content.cjs.map +1 -1
- package/dist/components/DictionaryFieldEditor/nodeTypeSelector.content.d.ts +29 -2
- package/dist/components/DictionaryFieldEditor/nodeTypeSelector.content.d.ts.map +1 -1
- package/dist/components/DictionaryFieldEditor/nodeTypeSelector.content.mjs +28 -14
- package/dist/components/DictionaryFieldEditor/nodeTypeSelector.content.mjs.map +1 -1
- package/dist/components/DropDown/index.cjs +2 -2
- package/dist/components/DropDown/index.cjs.map +1 -1
- package/dist/components/DropDown/index.d.ts.map +1 -1
- package/dist/components/DropDown/index.mjs +2 -2
- package/dist/components/DropDown/index.mjs.map +1 -1
- package/dist/components/EditableField/EditableFieldTextArea.cjs +2 -5
- package/dist/components/EditableField/EditableFieldTextArea.cjs.map +1 -1
- package/dist/components/EditableField/EditableFieldTextArea.mjs +2 -5
- package/dist/components/EditableField/EditableFieldTextArea.mjs.map +1 -1
- package/dist/components/ExpandCollapse/ExpandCollapse.cjs.map +1 -1
- package/dist/components/ExpandCollapse/ExpandCollapse.d.ts +2 -2
- package/dist/components/ExpandCollapse/ExpandCollapse.d.ts.map +1 -1
- package/dist/components/ExpandCollapse/ExpandCollapse.mjs.map +1 -1
- package/dist/components/ExpandCollapse/expandCollapse.content.cjs +5 -2
- package/dist/components/ExpandCollapse/expandCollapse.content.cjs.map +1 -1
- package/dist/components/ExpandCollapse/expandCollapse.content.d.ts +3 -0
- package/dist/components/ExpandCollapse/expandCollapse.content.d.ts.map +1 -1
- package/dist/components/ExpandCollapse/expandCollapse.content.mjs +5 -2
- package/dist/components/ExpandCollapse/expandCollapse.content.mjs.map +1 -1
- package/dist/components/Flags/Flag.cjs +2 -1
- package/dist/components/Flags/Flag.cjs.map +1 -1
- package/dist/components/Flags/Flag.d.ts.map +1 -1
- package/dist/components/Flags/Flag.mjs +2 -1
- package/dist/components/Flags/Flag.mjs.map +1 -1
- package/dist/components/IDE/Code.cjs +8 -8
- package/dist/components/IDE/Code.cjs.map +1 -1
- package/dist/components/IDE/Code.mjs +8 -8
- package/dist/components/IDE/Code.mjs.map +1 -1
- package/dist/components/IDE/CodeBlockClient.cjs +1 -1
- package/dist/components/IDE/CodeBlockClient.cjs.map +1 -1
- package/dist/components/IDE/CodeBlockClient.d.ts +1 -1
- package/dist/components/IDE/CodeBlockClient.d.ts.map +1 -1
- package/dist/components/IDE/CodeBlockClient.mjs +1 -1
- package/dist/components/IDE/CodeBlockClient.mjs.map +1 -1
- package/dist/components/IDE/CodeContext.cjs +1 -3
- package/dist/components/IDE/CodeContext.cjs.map +1 -1
- package/dist/components/IDE/CodeContext.mjs +1 -3
- package/dist/components/IDE/CodeContext.mjs.map +1 -1
- package/dist/components/IDE/code.content.cjs +5 -2
- package/dist/components/IDE/code.content.cjs.map +1 -1
- package/dist/components/IDE/code.content.d.ts +5 -0
- package/dist/components/IDE/code.content.d.ts.map +1 -1
- package/dist/components/IDE/code.content.mjs +5 -2
- package/dist/components/IDE/code.content.mjs.map +1 -1
- package/dist/components/IDE/copyCode.content.cjs +5 -2
- package/dist/components/IDE/copyCode.content.cjs.map +1 -1
- package/dist/components/IDE/copyCode.content.d.ts +5 -0
- package/dist/components/IDE/copyCode.content.d.ts.map +1 -1
- package/dist/components/IDE/copyCode.content.mjs +5 -2
- package/dist/components/IDE/copyCode.content.mjs.map +1 -1
- package/dist/components/Input/Checkbox.cjs.map +1 -1
- package/dist/components/Input/Checkbox.d.ts +2 -2
- package/dist/components/Input/Checkbox.d.ts.map +1 -1
- package/dist/components/Input/Checkbox.mjs.map +1 -1
- package/dist/components/Loader/index.content.cjs +6 -1
- package/dist/components/Loader/index.content.cjs.map +1 -1
- package/dist/components/Loader/index.content.d.ts +33 -2
- package/dist/components/Loader/index.content.d.ts.map +1 -1
- package/dist/components/Loader/index.content.mjs +6 -1
- package/dist/components/Loader/index.content.mjs.map +1 -1
- package/dist/components/LocaleSwitcherContentDropDown/LocaleSwitcherContent.cjs +1 -3
- package/dist/components/LocaleSwitcherContentDropDown/LocaleSwitcherContent.cjs.map +1 -1
- package/dist/components/LocaleSwitcherContentDropDown/LocaleSwitcherContent.mjs +1 -3
- package/dist/components/LocaleSwitcherContentDropDown/LocaleSwitcherContent.mjs.map +1 -1
- package/dist/components/LocaleSwitcherContentDropDown/LocaleSwitcherContentContext.cjs +1 -3
- package/dist/components/LocaleSwitcherContentDropDown/LocaleSwitcherContentContext.cjs.map +1 -1
- package/dist/components/LocaleSwitcherContentDropDown/LocaleSwitcherContentContext.mjs +1 -3
- package/dist/components/LocaleSwitcherContentDropDown/LocaleSwitcherContentContext.mjs.map +1 -1
- package/dist/components/LocaleSwitcherContentDropDown/localeSwitcher.content.cjs +17 -8
- package/dist/components/LocaleSwitcherContentDropDown/localeSwitcher.content.cjs.map +1 -1
- package/dist/components/LocaleSwitcherContentDropDown/localeSwitcher.content.d.ts +17 -0
- package/dist/components/LocaleSwitcherContentDropDown/localeSwitcher.content.d.ts.map +1 -1
- package/dist/components/LocaleSwitcherContentDropDown/localeSwitcher.content.mjs +17 -8
- package/dist/components/LocaleSwitcherContentDropDown/localeSwitcher.content.mjs.map +1 -1
- package/dist/components/LocaleSwitcherDropDown/localeSwitcher.content.cjs +13 -6
- package/dist/components/LocaleSwitcherDropDown/localeSwitcher.content.cjs.map +1 -1
- package/dist/components/LocaleSwitcherDropDown/localeSwitcher.content.d.ts +13 -0
- package/dist/components/LocaleSwitcherDropDown/localeSwitcher.content.d.ts.map +1 -1
- package/dist/components/LocaleSwitcherDropDown/localeSwitcher.content.mjs +13 -6
- package/dist/components/LocaleSwitcherDropDown/localeSwitcher.content.mjs.map +1 -1
- package/dist/components/Modal/Modal.cjs +2 -4
- package/dist/components/Modal/Modal.cjs.map +1 -1
- package/dist/components/Modal/Modal.mjs +2 -4
- package/dist/components/Modal/Modal.mjs.map +1 -1
- package/dist/components/Navbar/MobileNavbar.cjs +1 -3
- package/dist/components/Navbar/MobileNavbar.cjs.map +1 -1
- package/dist/components/Navbar/MobileNavbar.mjs +1 -3
- package/dist/components/Navbar/MobileNavbar.mjs.map +1 -1
- package/dist/components/Navbar/index.cjs +2 -4
- package/dist/components/Navbar/index.cjs.map +1 -1
- package/dist/components/Navbar/index.mjs +2 -4
- package/dist/components/Navbar/index.mjs.map +1 -1
- package/dist/components/SwitchSelector/index.cjs +1 -3
- package/dist/components/SwitchSelector/index.cjs.map +1 -1
- package/dist/components/SwitchSelector/index.mjs +1 -3
- package/dist/components/SwitchSelector/index.mjs.map +1 -1
- package/dist/components/TabSelector/TabSelector.cjs +1 -3
- package/dist/components/TabSelector/TabSelector.cjs.map +1 -1
- package/dist/components/TabSelector/TabSelector.d.ts +1 -1
- package/dist/components/TabSelector/TabSelector.d.ts.map +1 -1
- package/dist/components/TabSelector/TabSelector.mjs +1 -3
- package/dist/components/TabSelector/TabSelector.mjs.map +1 -1
- package/dist/components/Table/table.content.cjs +3 -1
- package/dist/components/Table/table.content.cjs.map +1 -1
- package/dist/components/Table/table.content.d.ts +3 -0
- package/dist/components/Table/table.content.d.ts.map +1 -1
- package/dist/components/Table/table.content.mjs +3 -1
- package/dist/components/Table/table.content.mjs.map +1 -1
- package/dist/components/Tag/index.cjs +22 -88
- package/dist/components/Tag/index.cjs.map +1 -1
- package/dist/components/Tag/index.d.ts +5 -5
- package/dist/components/Tag/index.mjs +22 -88
- package/dist/components/Tag/index.mjs.map +1 -1
- package/dist/components/TextArea/AutocompleteTextArea.cjs +20 -21
- package/dist/components/TextArea/AutocompleteTextArea.cjs.map +1 -1
- package/dist/components/TextArea/AutocompleteTextArea.d.ts.map +1 -1
- package/dist/components/TextArea/AutocompleteTextArea.mjs +20 -21
- package/dist/components/TextArea/AutocompleteTextArea.mjs.map +1 -1
- package/dist/hooks/auth.cjs +26 -11
- package/dist/hooks/auth.cjs.map +1 -1
- package/dist/hooks/auth.mjs +26 -11
- package/dist/hooks/auth.mjs.map +1 -1
- package/dist/hooks/index.cjs +65 -66
- package/dist/hooks/index.cjs.map +1 -1
- package/dist/hooks/index.d.ts +1 -3
- package/dist/hooks/index.d.ts.map +1 -1
- package/dist/hooks/index.mjs +4 -8
- package/dist/hooks/index.mjs.map +1 -1
- package/dist/hooks/reactQuery.cjs +648 -0
- package/dist/hooks/reactQuery.cjs.map +1 -0
- package/dist/hooks/reactQuery.d.ts +899 -0
- package/dist/hooks/reactQuery.d.ts.map +1 -0
- package/dist/hooks/reactQuery.mjs +646 -0
- package/dist/hooks/reactQuery.mjs.map +1 -0
- package/dist/hooks/useAuth/useOAuth2.cjs +13 -10
- package/dist/hooks/useAuth/useOAuth2.cjs.map +1 -1
- package/dist/hooks/useAuth/useOAuth2.d.ts +1 -1
- package/dist/hooks/useAuth/useOAuth2.d.ts.map +1 -1
- package/dist/hooks/useAuth/useOAuth2.mjs +13 -10
- package/dist/hooks/useAuth/useOAuth2.mjs.map +1 -1
- package/dist/hooks/useAuth/useSession.cjs +23 -26
- package/dist/hooks/useAuth/useSession.cjs.map +1 -1
- package/dist/hooks/useAuth/useSession.d.ts +4 -352
- package/dist/hooks/useAuth/useSession.d.ts.map +1 -1
- package/dist/hooks/useAuth/useSession.mjs +23 -26
- package/dist/hooks/useAuth/useSession.mjs.map +1 -1
- package/dist/hooks/useIntlayerAPI.cjs +2 -3
- package/dist/hooks/useIntlayerAPI.cjs.map +1 -1
- package/dist/hooks/useIntlayerAPI.mjs +2 -3
- package/dist/hooks/useIntlayerAPI.mjs.map +1 -1
- package/dist/hooks/useUser/index.cjs +6 -9
- package/dist/hooks/useUser/index.cjs.map +1 -1
- package/dist/hooks/useUser/index.d.ts +2 -18
- package/dist/hooks/useUser/index.d.ts.map +1 -1
- package/dist/hooks/useUser/index.mjs +6 -9
- package/dist/hooks/useUser/index.mjs.map +1 -1
- package/dist/index-BCuMWKyy.js.map +1 -1
- package/dist/index-BYzBot7l.cjs.map +1 -1
- package/dist/providers/ReactQueryProvider.cjs +85 -0
- package/dist/providers/ReactQueryProvider.cjs.map +1 -0
- package/dist/providers/ReactQueryProvider.d.ts +13 -0
- package/dist/providers/ReactQueryProvider.d.ts.map +1 -0
- package/dist/providers/ReactQueryProvider.mjs +85 -0
- package/dist/providers/ReactQueryProvider.mjs.map +1 -0
- package/dist/providers/index.cjs +5 -0
- package/dist/{hooks/useAsync → providers}/index.cjs.map +1 -1
- package/dist/providers/index.d.ts +2 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/index.mjs +5 -0
- package/dist/{hooks/useAsync → providers}/index.mjs.map +1 -1
- package/package.json +31 -21
- package/dist/hooks/intlayerAPIHooks.cjs +0 -542
- package/dist/hooks/intlayerAPIHooks.cjs.map +0 -1
- package/dist/hooks/intlayerAPIHooks.d.ts +0 -968
- package/dist/hooks/intlayerAPIHooks.d.ts.map +0 -1
- package/dist/hooks/intlayerAPIHooks.mjs +0 -542
- package/dist/hooks/intlayerAPIHooks.mjs.map +0 -1
- package/dist/hooks/useAsync/index.cjs +0 -8
- package/dist/hooks/useAsync/index.d.ts +0 -3
- package/dist/hooks/useAsync/index.d.ts.map +0 -1
- package/dist/hooks/useAsync/index.mjs +0 -8
- package/dist/hooks/useAsync/useAsync.cjs +0 -244
- package/dist/hooks/useAsync/useAsync.cjs.map +0 -1
- package/dist/hooks/useAsync/useAsync.d.ts +0 -73
- package/dist/hooks/useAsync/useAsync.d.ts.map +0 -1
- package/dist/hooks/useAsync/useAsync.mjs +0 -244
- package/dist/hooks/useAsync/useAsync.mjs.map +0 -1
- package/dist/hooks/useAsync/useAsyncStateStore.cjs +0 -150
- package/dist/hooks/useAsync/useAsyncStateStore.cjs.map +0 -1
- package/dist/hooks/useAsync/useAsyncStateStore.d.ts +0 -52
- package/dist/hooks/useAsync/useAsyncStateStore.d.ts.map +0 -1
- package/dist/hooks/useAsync/useAsyncStateStore.mjs +0 -150
- package/dist/hooks/useAsync/useAsyncStateStore.mjs.map +0 -1
- package/dist/hooks/useAsync/useAsyncWrapper.cjs +0 -10
- package/dist/hooks/useAsync/useAsyncWrapper.cjs.map +0 -1
- package/dist/hooks/useAsync/useAsyncWrapper.d.ts +0 -37
- package/dist/hooks/useAsync/useAsyncWrapper.d.ts.map +0 -1
- package/dist/hooks/useAsync/useAsyncWrapper.mjs +0 -10
- package/dist/hooks/useAsync/useAsyncWrapper.mjs.map +0 -1
- package/dist/hooks/useGetAllDictionaries.cjs +0 -35
- package/dist/hooks/useGetAllDictionaries.cjs.map +0 -1
- package/dist/hooks/useGetAllDictionaries.d.ts +0 -11
- package/dist/hooks/useGetAllDictionaries.d.ts.map +0 -1
- package/dist/hooks/useGetAllDictionaries.mjs +0 -35
- package/dist/hooks/useGetAllDictionaries.mjs.map +0 -1
|
@@ -1,244 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
"use strict";
|
|
3
|
-
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
4
|
-
const ReactExports = require("react");
|
|
5
|
-
const hooks_useAsync_useAsyncStateStore = require("./useAsyncStateStore.cjs");
|
|
6
|
-
const pendingPromises = /* @__PURE__ */ new Map();
|
|
7
|
-
const DEFAULT_CACHE_ENABLED = false;
|
|
8
|
-
const DEFAULT_STORE_ENABLED = false;
|
|
9
|
-
const DEFAULT_ENABLED = true;
|
|
10
|
-
const DEFAULT_AUTO_FETCH = false;
|
|
11
|
-
const DEFAULT_RETRY_LIMIT = 1;
|
|
12
|
-
const DEFAULT_REVALIDATION_ENABLED = false;
|
|
13
|
-
const DEFAULT_REVALIDATE_TIME = 5 * 60 * 1e3;
|
|
14
|
-
const DEFAULT_RETRY_TIME = 5 * 60 * 1e3;
|
|
15
|
-
const getArgs = (args) => args ? Array.isArray(args) ? args : [args] : [];
|
|
16
|
-
const getKeyWithArgs = (key, args) => getArgs(args).length > 0 ? `${key}/${JSON.stringify(args)}` : key;
|
|
17
|
-
const useAsync = (key, asyncFunction, options) => {
|
|
18
|
-
const retryLimit = options?.retryLimit ?? DEFAULT_RETRY_LIMIT;
|
|
19
|
-
const autoFetch = options?.autoFetch ?? DEFAULT_AUTO_FETCH;
|
|
20
|
-
const retryTime = options?.retryTime ?? DEFAULT_RETRY_TIME;
|
|
21
|
-
const cacheEnabled = options?.cache ?? DEFAULT_CACHE_ENABLED;
|
|
22
|
-
const storeEnabled = options?.store ?? DEFAULT_STORE_ENABLED;
|
|
23
|
-
const enabled = options?.enable ?? DEFAULT_ENABLED;
|
|
24
|
-
const revalidationEnabled = options?.revalidation ?? DEFAULT_REVALIDATION_ENABLED;
|
|
25
|
-
const revalidateTime = options?.revalidateTime ?? DEFAULT_REVALIDATE_TIME;
|
|
26
|
-
const updateQueries = options?.updateQueries ?? [];
|
|
27
|
-
const invalidateQueries = options?.invalidateQueries ?? [];
|
|
28
|
-
const onSuccess = options?.onSuccess;
|
|
29
|
-
const onError = options?.onError;
|
|
30
|
-
const args = getArgs(options?.args ?? []);
|
|
31
|
-
const { getStates, setQueryState, setQueriesState, makeQueryInError } = hooks_useAsync_useAsyncStateStore.useAsyncState();
|
|
32
|
-
const storedArgsRef = ReactExports.useRef(args);
|
|
33
|
-
const controllerRef = ReactExports.useRef(null);
|
|
34
|
-
const keyWithArgs = getKeyWithArgs(key, storedArgsRef.current);
|
|
35
|
-
const {
|
|
36
|
-
isFetched,
|
|
37
|
-
fetchedDateTime,
|
|
38
|
-
isLoading,
|
|
39
|
-
isEnabled,
|
|
40
|
-
error,
|
|
41
|
-
isSuccess,
|
|
42
|
-
isInvalidated,
|
|
43
|
-
data,
|
|
44
|
-
errorCount
|
|
45
|
-
} = getStates(keyWithArgs);
|
|
46
|
-
const fetch = (async (...args2) => {
|
|
47
|
-
const keyWithArgs2 = getKeyWithArgs(key, args2);
|
|
48
|
-
if (controllerRef.current) {
|
|
49
|
-
controllerRef.current.abort();
|
|
50
|
-
}
|
|
51
|
-
const controller = new AbortController();
|
|
52
|
-
controllerRef.current = controller;
|
|
53
|
-
if (pendingPromises.has(keyWithArgs2)) {
|
|
54
|
-
return pendingPromises.get(keyWithArgs2);
|
|
55
|
-
}
|
|
56
|
-
const promise = (async () => {
|
|
57
|
-
setQueryState(keyWithArgs2, { isLoading: true });
|
|
58
|
-
let response = null;
|
|
59
|
-
try {
|
|
60
|
-
const asyncResult = asyncFunction(...args2);
|
|
61
|
-
if (!asyncResult || typeof asyncResult.then !== "function") {
|
|
62
|
-
throw new Error("asyncFunction must return a Promise");
|
|
63
|
-
}
|
|
64
|
-
const result = await asyncResult;
|
|
65
|
-
response = result;
|
|
66
|
-
setQueryState(keyWithArgs2, {
|
|
67
|
-
data: result,
|
|
68
|
-
errorCount: 0,
|
|
69
|
-
isLoading: false,
|
|
70
|
-
isFetched: true,
|
|
71
|
-
fetchedDateTime: /* @__PURE__ */ new Date(),
|
|
72
|
-
isSuccess: true,
|
|
73
|
-
isInvalidated: false,
|
|
74
|
-
error: null
|
|
75
|
-
});
|
|
76
|
-
onSuccess?.(result);
|
|
77
|
-
if (invalidateQueries.length > 0) {
|
|
78
|
-
setQueriesState(invalidateQueries, {
|
|
79
|
-
isInvalidated: true
|
|
80
|
-
});
|
|
81
|
-
}
|
|
82
|
-
if (updateQueries.length > 0) {
|
|
83
|
-
setQueriesState(updateQueries, {
|
|
84
|
-
data: result
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
if (storeEnabled) {
|
|
88
|
-
localStorage.setItem(keyWithArgs2, JSON.stringify(result));
|
|
89
|
-
}
|
|
90
|
-
} catch (error2) {
|
|
91
|
-
const msg = error2 instanceof Error ? error2.message : String(error2);
|
|
92
|
-
makeQueryInError(keyWithArgs2, msg);
|
|
93
|
-
onError?.(msg);
|
|
94
|
-
} finally {
|
|
95
|
-
pendingPromises.delete(keyWithArgs2);
|
|
96
|
-
}
|
|
97
|
-
return response;
|
|
98
|
-
})();
|
|
99
|
-
pendingPromises.set(keyWithArgs2, promise);
|
|
100
|
-
return await promise;
|
|
101
|
-
});
|
|
102
|
-
const revalidate = (async (...args2) => {
|
|
103
|
-
if (!isEnabled || !enabled) return;
|
|
104
|
-
if (args2) {
|
|
105
|
-
storedArgsRef.current = getArgs(args2);
|
|
106
|
-
}
|
|
107
|
-
return await fetch(...storedArgsRef.current);
|
|
108
|
-
});
|
|
109
|
-
const execute = (async (...args2) => {
|
|
110
|
-
if (!isEnabled || !enabled) return;
|
|
111
|
-
if (isLoading) return;
|
|
112
|
-
const shouldReturnData = !isInvalidated && // If data are invalidated, we should refetch to revalidate the data
|
|
113
|
-
isSuccess && cacheEnabled && data;
|
|
114
|
-
if (shouldReturnData) return data;
|
|
115
|
-
return await revalidate(...args2);
|
|
116
|
-
});
|
|
117
|
-
ReactExports.useEffect(() => {
|
|
118
|
-
if (enabled !== isEnabled) {
|
|
119
|
-
setQueryState(keyWithArgs, {
|
|
120
|
-
isEnabled
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
}, [enabled, isEnabled, keyWithArgs]);
|
|
124
|
-
ReactExports.useEffect(() => {
|
|
125
|
-
if (!isEnabled || !enabled) return;
|
|
126
|
-
if (!storeEnabled) return;
|
|
127
|
-
if (isInvalidated || isFetched || data) return;
|
|
128
|
-
const storedData = localStorage.getItem(keyWithArgs);
|
|
129
|
-
try {
|
|
130
|
-
if (storedData) {
|
|
131
|
-
setQueryState(keyWithArgs, {
|
|
132
|
-
data: JSON.parse(storedData)
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
} catch (error2) {
|
|
136
|
-
console.error(error2);
|
|
137
|
-
}
|
|
138
|
-
}, [
|
|
139
|
-
storeEnabled,
|
|
140
|
-
keyWithArgs,
|
|
141
|
-
isFetched,
|
|
142
|
-
isInvalidated,
|
|
143
|
-
isEnabled,
|
|
144
|
-
enabled,
|
|
145
|
-
data
|
|
146
|
-
]);
|
|
147
|
-
ReactExports.useEffect(() => {
|
|
148
|
-
if (!autoFetch) return;
|
|
149
|
-
if (!isEnabled || !enabled) return;
|
|
150
|
-
if (isFetched && !isInvalidated) return;
|
|
151
|
-
if (isLoading) return;
|
|
152
|
-
fetch(...storedArgsRef.current);
|
|
153
|
-
}, [
|
|
154
|
-
autoFetch,
|
|
155
|
-
isEnabled,
|
|
156
|
-
enabled,
|
|
157
|
-
isFetched,
|
|
158
|
-
isInvalidated,
|
|
159
|
-
isLoading,
|
|
160
|
-
fetch
|
|
161
|
-
]);
|
|
162
|
-
ReactExports.useEffect(() => {
|
|
163
|
-
const isRetryEnabled = errorCount > 0 && retryLimit > 0;
|
|
164
|
-
const isRetryLimitReached = errorCount > retryLimit;
|
|
165
|
-
if (!isEnabled || !enabled) return;
|
|
166
|
-
if (!isRetryEnabled) return;
|
|
167
|
-
if (isRetryLimitReached) return;
|
|
168
|
-
if (isLoading) return;
|
|
169
|
-
if (isSuccess) return;
|
|
170
|
-
const timeout = setTimeout(() => {
|
|
171
|
-
fetch(...storedArgsRef.current);
|
|
172
|
-
}, retryTime);
|
|
173
|
-
return () => clearTimeout(timeout);
|
|
174
|
-
}, [
|
|
175
|
-
isEnabled,
|
|
176
|
-
errorCount,
|
|
177
|
-
retryLimit,
|
|
178
|
-
enabled,
|
|
179
|
-
retryTime,
|
|
180
|
-
isSuccess,
|
|
181
|
-
isLoading,
|
|
182
|
-
fetch
|
|
183
|
-
]);
|
|
184
|
-
ReactExports.useEffect(() => {
|
|
185
|
-
if (!revalidationEnabled || revalidateTime <= 0) return;
|
|
186
|
-
if (!isEnabled || !enabled) return;
|
|
187
|
-
if (isLoading) return;
|
|
188
|
-
if (!isSuccess || !fetchedDateTime) return;
|
|
189
|
-
const timeout = setTimeout(() => {
|
|
190
|
-
fetch(...storedArgsRef.current);
|
|
191
|
-
}, revalidateTime);
|
|
192
|
-
return () => clearTimeout(timeout);
|
|
193
|
-
}, [
|
|
194
|
-
revalidationEnabled,
|
|
195
|
-
revalidateTime,
|
|
196
|
-
isSuccess,
|
|
197
|
-
fetchedDateTime,
|
|
198
|
-
isLoading,
|
|
199
|
-
isEnabled,
|
|
200
|
-
enabled,
|
|
201
|
-
fetch
|
|
202
|
-
]);
|
|
203
|
-
const setDataMemo = (data2) => {
|
|
204
|
-
setQueryState(keyWithArgs, {
|
|
205
|
-
data: data2
|
|
206
|
-
});
|
|
207
|
-
};
|
|
208
|
-
const abort = () => {
|
|
209
|
-
if (controllerRef.current) {
|
|
210
|
-
controllerRef.current.abort();
|
|
211
|
-
}
|
|
212
|
-
};
|
|
213
|
-
const memoResult = {
|
|
214
|
-
isFetched,
|
|
215
|
-
isInvalidated,
|
|
216
|
-
error,
|
|
217
|
-
data,
|
|
218
|
-
errorCount,
|
|
219
|
-
isSuccess,
|
|
220
|
-
isEnabled,
|
|
221
|
-
isDisabled: !isEnabled,
|
|
222
|
-
isLoading,
|
|
223
|
-
isWaitingData: isLoading && !isFetched && !data,
|
|
224
|
-
// Check if the data is still being fetched. Stay at true during revalidation or if data are stored in local storage
|
|
225
|
-
isRevalidating: isLoading && isFetched,
|
|
226
|
-
// Check if the data is valid and is being revalidated
|
|
227
|
-
[key]: execute,
|
|
228
|
-
// Name the execute function as the given key to avoid conflicts with other hooks (e.g. `const { fetchUser } = useAsync('fetchUser', () => fetchUserFunction());`)
|
|
229
|
-
revalidate,
|
|
230
|
-
setData: setDataMemo,
|
|
231
|
-
abort
|
|
232
|
-
};
|
|
233
|
-
ReactExports.useEffect(
|
|
234
|
-
() => () => {
|
|
235
|
-
if (controllerRef.current) {
|
|
236
|
-
controllerRef.current.abort();
|
|
237
|
-
}
|
|
238
|
-
},
|
|
239
|
-
[]
|
|
240
|
-
);
|
|
241
|
-
return memoResult;
|
|
242
|
-
};
|
|
243
|
-
exports.useAsync = useAsync;
|
|
244
|
-
//# sourceMappingURL=useAsync.cjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useAsync.cjs","sources":["../../../src/hooks/useAsync/useAsync.ts"],"sourcesContent":["'use client';\n\nimport { useEffect, useRef } from 'react';\nimport { useAsyncState } from './useAsyncStateStore';\n\n// Pending promises cache to prevent parallel requests when multiple components use the hook\nconst pendingPromises = new Map();\n\n// Defines the base structure for the result of the custom hook.\ntype UseAsyncResultBase<T extends (...args: any[]) => Promise<any>> = {\n isFetched: boolean;\n isLoading: boolean;\n isInvalidated: boolean;\n isSuccess: boolean;\n isDisabled: boolean;\n isWaitingData: boolean;\n isRevalidating: boolean;\n error: string | null;\n data: Awaited<ReturnType<T>> | null;\n errorCount: number;\n revalidate: T;\n setData: (data: Awaited<ReturnType<T> | null>) => void;\n abort: () => void;\n};\n\n// Options type for the hook, allowing customization of behavior.\nexport type UseAsyncOptions<T extends (...args: any[]) => Promise<any>> = {\n retryLimit?: number; // The number of times the hook should retry the function on failure before giving up\n retryTime?: number; // Time in milliseconds for retrying the data\n cache?: boolean; // Cache the result of the function\n store?: boolean; // Store the result of the function in local storage\n enable?: boolean; // Enable the hook\n autoFetch?: boolean; // Automatically fetch the data when the hook is mounted\n revalidation?: boolean; // Enable revalidation\n revalidateTime?: number; // Time in milliseconds for revalidating the data\n invalidateQueries?: string[]; // Invalidate other queries when the data is updated\n updateQueries?: string[]; // Update other queries when the data is updated\n onSuccess?: (data: Awaited<ReturnType<T>>) => void; // Callback function that is called when the asynchronous function resolves successfully\n onError?: (error: string) => void; // Callback function that is called when the asynchronous function rejects or encounters an error\n abort?: () => void; // Abort the request\n args?: Parameters<T>; // Arguments to pass to the asynchronous function\n};\n\n// Default values for the hook's options\nconst DEFAULT_CACHE_ENABLED = false;\nconst DEFAULT_STORE_ENABLED = false;\nconst DEFAULT_ENABLED = true;\nconst DEFAULT_AUTO_FETCH = false;\nconst DEFAULT_RETRY_LIMIT = 1;\nconst DEFAULT_REVALIDATION_ENABLED = false;\nconst DEFAULT_REVALIDATE_TIME = 5 * 60 * 1000; // 5 minutes\nconst DEFAULT_RETRY_TIME = 5 * 60 * 1000; // 5 minutes\n\n// The main hook type that includes the async function along with its additional properties.\nexport type UseAsyncResult<\n U extends string,\n T extends (...args: any[]) => Promise<any>,\n> = UseAsyncResultBase<T> & Record<U, T>;\n\nconst getArgs = (args?: any[]): any[] =>\n args ? (Array.isArray(args) ? args : [args]) : [];\n\nconst getKeyWithArgs = (key: string, args: any[]) =>\n getArgs(args).length > 0 ? `${key}/${JSON.stringify(args)}` : key;\n\n/**\n * A custom React hook that manages asynchronous operations, providing easy-to-use states and controls over fetching, caching, and retry mechanisms.\n * This hook abstracts away the complexity of handling loading, error, and success states for any asynchronous function.\n *\n *\n * ```tsx\n * // Example of using useAsync to manage fetching user data from an API.\n * const fetchUserData = async (userId) => {\n * const response = await fetch(`/api/users/${userId}`);\n * if (!response.ok) throw new Error('Failed to fetch');\n * return await response.json();\n * };\n *\n * const UserDetails = ({ userId }) => {\n * const {\n * isLoading,\n * data,\n * error,\n * revalidate,\n * } = useAsync('userDetails', fetchUserData, {\n * cache: true,\n * revalidateTime: 60000, // 1 minute\n * autoFetch: true,\n * onSuccess: (data) => console.log('User data fetched successfully:', data),\n * onError: (error) => console.error('Error fetching user data:', error),\n * });\n *\n * if (isLoading) return <div>Loading...</div>;\n * if (error) return <div>Error: {error}</div>;\n * return (\n * <div>\n * <h1>{data.name}</h1>\n * <button onClick={() => revalidate()}>Refresh</button>\n * </div>\n * );\n * };\n * ```\n */\nexport const useAsync = <\n U extends string,\n T extends (...args: any[]) => Promise<any>,\n>(\n key: U,\n asyncFunction: T,\n options?: UseAsyncOptions<T>\n): UseAsyncResult<U, T> => {\n // Resolving optional parameters with default values\n const retryLimit = options?.retryLimit ?? DEFAULT_RETRY_LIMIT;\n const autoFetch = options?.autoFetch ?? DEFAULT_AUTO_FETCH;\n const retryTime = options?.retryTime ?? DEFAULT_RETRY_TIME;\n const cacheEnabled = options?.cache ?? DEFAULT_CACHE_ENABLED;\n const storeEnabled = options?.store ?? DEFAULT_STORE_ENABLED;\n const enabled = options?.enable ?? DEFAULT_ENABLED;\n const revalidationEnabled =\n options?.revalidation ?? DEFAULT_REVALIDATION_ENABLED;\n const revalidateTime = options?.revalidateTime ?? DEFAULT_REVALIDATE_TIME;\n const updateQueries = options?.updateQueries ?? [];\n const invalidateQueries = options?.invalidateQueries ?? [];\n const onSuccess = options?.onSuccess;\n const onError = options?.onError;\n const args = getArgs(options?.args ?? []);\n\n // Using a custom hook to manage state specific to asynchronous operations\n const { getStates, setQueryState, setQueriesState, makeQueryInError } =\n useAsyncState();\n\n // Storing the last arguments used to call the async function\n const storedArgsRef = useRef<any[]>(args);\n\n const controllerRef = useRef<AbortController | null>(null);\n\n // Apply different key for different requests\n const keyWithArgs = getKeyWithArgs(key, storedArgsRef.current);\n\n // Retrieving the current state of async operations using the same custom hook\n const {\n isFetched,\n fetchedDateTime,\n isLoading,\n isEnabled,\n error,\n isSuccess,\n isInvalidated,\n data,\n errorCount,\n } = getStates(keyWithArgs);\n\n /**\n * FETCH FUNCTION\n *\n * Manage parallel fetching across multiple instances of the hook\n * Manage state updates on success and error\n * Manage eventual invalidation of other queries\n */\n const fetch: T = (async (...args) => {\n const keyWithArgs = getKeyWithArgs(key, args);\n\n /**\n * ABORT CONTROLLER\n *\n * cancel an unnecessary request.\n * For example, if a user navigates away from a page or triggers a new request that makes the previous one obsolete, you can abort the previous fetch\n */\n if (controllerRef.current) {\n // Abort the previous request\n controllerRef.current.abort();\n }\n\n // Create a new AbortController\n const controller = new AbortController();\n controllerRef.current = controller;\n\n /**\n * PENDING PROMISES\n *\n * This logic ensures that if two parts of your application trigger the same request simultaneously,\n * only one network call is made, and both receive the same result.\n */\n if (pendingPromises.has(keyWithArgs)) {\n // Return the existing pending promise\n return pendingPromises.get(keyWithArgs);\n }\n\n const promise = (async () => {\n setQueryState(keyWithArgs, { isLoading: true });\n let response = null;\n\n try {\n // Ensure asyncFunction returns a Promise\n const asyncResult = asyncFunction(...args);\n\n // Check if the result is a Promise-like object\n if (!asyncResult || typeof asyncResult.then !== 'function') {\n throw new Error('asyncFunction must return a Promise');\n }\n\n const result = await asyncResult;\n response = result;\n\n setQueryState(keyWithArgs, {\n data: result,\n errorCount: 0,\n isLoading: false,\n isFetched: true,\n fetchedDateTime: new Date(),\n isSuccess: true,\n isInvalidated: false,\n error: null,\n });\n\n onSuccess?.(result);\n\n // Invalidate other queries if necessary\n if (invalidateQueries.length > 0) {\n setQueriesState(invalidateQueries, {\n isInvalidated: true,\n });\n }\n\n // Update other queries if necessary\n if (updateQueries.length > 0) {\n setQueriesState(updateQueries, {\n data: result,\n });\n }\n\n // Store the result in local storage\n if (storeEnabled) {\n localStorage.setItem(keyWithArgs, JSON.stringify(result));\n }\n } catch (error) {\n const msg = error instanceof Error ? error.message : String(error);\n\n makeQueryInError(keyWithArgs, msg);\n onError?.(msg);\n } finally {\n // Remove the pending promise from the cache\n pendingPromises.delete(keyWithArgs);\n }\n\n return response;\n })();\n\n // Store the pending promise in the cache\n pendingPromises.set(keyWithArgs, promise);\n\n return await promise;\n }) as T;\n\n /**\n * REVALIDATE FUNCTION\n *\n * Wrap core function to handle revalidation\n * Handle arguments caching\n *\n */\n const revalidate: T = (async (...args) => {\n if (!isEnabled || !enabled) return; // Hook is disabled\n\n if (args) {\n // Revalidation arguments can be different from the initial fetch arguments\n // If arguments are provided, store/update them for future periodic revalidation\n\n storedArgsRef.current = getArgs(args);\n }\n\n return await fetch(...storedArgsRef.current);\n }) as T;\n\n /**\n * EXECUTION FUNCTION\n *\n * Wrap revalidation function\n * If data is valid return it directly to avoid fetching again\n */\n const execute: T = (async (...args) => {\n if (!isEnabled || !enabled) return; // Hook is disabled\n if (isLoading) return; // Fetch is already in progress\n\n const shouldReturnData =\n !isInvalidated && // If data are invalidated, we should refetch to revalidate the data\n isSuccess &&\n cacheEnabled &&\n data;\n\n if (shouldReturnData) return data; // Data are already fetched and should be returned directly. Avoid fetching again.\n\n return await revalidate(...args);\n }) as T;\n\n /**\n * HANDLE SYNCHRONIZATION HOOKS DISACTIVATION\n *\n * If one instance of the hook is disabled, the other instances should be disabled too.\n * This is to prevent inconsistencies in the state of the hook.\n */\n useEffect(() => {\n if (enabled !== isEnabled) {\n setQueryState(keyWithArgs, {\n isEnabled,\n });\n }\n }, [enabled, isEnabled, keyWithArgs]);\n\n /**\n * HANDLE LOCAL STORAGE LOADING\n *\n * If store is enabled, load data from local storage\n */\n useEffect(() => {\n if (!isEnabled || !enabled) return; // Hook is disabled\n if (!storeEnabled) return; // Hook should not use local storage\n if (isInvalidated || isFetched || data) return; // Hook have been already mounted and fetched or invalidated\n\n const storedData = localStorage.getItem(keyWithArgs);\n\n // Wrap parsing in a try-catch block to handle invalid JSON data\n try {\n if (storedData) {\n setQueryState(keyWithArgs, {\n data: JSON.parse(storedData),\n });\n }\n } catch (error) {\n console.error(error);\n }\n }, [\n storeEnabled,\n keyWithArgs,\n isFetched,\n isInvalidated,\n isEnabled,\n enabled,\n data,\n ]);\n\n /**\n * HANDLE AUTO-FETCH ON HOOK MOUNT\n *\n * If autoFetch is enabled, fetch the data when the hook is mounted\n */\n useEffect(() => {\n if (!autoFetch) return; // Auto-fetch is disabled\n if (!isEnabled || !enabled) return; // Hook is disabled\n if (isFetched && !isInvalidated) return; // Hook have already fetched or invalidated\n if (isLoading) return; // Fetch is already in progress\n\n fetch(...storedArgsRef.current);\n }, [\n autoFetch,\n isEnabled,\n enabled,\n isFetched,\n isInvalidated,\n isLoading,\n fetch,\n ]);\n\n /**\n * HANDLE RETRY\n *\n * If fetching fails, retry the fetch after a certain time\n */\n useEffect(() => {\n const isRetryEnabled = errorCount > 0 && retryLimit > 0;\n const isRetryLimitReached = errorCount > retryLimit;\n\n if (!isEnabled || !enabled) return; // Hook is disabled\n if (!isRetryEnabled) return; // Retry is disabled\n if (isRetryLimitReached) return; // Retry limit has been reached\n if (isLoading) return; // Fetch is already in progress\n if (isSuccess) return; // Hook has already fetched successfully\n\n const timeout = setTimeout(() => {\n fetch(...storedArgsRef.current);\n }, retryTime);\n\n return () => clearTimeout(timeout);\n }, [\n isEnabled,\n errorCount,\n retryLimit,\n enabled,\n retryTime,\n isSuccess,\n isLoading,\n fetch,\n ]);\n\n /**\n * HANDLE PERIODIC REVALIDATION\n *\n * If revalidation is enabled, revalidate the data periodically\n */\n useEffect(() => {\n if (!revalidationEnabled || revalidateTime <= 0) return; // Revalidation is disabled\n if (!isEnabled || !enabled) return; // Hook is disabled\n if (isLoading) return; // Fetch is already in progress\n if (!isSuccess || !fetchedDateTime) return; // Should retry either of revalidate\n\n const timeout = setTimeout(() => {\n fetch(...storedArgsRef.current);\n }, revalidateTime);\n\n return () => clearTimeout(timeout);\n }, [\n revalidationEnabled,\n revalidateTime,\n isSuccess,\n fetchedDateTime,\n isLoading,\n isEnabled,\n enabled,\n fetch,\n ]);\n\n // Memoization of the setData function to prevent unnecessary re-renders\n const setDataMemo = (data: Awaited<ReturnType<T> | null>) => {\n setQueryState(keyWithArgs, {\n data,\n });\n };\n\n const abort = () => {\n if (controllerRef.current) {\n controllerRef.current.abort();\n }\n };\n\n // Memoization to prevent unnecessary re-renders\n const memoResult = {\n isFetched,\n isInvalidated,\n error,\n data,\n errorCount,\n isSuccess,\n isEnabled,\n isDisabled: !isEnabled,\n isLoading,\n isWaitingData: isLoading && !isFetched && !data, // Check if the data is still being fetched. Stay at true during revalidation or if data are stored in local storage\n isRevalidating: isLoading && isFetched, // Check if the data is valid and is being revalidated\n [key]: execute, // Name the execute function as the given key to avoid conflicts with other hooks (e.g. `const { fetchUser } = useAsync('fetchUser', () => fetchUserFunction());`)\n revalidate,\n setData: setDataMemo,\n abort,\n };\n\n useEffect(\n () => () => {\n // Clean up the controller on unmount\n if (controllerRef.current) {\n controllerRef.current.abort();\n }\n },\n []\n );\n\n // Return the hook's result, including all state and control functions\n return memoResult as UseAsyncResultBase<T> & Record<U, T>;\n};\n"],"names":["useAsyncState","useRef","args","keyWithArgs","error","useEffect","data"],"mappings":";;;;;AAMA,MAAM,sCAAsB,IAAA;AAsC5B,MAAM,wBAAwB;AAC9B,MAAM,wBAAwB;AAC9B,MAAM,kBAAkB;AACxB,MAAM,qBAAqB;AAC3B,MAAM,sBAAsB;AAC5B,MAAM,+BAA+B;AACrC,MAAM,0BAA0B,IAAI,KAAK;AACzC,MAAM,qBAAqB,IAAI,KAAK;AAQpC,MAAM,UAAU,CAAC,SACf,OAAQ,MAAM,QAAQ,IAAI,IAAI,OAAO,CAAC,IAAI,IAAK,CAAA;AAEjD,MAAM,iBAAiB,CAAC,KAAa,SACnC,QAAQ,IAAI,EAAE,SAAS,IAAI,GAAG,GAAG,IAAI,KAAK,UAAU,IAAI,CAAC,KAAK;AAwCzD,MAAM,WAAW,CAItB,KACA,eACA,YACyB;AAEzB,QAAM,aAAa,SAAS,cAAc;AAC1C,QAAM,Y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olean;
|
|
4
|
-
isInvalidated: boolean;
|
|
5
|
-
isSuccess: boolean;
|
|
6
|
-
isDisabled: boolean;
|
|
7
|
-
isWaitingData: boolean;
|
|
8
|
-
isRevalidating: boolean;
|
|
9
|
-
error: string | null;
|
|
10
|
-
data: Awaited<ReturnType<T>> | null;
|
|
11
|
-
errorCount: number;
|
|
12
|
-
revalidate: T;
|
|
13
|
-
setData: (data: Awaited<ReturnType<T> | null>) => void;
|
|
14
|
-
abort: () => void;
|
|
15
|
-
};
|
|
16
|
-
export type UseAsyncOptions<T extends (...args: any[]) => Promise<any>> = {
|
|
17
|
-
retryLimit?: number;
|
|
18
|
-
retryTime?: number;
|
|
19
|
-
cache?: boolean;
|
|
20
|
-
store?: boolean;
|
|
21
|
-
enable?: boolean;
|
|
22
|
-
autoFetch?: boolean;
|
|
23
|
-
revalidation?: boolean;
|
|
24
|
-
revalidateTime?: number;
|
|
25
|
-
invalidateQueries?: string[];
|
|
26
|
-
updateQueries?: string[];
|
|
27
|
-
onSuccess?: (data: Awaited<ReturnType<T>>) => void;
|
|
28
|
-
onError?: (error: string) => void;
|
|
29
|
-
abort?: () => void;
|
|
30
|
-
args?: Parameters<T>;
|
|
31
|
-
};
|
|
32
|
-
export type UseAsyncResult<U extends string, T extends (...args: any[]) => Promise<any>> = UseAsyncResultBase<T> & Record<U, T>;
|
|
33
|
-
/**
|
|
34
|
-
* A custom React hook that manages asynchronous operations, providing easy-to-use states and controls over fetching, caching, and retry mechanisms.
|
|
35
|
-
* This hook abstracts away the complexity of handling loading, error, and success states for any asynchronous function.
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
* ```tsx
|
|
39
|
-
* // Example of using useAsync to manage fetching user data from an API.
|
|
40
|
-
* const fetchUserData = async (userId) => {
|
|
41
|
-
* const response = await fetch(`/api/users/${userId}`);
|
|
42
|
-
* if (!response.ok) throw new Error('Failed to fetch');
|
|
43
|
-
* return await response.json();
|
|
44
|
-
* };
|
|
45
|
-
*
|
|
46
|
-
* const UserDetails = ({ userId }) => {
|
|
47
|
-
* const {
|
|
48
|
-
* isLoading,
|
|
49
|
-
* data,
|
|
50
|
-
* error,
|
|
51
|
-
* revalidate,
|
|
52
|
-
* } = useAsync('userDetails', fetchUserData, {
|
|
53
|
-
* cache: true,
|
|
54
|
-
* revalidateTime: 60000, // 1 minute
|
|
55
|
-
* autoFetch: true,
|
|
56
|
-
* onSuccess: (data) => console.log('User data fetched successfully:', data),
|
|
57
|
-
* onError: (error) => console.error('Error fetching user data:', error),
|
|
58
|
-
* });
|
|
59
|
-
*
|
|
60
|
-
* if (isLoading) return <div>Loading...</div>;
|
|
61
|
-
* if (error) return <div>Error: {error}</div>;
|
|
62
|
-
* return (
|
|
63
|
-
* <div>
|
|
64
|
-
* <h1>{data.name}</h1>
|
|
65
|
-
* <button onClick={() => revalidate()}>Refresh</button>
|
|
66
|
-
* </div>
|
|
67
|
-
* );
|
|
68
|
-
* };
|
|
69
|
-
* ```
|
|
70
|
-
*/
|
|
71
|
-
export declare const useAsync: <U extends string, T extends (...args: any[]) => Promise<any>>(key: U, asyncFunction: T, options?: UseAsyncOptions<T>) => UseAsyncResult<U, T>;
|
|
72
|
-
export {};
|
|
73
|
-
//# sourceMappingURL=useAsync.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useAsync.d.ts","sourceRoot":"","sources":["../../../src/hooks/useAsync/useAsync.ts"],"names":[],"mappings":"AASA,KAAK,kBAAkB,CAAC,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,GAAG,CAAC,IAAI;IACpE,SAAS,EAAE,OAAO,CAAC;IACnB,SAAS,EAAE,OAAO,CAAC;IACnB,aAAa,EAAE,OAAO,CAAC;IACvB,SAAS,EAAE,OAAO,CAAC;IACnB,UAAU,EAAE,OAAO,CAAC;IACpB,aAAa,EAAE,OAAO,CAAC;IACvB,cAAc,EAAE,OAAO,CAAC;IACxB,KAAK,EAAE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,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AAQzC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAqCG;AACH,eAAO,MAAM,QAAQ,GACnB,CAAC,SAAS,MAAM,EAChB,CAAC,SAAS,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,KAAK,OAAO,CAAC,GAAG,CAAC,EAE1C,KAAK,CAAC,EACN,eAAe,CAAC,EAChB,UAAU,eAAe,CAAC,CAAC,CAAC,KAC3B,cAAc,CAAC,CAAC,EAAE,CAAC,CAmWrB,CAAC"}
|
|
@@ -1,244 +0,0 @@
|
|
|
1
|
-
"use client";
|
|
2
|
-
import { useRef, useEffect } from "react";
|
|
3
|
-
import { useAsyncState } from "./useAsyncStateStore.mjs";
|
|
4
|
-
const pendingPromises = /* @__PURE__ */ new Map();
|
|
5
|
-
const DEFAULT_CACHE_ENABLED = false;
|
|
6
|
-
const DEFAULT_STORE_ENABLED = false;
|
|
7
|
-
const DEFAULT_ENABLED = true;
|
|
8
|
-
const DEFAULT_AUTO_FETCH = false;
|
|
9
|
-
const DEFAULT_RETRY_LIMIT = 1;
|
|
10
|
-
const DEFAULT_REVALIDATION_ENABLED = false;
|
|
11
|
-
const DEFAULT_REVALIDATE_TIME = 5 * 60 * 1e3;
|
|
12
|
-
const DEFAULT_RETRY_TIME = 5 * 60 * 1e3;
|
|
13
|
-
const getArgs = (args) => args ? Array.isArray(args) ? args : [args] : [];
|
|
14
|
-
const getKeyWithArgs = (key, args) => getArgs(args).length > 0 ? `${key}/${JSON.stringify(args)}` : key;
|
|
15
|
-
const useAsync = (key, asyncFunction, options) => {
|
|
16
|
-
const retryLimit = options?.retryLimit ?? DEFAULT_RETRY_LIMIT;
|
|
17
|
-
const autoFetch = options?.autoFetch ?? DEFAULT_AUTO_FETCH;
|
|
18
|
-
const retryTime = options?.retryTime ?? DEFAULT_RETRY_TIME;
|
|
19
|
-
const cacheEnabled = options?.cache ?? DEFAULT_CACHE_ENABLED;
|
|
20
|
-
const storeEnabled = options?.store ?? DEFAULT_STORE_ENABLED;
|
|
21
|
-
const enabled = options?.enable ?? DEFAULT_ENABLED;
|
|
22
|
-
const revalidationEnabled = options?.revalidation ?? DEFAULT_REVALIDATION_ENABLED;
|
|
23
|
-
const revalidateTime = options?.revalidateTime ?? DEFAULT_REVALIDATE_TIME;
|
|
24
|
-
const updateQueries = options?.updateQueries ?? [];
|
|
25
|
-
const invalidateQueries = options?.invalidateQueries ?? [];
|
|
26
|
-
const onSuccess = options?.onSuccess;
|
|
27
|
-
const onError = options?.onError;
|
|
28
|
-
const args = getArgs(options?.args ?? []);
|
|
29
|
-
const { getStates, setQueryState, setQueriesState, makeQueryInError } = useAsyncState();
|
|
30
|
-
const storedArgsRef = useRef(args);
|
|
31
|
-
const controllerRef = useRef(null);
|
|
32
|
-
const keyWithArgs = getKeyWithArgs(key, storedArgsRef.current);
|
|
33
|
-
const {
|
|
34
|
-
isFetched,
|
|
35
|
-
fetchedDateTime,
|
|
36
|
-
isLoading,
|
|
37
|
-
isEnabled,
|
|
38
|
-
error,
|
|
39
|
-
isSuccess,
|
|
40
|
-
isInvalidated,
|
|
41
|
-
data,
|
|
42
|
-
errorCount
|
|
43
|
-
} = getStates(keyWithArgs);
|
|
44
|
-
const fetch = (async (...args2) => {
|
|
45
|
-
const keyWithArgs2 = getKeyWithArgs(key, args2);
|
|
46
|
-
if (controllerRef.current) {
|
|
47
|
-
controllerRef.current.abort();
|
|
48
|
-
}
|
|
49
|
-
const controller = new AbortController();
|
|
50
|
-
controllerRef.current = controller;
|
|
51
|
-
if (pendingPromises.has(keyWithArgs2)) {
|
|
52
|
-
return pendingPromises.get(keyWithArgs2);
|
|
53
|
-
}
|
|
54
|
-
const promise = (async () => {
|
|
55
|
-
setQueryState(keyWithArgs2, { isLoading: true });
|
|
56
|
-
let response = null;
|
|
57
|
-
try {
|
|
58
|
-
const asyncResult = asyncFunction(...args2);
|
|
59
|
-
if (!asyncResult || typeof asyncResult.then !== "function") {
|
|
60
|
-
throw new Error("asyncFunction must return a Promise");
|
|
61
|
-
}
|
|
62
|
-
const result = await asyncResult;
|
|
63
|
-
response = result;
|
|
64
|
-
setQueryState(keyWithArgs2, {
|
|
65
|
-
data: result,
|
|
66
|
-
errorCount: 0,
|
|
67
|
-
isLoading: false,
|
|
68
|
-
isFetched: true,
|
|
69
|
-
fetchedDateTime: /* @__PURE__ */ new Date(),
|
|
70
|
-
isSuccess: true,
|
|
71
|
-
isInvalidated: false,
|
|
72
|
-
error: null
|
|
73
|
-
});
|
|
74
|
-
onSuccess?.(result);
|
|
75
|
-
if (invalidateQueries.length > 0) {
|
|
76
|
-
setQueriesState(invalidateQueries, {
|
|
77
|
-
isInvalidated: true
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
if (updateQueries.length > 0) {
|
|
81
|
-
setQueriesState(updateQueries, {
|
|
82
|
-
data: result
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
if (storeEnabled) {
|
|
86
|
-
localStorage.setItem(keyWithArgs2, JSON.stringify(result));
|
|
87
|
-
}
|
|
88
|
-
} catch (error2) {
|
|
89
|
-
const msg = error2 instanceof Error ? error2.message : String(error2);
|
|
90
|
-
makeQueryInError(keyWithArgs2, msg);
|
|
91
|
-
onError?.(msg);
|
|
92
|
-
} finally {
|
|
93
|
-
pendingPromises.delete(keyWithArgs2);
|
|
94
|
-
}
|
|
95
|
-
return response;
|
|
96
|
-
})();
|
|
97
|
-
pendingPromises.set(keyWithArgs2, promise);
|
|
98
|
-
return await promise;
|
|
99
|
-
});
|
|
100
|
-
const revalidate = (async (...args2) => {
|
|
101
|
-
if (!isEnabled || !enabled) return;
|
|
102
|
-
if (args2) {
|
|
103
|
-
storedArgsRef.current = getArgs(args2);
|
|
104
|
-
}
|
|
105
|
-
return await fetch(...storedArgsRef.current);
|
|
106
|
-
});
|
|
107
|
-
const execute = (async (...args2) => {
|
|
108
|
-
if (!isEnabled || !enabled) return;
|
|
109
|
-
if (isLoading) return;
|
|
110
|
-
const shouldReturnData = !isInvalidated && // If data are invalidated, we should refetch to revalidate the data
|
|
111
|
-
isSuccess && cacheEnabled && data;
|
|
112
|
-
if (shouldReturnData) return data;
|
|
113
|
-
return await revalidate(...args2);
|
|
114
|
-
});
|
|
115
|
-
useEffect(() => {
|
|
116
|
-
if (enabled !== isEnabled) {
|
|
117
|
-
setQueryState(keyWithArgs, {
|
|
118
|
-
isEnabled
|
|
119
|
-
});
|
|
120
|
-
}
|
|
121
|
-
}, [enabled, isEnabled, keyWithArgs]);
|
|
122
|
-
useEffect(() => {
|
|
123
|
-
if (!isEnabled || !enabled) return;
|
|
124
|
-
if (!storeEnabled) return;
|
|
125
|
-
if (isInvalidated || isFetched || data) return;
|
|
126
|
-
const storedData = localStorage.getItem(keyWithArgs);
|
|
127
|
-
try {
|
|
128
|
-
if (storedData) {
|
|
129
|
-
setQueryState(keyWithArgs, {
|
|
130
|
-
data: JSON.parse(storedData)
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
} catch (error2) {
|
|
134
|
-
console.error(error2);
|
|
135
|
-
}
|
|
136
|
-
}, [
|
|
137
|
-
storeEnabled,
|
|
138
|
-
keyWithArgs,
|
|
139
|
-
isFetched,
|
|
140
|
-
isInvalidated,
|
|
141
|
-
isEnabled,
|
|
142
|
-
enabled,
|
|
143
|
-
data
|
|
144
|
-
]);
|
|
145
|
-
useEffect(() => {
|
|
146
|
-
if (!autoFetch) return;
|
|
147
|
-
if (!isEnabled || !enabled) return;
|
|
148
|
-
if (isFetched && !isInvalidated) return;
|
|
149
|
-
if (isLoading) return;
|
|
150
|
-
fetch(...storedArgsRef.current);
|
|
151
|
-
}, [
|
|
152
|
-
autoFetch,
|
|
153
|
-
isEnabled,
|
|
154
|
-
enabled,
|
|
155
|
-
isFetched,
|
|
156
|
-
isInvalidated,
|
|
157
|
-
isLoading,
|
|
158
|
-
fetch
|
|
159
|
-
]);
|
|
160
|
-
useEffect(() => {
|
|
161
|
-
const isRetryEnabled = errorCount > 0 && retryLimit > 0;
|
|
162
|
-
const isRetryLimitReached = errorCount > retryLimit;
|
|
163
|
-
if (!isEnabled || !enabled) return;
|
|
164
|
-
if (!isRetryEnabled) return;
|
|
165
|
-
if (isRetryLimitReached) return;
|
|
166
|
-
if (isLoading) return;
|
|
167
|
-
if (isSuccess) return;
|
|
168
|
-
const timeout = setTimeout(() => {
|
|
169
|
-
fetch(...storedArgsRef.current);
|
|
170
|
-
}, retryTime);
|
|
171
|
-
return () => clearTimeout(timeout);
|
|
172
|
-
}, [
|
|
173
|
-
isEnabled,
|
|
174
|
-
errorCount,
|
|
175
|
-
retryLimit,
|
|
176
|
-
enabled,
|
|
177
|
-
retryTime,
|
|
178
|
-
isSuccess,
|
|
179
|
-
isLoading,
|
|
180
|
-
fetch
|
|
181
|
-
]);
|
|
182
|
-
useEffect(() => {
|
|
183
|
-
if (!revalidationEnabled || revalidateTime <= 0) return;
|
|
184
|
-
if (!isEnabled || !enabled) return;
|
|
185
|
-
if (isLoading) return;
|
|
186
|
-
if (!isSuccess || !fetchedDateTime) return;
|
|
187
|
-
const timeout = setTimeout(() => {
|
|
188
|
-
fetch(...storedArgsRef.current);
|
|
189
|
-
}, revalidateTime);
|
|
190
|
-
return () => clearTimeout(timeout);
|
|
191
|
-
}, [
|
|
192
|
-
revalidationEnabled,
|
|
193
|
-
revalidateTime,
|
|
194
|
-
isSuccess,
|
|
195
|
-
fetchedDateTime,
|
|
196
|
-
isLoading,
|
|
197
|
-
isEnabled,
|
|
198
|
-
enabled,
|
|
199
|
-
fetch
|
|
200
|
-
]);
|
|
201
|
-
const setDataMemo = (data2) => {
|
|
202
|
-
setQueryState(keyWithArgs, {
|
|
203
|
-
data: data2
|
|
204
|
-
});
|
|
205
|
-
};
|
|
206
|
-
const abort = () => {
|
|
207
|
-
if (controllerRef.current) {
|
|
208
|
-
controllerRef.current.abort();
|
|
209
|
-
}
|
|
210
|
-
};
|
|
211
|
-
const memoResult = {
|
|
212
|
-
isFetched,
|
|
213
|
-
isInvalidated,
|
|
214
|
-
error,
|
|
215
|
-
data,
|
|
216
|
-
errorCount,
|
|
217
|
-
isSuccess,
|
|
218
|
-
isEnabled,
|
|
219
|
-
isDisabled: !isEnabled,
|
|
220
|
-
isLoading,
|
|
221
|
-
isWaitingData: isLoading && !isFetched && !data,
|
|
222
|
-
// Check if the data is still being fetched. Stay at true during revalidation or if data are stored in local storage
|
|
223
|
-
isRevalidating: isLoading && isFetched,
|
|
224
|
-
// Check if the data is valid and is being revalidated
|
|
225
|
-
[key]: execute,
|
|
226
|
-
// Name the execute function as the given key to avoid conflicts with other hooks (e.g. `const { fetchUser } = useAsync('fetchUser', () => fetchUserFunction());`)
|
|
227
|
-
revalidate,
|
|
228
|
-
setData: setDataMemo,
|
|
229
|
-
abort
|
|
230
|
-
};
|
|
231
|
-
useEffect(
|
|
232
|
-
() => () => {
|
|
233
|
-
if (controllerRef.current) {
|
|
234
|
-
controllerRef.current.abort();
|
|
235
|
-
}
|
|
236
|
-
},
|
|
237
|
-
[]
|
|
238
|
-
);
|
|
239
|
-
return memoResult;
|
|
240
|
-
};
|
|
241
|
-
export {
|
|
242
|
-
useAsync
|
|
243
|
-
};
|
|
244
|
-
//# sourceMappingURL=useAsync.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"useAsync.mjs","sources":["../../../src/hooks/useAsync/useAsync.ts"],"sourcesContent":["'use client';\n\nimport { useEffect, useRef } from 'react';\nimport { useAsyncState } from './useAsyncStateStore';\n\n// Pending promises cache to prevent parallel requests when multiple components use the hook\nconst pendingPromises = new Map();\n\n// Defines the base structure for the result of the custom hook.\ntype UseAsyncResultBase<T extends (...args: any[]) => Promise<any>> = {\n isFetched: boolean;\n isLoading: boolean;\n isInvalidated: boolean;\n isSuccess: boolean;\n isDisabled: boolean;\n isWaitingData: boolean;\n isRevalidating: boolean;\n error: string | null;\n data: Awaited<ReturnType<T>> | null;\n errorCount: number;\n revalidate: T;\n setData: (data: Awaited<ReturnType<T> | null>) => void;\n abort: () => void;\n};\n\n// Options type for the hook, allowing customization of behavior.\nexport type UseAsyncOptions<T extends (...args: any[]) => Promise<any>> = {\n retryLimit?: number; // The number of times the hook should retry the function on failure before giving up\n retryTime?: number; // Time in milliseconds for retrying the data\n cache?: boolean; // Cache the result of the function\n store?: boolean; // Store the result of the function in local storage\n enable?: boolean; // Enable the hook\n autoFetch?: boolean; // Automatically fetch the data when the hook is mounted\n revalidation?: boolean; // Enable revalidation\n revalidateTime?: number; // Time in milliseconds for revalidating the data\n invalidateQueries?: string[]; // Invalidate other queries when the data is updated\n updateQueries?: string[]; // Update other queries when the data is updated\n onSuccess?: (data: Awaited<ReturnType<T>>) => void; // Callback function that is called when the asynchronous function resolves successfully\n onError?: (error: string) => void; // Callback function that is called when the asynchronous function rejects or encounters an error\n abort?: () => void; // Abort the request\n args?: Parameters<T>; // Arguments to pass to the asynchronous function\n};\n\n// Default values for the hook's options\nconst DEFAULT_CACHE_ENABLED = false;\nconst DEFAULT_STORE_ENABLED = false;\nconst DEFAULT_ENABLED = true;\nconst DEFAULT_AUTO_FETCH = false;\nconst DEFAULT_RETRY_LIMIT = 1;\nconst DEFAULT_REVALIDATION_ENABLED = false;\nconst DEFAULT_REVALIDATE_TIME = 5 * 60 * 1000; // 5 minutes\nconst DEFAULT_RETRY_TIME = 5 * 60 * 1000; // 5 minutes\n\n// The main hook type that includes the async function along with its additional properties.\nexport type UseAsyncResult<\n U extends string,\n T extends (...args: any[]) => Promise<any>,\n> = UseAsyncResultBase<T> & Record<U, T>;\n\nconst getArgs = (args?: any[]): any[] =>\n args ? (Array.isArray(args) ? args : [args]) : [];\n\nconst getKeyWithArgs = (key: string, args: any[]) =>\n getArgs(args).length > 0 ? `${key}/${JSON.stringify(args)}` : key;\n\n/**\n * A custom React hook that manages asynchronous operations, providing easy-to-use states and controls over fetching, caching, and retry mechanisms.\n * This hook abstracts away the complexity of handling loading, error, and success states for any asynchronous function.\n *\n *\n * ```tsx\n * // Example of using useAsync to manage fetching user data from an API.\n * const fetchUserData = async (userId) => {\n * const response = await fetch(`/api/users/${userId}`);\n * if (!response.ok) throw new Error('Failed to fetch');\n * return await response.json();\n * };\n *\n * const UserDetails = ({ userId }) => {\n * const {\n * isLoading,\n * data,\n * error,\n * revalidate,\n * } = useAsync('userDetails', fetchUserData, {\n * cache: true,\n * revalidateTime: 60000, // 1 minute\n * autoFetch: true,\n * onSuccess: (data) => console.log('User data fetched successfully:', data),\n * onError: (error) => console.error('Error fetching user data:', error),\n * });\n *\n * if (isLoading) return <div>Loading...</div>;\n * if (error) return <div>Error: {error}</div>;\n * return (\n * <div>\n * <h1>{data.name}</h1>\n * <button onClick={() => revalidate()}>Refresh</button>\n * </div>\n * );\n * };\n * ```\n */\nexport const useAsync = <\n U extends string,\n T extends (...args: any[]) => Promise<any>,\n>(\n key: U,\n asyncFunction: T,\n options?: UseAsyncOptions<T>\n): UseAsyncResult<U, T> => {\n // Resolving optional parameters with default values\n const retryLimit = options?.retryLimit ?? DEFAULT_RETRY_LIMIT;\n const autoFetch = options?.autoFetch ?? DEFAULT_AUTO_FETCH;\n const retryTime = options?.retryTime ?? DEFAULT_RETRY_TIME;\n const cacheEnabled = options?.cache ?? DEFAULT_CACHE_ENABLED;\n const storeEnabled = options?.store ?? DEFAULT_STORE_ENABLED;\n const enabled = options?.enable ?? DEFAULT_ENABLED;\n const revalidationEnabled =\n options?.revalidation ?? DEFAULT_REVALIDATION_ENABLED;\n const revalidateTime = options?.revalidateTime ?? DEFAULT_REVALIDATE_TIME;\n const updateQueries = options?.updateQueries ?? [];\n const invalidateQueries = options?.invalidateQueries ?? [];\n const onSuccess = options?.onSuccess;\n const onError = options?.onError;\n const args = getArgs(options?.args ?? []);\n\n // Using a custom hook to manage state specific to asynchronous operations\n const { getStates, setQueryState, setQueriesState, makeQueryInError } =\n useAsyncState();\n\n // Storing the last arguments used to call the async function\n const storedArgsRef = useRef<any[]>(args);\n\n const controllerRef = useRef<AbortController | null>(null);\n\n // Apply different key for different requests\n const keyWithArgs = getKeyWithArgs(key, storedArgsRef.current);\n\n // Retrieving the current state of async operations using the same custom hook\n const {\n isFetched,\n fetchedDateTime,\n isLoading,\n isEnabled,\n error,\n isSuccess,\n isInvalidated,\n data,\n errorCount,\n } = getStates(keyWithArgs);\n\n /**\n * FETCH FUNCTION\n *\n * Manage parallel fetching across multiple instances of the hook\n * Manage state updates on success and error\n * Manage eventual invalidation of other queries\n */\n const fetch: T = (async (...args) => {\n const keyWithArgs = getKeyWithArgs(key, args);\n\n /**\n * ABORT CONTROLLER\n *\n * cancel an unnecessary request.\n * For example, if a user navigates away from a page or triggers a new request that makes the previous one obsolete, you can abort the previous fetch\n */\n if (controllerRef.current) {\n // Abort the previous request\n controllerRef.current.abort();\n }\n\n // Create a new AbortController\n const controller = new AbortController();\n controllerRef.current = controller;\n\n /**\n * PENDING PROMISES\n *\n * This logic ensures that if two parts of your application trigger the same request simultaneously,\n * only one network call is made, and both receive the same result.\n */\n if (pendingPromises.has(keyWithArgs)) {\n // Return the existing pending promise\n return pendingPromises.get(keyWithArgs);\n }\n\n const promise = (async () => {\n setQueryState(keyWithArgs, { isLoading: true });\n let response = null;\n\n try {\n // Ensure asyncFunction returns a Promise\n const asyncResult = asyncFunction(...args);\n\n // Check if the result is a Promise-like object\n if (!asyncResult || typeof asyncResult.then !== 'function') {\n throw new Error('asyncFunction must return a Promise');\n }\n\n const result = await asyncResult;\n response = result;\n\n setQueryState(keyWithArgs, {\n data: result,\n errorCount: 0,\n isLoading: false,\n isFetched: true,\n fetchedDateTime: new Date(),\n isSuccess: true,\n isInvalidated: false,\n error: null,\n });\n\n onSuccess?.(result);\n\n // Invalidate other queries if necessary\n if (invalidateQueries.length > 0) {\n setQueriesState(invalidateQueries, {\n isInvalidated: true,\n });\n }\n\n // Update other queries if necessary\n if (updateQueries.length > 0) {\n setQueriesState(updateQueries, {\n data: result,\n });\n }\n\n // Store the result in local storage\n if (storeEnabled) {\n localStorage.setItem(keyWithArgs, JSON.stringify(result));\n }\n } catch (error) {\n const msg = error instanceof Error ? error.message : String(error);\n\n makeQueryInError(keyWithArgs, msg);\n onError?.(msg);\n } finally {\n // Remove the pending promise from the cache\n pendingPromises.delete(keyWithArgs);\n }\n\n return response;\n })();\n\n // Store the pending promise in the cache\n pendingPromises.set(keyWithArgs, promise);\n\n return await promise;\n }) as T;\n\n /**\n * REVALIDATE FUNCTION\n *\n * Wrap core function to handle revalidation\n * Handle arguments caching\n *\n */\n const revalidate: T = (async (...args) => {\n if (!isEnabled || !enabled) return; // Hook is disabled\n\n if (args) {\n // Revalidation arguments can be different from the initial fetch arguments\n // If arguments are provided, store/update them for future periodic revalidation\n\n storedArgsRef.current = getArgs(args);\n }\n\n return await fetch(...storedArgsRef.current);\n }) as T;\n\n /**\n * EXECUTION FUNCTION\n *\n * Wrap revalidation function\n * If data is valid return it directly to avoid fetching again\n */\n const execute: T = (async (...args) => {\n if (!isEnabled || !enabled) return; // Hook is disabled\n if (isLoading) return; // Fetch is already in progress\n\n const shouldReturnData =\n !isInvalidated && // If data are invalidated, we should refetch to revalidate the data\n isSuccess &&\n cacheEnabled &&\n data;\n\n if (shouldReturnData) return data; // Data are already fetched and should be returned directly. Avoid fetching again.\n\n return await revalidate(...args);\n }) as T;\n\n /**\n * HANDLE SYNCHRONIZATION HOOKS DISACTIVATION\n *\n * If one instance of the hook is disabled, the other instances should be disabled too.\n * This is to prevent inconsistencies in the state of the hook.\n */\n useEffect(() => {\n if (enabled !== isEnabled) {\n setQueryState(keyWithArgs, {\n isEnabled,\n });\n }\n }, [enabled, isEnabled, keyWithArgs]);\n\n /**\n * HANDLE LOCAL STORAGE LOADING\n *\n * If store is enabled, load data from local storage\n */\n useEffect(() => {\n if (!isEnabled || !enabled) return; // Hook is disabled\n if (!storeEnabled) return; // Hook should not use local storage\n if (isInvalidated || isFetched || data) return; // Hook have been already mounted and fetched or invalidated\n\n const storedData = localStorage.getItem(keyWithArgs);\n\n // Wrap parsing in a try-catch block to handle invalid JSON data\n try {\n if (storedData) {\n setQueryState(keyWithArgs, {\n data: JSON.parse(storedData),\n });\n }\n } catch (error) {\n console.error(error);\n }\n }, [\n storeEnabled,\n keyWithArgs,\n isFetched,\n isInvalidated,\n isEnabled,\n enabled,\n data,\n ]);\n\n /**\n * HANDLE AUTO-FETCH ON HOOK MOUNT\n *\n * If autoFetch is enabled, fetch the data when the hook is mounted\n */\n useEffect(() => {\n if (!autoFetch) return; // Auto-fetch is disabled\n if (!isEnabled || !enabled) return; // Hook is disabled\n if (isFetched && !isInvalidated) return; // Hook have already fetched or invalidated\n if (isLoading) return; // Fetch is already in progress\n\n fetch(...storedArgsRef.current);\n }, [\n autoFetch,\n isEnabled,\n enabled,\n isFetched,\n isInvalidated,\n isLoading,\n fetch,\n ]);\n\n /**\n * HANDLE RETRY\n *\n * If fetching fails, retry the fetch after a certain time\n */\n useEffect(() => {\n const isRetryEnabled = errorCount > 0 && retryLimit > 0;\n const isRetryLimitReached = errorCount > retryLimit;\n\n if (!isEnabled || !enabled) return; // Hook is disabled\n if (!isRetryEnabled) return; // Retry is disabled\n if (isRetryLimitReached) return; // Retry limit has been reached\n if (isLoading) return; // Fetch is already in progress\n if (isSuccess) return; // Hook has already fetched successfully\n\n const timeout = setTimeout(() => {\n fetch(...storedArgsRef.current);\n }, retryTime);\n\n return () => clearTimeout(timeout);\n }, [\n isEnabled,\n errorCount,\n retryLimit,\n enabled,\n retryTime,\n isSuccess,\n isLoading,\n fetch,\n ]);\n\n /**\n * HANDLE PERIODIC REVALIDATION\n *\n * If revalidation is enabled, revalidate the data periodically\n */\n useEffect(() => {\n if (!revalidationEnabled || revalidateTime <= 0) return; // Revalidation is disabled\n if (!isEnabled || !enabled) return; // Hook is disabled\n if (isLoading) return; // Fetch is already in progress\n if (!isSuccess || !fetchedDateTime) return; // Should retry either of revalidate\n\n const timeout = setTimeout(() => {\n fetch(...storedArgsRef.current);\n }, revalidateTime);\n\n return () => clearTimeout(timeout);\n }, [\n revalidationEnabled,\n revalidateTime,\n isSuccess,\n fetchedDateTime,\n isLoading,\n isEnabled,\n enabled,\n fetch,\n ]);\n\n // Memoization of the setData function to prevent unnecessary re-renders\n const setDataMemo = (data: Awaited<ReturnType<T> | null>) => {\n setQueryState(keyWithArgs, {\n data,\n });\n };\n\n const abort = () => {\n if (controllerRef.current) {\n controllerRef.current.abort();\n }\n };\n\n // Memoization to prevent unnecessary re-renders\n const memoResult = {\n isFetched,\n isInvalidated,\n error,\n data,\n errorCount,\n isSuccess,\n isEnabled,\n isDisabled: !isEnabled,\n isLoading,\n isWaitingData: isLoading && !isFetched && !data, // Check if the data is still being fetched. Stay at true during revalidation or if data are stored in local storage\n isRevalidating: isLoading && isFetched, // Check if the data is valid and is being revalidated\n [key]: execute, // Name the execute function as the given key to avoid conflicts with other hooks (e.g. `const { fetchUser } = useAsync('fetchUser', () => fetchUserFunction());`)\n revalidate,\n setData: setDataMemo,\n abort,\n };\n\n useEffect(\n () => () => {\n // Clean up the controller on unmount\n if (controllerRef.current) {\n controllerRef.current.abort();\n }\n },\n []\n );\n\n // Return the hook's result, including all state and control functions\n return memoResult as UseAsyncResultBase<T> & Record<U, T>;\n};\n"],"names":["args","keyWithArgs","error","data"],"mappings":";;;AAMA,MAAM,sCAAsB,IAAA;AAsC5B,MAAM,wBAAwB;AAC9B,MAAM,wBAAwB;AAC9B,MAAM,kBAAkB;AACxB,MAAM,qBAAqB;AAC3B,MAAM,sBAAsB;AAC5B,MAAM,+BAA+B;AACrC,MAAM,0BAA0B,IAAI,KAAK;AACzC,MAAM,qBAAqB,IAAI,KAAK;AAQpC,MAAM,UAAU,CAAC,SACf,OAAQ,MAAM,QAAQ,IAAI,IAAI,OAAO,CAAC,IAAI,IAAK,CAAA;AAEjD,MAAM,iBAAiB,CAAC,KAAa,SACnC,QAAQ,IAAI,EAAE,SAAS,IAAI,GAAG,GAAG,IAAI,KAAK,UAAU,IAAI,CAAC,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