@metamask/snap-networks-utils 1.0.0 → 2.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/CHANGELOG.md +63 -1
- package/README.md +4 -10
- package/dist/index.cjs +102 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +51 -2
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +51 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +32 -1
- package/dist/index.mjs.map +1 -1
- package/dist/providers/assets/AssetsProvider.cjs +36 -18
- package/dist/providers/assets/AssetsProvider.cjs.map +1 -1
- package/dist/providers/assets/AssetsProvider.d.cts +30 -2
- package/dist/providers/assets/AssetsProvider.d.cts.map +1 -1
- package/dist/providers/assets/AssetsProvider.d.mts +30 -2
- package/dist/providers/assets/AssetsProvider.d.mts.map +1 -1
- package/dist/providers/assets/AssetsProvider.mjs +36 -18
- package/dist/providers/assets/AssetsProvider.mjs.map +1 -1
- package/dist/providers/remote-feature-flags/RemoteFeatureFlagsProvider.cjs +4 -17
- package/dist/providers/remote-feature-flags/RemoteFeatureFlagsProvider.cjs.map +1 -1
- package/dist/providers/remote-feature-flags/RemoteFeatureFlagsProvider.mjs +4 -17
- package/dist/providers/remote-feature-flags/RemoteFeatureFlagsProvider.mjs.map +1 -1
- package/dist/services/analytics/AnalyticsService.cjs +225 -0
- package/dist/services/analytics/AnalyticsService.cjs.map +1 -0
- package/dist/services/analytics/AnalyticsService.d.cts +175 -0
- package/dist/services/analytics/AnalyticsService.d.cts.map +1 -0
- package/dist/services/analytics/AnalyticsService.d.mts +175 -0
- package/dist/services/analytics/AnalyticsService.d.mts.map +1 -0
- package/dist/services/analytics/AnalyticsService.mjs +221 -0
- package/dist/services/analytics/AnalyticsService.mjs.map +1 -0
- package/dist/ui/ActionHeader/ActionHeader.cjs +23 -0
- package/dist/ui/ActionHeader/ActionHeader.cjs.map +1 -0
- package/dist/ui/ActionHeader/ActionHeader.d.cts +27 -0
- package/dist/ui/ActionHeader/ActionHeader.d.cts.map +1 -0
- package/dist/ui/ActionHeader/ActionHeader.d.mts +27 -0
- package/dist/ui/ActionHeader/ActionHeader.d.mts.map +1 -0
- package/dist/ui/ActionHeader/ActionHeader.mjs +19 -0
- package/dist/ui/ActionHeader/ActionHeader.mjs.map +1 -0
- package/dist/ui/Asset/Asset.cjs +68 -0
- package/dist/ui/Asset/Asset.cjs.map +1 -0
- package/dist/ui/Asset/Asset.d.cts +83 -0
- package/dist/ui/Asset/Asset.d.cts.map +1 -0
- package/dist/ui/Asset/Asset.d.mts +83 -0
- package/dist/ui/Asset/Asset.d.mts.map +1 -0
- package/dist/ui/Asset/Asset.mjs +60 -0
- package/dist/ui/Asset/Asset.mjs.map +1 -0
- package/dist/ui/Asset/question-mark.svg +3 -0
- package/dist/ui/EstimatedChanges/EstimatedChanges.cjs +57 -0
- package/dist/ui/EstimatedChanges/EstimatedChanges.cjs.map +1 -0
- package/dist/ui/EstimatedChanges/EstimatedChanges.d.cts +50 -0
- package/dist/ui/EstimatedChanges/EstimatedChanges.d.cts.map +1 -0
- package/dist/ui/EstimatedChanges/EstimatedChanges.d.mts +50 -0
- package/dist/ui/EstimatedChanges/EstimatedChanges.d.mts.map +1 -0
- package/dist/ui/EstimatedChanges/EstimatedChanges.mjs +53 -0
- package/dist/ui/EstimatedChanges/EstimatedChanges.mjs.map +1 -0
- package/dist/ui/TransactionAlert/TransactionAlert.cjs +72 -0
- package/dist/ui/TransactionAlert/TransactionAlert.cjs.map +1 -0
- package/dist/ui/TransactionAlert/TransactionAlert.d.cts +58 -0
- package/dist/ui/TransactionAlert/TransactionAlert.d.cts.map +1 -0
- package/dist/ui/TransactionAlert/TransactionAlert.d.mts +58 -0
- package/dist/ui/TransactionAlert/TransactionAlert.d.mts.map +1 -0
- package/dist/ui/TransactionAlert/TransactionAlert.mjs +68 -0
- package/dist/ui/TransactionAlert/TransactionAlert.mjs.map +1 -0
- package/dist/utils/async/async.cjs +95 -0
- package/dist/utils/async/async.cjs.map +1 -0
- package/dist/utils/async/async.d.cts +51 -0
- package/dist/utils/async/async.d.cts.map +1 -0
- package/dist/utils/async/async.d.mts +51 -0
- package/dist/utils/async/async.d.mts.map +1 -0
- package/dist/utils/async/async.mjs +88 -0
- package/dist/utils/async/async.mjs.map +1 -0
- package/dist/utils/buildUrl/buildUrl.cjs +54 -0
- package/dist/utils/buildUrl/buildUrl.cjs.map +1 -0
- package/dist/utils/buildUrl/buildUrl.d.cts +15 -0
- package/dist/utils/buildUrl/buildUrl.d.cts.map +1 -0
- package/dist/utils/buildUrl/buildUrl.d.mts +15 -0
- package/dist/utils/buildUrl/buildUrl.d.mts.map +1 -0
- package/dist/utils/buildUrl/buildUrl.mjs +51 -0
- package/dist/utils/buildUrl/buildUrl.mjs.map +1 -0
- package/dist/utils/cache/InMemoryCache.cjs +134 -0
- package/dist/utils/cache/InMemoryCache.cjs.map +1 -0
- package/dist/utils/cache/InMemoryCache.d.cts +29 -0
- package/dist/utils/cache/InMemoryCache.d.cts.map +1 -0
- package/dist/utils/cache/InMemoryCache.d.mts +29 -0
- package/dist/utils/cache/InMemoryCache.d.mts.map +1 -0
- package/dist/utils/cache/InMemoryCache.mjs +130 -0
- package/dist/utils/cache/InMemoryCache.mjs.map +1 -0
- package/dist/utils/cache/StateCache.cjs +193 -0
- package/dist/utils/cache/StateCache.cjs.map +1 -0
- package/dist/utils/cache/StateCache.d.cts +96 -0
- package/dist/utils/cache/StateCache.d.cts.map +1 -0
- package/dist/utils/cache/StateCache.d.mts +96 -0
- package/dist/utils/cache/StateCache.d.mts.map +1 -0
- package/dist/utils/cache/StateCache.mjs +189 -0
- package/dist/utils/cache/StateCache.mjs.map +1 -0
- package/dist/utils/cache/types.cjs +3 -0
- package/dist/utils/cache/types.cjs.map +1 -0
- package/dist/utils/cache/types.d.cts +99 -0
- package/dist/utils/cache/types.d.cts.map +1 -0
- package/dist/utils/cache/types.d.mts +99 -0
- package/dist/utils/cache/types.d.mts.map +1 -0
- package/dist/utils/cache/types.mjs +2 -0
- package/dist/utils/cache/types.mjs.map +1 -0
- package/dist/utils/cache/useCache.cjs +82 -0
- package/dist/utils/cache/useCache.cjs.map +1 -0
- package/dist/utils/cache/useCache.d.cts +57 -0
- package/dist/utils/cache/useCache.d.cts.map +1 -0
- package/dist/utils/cache/useCache.d.mts +57 -0
- package/dist/utils/cache/useCache.d.mts.map +1 -0
- package/dist/utils/cache/useCache.mjs +78 -0
- package/dist/utils/cache/useCache.mjs.map +1 -0
- package/dist/utils/cache/useCacheUntil.cjs +80 -0
- package/dist/utils/cache/useCacheUntil.cjs.map +1 -0
- package/dist/utils/cache/useCacheUntil.d.cts +54 -0
- package/dist/utils/cache/useCacheUntil.d.cts.map +1 -0
- package/dist/utils/cache/useCacheUntil.d.mts +54 -0
- package/dist/utils/cache/useCacheUntil.d.mts.map +1 -0
- package/dist/utils/cache/useCacheUntil.mjs +76 -0
- package/dist/utils/cache/useCacheUntil.mjs.map +1 -0
- package/dist/utils/config/BaseConfigProvider.cjs +62 -0
- package/dist/utils/config/BaseConfigProvider.cjs.map +1 -0
- package/dist/utils/config/BaseConfigProvider.d.cts +54 -0
- package/dist/utils/config/BaseConfigProvider.d.cts.map +1 -0
- package/dist/utils/config/BaseConfigProvider.d.mts +54 -0
- package/dist/utils/config/BaseConfigProvider.d.mts.map +1 -0
- package/dist/utils/config/BaseConfigProvider.mjs +58 -0
- package/dist/utils/config/BaseConfigProvider.mjs.map +1 -0
- package/dist/utils/config/envStructs.cjs +69 -0
- package/dist/utils/config/envStructs.cjs.map +1 -0
- package/dist/utils/config/envStructs.d.cts +57 -0
- package/dist/utils/config/envStructs.d.cts.map +1 -0
- package/dist/utils/config/envStructs.d.mts +57 -0
- package/dist/utils/config/envStructs.d.mts.map +1 -0
- package/dist/utils/config/envStructs.mjs +61 -0
- package/dist/utils/config/envStructs.mjs.map +1 -0
- package/dist/utils/config/environmentStruct.cjs +19 -0
- package/dist/utils/config/environmentStruct.cjs.map +1 -0
- package/dist/utils/config/environmentStruct.d.cts +20 -0
- package/dist/utils/config/environmentStruct.d.cts.map +1 -0
- package/dist/utils/config/environmentStruct.d.mts +20 -0
- package/dist/utils/config/environmentStruct.d.mts.map +1 -0
- package/dist/utils/config/environmentStruct.mjs +16 -0
- package/dist/utils/config/environmentStruct.mjs.map +1 -0
- package/dist/utils/config/logLevelStruct.cjs +14 -0
- package/dist/utils/config/logLevelStruct.cjs.map +1 -0
- package/dist/utils/config/logLevelStruct.d.cts +17 -0
- package/dist/utils/config/logLevelStruct.d.cts.map +1 -0
- package/dist/utils/config/logLevelStruct.d.mts +17 -0
- package/dist/utils/config/logLevelStruct.d.mts.map +1 -0
- package/dist/utils/config/logLevelStruct.mjs +11 -0
- package/dist/utils/config/logLevelStruct.mjs.map +1 -0
- package/dist/utils/config/parseEnv.cjs +30 -0
- package/dist/utils/config/parseEnv.cjs.map +1 -0
- package/dist/utils/config/parseEnv.d.cts +19 -0
- package/dist/utils/config/parseEnv.d.cts.map +1 -0
- package/dist/utils/config/parseEnv.d.mts +19 -0
- package/dist/utils/config/parseEnv.d.mts.map +1 -0
- package/dist/utils/config/parseEnv.mjs +26 -0
- package/dist/utils/config/parseEnv.mjs.map +1 -0
- package/dist/utils/dedupe/InFlightCoalescer.cjs +28 -0
- package/dist/utils/dedupe/InFlightCoalescer.cjs.map +1 -0
- package/dist/utils/dedupe/InFlightCoalescer.d.cts +12 -0
- package/dist/utils/dedupe/InFlightCoalescer.d.cts.map +1 -0
- package/dist/utils/dedupe/InFlightCoalescer.d.mts +12 -0
- package/dist/utils/dedupe/InFlightCoalescer.d.mts.map +1 -0
- package/dist/utils/dedupe/InFlightCoalescer.mjs +24 -0
- package/dist/utils/dedupe/InFlightCoalescer.mjs.map +1 -0
- package/dist/utils/errors/errors.cjs +44 -0
- package/dist/utils/errors/errors.cjs.map +1 -0
- package/dist/utils/errors/errors.d.cts +37 -0
- package/dist/utils/errors/errors.d.cts.map +1 -0
- package/dist/utils/errors/errors.d.mts +37 -0
- package/dist/utils/errors/errors.d.mts.map +1 -0
- package/dist/utils/errors/errors.mjs +40 -0
- package/dist/utils/errors/errors.mjs.map +1 -0
- package/dist/utils/errors/index.cjs +18 -0
- package/dist/utils/errors/index.cjs.map +1 -0
- package/dist/utils/errors/index.d.cts +10 -0
- package/dist/utils/errors/index.d.cts.map +1 -0
- package/dist/utils/errors/index.d.mts +10 -0
- package/dist/utils/errors/index.d.mts.map +1 -0
- package/dist/utils/errors/index.mjs +6 -0
- package/dist/utils/errors/index.mjs.map +1 -0
- package/dist/utils/errors/snapRpcError.cjs +33 -0
- package/dist/utils/errors/snapRpcError.cjs.map +1 -0
- package/dist/utils/errors/snapRpcError.d.cts +11 -0
- package/dist/utils/errors/snapRpcError.d.cts.map +1 -0
- package/dist/utils/errors/snapRpcError.d.mts +11 -0
- package/dist/utils/errors/snapRpcError.d.mts.map +1 -0
- package/dist/utils/errors/snapRpcError.mjs +30 -0
- package/dist/utils/errors/snapRpcError.mjs.map +1 -0
- package/dist/utils/errors/stringifyReason.cjs +36 -0
- package/dist/utils/errors/stringifyReason.cjs.map +1 -0
- package/dist/utils/errors/stringifyReason.d.cts +15 -0
- package/dist/utils/errors/stringifyReason.d.cts.map +1 -0
- package/dist/utils/errors/stringifyReason.d.mts +15 -0
- package/dist/utils/errors/stringifyReason.d.mts.map +1 -0
- package/dist/utils/errors/stringifyReason.mjs +33 -0
- package/dist/utils/errors/stringifyReason.mjs.map +1 -0
- package/dist/utils/errors/syncError.cjs +98 -0
- package/dist/utils/errors/syncError.cjs.map +1 -0
- package/dist/utils/errors/syncError.d.cts +60 -0
- package/dist/utils/errors/syncError.d.cts.map +1 -0
- package/dist/utils/errors/syncError.d.mts +60 -0
- package/dist/utils/errors/syncError.d.mts.map +1 -0
- package/dist/utils/errors/syncError.mjs +92 -0
- package/dist/utils/errors/syncError.mjs.map +1 -0
- package/dist/utils/errors/trackError.cjs +76 -0
- package/dist/utils/errors/trackError.cjs.map +1 -0
- package/dist/utils/errors/trackError.d.cts +64 -0
- package/dist/utils/errors/trackError.d.cts.map +1 -0
- package/dist/utils/errors/trackError.d.mts +64 -0
- package/dist/utils/errors/trackError.d.mts.map +1 -0
- package/dist/utils/errors/trackError.mjs +72 -0
- package/dist/utils/errors/trackError.mjs.map +1 -0
- package/dist/utils/handlers/wrapSnapHandlers.cjs +57 -0
- package/dist/utils/handlers/wrapSnapHandlers.cjs.map +1 -0
- package/dist/utils/handlers/wrapSnapHandlers.d.cts +74 -0
- package/dist/utils/handlers/wrapSnapHandlers.d.cts.map +1 -0
- package/dist/utils/handlers/wrapSnapHandlers.d.mts +74 -0
- package/dist/utils/handlers/wrapSnapHandlers.d.mts.map +1 -0
- package/dist/utils/handlers/wrapSnapHandlers.mjs +53 -0
- package/dist/utils/handlers/wrapSnapHandlers.mjs.map +1 -0
- package/dist/utils/keyringAccount/keyringAccount.cjs +25 -0
- package/dist/utils/keyringAccount/keyringAccount.cjs.map +1 -0
- package/dist/utils/keyringAccount/keyringAccount.d.cts +29 -0
- package/dist/utils/keyringAccount/keyringAccount.d.cts.map +1 -0
- package/dist/utils/keyringAccount/keyringAccount.d.mts +29 -0
- package/dist/utils/keyringAccount/keyringAccount.d.mts.map +1 -0
- package/dist/utils/keyringAccount/keyringAccount.mjs +22 -0
- package/dist/utils/keyringAccount/keyringAccount.mjs.map +1 -0
- package/dist/utils/logger/Logger.cjs +140 -0
- package/dist/utils/logger/Logger.cjs.map +1 -0
- package/dist/utils/logger/Logger.d.cts +118 -0
- package/dist/utils/logger/Logger.d.cts.map +1 -0
- package/dist/utils/logger/Logger.d.mts +118 -0
- package/dist/utils/logger/Logger.d.mts.map +1 -0
- package/dist/utils/logger/Logger.mjs +136 -0
- package/dist/utils/logger/Logger.mjs.map +1 -0
- package/dist/utils/logger/__mocks__/Logger.cjs +26 -0
- package/dist/utils/logger/__mocks__/Logger.cjs.map +1 -0
- package/dist/utils/logger/__mocks__/Logger.d.cts +3 -0
- package/dist/utils/logger/__mocks__/Logger.d.cts.map +1 -0
- package/dist/utils/logger/__mocks__/Logger.d.mts +3 -0
- package/dist/utils/logger/__mocks__/Logger.d.mts.map +1 -0
- package/dist/utils/logger/__mocks__/Logger.mjs +23 -0
- package/dist/utils/logger/__mocks__/Logger.mjs.map +1 -0
- package/dist/utils/originPermissions/createOriginPermissions.cjs +30 -0
- package/dist/utils/originPermissions/createOriginPermissions.cjs.map +1 -0
- package/dist/utils/originPermissions/createOriginPermissions.d.cts +24 -0
- package/dist/utils/originPermissions/createOriginPermissions.d.cts.map +1 -0
- package/dist/utils/originPermissions/createOriginPermissions.d.mts +24 -0
- package/dist/utils/originPermissions/createOriginPermissions.d.mts.map +1 -0
- package/dist/utils/originPermissions/createOriginPermissions.mjs +26 -0
- package/dist/utils/originPermissions/createOriginPermissions.mjs.map +1 -0
- package/dist/utils/originPermissions/validateOrigin.cjs +23 -0
- package/dist/utils/originPermissions/validateOrigin.cjs.map +1 -0
- package/dist/utils/originPermissions/validateOrigin.d.cts +10 -0
- package/dist/utils/originPermissions/validateOrigin.d.cts.map +1 -0
- package/dist/utils/originPermissions/validateOrigin.d.mts +10 -0
- package/dist/utils/originPermissions/validateOrigin.d.mts.map +1 -0
- package/dist/utils/originPermissions/validateOrigin.mjs +19 -0
- package/dist/utils/originPermissions/validateOrigin.mjs.map +1 -0
- package/dist/utils/proofOfOwnership/proofOfOwnership.cjs +89 -0
- package/dist/utils/proofOfOwnership/proofOfOwnership.cjs.map +1 -0
- package/dist/utils/proofOfOwnership/proofOfOwnership.d.cts +143 -0
- package/dist/utils/proofOfOwnership/proofOfOwnership.d.cts.map +1 -0
- package/dist/utils/proofOfOwnership/proofOfOwnership.d.mts +143 -0
- package/dist/utils/proofOfOwnership/proofOfOwnership.d.mts.map +1 -0
- package/dist/utils/proofOfOwnership/proofOfOwnership.mjs +85 -0
- package/dist/utils/proofOfOwnership/proofOfOwnership.mjs.map +1 -0
- package/dist/utils/safeMerge/safeMerge.cjs +26 -0
- package/dist/utils/safeMerge/safeMerge.cjs.map +1 -0
- package/dist/utils/safeMerge/safeMerge.d.cts +17 -0
- package/dist/utils/safeMerge/safeMerge.d.cts.map +1 -0
- package/dist/utils/safeMerge/safeMerge.d.mts +17 -0
- package/dist/utils/safeMerge/safeMerge.d.mts.map +1 -0
- package/dist/utils/safeMerge/safeMerge.mjs +22 -0
- package/dist/utils/safeMerge/safeMerge.mjs.map +1 -0
- package/dist/utils/sanitize/sanitize.cjs +42 -0
- package/dist/utils/sanitize/sanitize.cjs.map +1 -0
- package/dist/utils/sanitize/sanitize.d.cts +15 -0
- package/dist/utils/sanitize/sanitize.d.cts.map +1 -0
- package/dist/utils/sanitize/sanitize.d.mts +15 -0
- package/dist/utils/sanitize/sanitize.d.mts.map +1 -0
- package/dist/utils/sanitize/sanitize.mjs +38 -0
- package/dist/utils/sanitize/sanitize.mjs.map +1 -0
- package/dist/utils/serialization/serialization.cjs +73 -0
- package/dist/utils/serialization/serialization.cjs.map +1 -0
- package/dist/utils/serialization/serialization.d.cts +19 -0
- package/dist/utils/serialization/serialization.d.cts.map +1 -0
- package/dist/utils/serialization/serialization.d.mts +19 -0
- package/dist/utils/serialization/serialization.d.mts.map +1 -0
- package/dist/utils/serialization/serialization.mjs +69 -0
- package/dist/utils/serialization/serialization.mjs.map +1 -0
- package/dist/utils/serialization/types.cjs +3 -0
- package/dist/utils/serialization/types.cjs.map +1 -0
- package/dist/utils/serialization/types.d.cts +8 -0
- package/dist/utils/serialization/types.d.cts.map +1 -0
- package/dist/utils/serialization/types.d.mts +8 -0
- package/dist/utils/serialization/types.d.mts.map +1 -0
- package/dist/utils/serialization/types.mjs +2 -0
- package/dist/utils/serialization/types.mjs.map +1 -0
- package/dist/utils/state/IStateManager.cjs +3 -0
- package/dist/utils/state/IStateManager.cjs.map +1 -0
- package/dist/utils/state/IStateManager.d.cts +69 -0
- package/dist/utils/state/IStateManager.d.cts.map +1 -0
- package/dist/utils/state/IStateManager.d.mts +69 -0
- package/dist/utils/state/IStateManager.d.mts.map +1 -0
- package/dist/utils/state/IStateManager.mjs +2 -0
- package/dist/utils/state/IStateManager.mjs.map +1 -0
- package/dist/utils/state/InMemoryState.cjs +41 -0
- package/dist/utils/state/InMemoryState.cjs.map +1 -0
- package/dist/utils/state/InMemoryState.d.cts +18 -0
- package/dist/utils/state/InMemoryState.d.cts.map +1 -0
- package/dist/utils/state/InMemoryState.d.mts +18 -0
- package/dist/utils/state/InMemoryState.d.mts.map +1 -0
- package/dist/utils/state/InMemoryState.mjs +38 -0
- package/dist/utils/state/InMemoryState.mjs.map +1 -0
- package/dist/utils/state/State.cjs +159 -0
- package/dist/utils/state/State.cjs.map +1 -0
- package/dist/utils/state/State.d.cts +25 -0
- package/dist/utils/state/State.d.cts.map +1 -0
- package/dist/utils/state/State.d.mts +25 -0
- package/dist/utils/state/State.d.mts.map +1 -0
- package/dist/utils/state/State.mjs +156 -0
- package/dist/utils/state/State.mjs.map +1 -0
- package/dist/utils/transactionType/transactionType.cjs +32 -0
- package/dist/utils/transactionType/transactionType.cjs.map +1 -0
- package/dist/utils/transactionType/transactionType.d.cts +17 -0
- package/dist/utils/transactionType/transactionType.d.cts.map +1 -0
- package/dist/utils/transactionType/transactionType.d.mts +17 -0
- package/dist/utils/transactionType/transactionType.d.mts.map +1 -0
- package/dist/utils/transactionType/transactionType.mjs +28 -0
- package/dist/utils/transactionType/transactionType.mjs.map +1 -0
- package/dist/utils/urlStruct/urlStruct.cjs +96 -0
- package/dist/utils/urlStruct/urlStruct.cjs.map +1 -0
- package/dist/utils/urlStruct/urlStruct.d.cts +25 -0
- package/dist/utils/urlStruct/urlStruct.d.cts.map +1 -0
- package/dist/utils/urlStruct/urlStruct.d.mts +25 -0
- package/dist/utils/urlStruct/urlStruct.d.mts.map +1 -0
- package/dist/utils/urlStruct/urlStruct.mjs +93 -0
- package/dist/utils/urlStruct/urlStruct.mjs.map +1 -0
- package/dist/utils/uuidStruct/uuidStruct.cjs +12 -0
- package/dist/utils/uuidStruct/uuidStruct.cjs.map +1 -0
- package/dist/utils/uuidStruct/uuidStruct.d.cts +8 -0
- package/dist/utils/uuidStruct/uuidStruct.d.cts.map +1 -0
- package/dist/utils/uuidStruct/uuidStruct.d.mts +8 -0
- package/dist/utils/uuidStruct/uuidStruct.d.mts.map +1 -0
- package/dist/utils/uuidStruct/uuidStruct.mjs +9 -0
- package/dist/utils/uuidStruct/uuidStruct.mjs.map +1 -0
- package/package.json +33 -13
- package/dist/logger.cjs +0 -57
- package/dist/logger.cjs.map +0 -1
- package/dist/logger.d.cts +0 -30
- package/dist/logger.d.cts.map +0 -1
- package/dist/logger.d.mts +0 -30
- package/dist/logger.d.mts.map +0 -1
- package/dist/logger.mjs +0 -53
- package/dist/logger.mjs.map +0 -1
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Logger } from "../logger/Logger.cjs";
|
|
2
|
+
import type { Serializable } from "../serialization/types.cjs";
|
|
3
|
+
import type { ICache } from "./types.cjs";
|
|
4
|
+
/**
|
|
5
|
+
* Result type for functions that provide their own expiry time.
|
|
6
|
+
*/
|
|
7
|
+
export type ResultWithExpiry<TResult> = {
|
|
8
|
+
result: TResult;
|
|
9
|
+
expiresAt: number;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Options for configuring the caching behavior of a function with dynamic expiry.
|
|
13
|
+
*/
|
|
14
|
+
export type CacheUntilOptions = {
|
|
15
|
+
/**
|
|
16
|
+
* Set this if you want to use a custom function name for the cache key.
|
|
17
|
+
*/
|
|
18
|
+
functionName?: string;
|
|
19
|
+
/**
|
|
20
|
+
* Optional function to generate the cache key for the function call.
|
|
21
|
+
* Defaults to a function that generates the key based on function name and JSON stringified args separated by colons.
|
|
22
|
+
*/
|
|
23
|
+
generateCacheKey?: (functionName: string, args: Serializable[]) => string;
|
|
24
|
+
/**
|
|
25
|
+
* Whether to refresh the cache.
|
|
26
|
+
* Defaults to false.
|
|
27
|
+
*/
|
|
28
|
+
refreshCache?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Optional logger for cache errors. Defaults to a silent logger.
|
|
31
|
+
*/
|
|
32
|
+
logger?: Logger;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Wraps an async function with caching behavior where expiry is determined
|
|
36
|
+
* by the function result itself (dynamic TTL).
|
|
37
|
+
*
|
|
38
|
+
* Unlike `useCache` which uses a fixed TTL, this utility allows the wrapped
|
|
39
|
+
* function to specify when its result expires. This is useful for caching
|
|
40
|
+
* data that has known invalidation points (e.g., blockchain maintenance periods).
|
|
41
|
+
*
|
|
42
|
+
* @template TArgs - Tuple type representing the arguments of the function.
|
|
43
|
+
* @template TResult - The return type of the function, must be Serializable.
|
|
44
|
+
* @param fn - The asynchronous function to wrap. Must return a Promise<ResultWithExpiry<TResult>>.
|
|
45
|
+
* @param cache - The cache instance to use.
|
|
46
|
+
* @param options - The caching options.
|
|
47
|
+
* @param options.refreshCache - Whether to refresh the cache.
|
|
48
|
+
* @param options.functionName - The name of the function.
|
|
49
|
+
* @param options.generateCacheKey - Optional function to generate the cache key.
|
|
50
|
+
* @param options.logger - Optional logger for cache errors.
|
|
51
|
+
* @returns A new asynchronous function with caching behavior.
|
|
52
|
+
*/
|
|
53
|
+
export declare const useCacheUntil: <TArgs extends Serializable[], TResult extends Serializable>(fn: (...args: TArgs) => Promise<ResultWithExpiry<TResult>>, cache: ICache<Serializable>, { functionName, generateCacheKey, refreshCache, logger, }: CacheUntilOptions) => ((...args: TArgs) => Promise<TResult>);
|
|
54
|
+
//# sourceMappingURL=useCacheUntil.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useCacheUntil.d.cts","sourceRoot":"","sources":["../../../src/utils/cache/useCacheUntil.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAY,6BAAyB;AAEpD,OAAO,KAAK,EAAE,YAAY,EAAE,mCAA+B;AAC3D,OAAO,KAAK,EAAE,MAAM,EAAE,oBAAgB;AAOtC;;GAEG;AACH,MAAM,MAAM,gBAAgB,CAAC,OAAO,IAAI;IACtC,MAAM,EAAE,OAAO,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC9B;;OAEG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;;OAGG;IACH,gBAAgB,CAAC,EAAE,CAAC,YAAY,EAAE,MAAM,EAAE,IAAI,EAAE,YAAY,EAAE,KAAK,MAAM,CAAC;IAC1E;;;OAGG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAeF;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,aAAa,GACxB,KAAK,SAAS,YAAY,EAAE,EAC5B,OAAO,SAAS,YAAY,EAE5B,IAAI,CAAC,GAAG,IAAI,EAAE,KAAK,KAAK,OAAO,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC,EAC1D,OAAO,MAAM,CAAC,YAAY,CAAC,EAC3B,2DAKG,iBAAiB,KACnB,CAAC,CAAC,GAAG,IAAI,EAAE,KAAK,KAAK,OAAO,CAAC,OAAO,CAAC,CAiDvC,CAAC"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { Logger } from "../logger/Logger.mjs";
|
|
2
|
+
import type { Serializable } from "../serialization/types.mjs";
|
|
3
|
+
import type { ICache } from "./types.mjs";
|
|
4
|
+
/**
|
|
5
|
+
* Result type for functions that provide their own expiry time.
|
|
6
|
+
*/
|
|
7
|
+
export type ResultWithExpiry<TResult> = {
|
|
8
|
+
result: TResult;
|
|
9
|
+
expiresAt: number;
|
|
10
|
+
};
|
|
11
|
+
/**
|
|
12
|
+
* Options for configuring the caching behavior of a function with dynamic expiry.
|
|
13
|
+
*/
|
|
14
|
+
export type CacheUntilOptions = {
|
|
15
|
+
/**
|
|
16
|
+
* Set this if you want to use a custom function name for the cache key.
|
|
17
|
+
*/
|
|
18
|
+
functionName?: string;
|
|
19
|
+
/**
|
|
20
|
+
* Optional function to generate the cache key for the function call.
|
|
21
|
+
* Defaults to a function that generates the key based on function name and JSON stringified args separated by colons.
|
|
22
|
+
*/
|
|
23
|
+
generateCacheKey?: (functionName: string, args: Serializable[]) => string;
|
|
24
|
+
/**
|
|
25
|
+
* Whether to refresh the cache.
|
|
26
|
+
* Defaults to false.
|
|
27
|
+
*/
|
|
28
|
+
refreshCache?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Optional logger for cache errors. Defaults to a silent logger.
|
|
31
|
+
*/
|
|
32
|
+
logger?: Logger;
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* Wraps an async function with caching behavior where expiry is determined
|
|
36
|
+
* by the function result itself (dynamic TTL).
|
|
37
|
+
*
|
|
38
|
+
* Unlike `useCache` which uses a fixed TTL, this utility allows the wrapped
|
|
39
|
+
* function to specify when its result expires. This is useful for caching
|
|
40
|
+
* data that has known invalidation points (e.g., blockchain maintenance periods).
|
|
41
|
+
*
|
|
42
|
+
* @template TArgs - Tuple type representing the arguments of the function.
|
|
43
|
+
* @template TResult - The return type of the function, must be Serializable.
|
|
44
|
+
* @param fn - The asynchronous function to wrap. Must return a Promise<ResultWithExpiry<TResult>>.
|
|
45
|
+
* @param cache - The cache instance to use.
|
|
46
|
+
* @param options - The caching options.
|
|
47
|
+
* @param options.refreshCache - Whether to refresh the cache.
|
|
48
|
+
* @param options.functionName - The name of the function.
|
|
49
|
+
* @param options.generateCacheKey - Optional function to generate the cache key.
|
|
50
|
+
* @param options.logger - Optional logger for cache errors.
|
|
51
|
+
* @returns A new asynchronous function with caching behavior.
|
|
52
|
+
*/
|
|
53
|
+
export declare const useCacheUntil: <TArgs extends Serializable[], TResult extends Serializable>(fn: (...args: TArgs) => Promise<ResultWithExpiry<TResult>>, cache: ICache<Serializable>, { functionName, generateCacheKey, refreshCache, logger, }: CacheUntilOptions) => ((...args: TArgs) => Promise<TResult>);
|
|
54
|
+
//# sourceMappingURL=useCacheUntil.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useCacheUntil.d.mts","sourceRoot":"","sources":["../../../src/utils/cache/useCacheUntil.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAY,6BAAyB;AAEpD,OAAO,KAAK,EAAE,YAAY,EAAE,mCAA+B;AAC3D,OAAO,KAAK,EAAE,MAAM,EAAE,oBAAgB;AAOtC;;GAEG;AACH,MAAM,MAAM,gBAAgB,CAAC,OAAO,IAAI;IACtC,MAAM,EAAE,OAAO,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,iBAAiB,GAAG;IAC9B;;OAEG;IACH,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB;;;OAGG;IACH,gBAAgB,CAAC,EAAE,CAAC,YAAY,EAAE,MAAM,EAAE,IAAI,EAAE,YAAY,EAAE,KAAK,MAAM,CAAC;IAC1E;;;OAGG;IACH,YAAY,CAAC,EAAE,OAAO,CAAC;IACvB;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC;AAeF;;;;;;;;;;;;;;;;;;GAkBG;AACH,eAAO,MAAM,aAAa,GACxB,KAAK,SAAS,YAAY,EAAE,EAC5B,OAAO,SAAS,YAAY,EAE5B,IAAI,CAAC,GAAG,IAAI,EAAE,KAAK,KAAK,OAAO,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC,EAC1D,OAAO,MAAM,CAAC,YAAY,CAAC,EAC3B,2DAKG,iBAAiB,KACnB,CAAC,CAAC,GAAG,IAAI,EAAE,KAAK,KAAK,OAAO,CAAC,OAAO,CAAC,CAiDvC,CAAC"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { Logger, LogLevel } from "../logger/Logger.mjs";
|
|
2
|
+
import { serialize } from "../serialization/serialization.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* A logger that discards all messages, used when no logger is provided.
|
|
5
|
+
*/
|
|
6
|
+
const silentLogger = new Logger({ level: LogLevel.SILENT });
|
|
7
|
+
/**
|
|
8
|
+
* Default function to generate the cache key for a function call.
|
|
9
|
+
*
|
|
10
|
+
* @param functionName - The name of the function.
|
|
11
|
+
* @param args - The arguments of the function call.
|
|
12
|
+
* @returns The cache key.
|
|
13
|
+
*/
|
|
14
|
+
const defaultGenerateCacheKey = (functionName, args) => `${functionName}:${args.map((arg) => JSON.stringify(serialize(arg))).join(':')}`;
|
|
15
|
+
/**
|
|
16
|
+
* Wraps an async function with caching behavior where expiry is determined
|
|
17
|
+
* by the function result itself (dynamic TTL).
|
|
18
|
+
*
|
|
19
|
+
* Unlike `useCache` which uses a fixed TTL, this utility allows the wrapped
|
|
20
|
+
* function to specify when its result expires. This is useful for caching
|
|
21
|
+
* data that has known invalidation points (e.g., blockchain maintenance periods).
|
|
22
|
+
*
|
|
23
|
+
* @template TArgs - Tuple type representing the arguments of the function.
|
|
24
|
+
* @template TResult - The return type of the function, must be Serializable.
|
|
25
|
+
* @param fn - The asynchronous function to wrap. Must return a Promise<ResultWithExpiry<TResult>>.
|
|
26
|
+
* @param cache - The cache instance to use.
|
|
27
|
+
* @param options - The caching options.
|
|
28
|
+
* @param options.refreshCache - Whether to refresh the cache.
|
|
29
|
+
* @param options.functionName - The name of the function.
|
|
30
|
+
* @param options.generateCacheKey - Optional function to generate the cache key.
|
|
31
|
+
* @param options.logger - Optional logger for cache errors.
|
|
32
|
+
* @returns A new asynchronous function with caching behavior.
|
|
33
|
+
*/
|
|
34
|
+
export const useCacheUntil = (fn, cache, { functionName, generateCacheKey, refreshCache = false, logger = silentLogger, }) => {
|
|
35
|
+
// Use provided key generator or default, adapting the default to use the function's name
|
|
36
|
+
const _generateCacheKey = generateCacheKey ?? defaultGenerateCacheKey;
|
|
37
|
+
// Get the function name for the default key generator, handle anonymous functions
|
|
38
|
+
const _functionName = functionName ?? fn.name ?? 'anonymousFunction';
|
|
39
|
+
// Map to track expiry timestamps for each cache key
|
|
40
|
+
const expiryMap = new Map();
|
|
41
|
+
return async (...args) => {
|
|
42
|
+
const cacheKey = _generateCacheKey(_functionName, args);
|
|
43
|
+
const now = Date.now();
|
|
44
|
+
// Check if cached and not expired.
|
|
45
|
+
// The cache owns the persisted TTL. When this wrapper is recreated after a
|
|
46
|
+
// Snap restart, expiryMap is empty, so consult the cache to hydrate a still
|
|
47
|
+
// valid entry instead of fetching it again.
|
|
48
|
+
const expiresAt = expiryMap.get(cacheKey);
|
|
49
|
+
if (!refreshCache && (expiresAt === undefined || now < expiresAt)) {
|
|
50
|
+
try {
|
|
51
|
+
const cached = await cache.get(cacheKey);
|
|
52
|
+
// Check explicitly for undefined, as null or other falsy values might be valid cache results
|
|
53
|
+
if (cached !== undefined) {
|
|
54
|
+
// Type assertion because cache stores Serializable, but we expect TResult
|
|
55
|
+
return cached;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
catch (error) {
|
|
59
|
+
// Log cache get errors but proceed to execute the function
|
|
60
|
+
logger.error(`Cache get error for key "${cacheKey}":`, error);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
// Execute the original function to get result and new expiry
|
|
64
|
+
const { result, expiresAt: newExpiresAt } = await fn(...args);
|
|
65
|
+
// Calculate TTL from expiry timestamp
|
|
66
|
+
const ttlMilliseconds = Math.max(0, newExpiresAt - now);
|
|
67
|
+
// Store result in cache with calculated TTL
|
|
68
|
+
await cache.set(cacheKey, result, ttlMilliseconds).catch((error) => {
|
|
69
|
+
logger.error(`Cache set error for key "${cacheKey}":`, error);
|
|
70
|
+
});
|
|
71
|
+
// Store expiry timestamp
|
|
72
|
+
expiryMap.set(cacheKey, newExpiresAt);
|
|
73
|
+
return result;
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
//# sourceMappingURL=useCacheUntil.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"useCacheUntil.mjs","sourceRoot":"","sources":["../../../src/utils/cache/useCacheUntil.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,6BAAyB;AACpD,OAAO,EAAE,SAAS,EAAE,2CAAuC;AAI3D;;GAEG;AACH,MAAM,YAAY,GAAG,IAAI,MAAM,CAAC,EAAE,KAAK,EAAE,QAAQ,CAAC,MAAM,EAAE,CAAC,CAAC;AAkC5D;;;;;;GAMG;AACH,MAAM,uBAAuB,GAAG,CAC9B,YAAoB,EACpB,IAAoB,EACZ,EAAE,CACV,GAAG,YAAY,IAAI,IAAI,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC;AAEnF;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,CAI3B,EAA0D,EAC1D,KAA2B,EAC3B,EACE,YAAY,EACZ,gBAAgB,EAChB,YAAY,GAAG,KAAK,EACpB,MAAM,GAAG,YAAY,GACH,EACoB,EAAE;IAC1C,yFAAyF;IACzF,MAAM,iBAAiB,GAAG,gBAAgB,IAAI,uBAAuB,CAAC;IAEtE,kFAAkF;IAClF,MAAM,aAAa,GAAG,YAAY,IAAI,EAAE,CAAC,IAAI,IAAI,mBAAmB,CAAC;IAErE,oDAAoD;IACpD,MAAM,SAAS,GAAG,IAAI,GAAG,EAAkB,CAAC;IAE5C,OAAO,KAAK,EAAE,GAAG,IAAW,EAAoB,EAAE;QAChD,MAAM,QAAQ,GAAG,iBAAiB,CAAC,aAAa,EAAE,IAAI,CAAC,CAAC;QACxD,MAAM,GAAG,GAAG,IAAI,CAAC,GAAG,EAAE,CAAC;QAEvB,mCAAmC;QACnC,2EAA2E;QAC3E,4EAA4E;QAC5E,4CAA4C;QAC5C,MAAM,SAAS,GAAG,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QAC1C,IAAI,CAAC,YAAY,IAAI,CAAC,SAAS,KAAK,SAAS,IAAI,GAAG,GAAG,SAAS,CAAC,EAAE,CAAC;YAClE,IAAI,CAAC;gBACH,MAAM,MAAM,GAAG,MAAM,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;gBACzC,6FAA6F;gBAC7F,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;oBACzB,0EAA0E;oBAC1E,OAAO,MAAiB,CAAC;gBAC3B,CAAC;YACH,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,2DAA2D;gBAC3D,MAAM,CAAC,KAAK,CAAC,4BAA4B,QAAQ,IAAI,EAAE,KAAK,CAAC,CAAC;YAChE,CAAC;QACH,CAAC;QAED,6DAA6D;QAC7D,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,YAAY,EAAE,GAAG,MAAM,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC;QAE9D,sCAAsC;QACtC,MAAM,eAAe,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,YAAY,GAAG,GAAG,CAAC,CAAC;QAExD,4CAA4C;QAC5C,MAAM,KAAK,CAAC,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,eAAe,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;YACjE,MAAM,CAAC,KAAK,CAAC,4BAA4B,QAAQ,IAAI,EAAE,KAAK,CAAC,CAAC;QAChE,CAAC,CAAC,CAAC;QAEH,yBAAyB;QACzB,SAAS,CAAC,GAAG,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAC;QAEtC,OAAO,MAAM,CAAC;IAChB,CAAC,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import { Logger, LogLevel } from '../logger/Logger';\nimport { serialize } from '../serialization/serialization';\nimport type { Serializable } from '../serialization/types';\nimport type { ICache } from './types';\n\n/**\n * A logger that discards all messages, used when no logger is provided.\n */\nconst silentLogger = new Logger({ level: LogLevel.SILENT });\n\n/**\n * Result type for functions that provide their own expiry time.\n */\nexport type ResultWithExpiry<TResult> = {\n result: TResult;\n expiresAt: number; // Unix timestamp in milliseconds\n};\n\n/**\n * Options for configuring the caching behavior of a function with dynamic expiry.\n */\nexport type CacheUntilOptions = {\n /**\n * Set this if you want to use a custom function name for the cache key.\n */\n functionName?: string;\n /**\n * Optional function to generate the cache key for the function call.\n * Defaults to a function that generates the key based on function name and JSON stringified args separated by colons.\n */\n generateCacheKey?: (functionName: string, args: Serializable[]) => string;\n /**\n * Whether to refresh the cache.\n * Defaults to false.\n */\n refreshCache?: boolean;\n /**\n * Optional logger for cache errors. Defaults to a silent logger.\n */\n logger?: Logger;\n};\n\n/**\n * Default function to generate the cache key for a function call.\n *\n * @param functionName - The name of the function.\n * @param args - The arguments of the function call.\n * @returns The cache key.\n */\nconst defaultGenerateCacheKey = (\n functionName: string,\n args: Serializable[],\n): string =>\n `${functionName}:${args.map((arg) => JSON.stringify(serialize(arg))).join(':')}`;\n\n/**\n * Wraps an async function with caching behavior where expiry is determined\n * by the function result itself (dynamic TTL).\n *\n * Unlike `useCache` which uses a fixed TTL, this utility allows the wrapped\n * function to specify when its result expires. This is useful for caching\n * data that has known invalidation points (e.g., blockchain maintenance periods).\n *\n * @template TArgs - Tuple type representing the arguments of the function.\n * @template TResult - The return type of the function, must be Serializable.\n * @param fn - The asynchronous function to wrap. Must return a Promise<ResultWithExpiry<TResult>>.\n * @param cache - The cache instance to use.\n * @param options - The caching options.\n * @param options.refreshCache - Whether to refresh the cache.\n * @param options.functionName - The name of the function.\n * @param options.generateCacheKey - Optional function to generate the cache key.\n * @param options.logger - Optional logger for cache errors.\n * @returns A new asynchronous function with caching behavior.\n */\nexport const useCacheUntil = <\n TArgs extends Serializable[],\n TResult extends Serializable,\n>(\n fn: (...args: TArgs) => Promise<ResultWithExpiry<TResult>>,\n cache: ICache<Serializable>,\n {\n functionName,\n generateCacheKey,\n refreshCache = false,\n logger = silentLogger,\n }: CacheUntilOptions,\n): ((...args: TArgs) => Promise<TResult>) => {\n // Use provided key generator or default, adapting the default to use the function's name\n const _generateCacheKey = generateCacheKey ?? defaultGenerateCacheKey;\n\n // Get the function name for the default key generator, handle anonymous functions\n const _functionName = functionName ?? fn.name ?? 'anonymousFunction';\n\n // Map to track expiry timestamps for each cache key\n const expiryMap = new Map<string, number>();\n\n return async (...args: TArgs): Promise<TResult> => {\n const cacheKey = _generateCacheKey(_functionName, args);\n const now = Date.now();\n\n // Check if cached and not expired.\n // The cache owns the persisted TTL. When this wrapper is recreated after a\n // Snap restart, expiryMap is empty, so consult the cache to hydrate a still\n // valid entry instead of fetching it again.\n const expiresAt = expiryMap.get(cacheKey);\n if (!refreshCache && (expiresAt === undefined || now < expiresAt)) {\n try {\n const cached = await cache.get(cacheKey);\n // Check explicitly for undefined, as null or other falsy values might be valid cache results\n if (cached !== undefined) {\n // Type assertion because cache stores Serializable, but we expect TResult\n return cached as TResult;\n }\n } catch (error) {\n // Log cache get errors but proceed to execute the function\n logger.error(`Cache get error for key \"${cacheKey}\":`, error);\n }\n }\n\n // Execute the original function to get result and new expiry\n const { result, expiresAt: newExpiresAt } = await fn(...args);\n\n // Calculate TTL from expiry timestamp\n const ttlMilliseconds = Math.max(0, newExpiresAt - now);\n\n // Store result in cache with calculated TTL\n await cache.set(cacheKey, result, ttlMilliseconds).catch((error) => {\n logger.error(`Cache set error for key \"${cacheKey}\":`, error);\n });\n\n // Store expiry timestamp\n expiryMap.set(cacheKey, newExpiresAt);\n\n return result;\n };\n};\n"]}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseConfigProvider = void 0;
|
|
4
|
+
const parseEnv_1 = require("./parseEnv.cjs");
|
|
5
|
+
/**
|
|
6
|
+
* A base class that provides the configuration of a snap.
|
|
7
|
+
*
|
|
8
|
+
* The environment is parsed and validated once, when the provider is
|
|
9
|
+
* constructed; an invalid environment throws. Subclasses pass the raw
|
|
10
|
+
* environment and the struct describing it to the base constructor. Use it
|
|
11
|
+
* directly when the snap needs no extra config helpers, or extend it to add
|
|
12
|
+
* them.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* const config = {
|
|
16
|
+
* environment: process.env.ENVIRONMENT,
|
|
17
|
+
* };
|
|
18
|
+
*
|
|
19
|
+
* const ConfigStruct = object({
|
|
20
|
+
* environment: enums(Object.values(Environment)),
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* // When no extra helpers are needed, use the base class directly:
|
|
24
|
+
* const configProvider = new BaseConfigProvider(config, ConfigStruct);
|
|
25
|
+
* const { environment } = configProvider.config;
|
|
26
|
+
*
|
|
27
|
+
* @example
|
|
28
|
+
* // Otherwise, extend it:
|
|
29
|
+
* export class ConfigProvider extends BaseConfigProvider<typeof ConfigStruct> {
|
|
30
|
+
* constructor() {
|
|
31
|
+
* super(config, ConfigStruct);
|
|
32
|
+
* }
|
|
33
|
+
* }
|
|
34
|
+
*
|
|
35
|
+
* const configProvider = new ConfigProvider();
|
|
36
|
+
* const { environment } = configProvider.config;
|
|
37
|
+
*/
|
|
38
|
+
class BaseConfigProvider {
|
|
39
|
+
#config;
|
|
40
|
+
/**
|
|
41
|
+
* Construct the provider, parsing the environment once.
|
|
42
|
+
*
|
|
43
|
+
* @param configJson - The configuration JSON for the snap: an object literal referencing
|
|
44
|
+
* the `process.env` variables consumed by the snap, nested to mirror the
|
|
45
|
+
* struct where needed.
|
|
46
|
+
* @param configStruct - The struct describing the configuration, including
|
|
47
|
+
* coercions from raw strings to typed values.
|
|
48
|
+
*/
|
|
49
|
+
constructor(configJson, configStruct) {
|
|
50
|
+
this.#config = (0, parseEnv_1.parseEnv)(configJson, configStruct);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Retrieves configuration of the snap.
|
|
54
|
+
*
|
|
55
|
+
* @returns The configuration, built once from the environment.
|
|
56
|
+
*/
|
|
57
|
+
get config() {
|
|
58
|
+
return this.#config;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
exports.BaseConfigProvider = BaseConfigProvider;
|
|
62
|
+
//# sourceMappingURL=BaseConfigProvider.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BaseConfigProvider.cjs","sourceRoot":"","sources":["../../../src/utils/config/BaseConfigProvider.ts"],"names":[],"mappings":";;;AAEA,6CAAsC;AAEtC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,MAAa,kBAAkB;IACpB,OAAO,CAAsB;IAEtC;;;;;;;;OAQG;IACH,YAAY,UAAmC,EAAE,YAA0B;QACzE,IAAI,CAAC,OAAO,GAAG,IAAA,mBAAQ,EAAC,UAAU,EAAE,YAAY,CAAC,CAAC;IACpD,CAAC;IAED;;;;OAIG;IACH,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;CACF;AAxBD,gDAwBC","sourcesContent":["import type { AnyStruct, Infer } from '@metamask/superstruct';\n\nimport { parseEnv } from './parseEnv';\n\n/**\n * A base class that provides the configuration of a snap.\n *\n * The environment is parsed and validated once, when the provider is\n * constructed; an invalid environment throws. Subclasses pass the raw\n * environment and the struct describing it to the base constructor. Use it\n * directly when the snap needs no extra config helpers, or extend it to add\n * them.\n *\n * @example\n * const config = {\n * environment: process.env.ENVIRONMENT,\n * };\n *\n * const ConfigStruct = object({\n * environment: enums(Object.values(Environment)),\n * });\n *\n * // When no extra helpers are needed, use the base class directly:\n * const configProvider = new BaseConfigProvider(config, ConfigStruct);\n * const { environment } = configProvider.config;\n *\n * @example\n * // Otherwise, extend it:\n * export class ConfigProvider extends BaseConfigProvider<typeof ConfigStruct> {\n * constructor() {\n * super(config, ConfigStruct);\n * }\n * }\n *\n * const configProvider = new ConfigProvider();\n * const { environment } = configProvider.config;\n */\nexport class BaseConfigProvider<ConfigStruct extends AnyStruct> {\n readonly #config: Infer<ConfigStruct>;\n\n /**\n * Construct the provider, parsing the environment once.\n *\n * @param configJson - The configuration JSON for the snap: an object literal referencing\n * the `process.env` variables consumed by the snap, nested to mirror the\n * struct where needed.\n * @param configStruct - The struct describing the configuration, including\n * coercions from raw strings to typed values.\n */\n constructor(configJson: Record<string, unknown>, configStruct: ConfigStruct) {\n this.#config = parseEnv(configJson, configStruct);\n }\n\n /**\n * Retrieves configuration of the snap.\n *\n * @returns The configuration, built once from the environment.\n */\n get config(): Infer<ConfigStruct> {\n return this.#config;\n }\n}\n"]}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { AnyStruct, Infer } from "@metamask/superstruct";
|
|
2
|
+
/**
|
|
3
|
+
* A base class that provides the configuration of a snap.
|
|
4
|
+
*
|
|
5
|
+
* The environment is parsed and validated once, when the provider is
|
|
6
|
+
* constructed; an invalid environment throws. Subclasses pass the raw
|
|
7
|
+
* environment and the struct describing it to the base constructor. Use it
|
|
8
|
+
* directly when the snap needs no extra config helpers, or extend it to add
|
|
9
|
+
* them.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const config = {
|
|
13
|
+
* environment: process.env.ENVIRONMENT,
|
|
14
|
+
* };
|
|
15
|
+
*
|
|
16
|
+
* const ConfigStruct = object({
|
|
17
|
+
* environment: enums(Object.values(Environment)),
|
|
18
|
+
* });
|
|
19
|
+
*
|
|
20
|
+
* // When no extra helpers are needed, use the base class directly:
|
|
21
|
+
* const configProvider = new BaseConfigProvider(config, ConfigStruct);
|
|
22
|
+
* const { environment } = configProvider.config;
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* // Otherwise, extend it:
|
|
26
|
+
* export class ConfigProvider extends BaseConfigProvider<typeof ConfigStruct> {
|
|
27
|
+
* constructor() {
|
|
28
|
+
* super(config, ConfigStruct);
|
|
29
|
+
* }
|
|
30
|
+
* }
|
|
31
|
+
*
|
|
32
|
+
* const configProvider = new ConfigProvider();
|
|
33
|
+
* const { environment } = configProvider.config;
|
|
34
|
+
*/
|
|
35
|
+
export declare class BaseConfigProvider<ConfigStruct extends AnyStruct> {
|
|
36
|
+
#private;
|
|
37
|
+
/**
|
|
38
|
+
* Construct the provider, parsing the environment once.
|
|
39
|
+
*
|
|
40
|
+
* @param configJson - The configuration JSON for the snap: an object literal referencing
|
|
41
|
+
* the `process.env` variables consumed by the snap, nested to mirror the
|
|
42
|
+
* struct where needed.
|
|
43
|
+
* @param configStruct - The struct describing the configuration, including
|
|
44
|
+
* coercions from raw strings to typed values.
|
|
45
|
+
*/
|
|
46
|
+
constructor(configJson: Record<string, unknown>, configStruct: ConfigStruct);
|
|
47
|
+
/**
|
|
48
|
+
* Retrieves configuration of the snap.
|
|
49
|
+
*
|
|
50
|
+
* @returns The configuration, built once from the environment.
|
|
51
|
+
*/
|
|
52
|
+
get config(): Infer<ConfigStruct>;
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=BaseConfigProvider.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BaseConfigProvider.d.cts","sourceRoot":"","sources":["../../../src/utils/config/BaseConfigProvider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,8BAA8B;AAI9D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,qBAAa,kBAAkB,CAAC,YAAY,SAAS,SAAS;;IAG5D;;;;;;;;OAQG;gBACS,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY,EAAE,YAAY;IAI3E;;;;OAIG;IACH,IAAI,MAAM,IAAI,KAAK,CAAC,YAAY,CAAC,CAEhC;CACF"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import type { AnyStruct, Infer } from "@metamask/superstruct";
|
|
2
|
+
/**
|
|
3
|
+
* A base class that provides the configuration of a snap.
|
|
4
|
+
*
|
|
5
|
+
* The environment is parsed and validated once, when the provider is
|
|
6
|
+
* constructed; an invalid environment throws. Subclasses pass the raw
|
|
7
|
+
* environment and the struct describing it to the base constructor. Use it
|
|
8
|
+
* directly when the snap needs no extra config helpers, or extend it to add
|
|
9
|
+
* them.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const config = {
|
|
13
|
+
* environment: process.env.ENVIRONMENT,
|
|
14
|
+
* };
|
|
15
|
+
*
|
|
16
|
+
* const ConfigStruct = object({
|
|
17
|
+
* environment: enums(Object.values(Environment)),
|
|
18
|
+
* });
|
|
19
|
+
*
|
|
20
|
+
* // When no extra helpers are needed, use the base class directly:
|
|
21
|
+
* const configProvider = new BaseConfigProvider(config, ConfigStruct);
|
|
22
|
+
* const { environment } = configProvider.config;
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* // Otherwise, extend it:
|
|
26
|
+
* export class ConfigProvider extends BaseConfigProvider<typeof ConfigStruct> {
|
|
27
|
+
* constructor() {
|
|
28
|
+
* super(config, ConfigStruct);
|
|
29
|
+
* }
|
|
30
|
+
* }
|
|
31
|
+
*
|
|
32
|
+
* const configProvider = new ConfigProvider();
|
|
33
|
+
* const { environment } = configProvider.config;
|
|
34
|
+
*/
|
|
35
|
+
export declare class BaseConfigProvider<ConfigStruct extends AnyStruct> {
|
|
36
|
+
#private;
|
|
37
|
+
/**
|
|
38
|
+
* Construct the provider, parsing the environment once.
|
|
39
|
+
*
|
|
40
|
+
* @param configJson - The configuration JSON for the snap: an object literal referencing
|
|
41
|
+
* the `process.env` variables consumed by the snap, nested to mirror the
|
|
42
|
+
* struct where needed.
|
|
43
|
+
* @param configStruct - The struct describing the configuration, including
|
|
44
|
+
* coercions from raw strings to typed values.
|
|
45
|
+
*/
|
|
46
|
+
constructor(configJson: Record<string, unknown>, configStruct: ConfigStruct);
|
|
47
|
+
/**
|
|
48
|
+
* Retrieves configuration of the snap.
|
|
49
|
+
*
|
|
50
|
+
* @returns The configuration, built once from the environment.
|
|
51
|
+
*/
|
|
52
|
+
get config(): Infer<ConfigStruct>;
|
|
53
|
+
}
|
|
54
|
+
//# sourceMappingURL=BaseConfigProvider.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BaseConfigProvider.d.mts","sourceRoot":"","sources":["../../../src/utils/config/BaseConfigProvider.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,KAAK,EAAE,8BAA8B;AAI9D;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,qBAAa,kBAAkB,CAAC,YAAY,SAAS,SAAS;;IAG5D;;;;;;;;OAQG;gBACS,UAAU,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY,EAAE,YAAY;IAI3E;;;;OAIG;IACH,IAAI,MAAM,IAAI,KAAK,CAAC,YAAY,CAAC,CAEhC;CACF"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { parseEnv } from "./parseEnv.mjs";
|
|
2
|
+
/**
|
|
3
|
+
* A base class that provides the configuration of a snap.
|
|
4
|
+
*
|
|
5
|
+
* The environment is parsed and validated once, when the provider is
|
|
6
|
+
* constructed; an invalid environment throws. Subclasses pass the raw
|
|
7
|
+
* environment and the struct describing it to the base constructor. Use it
|
|
8
|
+
* directly when the snap needs no extra config helpers, or extend it to add
|
|
9
|
+
* them.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* const config = {
|
|
13
|
+
* environment: process.env.ENVIRONMENT,
|
|
14
|
+
* };
|
|
15
|
+
*
|
|
16
|
+
* const ConfigStruct = object({
|
|
17
|
+
* environment: enums(Object.values(Environment)),
|
|
18
|
+
* });
|
|
19
|
+
*
|
|
20
|
+
* // When no extra helpers are needed, use the base class directly:
|
|
21
|
+
* const configProvider = new BaseConfigProvider(config, ConfigStruct);
|
|
22
|
+
* const { environment } = configProvider.config;
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* // Otherwise, extend it:
|
|
26
|
+
* export class ConfigProvider extends BaseConfigProvider<typeof ConfigStruct> {
|
|
27
|
+
* constructor() {
|
|
28
|
+
* super(config, ConfigStruct);
|
|
29
|
+
* }
|
|
30
|
+
* }
|
|
31
|
+
*
|
|
32
|
+
* const configProvider = new ConfigProvider();
|
|
33
|
+
* const { environment } = configProvider.config;
|
|
34
|
+
*/
|
|
35
|
+
export class BaseConfigProvider {
|
|
36
|
+
#config;
|
|
37
|
+
/**
|
|
38
|
+
* Construct the provider, parsing the environment once.
|
|
39
|
+
*
|
|
40
|
+
* @param configJson - The configuration JSON for the snap: an object literal referencing
|
|
41
|
+
* the `process.env` variables consumed by the snap, nested to mirror the
|
|
42
|
+
* struct where needed.
|
|
43
|
+
* @param configStruct - The struct describing the configuration, including
|
|
44
|
+
* coercions from raw strings to typed values.
|
|
45
|
+
*/
|
|
46
|
+
constructor(configJson, configStruct) {
|
|
47
|
+
this.#config = parseEnv(configJson, configStruct);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Retrieves configuration of the snap.
|
|
51
|
+
*
|
|
52
|
+
* @returns The configuration, built once from the environment.
|
|
53
|
+
*/
|
|
54
|
+
get config() {
|
|
55
|
+
return this.#config;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=BaseConfigProvider.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"BaseConfigProvider.mjs","sourceRoot":"","sources":["../../../src/utils/config/BaseConfigProvider.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,QAAQ,EAAE,uBAAmB;AAEtC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,MAAM,OAAO,kBAAkB;IACpB,OAAO,CAAsB;IAEtC;;;;;;;;OAQG;IACH,YAAY,UAAmC,EAAE,YAA0B;QACzE,IAAI,CAAC,OAAO,GAAG,QAAQ,CAAC,UAAU,EAAE,YAAY,CAAC,CAAC;IACpD,CAAC;IAED;;;;OAIG;IACH,IAAI,MAAM;QACR,OAAO,IAAI,CAAC,OAAO,CAAC;IACtB,CAAC;CACF","sourcesContent":["import type { AnyStruct, Infer } from '@metamask/superstruct';\n\nimport { parseEnv } from './parseEnv';\n\n/**\n * A base class that provides the configuration of a snap.\n *\n * The environment is parsed and validated once, when the provider is\n * constructed; an invalid environment throws. Subclasses pass the raw\n * environment and the struct describing it to the base constructor. Use it\n * directly when the snap needs no extra config helpers, or extend it to add\n * them.\n *\n * @example\n * const config = {\n * environment: process.env.ENVIRONMENT,\n * };\n *\n * const ConfigStruct = object({\n * environment: enums(Object.values(Environment)),\n * });\n *\n * // When no extra helpers are needed, use the base class directly:\n * const configProvider = new BaseConfigProvider(config, ConfigStruct);\n * const { environment } = configProvider.config;\n *\n * @example\n * // Otherwise, extend it:\n * export class ConfigProvider extends BaseConfigProvider<typeof ConfigStruct> {\n * constructor() {\n * super(config, ConfigStruct);\n * }\n * }\n *\n * const configProvider = new ConfigProvider();\n * const { environment } = configProvider.config;\n */\nexport class BaseConfigProvider<ConfigStruct extends AnyStruct> {\n readonly #config: Infer<ConfigStruct>;\n\n /**\n * Construct the provider, parsing the environment once.\n *\n * @param configJson - The configuration JSON for the snap: an object literal referencing\n * the `process.env` variables consumed by the snap, nested to mirror the\n * struct where needed.\n * @param configStruct - The struct describing the configuration, including\n * coercions from raw strings to typed values.\n */\n constructor(configJson: Record<string, unknown>, configStruct: ConfigStruct) {\n this.#config = parseEnv(configJson, configStruct);\n }\n\n /**\n * Retrieves configuration of the snap.\n *\n * @returns The configuration, built once from the environment.\n */\n get config(): Infer<ConfigStruct> {\n return this.#config;\n }\n}\n"]}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseFloatStruct = exports.parseIntegerStruct = exports.defaultedUrlStruct = exports.commaSeparatedListOf = exports.emptyToUndefined = void 0;
|
|
4
|
+
const superstruct_1 = require("@metamask/superstruct");
|
|
5
|
+
const urlStruct_1 = require("../urlStruct/urlStruct.cjs");
|
|
6
|
+
/**
|
|
7
|
+
* Create a struct for environment variables validated against the given
|
|
8
|
+
* struct, where unset and empty strings (the build-time injection default)
|
|
9
|
+
* mean the variable is not set.
|
|
10
|
+
*
|
|
11
|
+
* @param item - The struct to validate the value against when it is set,
|
|
12
|
+
* e.g. `UrlStruct`.
|
|
13
|
+
* @returns A struct that parses to the value or `undefined`.
|
|
14
|
+
* @example
|
|
15
|
+
* const PriceApiBaseUrlStruct = emptyToUndefined(UrlStruct);
|
|
16
|
+
*/
|
|
17
|
+
const emptyToUndefined = (item) => (0, superstruct_1.coerce)((0, superstruct_1.optional)(item), (0, superstruct_1.string)(), (value) => value === '' ? undefined : value);
|
|
18
|
+
exports.emptyToUndefined = emptyToUndefined;
|
|
19
|
+
/**
|
|
20
|
+
* Create a struct for comma-separated list environment variables.
|
|
21
|
+
*
|
|
22
|
+
* The raw string is split on commas and each item is validated against the
|
|
23
|
+
* given item struct.
|
|
24
|
+
*
|
|
25
|
+
* @param item - The struct to validate each list item against.
|
|
26
|
+
* @returns A struct that parses a comma-separated string into an array.
|
|
27
|
+
* @example
|
|
28
|
+
* const RpcUrlListStruct = commaSeparatedListOf(UrlStruct);
|
|
29
|
+
*/
|
|
30
|
+
const commaSeparatedListOf = (item) => (0, superstruct_1.coerce)((0, superstruct_1.array)(item), (0, superstruct_1.string)(), (value) => value.split(','));
|
|
31
|
+
exports.commaSeparatedListOf = commaSeparatedListOf;
|
|
32
|
+
/**
|
|
33
|
+
* Create a struct for a URL environment variable with a fallback: unset,
|
|
34
|
+
* empty, and whitespace-only values (the build-time injection default)
|
|
35
|
+
* resolve to the fallback, surrounding whitespace is trimmed, and invalid
|
|
36
|
+
* values are rejected.
|
|
37
|
+
*
|
|
38
|
+
* @param fallback - The URL to use when the variable is unset, empty, or
|
|
39
|
+
* whitespace-only.
|
|
40
|
+
* @returns A struct that parses to the URL.
|
|
41
|
+
* @example
|
|
42
|
+
* const EsploraUrlStruct = defaultedUrlStruct('https://blockstream.info/api');
|
|
43
|
+
*/
|
|
44
|
+
const defaultedUrlStruct = (fallback) => (0, superstruct_1.coerce)((0, superstruct_1.defaulted)(urlStruct_1.UrlStruct, fallback), (0, superstruct_1.string)(), (value) => {
|
|
45
|
+
const trimmed = value.trim();
|
|
46
|
+
return trimmed === '' ? undefined : trimmed;
|
|
47
|
+
});
|
|
48
|
+
exports.defaultedUrlStruct = defaultedUrlStruct;
|
|
49
|
+
/**
|
|
50
|
+
* Create a struct that parses an integer from a string, with a minimum value
|
|
51
|
+
* and a default for unset variables.
|
|
52
|
+
*
|
|
53
|
+
* @param minValue - The minimum value for the integer.
|
|
54
|
+
* @param defaultValue - The default value for the integer.
|
|
55
|
+
* @returns A struct that parses an integer from a string.
|
|
56
|
+
*/
|
|
57
|
+
const parseIntegerStruct = (minValue, defaultValue) => (0, superstruct_1.coerce)((0, superstruct_1.defaulted)((0, superstruct_1.min)((0, superstruct_1.number)(), minValue), defaultValue), (0, superstruct_1.string)(), (value) => (value === '' ? undefined : parseInt(value, 10)));
|
|
58
|
+
exports.parseIntegerStruct = parseIntegerStruct;
|
|
59
|
+
/**
|
|
60
|
+
* Create a struct that parses a float from a string, with a minimum value and
|
|
61
|
+
* a default for unset variables.
|
|
62
|
+
*
|
|
63
|
+
* @param minValue - The minimum value for the float.
|
|
64
|
+
* @param defaultValue - The default value for the float.
|
|
65
|
+
* @returns A struct that parses a float from a string.
|
|
66
|
+
*/
|
|
67
|
+
const parseFloatStruct = (minValue, defaultValue) => (0, superstruct_1.coerce)((0, superstruct_1.defaulted)((0, superstruct_1.min)((0, superstruct_1.number)(), minValue), defaultValue), (0, superstruct_1.string)(), (value) => (value === '' ? undefined : parseFloat(value)));
|
|
68
|
+
exports.parseFloatStruct = parseFloatStruct;
|
|
69
|
+
//# sourceMappingURL=envStructs.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"envStructs.cjs","sourceRoot":"","sources":["../../../src/utils/config/envStructs.ts"],"names":[],"mappings":";;;AACA,uDAQ+B;AAE/B,0DAAmD;AAEnD;;;;;;;;;;GAUG;AACI,MAAM,gBAAgB,GAAG,CAC9B,IAAkB,EACQ,EAAE,CAC5B,IAAA,oBAAM,EAAC,IAAA,sBAAQ,EAAC,IAAI,CAAC,EAAE,IAAA,oBAAM,GAAE,EAAE,CAAC,KAAa,EAAE,EAAE,CACjD,KAAK,KAAK,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,KAAK,CACjC,CAAC;AALS,QAAA,gBAAgB,oBAKzB;AAEJ;;;;;;;;;;GAUG;AACI,MAAM,oBAAoB,GAAG,CAClC,IAAkB,EACF,EAAE,CAClB,IAAA,oBAAM,EAAC,IAAA,mBAAK,EAAC,IAAI,CAAC,EAAE,IAAA,oBAAM,GAAE,EAAE,CAAC,KAAa,EAAE,EAAE,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;AAHxD,QAAA,oBAAoB,wBAGoC;AAErE;;;;;;;;;;;GAWG;AACI,MAAM,kBAAkB,GAAG,CAAC,QAAgB,EAAkB,EAAE,CACrE,IAAA,oBAAM,EAAC,IAAA,uBAAS,EAAC,qBAAS,EAAE,QAAQ,CAAC,EAAE,IAAA,oBAAM,GAAE,EAAE,CAAC,KAAa,EAAE,EAAE;IACjE,MAAM,OAAO,GAAG,KAAK,CAAC,IAAI,EAAE,CAAC;IAC7B,OAAO,OAAO,KAAK,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,OAAO,CAAC;AAC9C,CAAC,CAAC,CAAC;AAJQ,QAAA,kBAAkB,sBAI1B;AAEL;;;;;;;GAOG;AACI,MAAM,kBAAkB,GAAG,CAChC,QAAgB,EAChB,YAAoB,EACJ,EAAE,CAClB,IAAA,oBAAM,EACJ,IAAA,uBAAS,EAAC,IAAA,iBAAG,EAAC,IAAA,oBAAM,GAAE,EAAE,QAAQ,CAAC,EAAE,YAAY,CAAC,EAChD,IAAA,oBAAM,GAAE,EACR,CAAC,KAAa,EAAE,EAAE,CAAC,CAAC,KAAK,KAAK,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,QAAQ,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,CACpE,CAAC;AARS,QAAA,kBAAkB,sBAQ3B;AAEJ;;;;;;;GAOG;AACI,MAAM,gBAAgB,GAAG,CAC9B,QAAgB,EAChB,YAAoB,EACJ,EAAE,CAClB,IAAA,oBAAM,EACJ,IAAA,uBAAS,EAAC,IAAA,iBAAG,EAAC,IAAA,oBAAM,GAAE,EAAE,QAAQ,CAAC,EAAE,YAAY,CAAC,EAChD,IAAA,oBAAM,GAAE,EACR,CAAC,KAAa,EAAE,EAAE,CAAC,CAAC,KAAK,KAAK,EAAE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAClE,CAAC;AARS,QAAA,gBAAgB,oBAQzB","sourcesContent":["import type { Struct } from '@metamask/superstruct';\nimport {\n array,\n coerce,\n defaulted,\n min,\n number,\n optional,\n string,\n} from '@metamask/superstruct';\n\nimport { UrlStruct } from '../urlStruct/urlStruct';\n\n/**\n * Create a struct for environment variables validated against the given\n * struct, where unset and empty strings (the build-time injection default)\n * mean the variable is not set.\n *\n * @param item - The struct to validate the value against when it is set,\n * e.g. `UrlStruct`.\n * @returns A struct that parses to the value or `undefined`.\n * @example\n * const PriceApiBaseUrlStruct = emptyToUndefined(UrlStruct);\n */\nexport const emptyToUndefined = <Type>(\n item: Struct<Type>,\n): Struct<Type | undefined> =>\n coerce(optional(item), string(), (value: string) =>\n value === '' ? undefined : value,\n );\n\n/**\n * Create a struct for comma-separated list environment variables.\n *\n * The raw string is split on commas and each item is validated against the\n * given item struct.\n *\n * @param item - The struct to validate each list item against.\n * @returns A struct that parses a comma-separated string into an array.\n * @example\n * const RpcUrlListStruct = commaSeparatedListOf(UrlStruct);\n */\nexport const commaSeparatedListOf = <Type>(\n item: Struct<Type>,\n): Struct<Type[]> =>\n coerce(array(item), string(), (value: string) => value.split(','));\n\n/**\n * Create a struct for a URL environment variable with a fallback: unset,\n * empty, and whitespace-only values (the build-time injection default)\n * resolve to the fallback, surrounding whitespace is trimmed, and invalid\n * values are rejected.\n *\n * @param fallback - The URL to use when the variable is unset, empty, or\n * whitespace-only.\n * @returns A struct that parses to the URL.\n * @example\n * const EsploraUrlStruct = defaultedUrlStruct('https://blockstream.info/api');\n */\nexport const defaultedUrlStruct = (fallback: string): Struct<string> =>\n coerce(defaulted(UrlStruct, fallback), string(), (value: string) => {\n const trimmed = value.trim();\n return trimmed === '' ? undefined : trimmed;\n });\n\n/**\n * Create a struct that parses an integer from a string, with a minimum value\n * and a default for unset variables.\n *\n * @param minValue - The minimum value for the integer.\n * @param defaultValue - The default value for the integer.\n * @returns A struct that parses an integer from a string.\n */\nexport const parseIntegerStruct = (\n minValue: number,\n defaultValue: number,\n): Struct<number> =>\n coerce(\n defaulted(min(number(), minValue), defaultValue),\n string(),\n (value: string) => (value === '' ? undefined : parseInt(value, 10)),\n );\n\n/**\n * Create a struct that parses a float from a string, with a minimum value and\n * a default for unset variables.\n *\n * @param minValue - The minimum value for the float.\n * @param defaultValue - The default value for the float.\n * @returns A struct that parses a float from a string.\n */\nexport const parseFloatStruct = (\n minValue: number,\n defaultValue: number,\n): Struct<number> =>\n coerce(\n defaulted(min(number(), minValue), defaultValue),\n string(),\n (value: string) => (value === '' ? undefined : parseFloat(value)),\n );\n"]}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import type { Struct } from "@metamask/superstruct";
|
|
2
|
+
/**
|
|
3
|
+
* Create a struct for environment variables validated against the given
|
|
4
|
+
* struct, where unset and empty strings (the build-time injection default)
|
|
5
|
+
* mean the variable is not set.
|
|
6
|
+
*
|
|
7
|
+
* @param item - The struct to validate the value against when it is set,
|
|
8
|
+
* e.g. `UrlStruct`.
|
|
9
|
+
* @returns A struct that parses to the value or `undefined`.
|
|
10
|
+
* @example
|
|
11
|
+
* const PriceApiBaseUrlStruct = emptyToUndefined(UrlStruct);
|
|
12
|
+
*/
|
|
13
|
+
export declare const emptyToUndefined: <Type>(item: Struct<Type>) => Struct<Type | undefined>;
|
|
14
|
+
/**
|
|
15
|
+
* Create a struct for comma-separated list environment variables.
|
|
16
|
+
*
|
|
17
|
+
* The raw string is split on commas and each item is validated against the
|
|
18
|
+
* given item struct.
|
|
19
|
+
*
|
|
20
|
+
* @param item - The struct to validate each list item against.
|
|
21
|
+
* @returns A struct that parses a comma-separated string into an array.
|
|
22
|
+
* @example
|
|
23
|
+
* const RpcUrlListStruct = commaSeparatedListOf(UrlStruct);
|
|
24
|
+
*/
|
|
25
|
+
export declare const commaSeparatedListOf: <Type>(item: Struct<Type>) => Struct<Type[]>;
|
|
26
|
+
/**
|
|
27
|
+
* Create a struct for a URL environment variable with a fallback: unset,
|
|
28
|
+
* empty, and whitespace-only values (the build-time injection default)
|
|
29
|
+
* resolve to the fallback, surrounding whitespace is trimmed, and invalid
|
|
30
|
+
* values are rejected.
|
|
31
|
+
*
|
|
32
|
+
* @param fallback - The URL to use when the variable is unset, empty, or
|
|
33
|
+
* whitespace-only.
|
|
34
|
+
* @returns A struct that parses to the URL.
|
|
35
|
+
* @example
|
|
36
|
+
* const EsploraUrlStruct = defaultedUrlStruct('https://blockstream.info/api');
|
|
37
|
+
*/
|
|
38
|
+
export declare const defaultedUrlStruct: (fallback: string) => Struct<string>;
|
|
39
|
+
/**
|
|
40
|
+
* Create a struct that parses an integer from a string, with a minimum value
|
|
41
|
+
* and a default for unset variables.
|
|
42
|
+
*
|
|
43
|
+
* @param minValue - The minimum value for the integer.
|
|
44
|
+
* @param defaultValue - The default value for the integer.
|
|
45
|
+
* @returns A struct that parses an integer from a string.
|
|
46
|
+
*/
|
|
47
|
+
export declare const parseIntegerStruct: (minValue: number, defaultValue: number) => Struct<number>;
|
|
48
|
+
/**
|
|
49
|
+
* Create a struct that parses a float from a string, with a minimum value and
|
|
50
|
+
* a default for unset variables.
|
|
51
|
+
*
|
|
52
|
+
* @param minValue - The minimum value for the float.
|
|
53
|
+
* @param defaultValue - The default value for the float.
|
|
54
|
+
* @returns A struct that parses a float from a string.
|
|
55
|
+
*/
|
|
56
|
+
export declare const parseFloatStruct: (minValue: number, defaultValue: number) => Struct<number>;
|
|
57
|
+
//# sourceMappingURL=envStructs.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"envStructs.d.cts","sourceRoot":"","sources":["../../../src/utils/config/envStructs.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,8BAA8B;AAapD;;;;;;;;;;GAUG;AACH,eAAO,MAAM,gBAAgB,GAAI,IAAI,EACnC,MAAM,MAAM,CAAC,IAAI,CAAC,KACjB,MAAM,CAAC,IAAI,GAAG,SAAS,CAGvB,CAAC;AAEJ;;;;;;;;;;GAUG;AACH,eAAO,MAAM,oBAAoB,GAAI,IAAI,EACvC,MAAM,MAAM,CAAC,IAAI,CAAC,KACjB,MAAM,CAAC,IAAI,EAAE,CACoD,CAAC;AAErE;;;;;;;;;;;GAWG;AACH,eAAO,MAAM,kBAAkB,GAAI,UAAU,MAAM,KAAG,MAAM,CAAC,MAAM,CAI/D,CAAC;AAEL;;;;;;;GAOG;AACH,eAAO,MAAM,kBAAkB,GAC7B,UAAU,MAAM,EAChB,cAAc,MAAM,KACnB,MAAM,CAAC,MAAM,CAKb,CAAC;AAEJ;;;;;;;GAOG;AACH,eAAO,MAAM,gBAAgB,GAC3B,UAAU,MAAM,EAChB,cAAc,MAAM,KACnB,MAAM,CAAC,MAAM,CAKb,CAAC"}
|