@ubnt/store.core 11.22.0-beta.0 → 11.22.0-beta.10
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 +45 -0
- package/lib/assets/ai-360.jpg +0 -0
- package/lib/assets/ai-3602.jpg +0 -0
- package/lib/assets/ai-dome.jpg +0 -0
- package/lib/assets/ai-dome2.jpg +0 -0
- package/lib/assets/ai-theta.jpg +0 -0
- package/lib/assets/ai-theta2.jpg +0 -0
- package/lib/assets/ai-turret.jpg +0 -0
- package/lib/assets/ai-turret2.jpg +0 -0
- package/lib/assets/attention.svg +1 -1
- package/lib/assets/attention2.svg +1 -1
- package/lib/assets/bullet2.svg +1 -1
- package/lib/assets/bullet3.svg +1 -1
- package/lib/assets/cloudkeyplus.jpg +0 -0
- package/lib/assets/compact.svg +1 -1
- package/lib/assets/compact2.svg +1 -1
- package/lib/assets/door-hub.jpg +0 -0
- package/lib/assets/door-hub2.jpg +0 -0
- package/lib/assets/enterprise-nvr.jpg +0 -0
- package/lib/assets/g2-reader-pro.jpg +0 -0
- package/lib/assets/g2-reader-pro2.jpg +0 -0
- package/lib/assets/g2-reader.jpg +0 -0
- package/lib/assets/g2-reader2.jpg +0 -0
- package/lib/assets/g3-reader-flex.jpg +0 -0
- package/lib/assets/g3-reader-flex2.jpg +0 -0
- package/lib/assets/g5-bullet.jpg +0 -0
- package/lib/assets/g5-bullet2.jpg +0 -0
- package/lib/assets/g5-dome.jpg +0 -0
- package/lib/assets/g5-dome2.jpg +0 -0
- package/lib/assets/g6-pro-entry.jpg +0 -0
- package/lib/assets/intercom.jpg +0 -0
- package/lib/assets/intercom2.jpg +0 -0
- package/lib/assets/interior-design.svg +1 -1
- package/lib/assets/interior-design2.svg +1 -1
- package/lib/assets/logoUisp.svg +1 -10
- package/lib/assets/logoUnifi.svg +1 -11
- package/lib/assets/minus4.svg +1 -1
- package/lib/assets/minus5.svg +1 -1
- package/lib/assets/plus4.svg +1 -1
- package/lib/assets/plus5.svg +1 -1
- package/lib/assets/unvr-instant.jpg +0 -0
- package/lib/assets/unvr-pro.jpg +0 -0
- package/lib/assets/unvr-standard.jpg +0 -0
- package/lib/assets/unvrInstant.jpg +0 -0
- package/lib/components/Header/AccountFrame/AccountFrame.styles.js +5 -0
- package/lib/components/Header/AccountFrame/AccountFrame.styles.js.map +1 -1
- package/lib/components/Header/AccountFrame/AccountFrame.styles.mjs +5 -0
- package/lib/components/Header/AccountFrame/AccountFrame.styles.mjs.map +1 -1
- package/lib/components/Header/LanguageFrame/LanguageFrame.js +2 -1
- package/lib/components/Header/LanguageFrame/LanguageFrame.js.map +1 -1
- package/lib/components/Header/LanguageFrame/LanguageFrame.mjs +2 -1
- package/lib/components/Header/LanguageFrame/LanguageFrame.mjs.map +1 -1
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/InputCard/InputCard.js +1 -1
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/InputCard/InputCard.mjs +1 -1
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/InputCard/InputSections/CounterSection.js +2 -2
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/InputCard/InputSections/CounterSection.mjs +2 -2
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/InputCard/assets/attention.svg.js +1 -1
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/InputCard/assets/attention.svg.mjs +2 -2
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/InputCard/assets/minus.svg.js +1 -1
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/InputCard/assets/minus.svg.mjs +2 -2
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/InputCard/assets/plus.svg.js +1 -1
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/InputCard/assets/plus.svg.mjs +2 -2
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/ProgressBarChart/ProgressBarChart.js +1 -1
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/ProgressBarChart/ProgressBarChart.mjs +1 -1
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/ProgressBarChart/assets/attention.svg.js +1 -1
- package/lib/components/ResourceCalculatorV1/ComputeResourceCalculator/components/ProgressBarChart/assets/attention.svg.mjs +2 -2
- package/lib/components/ResourceCalculatorV1/PoeCalculator/components/PoeConsumerConfigurator/PoeConsumerSelectModal/PoeConsumerSelectModal.js +2 -2
- package/lib/components/ResourceCalculatorV1/PoeCalculator/components/PoeConsumerConfigurator/PoeConsumerSelectModal/PoeConsumerSelectModal.mjs +2 -2
- package/lib/components/ResourceCalculatorV1/PoeCalculator/components/PoeConsumerConfigurator/assets/minus.svg.js +1 -1
- package/lib/components/ResourceCalculatorV1/PoeCalculator/components/PoeConsumerConfigurator/assets/minus.svg.mjs +2 -2
- package/lib/components/ResourceCalculatorV1/PoeCalculator/components/PoeConsumerConfigurator/assets/plus.svg.js +1 -1
- package/lib/components/ResourceCalculatorV1/PoeCalculator/components/PoeConsumerConfigurator/assets/plus.svg.mjs +2 -2
- package/lib/components/StaticModal/StaticModal.d.ts +1 -1
- package/lib/components/StaticModal/StaticModal.js +45 -1
- package/lib/components/StaticModal/StaticModal.js.map +1 -1
- package/lib/components/StaticModal/StaticModal.mjs +46 -2
- package/lib/components/StaticModal/StaticModal.mjs.map +1 -1
- package/lib/components/StaticModal/StaticModal.styles.js +1 -0
- package/lib/components/StaticModal/StaticModal.styles.js.map +1 -1
- package/lib/components/StaticModal/StaticModal.styles.mjs +1 -0
- package/lib/components/StaticModal/StaticModal.styles.mjs.map +1 -1
- package/lib/components/StaticModal/StaticModal.types.d.ts +2 -0
- package/lib/constants/address.d.ts +8 -0
- package/lib/constants/address.js +8 -0
- package/lib/constants/address.js.map +1 -1
- package/lib/constants/address.mjs +8 -0
- package/lib/constants/address.mjs.map +1 -1
- package/lib/constants/modelCompareData/assets/Assets-V4/PhysicalSecurity/g6-pro-entry.jpg.js +27 -0
- package/lib/constants/modelCompareData/assets/Assets-V4/PhysicalSecurity/g6-pro-entry.jpg.js.map +1 -0
- package/lib/constants/modelCompareData/assets/Assets-V4/PhysicalSecurity/g6-pro-entry.jpg.mjs +3 -0
- package/lib/constants/modelCompareData/assets/Assets-V4/PhysicalSecurity/g6-pro-entry.jpg.mjs.map +1 -0
- package/lib/constants/modelCompareData/assets/Bullet/bullet.svg.js +1 -1
- package/lib/constants/modelCompareData/assets/Bullet/bullet.svg.mjs +2 -2
- package/lib/constants/modelCompareData/assets/Bullet/g5-bullet.jpg.js +1 -1
- package/lib/constants/modelCompareData/assets/Bullet/g5-bullet.jpg.mjs +2 -2
- package/lib/constants/modelCompareData/assets/DomeTurret/ai-360.jpg.js +1 -1
- package/lib/constants/modelCompareData/assets/DomeTurret/ai-360.jpg.mjs +2 -2
- package/lib/constants/modelCompareData/assets/DomeTurret/ai-dome.jpg.js +1 -1
- package/lib/constants/modelCompareData/assets/DomeTurret/ai-dome.jpg.mjs +2 -2
- package/lib/constants/modelCompareData/assets/DomeTurret/ai-turret.jpg.js +1 -1
- package/lib/constants/modelCompareData/assets/DomeTurret/ai-turret.jpg.mjs +2 -2
- package/lib/constants/modelCompareData/assets/DomeTurret/g5-dome.jpg.js +1 -1
- package/lib/constants/modelCompareData/assets/DomeTurret/g5-dome.jpg.mjs +2 -2
- package/lib/constants/modelCompareData/assets/DoorAccess/door-hub.jpg.js +1 -1
- package/lib/constants/modelCompareData/assets/DoorAccess/door-hub.jpg.mjs +2 -2
- package/lib/constants/modelCompareData/assets/DoorAccess/g2-reader-pro.jpg.js +1 -1
- package/lib/constants/modelCompareData/assets/DoorAccess/g2-reader-pro.jpg.mjs +2 -2
- package/lib/constants/modelCompareData/assets/DoorAccess/g2-reader.jpg.js +1 -1
- package/lib/constants/modelCompareData/assets/DoorAccess/g2-reader.jpg.mjs +2 -2
- package/lib/constants/modelCompareData/assets/DoorAccess/g3-reader-flex.jpg.js +1 -1
- package/lib/constants/modelCompareData/assets/DoorAccess/g3-reader-flex.jpg.mjs +2 -2
- package/lib/constants/modelCompareData/assets/DoorAccess/intercom.jpg.js +1 -1
- package/lib/constants/modelCompareData/assets/DoorAccess/intercom.jpg.mjs +2 -2
- package/lib/constants/modelCompareData/assets/Interior/ai-theta.jpg.js +1 -1
- package/lib/constants/modelCompareData/assets/Interior/ai-theta.jpg.mjs +2 -2
- package/lib/constants/modelCompareData/assets/VideoRecorders/unvrInstant.jpg.js +27 -0
- package/lib/constants/modelCompareData/assets/VideoRecorders/unvrInstant.jpg.js.map +1 -0
- package/lib/constants/modelCompareData/assets/VideoRecorders/unvrInstant.jpg.mjs +3 -0
- package/lib/constants/modelCompareData/assets/VideoRecorders/unvrInstant.jpg.mjs.map +1 -0
- package/lib/constants/modelCompareData/assets/compact.svg.js +1 -1
- package/lib/constants/modelCompareData/assets/compact.svg.mjs +2 -2
- package/lib/constants/modelCompareData/assets/interior-design.svg.js +1 -1
- package/lib/constants/modelCompareData/assets/interior-design.svg.mjs +2 -2
- package/lib/constants/modelCompareData/assets/product-list-assets/bullet.svg.js +1 -1
- package/lib/constants/modelCompareData/assets/product-list-assets/bullet.svg.mjs +2 -2
- package/lib/constants/modelCompareData/assets/product-list-assets/cameras/bullet/g5-bullet.jpg.js +1 -1
- package/lib/constants/modelCompareData/assets/product-list-assets/cameras/bullet/g5-bullet.jpg.mjs +2 -2
- package/lib/constants/modelCompareData/assets/product-list-assets/cameras/dome-and-turret/ai-360.jpg.js +1 -1
- package/lib/constants/modelCompareData/assets/product-list-assets/cameras/dome-and-turret/ai-360.jpg.mjs +2 -2
- package/lib/constants/modelCompareData/assets/product-list-assets/cameras/dome-and-turret/ai-dome.jpg.js +1 -1
- package/lib/constants/modelCompareData/assets/product-list-assets/cameras/dome-and-turret/ai-dome.jpg.mjs +2 -2
- package/lib/constants/modelCompareData/assets/product-list-assets/cameras/dome-and-turret/ai-turret.jpg.js +1 -1
- package/lib/constants/modelCompareData/assets/product-list-assets/cameras/dome-and-turret/ai-turret.jpg.mjs +2 -2
- package/lib/constants/modelCompareData/assets/product-list-assets/cameras/dome-and-turret/g5-dome.jpg.js +1 -1
- package/lib/constants/modelCompareData/assets/product-list-assets/cameras/dome-and-turret/g5-dome.jpg.mjs +2 -2
- package/lib/constants/modelCompareData/assets/product-list-assets/cameras/door-access/door-hub.jpg.js +1 -1
- package/lib/constants/modelCompareData/assets/product-list-assets/cameras/door-access/door-hub.jpg.mjs +2 -2
- package/lib/constants/modelCompareData/assets/product-list-assets/cameras/door-access/g2-reader-pro.jpg.js +1 -1
- package/lib/constants/modelCompareData/assets/product-list-assets/cameras/door-access/g2-reader-pro.jpg.mjs +2 -2
- package/lib/constants/modelCompareData/assets/product-list-assets/cameras/door-access/g2-reader.jpg.js +1 -1
- package/lib/constants/modelCompareData/assets/product-list-assets/cameras/door-access/g2-reader.jpg.mjs +2 -2
- package/lib/constants/modelCompareData/assets/product-list-assets/cameras/door-access/g3-reader-flex.jpg.js +1 -1
- package/lib/constants/modelCompareData/assets/product-list-assets/cameras/door-access/g3-reader-flex.jpg.mjs +2 -2
- package/lib/constants/modelCompareData/assets/product-list-assets/cameras/door-access/intercom.jpg.js +1 -1
- package/lib/constants/modelCompareData/assets/product-list-assets/cameras/door-access/intercom.jpg.mjs +2 -2
- package/lib/constants/modelCompareData/assets/product-list-assets/cameras/interior-design/ai-theta.jpg.js +1 -1
- package/lib/constants/modelCompareData/assets/product-list-assets/cameras/interior-design/ai-theta.jpg.mjs +2 -2
- package/lib/constants/modelCompareData/assets/product-list-assets/cameras/video-recorders/unvr-instant.jpg.js +27 -0
- package/lib/constants/modelCompareData/assets/product-list-assets/cameras/video-recorders/unvr-instant.jpg.js.map +1 -0
- package/lib/constants/modelCompareData/assets/product-list-assets/cameras/video-recorders/unvr-instant.jpg.mjs +3 -0
- package/lib/constants/modelCompareData/assets/product-list-assets/cameras/video-recorders/unvr-instant.jpg.mjs.map +1 -0
- package/lib/constants/modelCompareData/assets/product-list-assets/compact.svg.js +1 -1
- package/lib/constants/modelCompareData/assets/product-list-assets/compact.svg.mjs +2 -2
- package/lib/constants/modelCompareData/assets/product-list-assets/interior-design.svg.js +1 -1
- package/lib/constants/modelCompareData/assets/product-list-assets/interior-design.svg.mjs +2 -2
- package/lib/constants/modelCompareData/data/Readers.js +2 -1
- package/lib/constants/modelCompareData/data/Readers.js.map +1 -1
- package/lib/constants/modelCompareData/data/Readers.mjs +2 -1
- package/lib/constants/modelCompareData/data/Readers.mjs.map +1 -1
- package/lib/constants/modelCompareData/data/VideoRecorders.js +2 -1
- package/lib/constants/modelCompareData/data/VideoRecorders.js.map +1 -1
- package/lib/constants/modelCompareData/data/VideoRecorders.mjs +2 -1
- package/lib/constants/modelCompareData/data/VideoRecorders.mjs.map +1 -1
- package/lib/constants/modelCompareData/data/product-groups/cameraSecurity/Bullet.js +1 -1
- package/lib/constants/modelCompareData/data/product-groups/cameraSecurity/Bullet.mjs +1 -1
- package/lib/constants/modelCompareData/data/product-groups/cameraSecurity/Compact.js +1 -1
- package/lib/constants/modelCompareData/data/product-groups/cameraSecurity/Compact.mjs +1 -1
- package/lib/constants/modelCompareData/data/product-groups/cameraSecurity/InteriorDesign.js +1 -1
- package/lib/constants/modelCompareData/data/product-groups/cameraSecurity/InteriorDesign.mjs +1 -1
- package/lib/constants/modelCompareData/data/product-list-groups/bullet.js +1 -1
- package/lib/constants/modelCompareData/data/product-list-groups/bullet.mjs +1 -1
- package/lib/constants/modelCompareData/data/product-list-groups/compact.js +1 -1
- package/lib/constants/modelCompareData/data/product-list-groups/compact.mjs +1 -1
- package/lib/constants/modelCompareData/data/product-list-groups/interiorDesign.js +1 -1
- package/lib/constants/modelCompareData/data/product-list-groups/interiorDesign.mjs +1 -1
- package/lib/constants/modelCompareData/data/products/ai360.js +2 -2
- package/lib/constants/modelCompareData/data/products/ai360.mjs +2 -2
- package/lib/constants/modelCompareData/data/products/aiDome.js +2 -2
- package/lib/constants/modelCompareData/data/products/aiDome.mjs +2 -2
- package/lib/constants/modelCompareData/data/products/aiTurret.js +2 -2
- package/lib/constants/modelCompareData/data/products/aiTurret.mjs +2 -2
- package/lib/constants/modelCompareData/data/products/cloudKeyG2Plus.js +1 -1
- package/lib/constants/modelCompareData/data/products/cloudKeyG2Plus.js.map +1 -1
- package/lib/constants/modelCompareData/data/products/cloudKeyG2Plus.mjs +1 -1
- package/lib/constants/modelCompareData/data/products/cloudKeyG2Plus.mjs.map +1 -1
- package/lib/constants/modelCompareData/data/products/doorHub.js +2 -2
- package/lib/constants/modelCompareData/data/products/doorHub.mjs +2 -2
- package/lib/constants/modelCompareData/data/products/doorbellLite.d.ts +2 -0
- package/lib/constants/modelCompareData/data/products/g2Reader.js +2 -2
- package/lib/constants/modelCompareData/data/products/g2Reader.mjs +2 -2
- package/lib/constants/modelCompareData/data/products/g2ReaderPro.js +2 -2
- package/lib/constants/modelCompareData/data/products/g2ReaderPro.mjs +2 -2
- package/lib/constants/modelCompareData/data/products/g3Reader.js +0 -1
- package/lib/constants/modelCompareData/data/products/g3Reader.js.map +1 -1
- package/lib/constants/modelCompareData/data/products/g3Reader.mjs +0 -1
- package/lib/constants/modelCompareData/data/products/g3Reader.mjs.map +1 -1
- package/lib/constants/modelCompareData/data/products/g3ReaderFlex.js +2 -3
- package/lib/constants/modelCompareData/data/products/g3ReaderFlex.js.map +1 -1
- package/lib/constants/modelCompareData/data/products/g3ReaderFlex.mjs +2 -3
- package/lib/constants/modelCompareData/data/products/g3ReaderFlex.mjs.map +1 -1
- package/lib/constants/modelCompareData/data/products/g3ReaderPro.js +0 -1
- package/lib/constants/modelCompareData/data/products/g3ReaderPro.js.map +1 -1
- package/lib/constants/modelCompareData/data/products/g3ReaderPro.mjs +0 -1
- package/lib/constants/modelCompareData/data/products/g3ReaderPro.mjs.map +1 -1
- package/lib/constants/modelCompareData/data/products/g5Bullet.js +2 -2
- package/lib/constants/modelCompareData/data/products/g5Bullet.mjs +2 -2
- package/lib/constants/modelCompareData/data/products/g5Dome.js +2 -2
- package/lib/constants/modelCompareData/data/products/g5Dome.mjs +2 -2
- package/lib/constants/modelCompareData/data/products/g6Entry.d.ts +2 -0
- package/lib/constants/modelCompareData/data/products/g6ProEntry.d.ts +2 -0
- package/lib/constants/modelCompareData/data/products/g6ProEntry.js +106 -0
- package/lib/constants/modelCompareData/data/products/g6ProEntry.js.map +1 -0
- package/lib/constants/modelCompareData/data/products/g6ProEntry.mjs +98 -0
- package/lib/constants/modelCompareData/data/products/g6ProEntry.mjs.map +1 -0
- package/lib/constants/modelCompareData/data/products/intercom.js +2 -2
- package/lib/constants/modelCompareData/data/products/intercom.mjs +2 -2
- package/lib/constants/modelCompareData/data/products/nvrInstant.d.ts +2 -0
- package/lib/constants/modelCompareData/data/products/nvrInstant.js +111 -0
- package/lib/constants/modelCompareData/data/products/nvrInstant.js.map +1 -0
- package/lib/constants/modelCompareData/data/products/nvrInstant.mjs +102 -0
- package/lib/constants/modelCompareData/data/products/nvrInstant.mjs.map +1 -0
- package/lib/constants/modelCompareData/data/products/theta.js +2 -2
- package/lib/constants/modelCompareData/data/products/theta.mjs +2 -2
- package/lib/constants/modelCompareData/translations/ar.json.js +18 -12
- package/lib/constants/modelCompareData/translations/ar.json.js.map +1 -1
- package/lib/constants/modelCompareData/translations/ar.json.mjs +18 -12
- package/lib/constants/modelCompareData/translations/ar.json.mjs.map +1 -1
- package/lib/constants/modelCompareData/translations/ca.json.js +6 -0
- package/lib/constants/modelCompareData/translations/ca.json.js.map +1 -1
- package/lib/constants/modelCompareData/translations/ca.json.mjs +6 -0
- package/lib/constants/modelCompareData/translations/ca.json.mjs.map +1 -1
- package/lib/constants/modelCompareData/translations/cs.json.js +7 -1
- package/lib/constants/modelCompareData/translations/cs.json.js.map +1 -1
- package/lib/constants/modelCompareData/translations/cs.json.mjs +7 -1
- package/lib/constants/modelCompareData/translations/cs.json.mjs.map +1 -1
- package/lib/constants/modelCompareData/translations/da.json.js +6 -0
- package/lib/constants/modelCompareData/translations/da.json.js.map +1 -1
- package/lib/constants/modelCompareData/translations/da.json.mjs +6 -0
- package/lib/constants/modelCompareData/translations/da.json.mjs.map +1 -1
- package/lib/constants/modelCompareData/translations/de.json.js +7 -1
- package/lib/constants/modelCompareData/translations/de.json.js.map +1 -1
- package/lib/constants/modelCompareData/translations/de.json.mjs +7 -1
- package/lib/constants/modelCompareData/translations/de.json.mjs.map +1 -1
- package/lib/constants/modelCompareData/translations/el.json.js +8 -2
- package/lib/constants/modelCompareData/translations/el.json.js.map +1 -1
- package/lib/constants/modelCompareData/translations/el.json.mjs +8 -2
- package/lib/constants/modelCompareData/translations/el.json.mjs.map +1 -1
- package/lib/constants/modelCompareData/translations/en.json.js +9 -0
- package/lib/constants/modelCompareData/translations/en.json.js.map +1 -1
- package/lib/constants/modelCompareData/translations/en.json.mjs +9 -0
- package/lib/constants/modelCompareData/translations/en.json.mjs.map +1 -1
- package/lib/constants/modelCompareData/translations/es-ES.json.js +7 -1
- package/lib/constants/modelCompareData/translations/es-ES.json.js.map +1 -1
- package/lib/constants/modelCompareData/translations/es-ES.json.mjs +7 -1
- package/lib/constants/modelCompareData/translations/es-ES.json.mjs.map +1 -1
- package/lib/constants/modelCompareData/translations/es-MX.json.js +6 -0
- package/lib/constants/modelCompareData/translations/es-MX.json.js.map +1 -1
- package/lib/constants/modelCompareData/translations/es-MX.json.mjs +6 -0
- package/lib/constants/modelCompareData/translations/es-MX.json.mjs.map +1 -1
- package/lib/constants/modelCompareData/translations/fr.json.js +7 -1
- package/lib/constants/modelCompareData/translations/fr.json.js.map +1 -1
- package/lib/constants/modelCompareData/translations/fr.json.mjs +7 -1
- package/lib/constants/modelCompareData/translations/fr.json.mjs.map +1 -1
- package/lib/constants/modelCompareData/translations/it.json.js +10 -4
- package/lib/constants/modelCompareData/translations/it.json.js.map +1 -1
- package/lib/constants/modelCompareData/translations/it.json.mjs +10 -4
- package/lib/constants/modelCompareData/translations/it.json.mjs.map +1 -1
- package/lib/constants/modelCompareData/translations/ja.json.js +12 -6
- package/lib/constants/modelCompareData/translations/ja.json.js.map +1 -1
- package/lib/constants/modelCompareData/translations/ja.json.mjs +12 -6
- package/lib/constants/modelCompareData/translations/ja.json.mjs.map +1 -1
- package/lib/constants/modelCompareData/translations/ko.json.js +6 -0
- package/lib/constants/modelCompareData/translations/ko.json.js.map +1 -1
- package/lib/constants/modelCompareData/translations/ko.json.mjs +6 -0
- package/lib/constants/modelCompareData/translations/ko.json.mjs.map +1 -1
- package/lib/constants/modelCompareData/translations/nl.json.js +6 -0
- package/lib/constants/modelCompareData/translations/nl.json.js.map +1 -1
- package/lib/constants/modelCompareData/translations/nl.json.mjs +6 -0
- package/lib/constants/modelCompareData/translations/nl.json.mjs.map +1 -1
- package/lib/constants/modelCompareData/translations/no.json.js +1475 -1469
- package/lib/constants/modelCompareData/translations/no.json.js.map +1 -1
- package/lib/constants/modelCompareData/translations/no.json.mjs +1475 -1469
- package/lib/constants/modelCompareData/translations/no.json.mjs.map +1 -1
- package/lib/constants/modelCompareData/translations/pl.json.js +6 -0
- package/lib/constants/modelCompareData/translations/pl.json.js.map +1 -1
- package/lib/constants/modelCompareData/translations/pl.json.mjs +6 -0
- package/lib/constants/modelCompareData/translations/pl.json.mjs.map +1 -1
- package/lib/constants/modelCompareData/translations/pt-BR.json.js +7 -1
- package/lib/constants/modelCompareData/translations/pt-BR.json.js.map +1 -1
- package/lib/constants/modelCompareData/translations/pt-BR.json.mjs +7 -1
- package/lib/constants/modelCompareData/translations/pt-BR.json.mjs.map +1 -1
- package/lib/constants/modelCompareData/translations/pt-PT.json.js +6 -0
- package/lib/constants/modelCompareData/translations/pt-PT.json.js.map +1 -1
- package/lib/constants/modelCompareData/translations/pt-PT.json.mjs +6 -0
- package/lib/constants/modelCompareData/translations/pt-PT.json.mjs.map +1 -1
- package/lib/constants/modelCompareData/translations/ru.json.js +6 -0
- package/lib/constants/modelCompareData/translations/ru.json.js.map +1 -1
- package/lib/constants/modelCompareData/translations/ru.json.mjs +6 -0
- package/lib/constants/modelCompareData/translations/ru.json.mjs.map +1 -1
- package/lib/constants/modelCompareData/translations/sv.json.js +6 -0
- package/lib/constants/modelCompareData/translations/sv.json.js.map +1 -1
- package/lib/constants/modelCompareData/translations/sv.json.mjs +6 -0
- package/lib/constants/modelCompareData/translations/sv.json.mjs.map +1 -1
- package/lib/constants/modelCompareData/translations/tr.json.js +7 -1
- package/lib/constants/modelCompareData/translations/tr.json.js.map +1 -1
- package/lib/constants/modelCompareData/translations/tr.json.mjs +7 -1
- package/lib/constants/modelCompareData/translations/tr.json.mjs.map +1 -1
- package/lib/constants/modelCompareData/translations/useModelCompareTranslation.d.ts +2 -2
- package/lib/constants/modelCompareData/translations/zh-Hans.json.js +6 -0
- package/lib/constants/modelCompareData/translations/zh-Hans.json.js.map +1 -1
- package/lib/constants/modelCompareData/translations/zh-Hans.json.mjs +6 -0
- package/lib/constants/modelCompareData/translations/zh-Hans.json.mjs.map +1 -1
- package/lib/constants/modelCompareData/translations/zh-Hant-TW.json.js +7 -1
- package/lib/constants/modelCompareData/translations/zh-Hant-TW.json.js.map +1 -1
- package/lib/constants/modelCompareData/translations/zh-Hant-TW.json.mjs +7 -1
- package/lib/constants/modelCompareData/translations/zh-Hant-TW.json.mjs.map +1 -1
- package/lib/constants/modelCompareData/types.d.ts +5 -1
- package/lib/constants/modelCompareData/types.js +4 -0
- package/lib/constants/modelCompareData/types.js.map +1 -1
- package/lib/constants/modelCompareData/types.mjs +4 -0
- package/lib/constants/modelCompareData/types.mjs.map +1 -1
- package/lib/constants/proStoreFilters/mapping.d.ts +265 -1
- package/lib/constants/proStoreFilters/mapping.js.map +1 -1
- package/lib/constants/proStoreFilters/mapping.mjs.map +1 -1
- package/lib/contexts/MicrositeMenuContext/mapping.js +1 -0
- package/lib/contexts/MicrositeMenuContext/mapping.js.map +1 -1
- package/lib/contexts/MicrositeMenuContext/mapping.mjs +1 -0
- package/lib/contexts/MicrositeMenuContext/mapping.mjs.map +1 -1
- package/lib/types/generated.d.ts +1 -0
- package/lib/types/generated.js +1 -0
- package/lib/types/generated.js.map +1 -1
- package/lib/types/generated.mjs +1 -0
- package/lib/types/generated.mjs.map +1 -1
- package/lib/utils/address.js +1 -1
- package/lib/utils/address.js.map +1 -1
- package/lib/utils/address.mjs +1 -1
- package/lib/utils/address.mjs.map +1 -1
- package/lib/utils/tax/index.d.ts +8 -0
- package/lib/utils/tax/index.js +18 -0
- package/lib/utils/tax/index.js.map +1 -1
- package/lib/utils/tax/index.mjs +18 -0
- package/lib/utils/tax/index.mjs.map +1 -1
- package/lib/utils/tax/types.d.ts +3 -1
- package/lib/utils/tax/types.js +2 -0
- package/lib/utils/tax/types.js.map +1 -1
- package/lib/utils/tax/types.mjs +2 -0
- package/lib/utils/tax/types.mjs.map +1 -1
- package/lib/utils/url.d.ts +8 -2
- package/lib/utils/url.js +4 -0
- package/lib/utils/url.js.map +1 -1
- package/lib/utils/url.mjs +4 -0
- package/lib/utils/url.mjs.map +1 -1
- package/lib/utils/validations/address2SuperRefinement.js +1 -1
- package/lib/utils/validations/address2SuperRefinement.js.map +1 -1
- package/lib/utils/validations/address2SuperRefinement.mjs +1 -1
- package/lib/utils/validations/address2SuperRefinement.mjs.map +1 -1
- package/lib/utils/validations/taxIdSuperRefinement.js +1 -1
- package/lib/utils/validations/taxIdSuperRefinement.js.map +1 -1
- package/lib/utils/validations/taxIdSuperRefinement.mjs +1 -1
- package/lib/utils/validations/taxIdSuperRefinement.mjs.map +1 -1
- package/package.json +1 -1
package/lib/utils/tax/index.d.ts
CHANGED
|
@@ -150,6 +150,10 @@ export declare const getValidationMappingByType: (type: AddressType) => {
|
|
|
150
150
|
readonly validation: RegExp;
|
|
151
151
|
readonly isOptional: false;
|
|
152
152
|
};
|
|
153
|
+
readonly PH: {
|
|
154
|
+
readonly validation: RegExp;
|
|
155
|
+
readonly isOptional: false;
|
|
156
|
+
};
|
|
153
157
|
} | {
|
|
154
158
|
readonly BR: {
|
|
155
159
|
readonly validation: import("./types").CountryTaxIdValidationFn;
|
|
@@ -159,6 +163,10 @@ export declare const getValidationMappingByType: (type: AddressType) => {
|
|
|
159
163
|
readonly validation: RegExp;
|
|
160
164
|
readonly isOptional: true;
|
|
161
165
|
};
|
|
166
|
+
readonly PH: {
|
|
167
|
+
readonly validation: RegExp;
|
|
168
|
+
readonly isOptional: false;
|
|
169
|
+
};
|
|
162
170
|
};
|
|
163
171
|
/**
|
|
164
172
|
* Checks if given tax country has regex pattern for tax id's
|
package/lib/utils/tax/index.js
CHANGED
|
@@ -131,6 +131,24 @@ const getTaxLabelTypeByCountry = (country, options) => {
|
|
|
131
131
|
return types.TaxLabelType.Vat;
|
|
132
132
|
}
|
|
133
133
|
}
|
|
134
|
+
case generated.CountryCode.Ph:
|
|
135
|
+
{
|
|
136
|
+
switch (options.type) {
|
|
137
|
+
case "general":
|
|
138
|
+
return types.TaxLabelType.Vat;
|
|
139
|
+
case "specific":
|
|
140
|
+
{
|
|
141
|
+
const taxLabelTypesByAddressType = {
|
|
142
|
+
[generated.AddressType.Residential]: types.TaxLabelType.Psn,
|
|
143
|
+
[generated.AddressType.Business]: types.TaxLabelType.Tin
|
|
144
|
+
};
|
|
145
|
+
return taxLabelTypesByAddressType[options.addressType];
|
|
146
|
+
}
|
|
147
|
+
default:
|
|
148
|
+
errors.checkUnreachable(options);
|
|
149
|
+
return types.TaxLabelType.Vat;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
134
152
|
default:
|
|
135
153
|
{
|
|
136
154
|
return types.TaxLabelType.Vat;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../../../src/utils/tax/index.ts"],"sourcesContent":["import isRegExp from \"lodash/isRegExp\";\n\nimport {\n BUSINESS_COUNTRY_CODE_MAPPING,\n RESIDENTIAL_COUNTRY_CODE_MAPPING,\n USER_ASSIGNED_COUNTRY_MAPPING_RULES,\n} from \"../../constants/address\";\nimport type { Nullish, UserAssignedCountryCode } from \"../../types\";\nimport { AddressType, CountryCode, StoreId } from \"../../types\";\nimport { assertUnreachable, checkUnreachable } from \"../errors\";\nimport { formatCnpj, formatCpf } from \"./brazil\";\nimport type { TaxCountryCode } from \"./types\";\nimport { TaxLabelType } from \"./types\";\n\n/**\n * Util to get the country code mapping based on address type in a type-safe way\n */\nexport const getValidationMappingByType = (type: AddressType) => {\n switch (type) {\n case AddressType.Business: {\n return BUSINESS_COUNTRY_CODE_MAPPING;\n }\n case AddressType.Residential: {\n return RESIDENTIAL_COUNTRY_CODE_MAPPING;\n }\n default: {\n assertUnreachable(type);\n }\n }\n};\n\n/**\n * Checks if given tax country has regex pattern for tax id's\n */\nexport const hasTaxNumberPatternValidation = (\n type: AddressType,\n taxCountryCode: TaxCountryCode,\n): taxCountryCode is keyof ReturnType<typeof getValidationMappingByType> =>\n taxCountryCode in getValidationMappingByType(type);\n\n/**\n * Checks if given tax id meets static pattern validation\n */\nexport const isValidTaxNumberPattern = (\n type: AddressType,\n taxCountryCode: TaxCountryCode,\n taxId: Nullish<string>,\n): boolean => {\n // If country doesn't have a tax number pattern then mark input as valid\n if (!hasTaxNumberPatternValidation(type, taxCountryCode)) {\n return true;\n }\n\n const mapping = getValidationMappingByType(type);\n const { validation, isOptional } = mapping[taxCountryCode];\n\n // Mark input as valid if the tax ID is missing but it's optional\n if (!taxId && isOptional) {\n return true;\n }\n\n // Check for empty tax id's in cases when they are required\n if (!taxId) {\n return false;\n }\n\n // Technically, this should already be covered by the \"hasTaxNumberPatternValidation\"\n // check above but just in case something goes really wrong, we are adding this\n // to avoid unexpected type errors during runtime\n if (!validation) {\n return true;\n }\n\n // Validate pattern\n return isRegExp(validation)\n ? validation.test(taxId)\n : validation({ taxId, taxCountryCode });\n};\n\n/**\n * Depending on store Tax is called different names, so this mapping determines which one to use\n * @see https://ui.slack.com/archives/D0191CBEB27/p1678723218358039?thread_ts=1678722532.763459&cid=D0191CBEB27\n */\nexport const getTaxLabelType = (storeId: StoreId) => {\n switch (storeId) {\n case StoreId.Me:\n case StoreId.Za:\n case StoreId.Eu:\n case StoreId.Uk:\n case StoreId.Br:\n case StoreId.Tw:\n case StoreId.Jp:\n case StoreId.Cn:\n case StoreId.Ph: {\n return TaxLabelType.Vat;\n }\n case StoreId.Ca:\n case StoreId.Us: {\n return TaxLabelType.Tax;\n }\n case StoreId.In:\n case StoreId.Sg: {\n return TaxLabelType.Gst;\n }\n case StoreId.Mx: {\n return TaxLabelType.Rfc;\n }\n default: {\n checkUnreachable(storeId);\n return TaxLabelType.Tax;\n }\n }\n};\n\nexport const getTaxLabelTypeByCountry = (\n country: CountryCode,\n options:\n | {\n type: \"general\";\n }\n | {\n type: \"specific\";\n addressType: AddressType;\n },\n) => {\n switch (country) {\n case CountryCode.Ca:\n case CountryCode.Us:\n case CountryCode.Pr:\n case CountryCode.Pw:\n case CountryCode.Gu:\n case CountryCode.As:\n case CountryCode.Vi:\n case CountryCode.Mp:\n case CountryCode.Mh:\n case CountryCode.Fm: {\n return TaxLabelType.Tax;\n }\n case CountryCode.In:\n case CountryCode.Sg: {\n return TaxLabelType.Gst;\n }\n case CountryCode.Mx: {\n return TaxLabelType.Rfc;\n }\n case CountryCode.Br: {\n switch (options.type) {\n case \"general\":\n return TaxLabelType.Vat;\n case \"specific\": {\n const taxLabelTypesByAddressType = {\n [AddressType.Residential]: TaxLabelType.Cpf,\n [AddressType.Business]: TaxLabelType.Cnpj,\n } satisfies Record<AddressType, TaxLabelType>;\n\n return taxLabelTypesByAddressType[options.addressType];\n }\n default:\n checkUnreachable(options);\n return TaxLabelType.Vat;\n }\n }\n default: {\n return TaxLabelType.Vat;\n }\n }\n};\n\n// #############################################################################\n// Assigned country code\n// #############################################################################\n/**\n * We can't 100% trust the order of countries as compiles can possible rearrange the values, so we have to sort the\n * values with country+province first when looking for the match we can stop at first occurrence\n * @see https://stackoverflow.com/a/5525820\n */\nconst userAssignedCountryRulesSorted = Object.entries(\n USER_ASSIGNED_COUNTRY_MAPPING_RULES,\n)\n .flatMap(([userAssignedCountry, rules]) =>\n rules.map((rule) => {\n return [userAssignedCountry as UserAssignedCountryCode, rule] as const;\n }),\n )\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n .sort(([_, a], [__, b]) => {\n return b.length - a.length;\n });\n\n/**\n * Util to get TAX country code for a given country + province combination\n */\nexport const getTaxCountry = ({\n country,\n province,\n}: {\n country: CountryCode;\n province: Nullish<string>;\n}): TaxCountryCode => {\n const match = userAssignedCountryRulesSorted.find(\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n ([_, [refCountry, refProvince]]) => {\n // Don't match - if countries don't match\n if (refCountry !== country) {\n return false;\n }\n\n // Match - if pattern doesn't include a province\n // TODO: this is debatable whether it should match as input province can be non-nullish\n if (refProvince == null) {\n return true;\n }\n\n return refProvince === province;\n },\n );\n\n const userAssignedCountry = match?.[0];\n\n // If there isn't a user assigned country that matches the result (most likely indicating that it doesnt require\n // unique lookup) then fallback to input country\n return userAssignedCountry ?? country;\n};\n\nexport const isTaxNumberOptional = (\n type: AddressType,\n taxCountryCode: TaxCountryCode,\n) => {\n if (!hasTaxNumberPatternValidation(type, taxCountryCode)) {\n return true;\n }\n\n const mapping = getValidationMappingByType(type);\n const { isOptional } = mapping[taxCountryCode];\n\n return isOptional;\n};\n\nexport const formatTaxNumber = (\n taxNumber: string,\n { country, addressType }: { country: CountryCode; addressType: AddressType },\n) => {\n // Tax number formatting only needed for Brazil in the foreseeable future.\n if (country !== CountryCode.Br) {\n return taxNumber;\n }\n\n switch (addressType) {\n case AddressType.Residential:\n return formatCpf(taxNumber);\n\n case AddressType.Business:\n return formatCnpj(taxNumber);\n\n default:\n checkUnreachable(addressType);\n return taxNumber;\n }\n};\n"],"names":["getValidationMappingByType","type","AddressType","Business","BUSINESS_COUNTRY_CODE_MAPPING","Residential","RESIDENTIAL_COUNTRY_CODE_MAPPING","assertUnreachable","hasTaxNumberPatternValidation","taxCountryCode","isValidTaxNumberPattern","taxId","mapping","validation","isOptional","isRegExp","test","getTaxLabelType","storeId","StoreId","Me","Za","Eu","Uk","Br","Tw","Jp","Cn","Ph","TaxLabelType","Vat","Ca","Us","Tax","In","Sg","Gst","Mx","Rfc","checkUnreachable","getTaxLabelTypeByCountry","country","options","CountryCode","Pr","Pw","Gu","As","Vi","Mp","Mh","Fm","taxLabelTypesByAddressType","Cpf","Cnpj","addressType","userAssignedCountryRulesSorted","Object","entries","USER_ASSIGNED_COUNTRY_MAPPING_RULES","flatMap","userAssignedCountry","rules","map","rule","sort","_","a","__","b","length","getTaxCountry","province","match","find","refCountry","refProvince","isTaxNumberOptional","formatTaxNumber","taxNumber","formatCpf","formatCnpj"],"mappings":";;;;;;;;;;;;;;;;AAiBO,MAAMA,0BAAA,GAA8BC,IAAA,IAAsB;EAC/D,QAAQA,IAAA;IACN,KAAKC,SAAAA,CAAAA,YAAYC,QAAA;MAAU;QACzB,OAAOC,OAAAA,CAAAA,6BAAA;MACT;IACA,KAAKF,SAAAA,CAAAA,YAAYG,WAAA;MAAa;QAC5B,OAAOC,OAAAA,CAAAA,gCAAA;MACT;IACA;MAAS;QACPC,MAAAA,CAAAA,iBAAA,CAAkBN,IAAI,CAAA;MACxB;EAAA;AAEJ,CAAA;AAKO,MAAMO,gCAAgCA,CAC3CP,IAAA,EACAQ,cAAA,KAEAA,cAAA,IAAkBT,2BAA2BC,IAAI,CAAA;AAK5C,MAAMS,uBAAA,GAA0BA,CACrCT,IAAA,EACAQ,cAAA,EACAE,KAAA,KACY;EAEZ,IAAI,CAACH,6BAAA,CAA8BP,IAAA,EAAMQ,cAAc,CAAA,EAAG;IACxD,OAAO,IAAA;EACT;EAEA,MAAMG,OAAA,GAAUZ,2BAA2BC,IAAI,CAAA;EAC/C,MAAM;IAAEY,UAAA;IAAYC;GAAW,GAAIF,QAAQH,cAAc,CAAA;EAGzD,IAAI,CAACE,SAASG,UAAA,EAAY;IACxB,OAAO,IAAA;EACT;EAGA,IAAI,CAACH,KAAA,EAAO;IACV,OAAO,KAAA;EACT;EAKA,IAAI,CAACE,UAAA,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;EACT;EAEA,MAAMG,OAAA,GAAUZ,2BAA2BC,IAAI,CAAA;EAC/C,MAAM;IAAEa;EAAA,CAAW,GAAIF,OAAA,CAAQH,cAAc,CAAA;EAE7C,OAAOK,UAAA;AACT,CAAA;AAEO,MAAMgE,kBAAkBA,CAC7BC,SAAA,EACA;EAAEtC,OAAA;EAASc;CAAY,KACpB;EAEH,IAAId,OAAA,KAAYE,SAAAA,CAAAA,YAAYnB,EAAA,EAAI;IAC9B,OAAOuD,SAAA;EACT;EAEA,QAAQxB,WAAA;IACN,KAAKrD,SAAAA,CAAAA,WAAA,CAAYG,WAAA;MACf,OAAO2E,MAAAA,CAAAA,UAAUD,SAAS,CAAA;IAE5B,KAAK7E,SAAAA,CAAAA,WAAA,CAAYC,QAAA;MACf,OAAO8E,MAAAA,CAAAA,WAAWF,SAAS,CAAA;IAE7B;MACExC,MAAAA,CAAAA,gBAAA,CAAiBgB,WAAW,CAAA;MAC5B,OAAOwB,SAAA;EAAA;AAEb,CAAA;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../../../src/utils/tax/index.ts"],"sourcesContent":["import isRegExp from \"lodash/isRegExp\";\n\nimport {\n BUSINESS_COUNTRY_CODE_MAPPING,\n RESIDENTIAL_COUNTRY_CODE_MAPPING,\n USER_ASSIGNED_COUNTRY_MAPPING_RULES,\n} from \"../../constants/address\";\nimport type { Nullish, UserAssignedCountryCode } from \"../../types\";\nimport { AddressType, CountryCode, StoreId } from \"../../types\";\nimport { assertUnreachable, checkUnreachable } from \"../errors\";\nimport { formatCnpj, formatCpf } from \"./brazil\";\nimport type { TaxCountryCode } from \"./types\";\nimport { TaxLabelType } from \"./types\";\n\n/**\n * Util to get the country code mapping based on address type in a type-safe way\n */\nexport const getValidationMappingByType = (type: AddressType) => {\n switch (type) {\n case AddressType.Business: {\n return BUSINESS_COUNTRY_CODE_MAPPING;\n }\n case AddressType.Residential: {\n return RESIDENTIAL_COUNTRY_CODE_MAPPING;\n }\n default: {\n assertUnreachable(type);\n }\n }\n};\n\n/**\n * Checks if given tax country has regex pattern for tax id's\n */\nexport const hasTaxNumberPatternValidation = (\n type: AddressType,\n taxCountryCode: TaxCountryCode,\n): taxCountryCode is keyof ReturnType<typeof getValidationMappingByType> =>\n taxCountryCode in getValidationMappingByType(type);\n\n/**\n * Checks if given tax id meets static pattern validation\n */\nexport const isValidTaxNumberPattern = (\n type: AddressType,\n taxCountryCode: TaxCountryCode,\n taxId: Nullish<string>,\n): boolean => {\n // If country doesn't have a tax number pattern then mark input as valid\n if (!hasTaxNumberPatternValidation(type, taxCountryCode)) {\n return true;\n }\n\n const mapping = getValidationMappingByType(type);\n const { validation, isOptional } = mapping[taxCountryCode];\n\n // Mark input as valid if the tax ID is missing but it's optional\n if (!taxId && isOptional) {\n return true;\n }\n\n // Check for empty tax id's in cases when they are required\n if (!taxId) {\n return false;\n }\n\n // Technically, this should already be covered by the \"hasTaxNumberPatternValidation\"\n // check above but just in case something goes really wrong, we are adding this\n // to avoid unexpected type errors during runtime\n if (!validation) {\n return true;\n }\n\n // Validate pattern\n return isRegExp(validation)\n ? validation.test(taxId)\n : validation({ taxId, taxCountryCode });\n};\n\n/**\n * Depending on store Tax is called different names, so this mapping determines which one to use\n * @see https://ui.slack.com/archives/D0191CBEB27/p1678723218358039?thread_ts=1678722532.763459&cid=D0191CBEB27\n */\nexport const getTaxLabelType = (storeId: StoreId) => {\n switch (storeId) {\n case StoreId.Me:\n case StoreId.Za:\n case StoreId.Eu:\n case StoreId.Uk:\n case StoreId.Br:\n case StoreId.Tw:\n case StoreId.Jp:\n case StoreId.Cn:\n case StoreId.Ph: {\n return TaxLabelType.Vat;\n }\n case StoreId.Ca:\n case StoreId.Us: {\n return TaxLabelType.Tax;\n }\n case StoreId.In:\n case StoreId.Sg: {\n return TaxLabelType.Gst;\n }\n case StoreId.Mx: {\n return TaxLabelType.Rfc;\n }\n default: {\n checkUnreachable(storeId);\n return TaxLabelType.Tax;\n }\n }\n};\n\nexport const getTaxLabelTypeByCountry = (\n country: CountryCode,\n options:\n | {\n type: \"general\";\n }\n | {\n type: \"specific\";\n addressType: AddressType;\n },\n) => {\n switch (country) {\n case CountryCode.Ca:\n case CountryCode.Us:\n case CountryCode.Pr:\n case CountryCode.Pw:\n case CountryCode.Gu:\n case CountryCode.As:\n case CountryCode.Vi:\n case CountryCode.Mp:\n case CountryCode.Mh:\n case CountryCode.Fm: {\n return TaxLabelType.Tax;\n }\n case CountryCode.In:\n case CountryCode.Sg: {\n return TaxLabelType.Gst;\n }\n case CountryCode.Mx: {\n return TaxLabelType.Rfc;\n }\n case CountryCode.Br: {\n switch (options.type) {\n case \"general\":\n return TaxLabelType.Vat;\n case \"specific\": {\n const taxLabelTypesByAddressType = {\n [AddressType.Residential]: TaxLabelType.Cpf,\n [AddressType.Business]: TaxLabelType.Cnpj,\n } satisfies Record<AddressType, TaxLabelType>;\n\n return taxLabelTypesByAddressType[options.addressType];\n }\n default:\n checkUnreachable(options);\n return TaxLabelType.Vat;\n }\n }\n case CountryCode.Ph: {\n switch (options.type) {\n case \"general\":\n return TaxLabelType.Vat;\n case \"specific\": {\n const taxLabelTypesByAddressType = {\n [AddressType.Residential]: TaxLabelType.Psn,\n [AddressType.Business]: TaxLabelType.Tin,\n } satisfies Record<AddressType, TaxLabelType>;\n\n return taxLabelTypesByAddressType[options.addressType];\n }\n default:\n checkUnreachable(options);\n return TaxLabelType.Vat;\n }\n }\n default: {\n return TaxLabelType.Vat;\n }\n }\n};\n\n// #############################################################################\n// Assigned country code\n// #############################################################################\n/**\n * We can't 100% trust the order of countries as compiles can possible rearrange the values, so we have to sort the\n * values with country+province first when looking for the match we can stop at first occurrence\n * @see https://stackoverflow.com/a/5525820\n */\nconst userAssignedCountryRulesSorted = Object.entries(\n USER_ASSIGNED_COUNTRY_MAPPING_RULES,\n)\n .flatMap(([userAssignedCountry, rules]) =>\n rules.map((rule) => {\n return [userAssignedCountry as UserAssignedCountryCode, rule] as const;\n }),\n )\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n .sort(([_, a], [__, b]) => {\n return b.length - a.length;\n });\n\n/**\n * Util to get TAX country code for a given country + province combination\n */\nexport const getTaxCountry = ({\n country,\n province,\n}: {\n country: CountryCode;\n province: Nullish<string>;\n}): TaxCountryCode => {\n const match = userAssignedCountryRulesSorted.find(\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n ([_, [refCountry, refProvince]]) => {\n // Don't match - if countries don't match\n if (refCountry !== country) {\n return false;\n }\n\n // Match - if pattern doesn't include a province\n // TODO: this is debatable whether it should match as input province can be non-nullish\n if (refProvince == null) {\n return true;\n }\n\n return refProvince === province;\n },\n );\n\n const userAssignedCountry = match?.[0];\n\n // If there isn't a user assigned country that matches the result (most likely indicating that it doesnt require\n // unique lookup) then fallback to input country\n return userAssignedCountry ?? country;\n};\n\nexport const isTaxNumberOptional = (\n type: AddressType,\n taxCountryCode: TaxCountryCode,\n) => {\n if (!hasTaxNumberPatternValidation(type, taxCountryCode)) {\n return true;\n }\n\n const mapping = getValidationMappingByType(type);\n const { isOptional } = mapping[taxCountryCode];\n\n return isOptional;\n};\n\nexport const formatTaxNumber = (\n taxNumber: string,\n { country, addressType }: { country: CountryCode; addressType: AddressType },\n) => {\n // Tax number formatting only needed for Brazil in the foreseeable future.\n if (country !== CountryCode.Br) {\n return taxNumber;\n }\n\n switch (addressType) {\n case AddressType.Residential:\n return formatCpf(taxNumber);\n\n case AddressType.Business:\n return formatCnpj(taxNumber);\n\n default:\n checkUnreachable(addressType);\n return taxNumber;\n }\n};\n"],"names":["getValidationMappingByType","type","AddressType","Business","BUSINESS_COUNTRY_CODE_MAPPING","Residential","RESIDENTIAL_COUNTRY_CODE_MAPPING","assertUnreachable","hasTaxNumberPatternValidation","taxCountryCode","isValidTaxNumberPattern","taxId","mapping","validation","isOptional","isRegExp","test","getTaxLabelType","storeId","StoreId","Me","Za","Eu","Uk","Br","Tw","Jp","Cn","Ph","TaxLabelType","Vat","Ca","Us","Tax","In","Sg","Gst","Mx","Rfc","checkUnreachable","getTaxLabelTypeByCountry","country","options","CountryCode","Pr","Pw","Gu","As","Vi","Mp","Mh","Fm","taxLabelTypesByAddressType","Cpf","Cnpj","addressType","Psn","Tin","userAssignedCountryRulesSorted","Object","entries","USER_ASSIGNED_COUNTRY_MAPPING_RULES","flatMap","userAssignedCountry","rules","map","rule","sort","_","a","__","b","length","getTaxCountry","province","match","find","refCountry","refProvince","isTaxNumberOptional","formatTaxNumber","taxNumber","formatCpf","formatCnpj"],"mappings":";;;;;;;;;;;;;;;;AAiBO,MAAMA,0BAAA,GAA8BC,IAAA,IAAsB;EAC/D,QAAQA,IAAA;IACN,KAAKC,SAAAA,CAAAA,YAAYC,QAAA;MAAU;QACzB,OAAOC,OAAAA,CAAAA,6BAAA;MACT;IACA,KAAKF,SAAAA,CAAAA,YAAYG,WAAA;MAAa;QAC5B,OAAOC,OAAAA,CAAAA,gCAAA;MACT;IACA;MAAS;QACPC,MAAAA,CAAAA,iBAAA,CAAkBN,IAAI,CAAA;MACxB;EAAA;AAEJ,CAAA;AAKO,MAAMO,gCAAgCA,CAC3CP,IAAA,EACAQ,cAAA,KAEAA,cAAA,IAAkBT,2BAA2BC,IAAI,CAAA;AAK5C,MAAMS,uBAAA,GAA0BA,CACrCT,IAAA,EACAQ,cAAA,EACAE,KAAA,KACY;EAEZ,IAAI,CAACH,6BAAA,CAA8BP,IAAA,EAAMQ,cAAc,CAAA,EAAG;IACxD,OAAO,IAAA;EACT;EAEA,MAAMG,OAAA,GAAUZ,2BAA2BC,IAAI,CAAA;EAC/C,MAAM;IAAEY,UAAA;IAAYC;GAAW,GAAIF,QAAQH,cAAc,CAAA;EAGzD,IAAI,CAACE,SAASG,UAAA,EAAY;IACxB,OAAO,IAAA;EACT;EAGA,IAAI,CAACH,KAAA,EAAO;IACV,OAAO,KAAA;EACT;EAKA,IAAI,CAACE,UAAA,EAAY;IACf,OAAO,IAAA;EACT;EAGA,OAAOE,4BAAA,CAASF,UAAU,C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}
|
|
125
125
|
}
|
|
126
|
+
case CountryCode.Ph:
|
|
127
|
+
{
|
|
128
|
+
switch (options.type) {
|
|
129
|
+
case "general":
|
|
130
|
+
return TaxLabelType.Vat;
|
|
131
|
+
case "specific":
|
|
132
|
+
{
|
|
133
|
+
const taxLabelTypesByAddressType = {
|
|
134
|
+
[AddressType.Residential]: TaxLabelType.Psn,
|
|
135
|
+
[AddressType.Business]: TaxLabelType.Tin
|
|
136
|
+
};
|
|
137
|
+
return taxLabelTypesByAddressType[options.addressType];
|
|
138
|
+
}
|
|
139
|
+
default:
|
|
140
|
+
checkUnreachable(options);
|
|
141
|
+
return TaxLabelType.Vat;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
126
144
|
default:
|
|
127
145
|
{
|
|
128
146
|
return TaxLabelType.Vat;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sources":["../../../src/utils/tax/index.ts"],"sourcesContent":["import isRegExp from \"lodash/isRegExp\";\n\nimport {\n BUSINESS_COUNTRY_CODE_MAPPING,\n RESIDENTIAL_COUNTRY_CODE_MAPPING,\n USER_ASSIGNED_COUNTRY_MAPPING_RULES,\n} from \"../../constants/address\";\nimport type { Nullish, UserAssignedCountryCode } from \"../../types\";\nimport { AddressType, CountryCode, StoreId } from \"../../types\";\nimport { assertUnreachable, checkUnreachable } from \"../errors\";\nimport { formatCnpj, formatCpf } from \"./brazil\";\nimport type { TaxCountryCode } from \"./types\";\nimport { TaxLabelType } from \"./types\";\n\n/**\n * Util to get the country code mapping based on address type in a type-safe way\n */\nexport const getValidationMappingByType = (type: AddressType) => {\n switch (type) {\n case AddressType.Business: {\n return BUSINESS_COUNTRY_CODE_MAPPING;\n }\n case AddressType.Residential: {\n return RESIDENTIAL_COUNTRY_CODE_MAPPING;\n }\n default: {\n assertUnreachable(type);\n }\n }\n};\n\n/**\n * Checks if given tax country has regex pattern for tax id's\n */\nexport const hasTaxNumberPatternValidation = (\n type: AddressType,\n taxCountryCode: TaxCountryCode,\n): taxCountryCode is keyof ReturnType<typeof getValidationMappingByType> =>\n taxCountryCode in getValidationMappingByType(type);\n\n/**\n * Checks if given tax id meets static pattern validation\n */\nexport const isValidTaxNumberPattern = (\n type: AddressType,\n taxCountryCode: TaxCountryCode,\n taxId: Nullish<string>,\n): boolean => {\n // If country doesn't have a tax number pattern then mark input as valid\n if (!hasTaxNumberPatternValidation(type, taxCountryCode)) {\n return true;\n }\n\n const mapping = getValidationMappingByType(type);\n const { validation, isOptional } = mapping[taxCountryCode];\n\n // Mark input as valid if the tax ID is missing but it's optional\n if (!taxId && isOptional) {\n return true;\n }\n\n // Check for empty tax id's in cases when they are required\n if (!taxId) {\n return false;\n }\n\n // Technically, this should already be covered by the \"hasTaxNumberPatternValidation\"\n // check above but just in case something goes really wrong, we are adding this\n // to avoid unexpected type errors during runtime\n if (!validation) {\n return true;\n }\n\n // Validate pattern\n return isRegExp(validation)\n ? validation.test(taxId)\n : validation({ taxId, taxCountryCode });\n};\n\n/**\n * Depending on store Tax is called different names, so this mapping determines which one to use\n * @see https://ui.slack.com/archives/D0191CBEB27/p1678723218358039?thread_ts=1678722532.763459&cid=D0191CBEB27\n */\nexport const getTaxLabelType = (storeId: StoreId) => {\n switch (storeId) {\n case StoreId.Me:\n case StoreId.Za:\n case StoreId.Eu:\n case StoreId.Uk:\n case StoreId.Br:\n case StoreId.Tw:\n case StoreId.Jp:\n case StoreId.Cn:\n case StoreId.Ph: {\n return TaxLabelType.Vat;\n }\n case StoreId.Ca:\n case StoreId.Us: {\n return TaxLabelType.Tax;\n }\n case StoreId.In:\n case StoreId.Sg: {\n return TaxLabelType.Gst;\n }\n case StoreId.Mx: {\n return TaxLabelType.Rfc;\n }\n default: {\n checkUnreachable(storeId);\n return TaxLabelType.Tax;\n }\n }\n};\n\nexport const getTaxLabelTypeByCountry = (\n country: CountryCode,\n options:\n | {\n type: \"general\";\n }\n | {\n type: \"specific\";\n addressType: AddressType;\n },\n) => {\n switch (country) {\n case CountryCode.Ca:\n case CountryCode.Us:\n case CountryCode.Pr:\n case CountryCode.Pw:\n case CountryCode.Gu:\n case CountryCode.As:\n case CountryCode.Vi:\n case CountryCode.Mp:\n case CountryCode.Mh:\n case CountryCode.Fm: {\n return TaxLabelType.Tax;\n }\n case CountryCode.In:\n case CountryCode.Sg: {\n return TaxLabelType.Gst;\n }\n case CountryCode.Mx: {\n return TaxLabelType.Rfc;\n }\n case CountryCode.Br: {\n switch (options.type) {\n case \"general\":\n return TaxLabelType.Vat;\n case \"specific\": {\n const taxLabelTypesByAddressType = {\n [AddressType.Residential]: TaxLabelType.Cpf,\n [AddressType.Business]: TaxLabelType.Cnpj,\n } satisfies Record<AddressType, TaxLabelType>;\n\n return taxLabelTypesByAddressType[options.addressType];\n }\n default:\n checkUnreachable(options);\n return TaxLabelType.Vat;\n }\n }\n default: {\n return TaxLabelType.Vat;\n }\n }\n};\n\n// #############################################################################\n// Assigned country code\n// #############################################################################\n/**\n * We can't 100% trust the order of countries as compiles can possible rearrange the values, so we have to sort the\n * values with country+province first when looking for the match we can stop at first occurrence\n * @see https://stackoverflow.com/a/5525820\n */\nconst userAssignedCountryRulesSorted = Object.entries(\n USER_ASSIGNED_COUNTRY_MAPPING_RULES,\n)\n .flatMap(([userAssignedCountry, rules]) =>\n rules.map((rule) => {\n return [userAssignedCountry as UserAssignedCountryCode, rule] as const;\n }),\n )\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n .sort(([_, a], [__, b]) => {\n return b.length - a.length;\n });\n\n/**\n * Util to get TAX country code for a given country + province combination\n */\nexport const getTaxCountry = ({\n country,\n province,\n}: {\n country: CountryCode;\n province: Nullish<string>;\n}): TaxCountryCode => {\n const match = userAssignedCountryRulesSorted.find(\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n ([_, [refCountry, refProvince]]) => {\n // Don't match - if countries don't match\n if (refCountry !== country) {\n return false;\n }\n\n // Match - if pattern doesn't include a province\n // TODO: this is debatable whether it should match as input province can be non-nullish\n if (refProvince == null) {\n return true;\n }\n\n return refProvince === province;\n },\n );\n\n const userAssignedCountry = match?.[0];\n\n // If there isn't a user assigned country that matches the result (most likely indicating that it doesnt require\n // unique lookup) then fallback to input country\n return userAssignedCountry ?? country;\n};\n\nexport const isTaxNumberOptional = (\n type: AddressType,\n taxCountryCode: TaxCountryCode,\n) => {\n if (!hasTaxNumberPatternValidation(type, taxCountryCode)) {\n return true;\n }\n\n const mapping = getValidationMappingByType(type);\n const { isOptional } = mapping[taxCountryCode];\n\n return isOptional;\n};\n\nexport const formatTaxNumber = (\n taxNumber: string,\n { country, addressType }: { country: CountryCode; addressType: AddressType },\n) => {\n // Tax number formatting only needed for Brazil in the foreseeable future.\n if (country !== CountryCode.Br) {\n return taxNumber;\n }\n\n switch (addressType) {\n case AddressType.Residential:\n return formatCpf(taxNumber);\n\n case AddressType.Business:\n return formatCnpj(taxNumber);\n\n default:\n checkUnreachable(addressType);\n return taxNumber;\n }\n};\n"],"names":["getValidationMappingByType","type","AddressType","Business","BUSINESS_COUNTRY_CODE_MAPPING","Residential","RESIDENTIAL_COUNTRY_CODE_MAPPING","assertUnreachable","hasTaxNumberPatternValidation","taxCountryCode","isValidTaxNumberPattern","taxId","mapping","validation","isOptional","isRegExp","test","getTaxLabelType","storeId","StoreId","Me","Za","Eu","Uk","Br","Tw","Jp","Cn","Ph","TaxLabelType","Vat","Ca","Us","Tax","In","Sg","Gst","Mx","Rfc","checkUnreachable","getTaxLabelTypeByCountry","country","options","CountryCode","Pr","Pw","Gu","As","Vi","Mp","Mh","Fm","taxLabelTypesByAddressType","Cpf","Cnpj","addressType","userAssignedCountryRulesSorted","Object","entries","USER_ASSIGNED_COUNTRY_MAPPING_RULES","flatMap","userAssignedCountry","rules","map","rule","sort","_","a","__","b","length","getTaxCountry","province","match","find","refCountry","refProvince","isTaxNumberOptional","formatTaxNumber","taxNumber","formatCpf","formatCnpj"],"mappings":";;;;;;;;AAiBO,MAAMA,0BAAA,GAA8BC,IAAA,IAAsB;E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A,CAA8BP,IAAA,EAAMQ,cAAc,CAAA,EAAG;IACxD,OAAO,IAAA;EACT;EAEA,MAAMG,OAAA,GAAUZ,2BAA2BC,IAAI,CAAA;EAC/C,MAAM;IAAEa;EAAA,CAAW,GAAIF,OAAA,CAAQH,cAAc,CAAA;EAE7C,OAAOK,UAAA;AACT,CAAA;AAEO,MAAMgE,kBAAkBA,CAC7BC,SAAA,EACA;EAAEtC,OAAA;EAASc;CAAY,KACpB;EAEH,IAAId,OAAA,KAAYE,YAAYnB,EAAA,EAAI;IAC9B,OAAOuD,SAAA;EACT;EAEA,QAAQxB,WAAA;IACN,KAAKrD,WAAA,CAAYG,WAAA;MACf,OAAO2E,UAAUD,SAAS,CAAA;IAE5B,KAAK7E,WAAA,CAAYC,QAAA;MACf,OAAO8E,WAAWF,SAAS,CAAA;IAE7B;MACExC,gBAAA,CAAiBgB,WAAW,CAAA;MAC5B,OAAOwB,SAAA;EAAA;AAEb,CAAA;"}
|
|
1
|
+
{"version":3,"file":"index.mjs","sources":["../../../src/utils/tax/index.ts"],"sourcesContent":["import isRegExp from \"lodash/isRegExp\";\n\nimport {\n BUSINESS_COUNTRY_CODE_MAPPING,\n RESIDENTIAL_COUNTRY_CODE_MAPPING,\n USER_ASSIGNED_COUNTRY_MAPPING_RULES,\n} from \"../../constants/address\";\nimport type { Nullish, UserAssignedCountryCode } from \"../../types\";\nimport { AddressType, CountryCode, StoreId } from \"../../types\";\nimport { assertUnreachable, checkUnreachable } from \"../errors\";\nimport { formatCnpj, formatCpf } from \"./brazil\";\nimport type { TaxCountryCode } from \"./types\";\nimport { TaxLabelType } from \"./types\";\n\n/**\n * Util to get the country code mapping based on address type in a type-safe way\n */\nexport const getValidationMappingByType = (type: AddressType) => {\n switch (type) {\n case AddressType.Business: {\n return BUSINESS_COUNTRY_CODE_MAPPING;\n }\n case AddressType.Residential: {\n return RESIDENTIAL_COUNTRY_CODE_MAPPING;\n }\n default: {\n assertUnreachable(type);\n }\n }\n};\n\n/**\n * Checks if given tax country has regex pattern for tax id's\n */\nexport const hasTaxNumberPatternValidation = (\n type: AddressType,\n taxCountryCode: TaxCountryCode,\n): taxCountryCode is keyof ReturnType<typeof getValidationMappingByType> =>\n taxCountryCode in getValidationMappingByType(type);\n\n/**\n * Checks if given tax id meets static pattern validation\n */\nexport const isValidTaxNumberPattern = (\n type: AddressType,\n taxCountryCode: TaxCountryCode,\n taxId: Nullish<string>,\n): boolean => {\n // If country doesn't have a tax number pattern then mark input as valid\n if (!hasTaxNumberPatternValidation(type, taxCountryCode)) {\n return true;\n }\n\n const mapping = getValidationMappingByType(type);\n const { validation, isOptional } = mapping[taxCountryCode];\n\n // Mark input as valid if the tax ID is missing but it's optional\n if (!taxId && isOptional) {\n return true;\n }\n\n // Check for empty tax id's in cases when they are required\n if (!taxId) {\n return false;\n }\n\n // Technically, this should already be covered by the \"hasTaxNumberPatternValidation\"\n // check above but just in case something goes really wrong, we are adding this\n // to avoid unexpected type errors during runtime\n if (!validation) {\n return true;\n }\n\n // Validate pattern\n return isRegExp(validation)\n ? validation.test(taxId)\n : validation({ taxId, taxCountryCode });\n};\n\n/**\n * Depending on store Tax is called different names, so this mapping determines which one to use\n * @see https://ui.slack.com/archives/D0191CBEB27/p1678723218358039?thread_ts=1678722532.763459&cid=D0191CBEB27\n */\nexport const getTaxLabelType = (storeId: StoreId) => {\n switch (storeId) {\n case StoreId.Me:\n case StoreId.Za:\n case StoreId.Eu:\n case StoreId.Uk:\n case StoreId.Br:\n case StoreId.Tw:\n case StoreId.Jp:\n case StoreId.Cn:\n case StoreId.Ph: {\n return TaxLabelType.Vat;\n }\n case StoreId.Ca:\n case StoreId.Us: {\n return TaxLabelType.Tax;\n }\n case StoreId.In:\n case StoreId.Sg: {\n return TaxLabelType.Gst;\n }\n case StoreId.Mx: {\n return TaxLabelType.Rfc;\n }\n default: {\n checkUnreachable(storeId);\n return TaxLabelType.Tax;\n }\n }\n};\n\nexport const getTaxLabelTypeByCountry = (\n country: CountryCode,\n options:\n | {\n type: \"general\";\n }\n | {\n type: \"specific\";\n addressType: AddressType;\n },\n) => {\n switch (country) {\n case CountryCode.Ca:\n case CountryCode.Us:\n case CountryCode.Pr:\n case CountryCode.Pw:\n case CountryCode.Gu:\n case CountryCode.As:\n case CountryCode.Vi:\n case CountryCode.Mp:\n case CountryCode.Mh:\n case CountryCode.Fm: {\n return TaxLabelType.Tax;\n }\n case CountryCode.In:\n case CountryCode.Sg: {\n return TaxLabelType.Gst;\n }\n case CountryCode.Mx: {\n return TaxLabelType.Rfc;\n }\n case CountryCode.Br: {\n switch (options.type) {\n case \"general\":\n return TaxLabelType.Vat;\n case \"specific\": {\n const taxLabelTypesByAddressType = {\n [AddressType.Residential]: TaxLabelType.Cpf,\n [AddressType.Business]: TaxLabelType.Cnpj,\n } satisfies Record<AddressType, TaxLabelType>;\n\n return taxLabelTypesByAddressType[options.addressType];\n }\n default:\n checkUnreachable(options);\n return TaxLabelType.Vat;\n }\n }\n case CountryCode.Ph: {\n switch (options.type) {\n case \"general\":\n return TaxLabelType.Vat;\n case \"specific\": {\n const taxLabelTypesByAddressType = {\n [AddressType.Residential]: TaxLabelType.Psn,\n [AddressType.Business]: TaxLabelType.Tin,\n } satisfies Record<AddressType, TaxLabelType>;\n\n return taxLabelTypesByAddressType[options.addressType];\n }\n default:\n checkUnreachable(options);\n return TaxLabelType.Vat;\n }\n }\n default: {\n return TaxLabelType.Vat;\n }\n }\n};\n\n// #############################################################################\n// Assigned country code\n// #############################################################################\n/**\n * We can't 100% trust the order of countries as compiles can possible rearrange the values, so we have to sort the\n * values with country+province first when looking for the match we can stop at first occurrence\n * @see https://stackoverflow.com/a/5525820\n */\nconst userAssignedCountryRulesSorted = Object.entries(\n USER_ASSIGNED_COUNTRY_MAPPING_RULES,\n)\n .flatMap(([userAssignedCountry, rules]) =>\n rules.map((rule) => {\n return [userAssignedCountry as UserAssignedCountryCode, rule] as const;\n }),\n )\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n .sort(([_, a], [__, b]) => {\n return b.length - a.length;\n });\n\n/**\n * Util to get TAX country code for a given country + province combination\n */\nexport const getTaxCountry = ({\n country,\n province,\n}: {\n country: CountryCode;\n province: Nullish<string>;\n}): TaxCountryCode => {\n const match = userAssignedCountryRulesSorted.find(\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n ([_, [refCountry, refProvince]]) => {\n // Don't match - if countries don't match\n if (refCountry !== country) {\n return false;\n }\n\n // Match - if pattern doesn't include a province\n // TODO: this is debatable whether it should match as input province can be non-nullish\n if (refProvince == null) {\n return true;\n }\n\n return refProvince === province;\n },\n );\n\n const userAssignedCountry = match?.[0];\n\n // If there isn't a user assigned country that matches the result (most likely indicating that it doesnt require\n // unique lookup) then fallback to input country\n return userAssignedCountry ?? country;\n};\n\nexport const isTaxNumberOptional = (\n type: AddressType,\n taxCountryCode: TaxCountryCode,\n) => {\n if (!hasTaxNumberPatternValidation(type, taxCountryCode)) {\n return true;\n }\n\n const mapping = getValidationMappingByType(type);\n const { isOptional } = mapping[taxCountryCode];\n\n return isOptional;\n};\n\nexport const formatTaxNumber = (\n taxNumber: string,\n { country, addressType }: { country: CountryCode; addressType: AddressType },\n) => {\n // Tax number formatting only needed for Brazil in the foreseeable future.\n if (country !== CountryCode.Br) {\n return taxNumber;\n }\n\n switch (addressType) {\n case AddressType.Residential:\n return formatCpf(taxNumber);\n\n case AddressType.Business:\n return formatCnpj(taxNumber);\n\n default:\n checkUnreachable(addressType);\n return taxNumber;\n }\n};\n"],"names":["getValidationMappingByType","type","AddressType","Business","BUSINESS_COUNTRY_CODE_MAPPING","Residential","RESIDENTIAL_COUNTRY_CODE_MAPPING","assertUnreachable","hasTaxNumberPatternValidation","taxCountryCode","isValidTaxNumberPattern","taxId","mapping","validation","isOptional","isRegExp","test","getTaxLabelType","storeId","StoreId","Me","Za","Eu","Uk","Br","Tw","Jp","Cn","Ph","TaxLabelType","Vat","Ca","Us","Tax","In","Sg","Gst","Mx","Rfc","checkUnreachable","getTaxLabelTypeByCountry","country","options","CountryCode","Pr","Pw","Gu","As","Vi","Mp","Mh","Fm","taxLabelTypesByAddressType","Cpf","Cnpj","addressType","Psn","Tin","userAssignedCountryRulesSorted","Object","entries","USER_ASSIGNED_COUNTRY_MAPPING_RULES","flatMap","userAssignedCountry","rules","map","rule","sort","_","a","__","b","length","getTaxCountry","province","match","find","refCountry","refProvince","isTaxNumberOptional","formatTaxNumber","taxNumber","formatCpf","formatCnpj"],"mappings":";;;;;;;;AAiBO,MAAMA,0BAAA,GAA8BC,IAAA,IAAsB;EAC/D,QAAQA,IAAA;IACN,KAAKC,YAAYC,QAAA;MAAU;QACzB,OAAOC,6BAAA;MACT;IACA,KAAKF,YAAYG,WAAA;MAAa;QAC5B,OAAOC,gCAAA;MACT;IACA;MAAS;QACPC,iBAAA,CAAkBN,IAAI,CAAA;MACxB;EAAA;AAEJ,CAAA;AAKO,MAAMO,gCAAgCA,CAC3CP,IAAA,EACAQ,cAAA,KAEAA,cAAA,IAAkBT,2BAA2BC,IAAI,CAAA;AAK5C,MAAMS,uBAAA,GAA0BA,CACrCT,IAAA,EACAQ,cAAA,EACAE,KAAA,KACY;EAEZ,IAAI,CAACH,6BAAA,CAA8BP,IAAA,EAAMQ,cAAc,CAAA,EAAG;IACxD,OAAO,IAAA;EACT;EAEA,MAAMG,OAAA,GAAUZ,2BAA2BC,IAAI,CAAA;EAC/C,MAAM;IAAEY,UAAA;IAAYC;GAAW,GAAIF,QAAQH,cAAc,CAAA;EAGzD,IAAI,CAACE,SAASG,UAAA,EAAY;IACxB,OAAO,IAAA;EACT;EAGA,IAAI,CAACH,KAAA,EAAO;IACV,OAAO,KAAA;EACT;EAKA,IAAI,CAACE,UAAA,EAAY;IACf,OAAO,IAAA;EACT;EAGA,OAAOE,QAAA,CAASF,UAAU,CAAA,GACtBA,UAAA,CAAWG,IAAA,CAAKL,KAAK,CAAA,GACrBE,UAAA,CAAW;IAAEF,KAAA;IAAOF;GAAgB,CAAA;AAC1C,CAAA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AK/E,WAAA,CAAYC,QAAA;MACf,OAAOgF,WAAWF,SAAS,CAAA;IAE7B;MACE1C,gBAAA,CAAiBgB,WAAW,CAAA;MAC5B,OAAO0B,SAAA;EAAA;AAEb,CAAA;"}
|
package/lib/utils/tax/types.d.ts
CHANGED
package/lib/utils/tax/types.js
CHANGED
|
@@ -8,6 +8,8 @@ var TaxLabelType = /* @__PURE__ */(TaxLabelType2 => {
|
|
|
8
8
|
TaxLabelType2["Cpf"] = "cpf";
|
|
9
9
|
TaxLabelType2["Cnpj"] = "cnpj";
|
|
10
10
|
TaxLabelType2["Rfc"] = "rfc";
|
|
11
|
+
TaxLabelType2["Psn"] = "psn";
|
|
12
|
+
TaxLabelType2["Tin"] = "tin";
|
|
11
13
|
return TaxLabelType2;
|
|
12
14
|
})(TaxLabelType || {});
|
|
13
15
|
exports.TaxLabelType = TaxLabelType;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sources":["../../../src/utils/tax/types.ts"],"sourcesContent":["import type { CountryCode, UserAssignedCountryCode } from \"../../types\";\n\nexport type TaxCountryCode = CountryCode | UserAssignedCountryCode;\n\nexport enum TaxLabelType {\n Tax = \"tax\",\n Vat = \"vat\",\n Gst = \"gst\",\n Cpf = \"cpf\",\n Cnpj = \"cnpj\",\n Rfc = \"rfc\",\n}\n\n/**\n * Ability to create custom logic (not regex based for taxId) validation\n */\nexport type CountryTaxIdValidationFn = (options: {\n taxCountryCode: TaxCountryCode;\n taxId: string;\n}) => boolean;\n\nexport type CountryTaxValidationMapping = Partial<\n Record<\n TaxCountryCode,\n { validation: CountryTaxIdValidationFn | RegExp; isOptional?: boolean }\n >\n>;\n"],"names":["TaxLabelType"],"mappings":";;;AAIO,IAAKA,YAAA,GAAA,gBAAAA,aAAAA,IAAL;EACLA,cAAA,KAAA,CAAA,GAAM,KAAA;EACNA,cAAA,KAAA,CAAA,GAAM,KAAA;EACNA,cAAA,KAAA,CAAA,GAAM,KAAA;EACNA,cAAA,KAAA,CAAA,GAAM,KAAA;EACNA,cAAA,MAAA,CAAA,GAAO,MAAA;EACPA,cAAA,KAAA,CAAA,GAAM,KAAA;
|
|
1
|
+
{"version":3,"file":"types.js","sources":["../../../src/utils/tax/types.ts"],"sourcesContent":["import type { CountryCode, UserAssignedCountryCode } from \"../../types\";\n\nexport type TaxCountryCode = CountryCode | UserAssignedCountryCode;\n\nexport enum TaxLabelType {\n Tax = \"tax\",\n Vat = \"vat\",\n Gst = \"gst\",\n Cpf = \"cpf\",\n Cnpj = \"cnpj\",\n Rfc = \"rfc\",\n Psn = \"psn\",\n Tin = \"tin\",\n}\n\n/**\n * Ability to create custom logic (not regex based for taxId) validation\n */\nexport type CountryTaxIdValidationFn = (options: {\n taxCountryCode: TaxCountryCode;\n taxId: string;\n}) => boolean;\n\nexport type CountryTaxValidationMapping = Partial<\n Record<\n TaxCountryCode,\n { validation: CountryTaxIdValidationFn | RegExp; isOptional?: boolean }\n >\n>;\n"],"names":["TaxLabelType"],"mappings":";;;AAIO,IAAKA,YAAA,GAAA,gBAAAA,aAAAA,IAAL;EACLA,cAAA,KAAA,CAAA,GAAM,KAAA;EACNA,cAAA,KAAA,CAAA,GAAM,KAAA;EACNA,cAAA,KAAA,CAAA,GAAM,KAAA;EACNA,cAAA,KAAA,CAAA,GAAM,KAAA;EACNA,cAAA,MAAA,CAAA,GAAO,MAAA;EACPA,cAAA,KAAA,CAAA,GAAM,KAAA;EACNA,cAAA,KAAA,CAAA,GAAM,KAAA;EACNA,cAAA,KAAA,CAAA,GAAM,KAAA;EARI,OAAAA,aAAAA;AAAA,CAAA,EAAAA,YAAA,IAAA,CAAA,CAAA,CAAA;"}
|
package/lib/utils/tax/types.mjs
CHANGED
|
@@ -7,6 +7,8 @@ var TaxLabelType = /* @__PURE__ */(TaxLabelType2 => {
|
|
|
7
7
|
TaxLabelType2["Cpf"] = "cpf";
|
|
8
8
|
TaxLabelType2["Cnpj"] = "cnpj";
|
|
9
9
|
TaxLabelType2["Rfc"] = "rfc";
|
|
10
|
+
TaxLabelType2["Psn"] = "psn";
|
|
11
|
+
TaxLabelType2["Tin"] = "tin";
|
|
10
12
|
return TaxLabelType2;
|
|
11
13
|
})(TaxLabelType || {});
|
|
12
14
|
export { TaxLabelType };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.mjs","sources":["../../../src/utils/tax/types.ts"],"sourcesContent":["import type { CountryCode, UserAssignedCountryCode } from \"../../types\";\n\nexport type TaxCountryCode = CountryCode | UserAssignedCountryCode;\n\nexport enum TaxLabelType {\n Tax = \"tax\",\n Vat = \"vat\",\n Gst = \"gst\",\n Cpf = \"cpf\",\n Cnpj = \"cnpj\",\n Rfc = \"rfc\",\n}\n\n/**\n * Ability to create custom logic (not regex based for taxId) validation\n */\nexport type CountryTaxIdValidationFn = (options: {\n taxCountryCode: TaxCountryCode;\n taxId: string;\n}) => boolean;\n\nexport type CountryTaxValidationMapping = Partial<\n Record<\n TaxCountryCode,\n { validation: CountryTaxIdValidationFn | RegExp; isOptional?: boolean }\n >\n>;\n"],"names":["TaxLabelType"],"mappings":";;AAIO,IAAKA,YAAA,GAAA,gBAAAA,aAAAA,IAAL;EACLA,cAAA,KAAA,CAAA,GAAM,KAAA;EACNA,cAAA,KAAA,CAAA,GAAM,KAAA;EACNA,cAAA,KAAA,CAAA,GAAM,KAAA;EACNA,cAAA,KAAA,CAAA,GAAM,KAAA;EACNA,cAAA,MAAA,CAAA,GAAO,MAAA;EACPA,cAAA,KAAA,CAAA,GAAM,KAAA;
|
|
1
|
+
{"version":3,"file":"types.mjs","sources":["../../../src/utils/tax/types.ts"],"sourcesContent":["import type { CountryCode, UserAssignedCountryCode } from \"../../types\";\n\nexport type TaxCountryCode = CountryCode | UserAssignedCountryCode;\n\nexport enum TaxLabelType {\n Tax = \"tax\",\n Vat = \"vat\",\n Gst = \"gst\",\n Cpf = \"cpf\",\n Cnpj = \"cnpj\",\n Rfc = \"rfc\",\n Psn = \"psn\",\n Tin = \"tin\",\n}\n\n/**\n * Ability to create custom logic (not regex based for taxId) validation\n */\nexport type CountryTaxIdValidationFn = (options: {\n taxCountryCode: TaxCountryCode;\n taxId: string;\n}) => boolean;\n\nexport type CountryTaxValidationMapping = Partial<\n Record<\n TaxCountryCode,\n { validation: CountryTaxIdValidationFn | RegExp; isOptional?: boolean }\n >\n>;\n"],"names":["TaxLabelType"],"mappings":";;AAIO,IAAKA,YAAA,GAAA,gBAAAA,aAAAA,IAAL;EACLA,cAAA,KAAA,CAAA,GAAM,KAAA;EACNA,cAAA,KAAA,CAAA,GAAM,KAAA;EACNA,cAAA,KAAA,CAAA,GAAM,KAAA;EACNA,cAAA,KAAA,CAAA,GAAM,KAAA;EACNA,cAAA,MAAA,CAAA,GAAO,MAAA;EACPA,cAAA,KAAA,CAAA,GAAM,KAAA;EACNA,cAAA,KAAA,CAAA,GAAM,KAAA;EACNA,cAAA,KAAA,CAAA,GAAM,KAAA;EARI,OAAAA,aAAAA;AAAA,CAAA,EAAAA,YAAA,IAAA,CAAA,CAAA,CAAA;"}
|
package/lib/utils/url.d.ts
CHANGED
|
@@ -24,9 +24,15 @@ export type CreateUrlBuilderOptions = {
|
|
|
24
24
|
* null|undefined values will be ignored
|
|
25
25
|
* rest of available types the will be stringified via `.toString()`
|
|
26
26
|
* @example
|
|
27
|
-
* {searchParams: { hello: 'world', willBeRemoved: undefined}}
|
|
27
|
+
* { searchParams: { hello: 'world', willBeRemoved: undefined } }
|
|
28
28
|
*/
|
|
29
29
|
searchParams?: SearchParams;
|
|
30
|
+
/**
|
|
31
|
+
* Optional hash to add/override to the URL (the part after the `#` in a url)
|
|
32
|
+
* @example
|
|
33
|
+
* { hash: 'section-2' } => 'https://example.com/pathname#section-2'
|
|
34
|
+
*/
|
|
35
|
+
hash?: string;
|
|
30
36
|
/**
|
|
31
37
|
* Preserve pathname of base which helps in some special scenario's for storefront url's as specific "base" store
|
|
32
38
|
* url's include `/${storeId}` as first part of the pathname
|
|
@@ -42,7 +48,7 @@ export type CreateUrlBuilderOptions = {
|
|
|
42
48
|
/**
|
|
43
49
|
* Utility to create url's via a base URL
|
|
44
50
|
*/
|
|
45
|
-
export declare const createUrlBuilder: (base: string) => ({ pathname, searchParams, overrideBasePathname, returnType, }: CreateUrlBuilderOptions) => string;
|
|
51
|
+
export declare const createUrlBuilder: (base: string) => ({ pathname, searchParams, overrideBasePathname, hash, returnType, }: CreateUrlBuilderOptions) => string;
|
|
46
52
|
/**
|
|
47
53
|
* Converts javascript object to URLSearchParams instance
|
|
48
54
|
*
|
package/lib/utils/url.js
CHANGED
|
@@ -32,6 +32,7 @@ const createUrlBuilder = base => ({
|
|
|
32
32
|
pathname,
|
|
33
33
|
searchParams,
|
|
34
34
|
overrideBasePathname,
|
|
35
|
+
hash,
|
|
35
36
|
returnType
|
|
36
37
|
}) => {
|
|
37
38
|
const url = new URL(base);
|
|
@@ -41,6 +42,9 @@ const createUrlBuilder = base => ({
|
|
|
41
42
|
if (searchParams) {
|
|
42
43
|
url.search = objectToSearchParams(searchParams, url.searchParams).toString();
|
|
43
44
|
}
|
|
45
|
+
if (hash) {
|
|
46
|
+
url.hash = hash;
|
|
47
|
+
}
|
|
44
48
|
const urlString = url.toString();
|
|
45
49
|
return returnType === "relative" ? urlString.replace(url.origin, "") : urlString;
|
|
46
50
|
};
|
package/lib/utils/url.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"url.js","sources":["../../src/utils/url.ts"],"sourcesContent":["import _slugify from \"slugify\";\nimport urlJoin from \"url-join\";\n\n// #############################################################################\n// Types\n// #############################################################################\nexport type SearchParamScalar = string | number | boolean | null | undefined;\nexport type SearchParams = Record<\n string,\n SearchParamScalar | Array<SearchParamScalar>\n>;\n\n// #############################################################################\n// Functions\n// #############################################################################\n\n/**\n * Slugify re-export but set if common options\n */\nexport const slugify = (str: string): string =>\n _slugify(str, {\n replacement: \"-\",\n lower: true,\n strict: true,\n });\n\n/**\n * Helper function for creating url pathnames with safely encoded variables in them\n *\n * @example\n * e`/login` // \"/login\"\n * e`/settings/store/${\"123-456-789\"}/${\" string with titles and title with spaces \"}` // \"/settings/store/123-456-789/%20string%20with%20titles%20and%20title%20with%20spaces%20\"\n */\nexport function e(\n strings: TemplateStringsArray,\n ...placeholders: (string | number | boolean)[]\n): string {\n let result = \"\";\n for (let i = 0, l = strings.length; i < l; i++) {\n result += strings[i];\n if (i < placeholders.length) {\n const placeholder = placeholders[i];\n if (placeholder != null) {\n // TODO: maybe also convert to lowercase?\n result += encodeURIComponent(placeholder);\n }\n }\n }\n return result;\n}\n\nexport type CreateUrlBuilderOptions = {\n /**\n * ! Warning: Make sure to escape the pathname yourself before using `e` from `@ubnt/store.core\n * @example\n * import { e } from \"@ubnt/store.core/lib/utils/url\";\n * const mySafePathname = e`${storeId}/${language}`;\n */\n pathname?: string;\n /**\n * null|undefined values will be ignored\n * rest of available types the will be stringified via `.toString()`\n * @example\n * {searchParams: { hello: 'world', willBeRemoved: undefined}}\n */\n searchParams?: SearchParams;\n /**\n * Preserve pathname of base which helps in some special scenario's for storefront url's as specific \"base\" store\n * url's include `/${storeId}` as first part of the pathname\n * @default false\n */\n overrideBasePathname?: boolean;\n /**\n * Whether to return an absolute url (default behaviour) or relative (the host) url\n * @default 'absolute'\n */\n returnType?: \"absolute\" | \"relative\";\n};\n\n/**\n * Utility to create url's via a base URL\n */\nexport const createUrlBuilder =\n (base: string) =>\n ({\n pathname,\n searchParams,\n overrideBasePathname,\n returnType,\n }: CreateUrlBuilderOptions): string => {\n const url = new URL(base);\n\n if (pathname) {\n url.pathname = overrideBasePathname\n ? pathname\n : urlJoin(url.pathname, pathname);\n }\n\n if (searchParams) {\n // Have to set `url.search` instead of `url.searchParams` as it's a read only property\n url.search = objectToSearchParams(\n searchParams,\n url.searchParams,\n ).toString();\n }\n\n const urlString = url.toString();\n return returnType === \"relative\"\n ? urlString.replace(url.origin, \"\")\n : urlString;\n };\n\n/**\n * Converts javascript object to URLSearchParams instance\n *\n * null|undefined values will be ignored\n * rest of available types the will be stringified via `.toString()`\n * @example\n * {searchParams: { hello: 'world', willBeRemoved: undefined}}\n */\nexport const objectToSearchParams = (\n searchParams: SearchParams,\n /**\n * Optionally pass in an existing instance to append values\n */\n urlSearchParamsInstance: URLSearchParams = new URLSearchParams(),\n): URLSearchParams => {\n for (const [key, value] of Object.entries(searchParams)) {\n /**\n * Although we don't allow \"empty\" values, in runtime we had an issue if passing it, which failed this function,\n * seemed to be to dramatic so add actual runtime check if value is not null|undefined\n */\n if (value != null) {\n if (Array.isArray(value)) {\n value.forEach((v) => {\n if (v != null) {\n urlSearchParamsInstance.append(key, v.toString());\n }\n });\n } else {\n urlSearchParamsInstance.append(key, value.toString());\n }\n }\n }\n\n return urlSearchParamsInstance;\n};\n\n/**\n * Converts URLSearchParams instance to javascript object\n *\n * @example\n * const urlString =\n * \"https://example.com/?name=John&age=30&hobby=reading&hobby=sports\";\n * const url = new URL(urlString);\n * const params = url.searchParams;\n *\n * urlSearchParamsToObject(params) // { name: \"John\", age: \"30\", hobby: [\"reading\", \"sports\"] }\n */\nexport const searchParamsToObject = (\n urlSearchParams: URLSearchParams,\n): SearchParams => {\n const result: { [key: string]: string | string[] } = {};\n\n urlSearchParams.forEach((value, key) => {\n if (result[key] !== undefined) {\n const storedValue = result[key];\n if (Array.isArray(storedValue)) {\n // Add new value to array\n storedValue.push(value);\n } else {\n // Convert to array\n result[key] = [result[key] as string, value];\n }\n } else {\n // Add initial value\n result[key] = value;\n }\n });\n\n return result;\n};\n\n/**\n * Utility for the root middleware to easily parse parts of a pathname (ignores search params)\n * @see https://nodejs.org/api/url.html#urlpathname\n */\nexport const pathnameToParts = (dirtyPathname: string): string[] => {\n const pathname = getPurePathname(dirtyPathname);\n\n // Handle \"empty\" pathname scenarios\n if (!pathname || pathname === \"/\") {\n return [];\n }\n\n const pathParts = pathname.split(\"/\");\n\n /**\n * Handle the case if given pathname starts with slash because '/test/test'.split('/') => ['', 'test', 'test']\n * It's the normal behaviour for pathname to start with a slash\n */\n if (pathname.startsWith(\"/\")) {\n pathParts.shift();\n }\n\n return pathParts;\n};\n\n/**\n * Safely (with proper encodeURIComponent) creates a URL pathname from parts\n * @example\n * partsToPathName(['a', 'b']) => '/a/b'\n * partsToPathName(['a', 'b/c']) => '/a/b%2Fc'\n */\nexport const partsToPathname = (parts: string[]): string => {\n if (!parts || parts.length === 0) {\n return \"/\";\n }\n return `/${parts.filter(Boolean).map(encodeURIComponent).join(\"/\")}`;\n};\n\n/**\n * Removes query string from a pathname and returns a URL class pathname\n * @example\n * getPurePathname('/a/b?c=d') => '/a/b'\n */\nexport const getPurePathname = (dirtyPathname: string): string => {\n return new URL(dirtyPathname, \"https://www.example.com\").pathname;\n};\n\n// Re-export so we don't require the whole url-join package\nexport { default as urlJoin } from \"url-join\";\n"],"names":["slugify","str","_slugify","replacement","lower","strict","e","strings","placeholders","result","i","l","length","placeholder","encodeURIComponent","createUrlBuilder","base","pathname","searchParams","overrideBasePathname","returnType","url","URL","urlJoin","search","objectToSearchParams","toString","urlString","replace","origin","urlSearchParamsInstance","URLSearchParams","key","value","Object","entries","Array","isArray","forEach","v","append","searchParamsToObject","urlSearchParams","storedValue","push","pathnameToParts","dirtyPathname","getPurePathname","pathParts","split","startsWith","shift","partsToPathname","parts","filter","Boolean","map","join"],"mappings":";;;;;;;;;;;;AAmBO,MAAMA,OAAA,GAAWC,GAAA,IACtBC,iBAAAA,CAAAA,SAAAA,CAAA,CAASD,GAAA,EAAK;EACZE,WAAA,EAAa,GAAA;EACbC,KAAA,EAAO,IAAA;EACPC,MAAA,EAAQ;AACV,CAAC,CAAA;AASI,SAASC,CAAAA,CACdC,YACGC,YAAA,EACK;EACR,IAAIC,MAAA,GAAS,EAAA;EACb,KAAA,IAASC,IAAI,CAAA,EAAGC,CAAA,GAAIJ,QAAQK,MAAA,EAAQF,CAAA,GAAIC,GAAGD,CAAA,EAAA,EAAK;IAC9CD,MAAA,IAAUF,QAAQG,CAAC,CAAA;IACnB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,CAAMzC,MAAA,KAAW,CAAA,EAAG;IAChC,OAAO,GAAA;EACT;EACA,OAAO,IAAIyC,KAAA,CAAMC,MAAA,CAAOC,OAAO,CAAA,CAAEC,IAAI1C,kBAAkB,CAAA,CAAE2C,IAAA,CAAK,GAAG,CAAC,EAAA;AACpE,CAAA;AAOO,MAAMV,eAAA,GAAmBD,aAAA,IAAkC;EAChE,OAAO,IAAIxB,GAAA,CAAIwB,aAAA,EAAe,yBAAyB,CAAA,CAAE7B,QAAA;AAC3D,CAAA;;;;;;;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"url.js","sources":["../../src/utils/url.ts"],"sourcesContent":["import _slugify from \"slugify\";\nimport urlJoin from \"url-join\";\n\n// #############################################################################\n// Types\n// #############################################################################\nexport type SearchParamScalar = string | number | boolean | null | undefined;\nexport type SearchParams = Record<\n string,\n SearchParamScalar | Array<SearchParamScalar>\n>;\n\n// #############################################################################\n// Functions\n// #############################################################################\n\n/**\n * Slugify re-export but set if common options\n */\nexport const slugify = (str: string): string =>\n _slugify(str, {\n replacement: \"-\",\n lower: true,\n strict: true,\n });\n\n/**\n * Helper function for creating url pathnames with safely encoded variables in them\n *\n * @example\n * e`/login` // \"/login\"\n * e`/settings/store/${\"123-456-789\"}/${\" string with titles and title with spaces \"}` // \"/settings/store/123-456-789/%20string%20with%20titles%20and%20title%20with%20spaces%20\"\n */\nexport function e(\n strings: TemplateStringsArray,\n ...placeholders: (string | number | boolean)[]\n): string {\n let result = \"\";\n for (let i = 0, l = strings.length; i < l; i++) {\n result += strings[i];\n if (i < placeholders.length) {\n const placeholder = placeholders[i];\n if (placeholder != null) {\n // TODO: maybe also convert to lowercase?\n result += encodeURIComponent(placeholder);\n }\n }\n }\n return result;\n}\n\nexport type CreateUrlBuilderOptions = {\n /**\n * ! Warning: Make sure to escape the pathname yourself before using `e` from `@ubnt/store.core\n * @example\n * import { e } from \"@ubnt/store.core/lib/utils/url\";\n * const mySafePathname = e`${storeId}/${language}`;\n */\n pathname?: string;\n /**\n * null|undefined values will be ignored\n * rest of available types the will be stringified via `.toString()`\n * @example\n * { searchParams: { hello: 'world', willBeRemoved: undefined } }\n */\n searchParams?: SearchParams;\n /**\n * Optional hash to add/override to the URL (the part after the `#` in a url)\n * @example\n * { hash: 'section-2' } => 'https://example.com/pathname#section-2'\n */\n hash?: string;\n /**\n * Preserve pathname of base which helps in some special scenario's for storefront url's as specific \"base\" store\n * url's include `/${storeId}` as first part of the pathname\n * @default false\n */\n overrideBasePathname?: boolean;\n /**\n * Whether to return an absolute url (default behaviour) or relative (the host) url\n * @default 'absolute'\n */\n returnType?: \"absolute\" | \"relative\";\n};\n\n/**\n * Utility to create url's via a base URL\n */\nexport const createUrlBuilder =\n (base: string) =>\n ({\n pathname,\n searchParams,\n overrideBasePathname,\n hash,\n returnType,\n }: CreateUrlBuilderOptions): string => {\n const url = new URL(base);\n\n if (pathname) {\n url.pathname = overrideBasePathname\n ? pathname\n : urlJoin(url.pathname, pathname);\n }\n\n if (searchParams) {\n // Have to set `url.search` instead of `url.searchParams` as it's a read only property\n url.search = objectToSearchParams(\n searchParams,\n url.searchParams,\n ).toString();\n }\n\n // Override hash if given\n if (hash) {\n url.hash = hash;\n }\n\n const urlString = url.toString();\n return returnType === \"relative\"\n ? urlString.replace(url.origin, \"\")\n : urlString;\n };\n\n/**\n * Converts javascript object to URLSearchParams instance\n *\n * null|undefined values will be ignored\n * rest of available types the will be stringified via `.toString()`\n * @example\n * {searchParams: { hello: 'world', willBeRemoved: undefined}}\n */\nexport const objectToSearchParams = (\n searchParams: SearchParams,\n /**\n * Optionally pass in an existing instance to append values\n */\n urlSearchParamsInstance: URLSearchParams = new URLSearchParams(),\n): URLSearchParams => {\n for (const [key, value] of Object.entries(searchParams)) {\n /**\n * Although we don't allow \"empty\" values, in runtime we had an issue if passing it, which failed this function,\n * seemed to be to dramatic so add actual runtime check if value is not null|undefined\n */\n if (value != null) {\n if (Array.isArray(value)) {\n value.forEach((v) => {\n if (v != null) {\n urlSearchParamsInstance.append(key, v.toString());\n }\n });\n } else {\n urlSearchParamsInstance.append(key, value.toString());\n }\n }\n }\n\n return urlSearchParamsInstance;\n};\n\n/**\n * Converts URLSearchParams instance to javascript object\n *\n * @example\n * const urlString =\n * \"https://example.com/?name=John&age=30&hobby=reading&hobby=sports\";\n * const url = new URL(urlString);\n * const params = url.searchParams;\n *\n * urlSearchParamsToObject(params) // { name: \"John\", age: \"30\", hobby: [\"reading\", \"sports\"] }\n */\nexport const searchParamsToObject = (\n urlSearchParams: URLSearchParams,\n): SearchParams => {\n const result: { [key: string]: string | string[] } = {};\n\n urlSearchParams.forEach((value, key) => {\n if (result[key] !== undefined) {\n const storedValue = result[key];\n if (Array.isArray(storedValue)) {\n // Add new value to array\n storedValue.push(value);\n } else {\n // Convert to array\n result[key] = [result[key] as string, value];\n }\n } else {\n // Add initial value\n result[key] = value;\n }\n });\n\n return result;\n};\n\n/**\n * Utility for the root middleware to easily parse parts of a pathname (ignores search params)\n * @see https://nodejs.org/api/url.html#urlpathname\n */\nexport const pathnameToParts = (dirtyPathname: string): string[] => {\n const pathname = getPurePathname(dirtyPathname);\n\n // Handle \"empty\" pathname scenarios\n if (!pathname || pathname === \"/\") {\n return [];\n }\n\n const pathParts = pathname.split(\"/\");\n\n /**\n * Handle the case if given pathname starts with slash because '/test/test'.split('/') => ['', 'test', 'test']\n * It's the normal behaviour for pathname to start with a slash\n */\n if (pathname.startsWith(\"/\")) {\n pathParts.shift();\n }\n\n return pathParts;\n};\n\n/**\n * Safely (with proper encodeURIComponent) creates a URL pathname from parts\n * @example\n * partsToPathName(['a', 'b']) => '/a/b'\n * partsToPathName(['a', 'b/c']) => '/a/b%2Fc'\n */\nexport const partsToPathname = (parts: string[]): string => {\n if (!parts || parts.length === 0) {\n return \"/\";\n }\n return `/${parts.filter(Boolean).map(encodeURIComponent).join(\"/\")}`;\n};\n\n/**\n * Removes query string from a pathname and returns a URL class pathname\n * @example\n * getPurePathname('/a/b?c=d') => '/a/b'\n */\nexport const getPurePathname = (dirtyPathname: string): string => {\n return new URL(dirtyPathname, \"https://www.example.com\").pathname;\n};\n\n// Re-export so we don't require the whole url-join package\nexport { default as urlJoin } from \"url-join\";\n"],"names":["slugify","str","_slugify","replacement","lower","strict","e","strings","placeholders","result","i","l","length","placeholder","encodeURIComponent","createUrlBuilder","base","pathname","searchParams","overrideBasePathname","hash","returnType","url","URL","urlJoin","search","objectToSearchParams","toString","urlString","replace","origin","urlSearchParamsInstance","URLSearchParams","key","value","Object","entries","Array","isArray","forEach","v","append","searchParamsToObject","urlSearchParams","storedValue","push","pathnameToParts","dirtyPathname","getPurePathname","pathParts","split","startsWith","shift","partsToPathname","parts","filter","Boolean","map","join"],"mappings":";;;;;;;;;;;;AAmBO,MAAMA,OAAA,GAAWC,GAAA,IACtBC,iBAAAA,CAAAA,SAAAA,CAAA,CAASD,GAAA,EAAK;EACZE,WAAA,EAAa,GAAA;EACbC,KAAA,EAAO,IAAA;EACPC,MAAA,EAAQ;AACV,CAAC,CAAA;AASI,SAASC,CAAAA,CACdC,YACGC,YAAA,EACK;EACR,IAAIC,MAAA,GAAS,EAAA;EACb,KAAA,IAASC,IAAI,CAAA,EAAGC,CAAA,GAAIJ,QAAQK,MAAA,EAAQF,CAAA,GAAIC,GAAGD,CAAA,EAAA,EAAK;IAC9CD,MAAA,IAAUF,QAAQG,CAAC,CAAA;IACnB,IAAIA,CAAA,GAAIF,aAAaI,MAAA,EAAQ;MAC3B,MAAMC,WAAA,GAAcL,aAAaE,CAAC,CAAA;MAClC,IAAIG,eAAe,IAAA,EAAM;QAEvBJ,MAAA,IAAUK,mBAAmBD,WAAW,CAAA;MAC1C;IACF;EACF;EACA,OAAOJ,MAAA;AACT;AAuCO,MAAMM,gBAAA,GACVC,IAAA,IACD,CAAC;EACCC,QAAA;EACAC,YAAA;EACAC,oBAAA;EACAC,IAAA;EACAC;AACF,CAAA,KAAuC;EACrC,MAAMC,GAAA,GAAM,IAAIC,GAAA,CAAIP,IA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,CAAA,CAAE9B,QAAA;AAC3D,CAAA;;;;;;;;;;;;;;"}
|
package/lib/utils/url.mjs
CHANGED
|
@@ -24,6 +24,7 @@ const createUrlBuilder = base => ({
|
|
|
24
24
|
pathname,
|
|
25
25
|
searchParams,
|
|
26
26
|
overrideBasePathname,
|
|
27
|
+
hash,
|
|
27
28
|
returnType
|
|
28
29
|
}) => {
|
|
29
30
|
const url = new URL(base);
|
|
@@ -33,6 +34,9 @@ const createUrlBuilder = base => ({
|
|
|
33
34
|
if (searchParams) {
|
|
34
35
|
url.search = objectToSearchParams(searchParams, url.searchParams).toString();
|
|
35
36
|
}
|
|
37
|
+
if (hash) {
|
|
38
|
+
url.hash = hash;
|
|
39
|
+
}
|
|
36
40
|
const urlString = url.toString();
|
|
37
41
|
return returnType === "relative" ? urlString.replace(url.origin, "") : urlString;
|
|
38
42
|
};
|
package/lib/utils/url.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"url.mjs","sources":["../../src/utils/url.ts"],"sourcesContent":["import _slugify from \"slugify\";\nimport urlJoin from \"url-join\";\n\n// #############################################################################\n// Types\n// #############################################################################\nexport type SearchParamScalar = string | number | boolean | null | undefined;\nexport type SearchParams = Record<\n string,\n SearchParamScalar | Array<SearchParamScalar>\n>;\n\n// #############################################################################\n// Functions\n// #############################################################################\n\n/**\n * Slugify re-export but set if common options\n */\nexport const slugify = (str: string): string =>\n _slugify(str, {\n replacement: \"-\",\n lower: true,\n strict: true,\n });\n\n/**\n * Helper function for creating url pathnames with safely encoded variables in them\n *\n * @example\n * e`/login` // \"/login\"\n * e`/settings/store/${\"123-456-789\"}/${\" string with titles and title with spaces \"}` // \"/settings/store/123-456-789/%20string%20with%20titles%20and%20title%20with%20spaces%20\"\n */\nexport function e(\n strings: TemplateStringsArray,\n ...placeholders: (string | number | boolean)[]\n): string {\n let result = \"\";\n for (let i = 0, l = strings.length; i < l; i++) {\n result += strings[i];\n if (i < placeholders.length) {\n const placeholder = placeholders[i];\n if (placeholder != null) {\n // TODO: maybe also convert to lowercase?\n result += encodeURIComponent(placeholder);\n }\n }\n }\n return result;\n}\n\nexport type CreateUrlBuilderOptions = {\n /**\n * ! Warning: Make sure to escape the pathname yourself before using `e` from `@ubnt/store.core\n * @example\n * import { e } from \"@ubnt/store.core/lib/utils/url\";\n * const mySafePathname = e`${storeId}/${language}`;\n */\n pathname?: string;\n /**\n * null|undefined values will be ignored\n * rest of available types the will be stringified via `.toString()`\n * @example\n * {searchParams: { hello: 'world', willBeRemoved: undefined}}\n */\n searchParams?: SearchParams;\n /**\n * Preserve pathname of base which helps in some special scenario's for storefront url's as specific \"base\" store\n * url's include `/${storeId}` as first part of the pathname\n * @default false\n */\n overrideBasePathname?: boolean;\n /**\n * Whether to return an absolute url (default behaviour) or relative (the host) url\n * @default 'absolute'\n */\n returnType?: \"absolute\" | \"relative\";\n};\n\n/**\n * Utility to create url's via a base URL\n */\nexport const createUrlBuilder =\n (base: string) =>\n ({\n pathname,\n searchParams,\n overrideBasePathname,\n returnType,\n }: CreateUrlBuilderOptions): string => {\n const url = new URL(base);\n\n if (pathname) {\n url.pathname = overrideBasePathname\n ? pathname\n : urlJoin(url.pathname, pathname);\n }\n\n if (searchParams) {\n // Have to set `url.search` instead of `url.searchParams` as it's a read only property\n url.search = objectToSearchParams(\n searchParams,\n url.searchParams,\n ).toString();\n }\n\n const urlString = url.toString();\n return returnType === \"relative\"\n ? urlString.replace(url.origin, \"\")\n : urlString;\n };\n\n/**\n * Converts javascript object to URLSearchParams instance\n *\n * null|undefined values will be ignored\n * rest of available types the will be stringified via `.toString()`\n * @example\n * {searchParams: { hello: 'world', willBeRemoved: undefined}}\n */\nexport const objectToSearchParams = (\n searchParams: SearchParams,\n /**\n * Optionally pass in an existing instance to append values\n */\n urlSearchParamsInstance: URLSearchParams = new URLSearchParams(),\n): URLSearchParams => {\n for (const [key, value] of Object.entries(searchParams)) {\n /**\n * Although we don't allow \"empty\" values, in runtime we had an issue if passing it, which failed this function,\n * seemed to be to dramatic so add actual runtime check if value is not null|undefined\n */\n if (value != null) {\n if (Array.isArray(value)) {\n value.forEach((v) => {\n if (v != null) {\n urlSearchParamsInstance.append(key, v.toString());\n }\n });\n } else {\n urlSearchParamsInstance.append(key, value.toString());\n }\n }\n }\n\n return urlSearchParamsInstance;\n};\n\n/**\n * Converts URLSearchParams instance to javascript object\n *\n * @example\n * const urlString =\n * \"https://example.com/?name=John&age=30&hobby=reading&hobby=sports\";\n * const url = new URL(urlString);\n * const params = url.searchParams;\n *\n * urlSearchParamsToObject(params) // { name: \"John\", age: \"30\", hobby: [\"reading\", \"sports\"] }\n */\nexport const searchParamsToObject = (\n urlSearchParams: URLSearchParams,\n): SearchParams => {\n const result: { [key: string]: string | string[] } = {};\n\n urlSearchParams.forEach((value, key) => {\n if (result[key] !== undefined) {\n const storedValue = result[key];\n if (Array.isArray(storedValue)) {\n // Add new value to array\n storedValue.push(value);\n } else {\n // Convert to array\n result[key] = [result[key] as string, value];\n }\n } else {\n // Add initial value\n result[key] = value;\n }\n });\n\n return result;\n};\n\n/**\n * Utility for the root middleware to easily parse parts of a pathname (ignores search params)\n * @see https://nodejs.org/api/url.html#urlpathname\n */\nexport const pathnameToParts = (dirtyPathname: string): string[] => {\n const pathname = getPurePathname(dirtyPathname);\n\n // Handle \"empty\" pathname scenarios\n if (!pathname || pathname === \"/\") {\n return [];\n }\n\n const pathParts = pathname.split(\"/\");\n\n /**\n * Handle the case if given pathname starts with slash because '/test/test'.split('/') => ['', 'test', 'test']\n * It's the normal behaviour for pathname to start with a slash\n */\n if (pathname.startsWith(\"/\")) {\n pathParts.shift();\n }\n\n return pathParts;\n};\n\n/**\n * Safely (with proper encodeURIComponent) creates a URL pathname from parts\n * @example\n * partsToPathName(['a', 'b']) => '/a/b'\n * partsToPathName(['a', 'b/c']) => '/a/b%2Fc'\n */\nexport const partsToPathname = (parts: string[]): string => {\n if (!parts || parts.length === 0) {\n return \"/\";\n }\n return `/${parts.filter(Boolean).map(encodeURIComponent).join(\"/\")}`;\n};\n\n/**\n * Removes query string from a pathname and returns a URL class pathname\n * @example\n * getPurePathname('/a/b?c=d') => '/a/b'\n */\nexport const getPurePathname = (dirtyPathname: string): string => {\n return new URL(dirtyPathname, \"https://www.example.com\").pathname;\n};\n\n// Re-export so we don't require the whole url-join package\nexport { default as urlJoin } from \"url-join\";\n"],"names":["slugify","str","_slugify","replacement","lower","strict","e","strings","placeholders","result","i","l","length","placeholder","encodeURIComponent","createUrlBuilder","base","pathname","searchParams","overrideBasePathname","returnType","url","URL","urlJoin","search","objectToSearchParams","toString","urlString","replace","origin","urlSearchParamsInstance","URLSearchParams","key","value","Object","entries","Array","isArray","forEach","v","append","searchParamsToObject","urlSearchParams","storedValue","push","pathnameToParts","dirtyPathname","getPurePathname","pathParts","split","startsWith","shift","partsToPathname","parts","filter","Boolean","map","join"],"mappings":";;;;AAmBO,MAAMA,OAAA,GAAWC,GAAA,IACtBC,QAAA,CAASD,GAAA,EAAK;EACZE,WAAA,EAAa,GAAA;EACbC,KAAA,EAAO,IAAA;EACPC,MAAA,EAAQ;AACV,CAAC,CAAA;AASI,SAASC,CAAAA,CACdC,YACGC,YAAA,EACK;EACR,IAAIC,MAAA,GAAS,EAAA;EACb,KAAA,IAASC,IAAI,CAAA,EAAGC,CAAA,GAAIJ,QAAQK,MAAA,EAAQF,CAAA,GAAIC,GAAGD,CAAA,EAAA,EAAK;IAC9CD,MAAA,IAAUF,QAAQG,CAAC,CAAA;IACnB,IAAIA,CAAA,GAAIF,aAAaI,MAAA,EAAQ;MAC3B,MAAMC,WAAA,GAAcL,aAAaE,CAAC,CAAA;MAClC,IAAIG,eAAe,IAAA,EAAM;QAEvBJ,MAAA,IAAUK,mBAAmBD,WAAW,CAAA;MAC1C;IACF;EACF;EACA,OAAOJ,MAAA;AACT;AAiCO,MAAMM,gBAAA,GACVC,IAAA,IACD,CAAC;EACCC,QAAA;EACAC,YAAA;EACAC,oBAAA;EACAC;AACF,CAAA,K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|
|
1
|
+
{"version":3,"file":"url.mjs","sources":["../../src/utils/url.ts"],"sourcesContent":["import _slugify from \"slugify\";\nimport urlJoin from \"url-join\";\n\n// #############################################################################\n// Types\n// #############################################################################\nexport type SearchParamScalar = string | number | boolean | null | undefined;\nexport type SearchParams = Record<\n string,\n SearchParamScalar | Array<SearchParamScalar>\n>;\n\n// #############################################################################\n// Functions\n// #############################################################################\n\n/**\n * Slugify re-export but set if common options\n */\nexport const slugify = (str: string): string =>\n _slugify(str, {\n replacement: \"-\",\n lower: true,\n strict: true,\n });\n\n/**\n * Helper function for creating url pathnames with safely encoded variables in them\n *\n * @example\n * e`/login` // \"/login\"\n * e`/settings/store/${\"123-456-789\"}/${\" string with titles and title with spaces \"}` // \"/settings/store/123-456-789/%20string%20with%20titles%20and%20title%20with%20spaces%20\"\n */\nexport function e(\n strings: TemplateStringsArray,\n ...placeholders: (string | number | boolean)[]\n): string {\n let result = \"\";\n for (let i = 0, l = strings.length; i < l; i++) {\n result += strings[i];\n if (i < placeholders.length) {\n const placeholder = placeholders[i];\n if (placeholder != null) {\n // TODO: maybe also convert to lowercase?\n result += encodeURIComponent(placeholder);\n }\n }\n }\n return result;\n}\n\nexport type CreateUrlBuilderOptions = {\n /**\n * ! Warning: Make sure to escape the pathname yourself before using `e` from `@ubnt/store.core\n * @example\n * import { e } from \"@ubnt/store.core/lib/utils/url\";\n * const mySafePathname = e`${storeId}/${language}`;\n */\n pathname?: string;\n /**\n * null|undefined values will be ignored\n * rest of available types the will be stringified via `.toString()`\n * @example\n * { searchParams: { hello: 'world', willBeRemoved: undefined } }\n */\n searchParams?: SearchParams;\n /**\n * Optional hash to add/override to the URL (the part after the `#` in a url)\n * @example\n * { hash: 'section-2' } => 'https://example.com/pathname#section-2'\n */\n hash?: string;\n /**\n * Preserve pathname of base which helps in some special scenario's for storefront url's as specific \"base\" store\n * url's include `/${storeId}` as first part of the pathname\n * @default false\n */\n overrideBasePathname?: boolean;\n /**\n * Whether to return an absolute url (default behaviour) or relative (the host) url\n * @default 'absolute'\n */\n returnType?: \"absolute\" | \"relative\";\n};\n\n/**\n * Utility to create url's via a base URL\n */\nexport const createUrlBuilder =\n (base: string) =>\n ({\n pathname,\n searchParams,\n overrideBasePathname,\n hash,\n returnType,\n }: CreateUrlBuilderOptions): string => {\n const url = new URL(base);\n\n if (pathname) {\n url.pathname = overrideBasePathname\n ? pathname\n : urlJoin(url.pathname, pathname);\n }\n\n if (searchParams) {\n // Have to set `url.search` instead of `url.searchParams` as it's a read only property\n url.search = objectToSearchParams(\n searchParams,\n url.searchParams,\n ).toString();\n }\n\n // Override hash if given\n if (hash) {\n url.hash = hash;\n }\n\n const urlString = url.toString();\n return returnType === \"relative\"\n ? urlString.replace(url.origin, \"\")\n : urlString;\n };\n\n/**\n * Converts javascript object to URLSearchParams instance\n *\n * null|undefined values will be ignored\n * rest of available types the will be stringified via `.toString()`\n * @example\n * {searchParams: { hello: 'world', willBeRemoved: undefined}}\n */\nexport const objectToSearchParams = (\n searchParams: SearchParams,\n /**\n * Optionally pass in an existing instance to append values\n */\n urlSearchParamsInstance: URLSearchParams = new URLSearchParams(),\n): URLSearchParams => {\n for (const [key, value] of Object.entries(searchParams)) {\n /**\n * Although we don't allow \"empty\" values, in runtime we had an issue if passing it, which failed this function,\n * seemed to be to dramatic so add actual runtime check if value is not null|undefined\n */\n if (value != null) {\n if (Array.isArray(value)) {\n value.forEach((v) => {\n if (v != null) {\n urlSearchParamsInstance.append(key, v.toString());\n }\n });\n } else {\n urlSearchParamsInstance.append(key, value.toString());\n }\n }\n }\n\n return urlSearchParamsInstance;\n};\n\n/**\n * Converts URLSearchParams instance to javascript object\n *\n * @example\n * const urlString =\n * \"https://example.com/?name=John&age=30&hobby=reading&hobby=sports\";\n * const url = new URL(urlString);\n * const params = url.searchParams;\n *\n * urlSearchParamsToObject(params) // { name: \"John\", age: \"30\", hobby: [\"reading\", \"sports\"] }\n */\nexport const searchParamsToObject = (\n urlSearchParams: URLSearchParams,\n): SearchParams => {\n const result: { [key: string]: string | string[] } = {};\n\n urlSearchParams.forEach((value, key) => {\n if (result[key] !== undefined) {\n const storedValue = result[key];\n if (Array.isArray(storedValue)) {\n // Add new value to array\n storedValue.push(value);\n } else {\n // Convert to array\n result[key] = [result[key] as string, value];\n }\n } else {\n // Add initial value\n result[key] = value;\n }\n });\n\n return result;\n};\n\n/**\n * Utility for the root middleware to easily parse parts of a pathname (ignores search params)\n * @see https://nodejs.org/api/url.html#urlpathname\n */\nexport const pathnameToParts = (dirtyPathname: string): string[] => {\n const pathname = getPurePathname(dirtyPathname);\n\n // Handle \"empty\" pathname scenarios\n if (!pathname || pathname === \"/\") {\n return [];\n }\n\n const pathParts = pathname.split(\"/\");\n\n /**\n * Handle the case if given pathname starts with slash because '/test/test'.split('/') => ['', 'test', 'test']\n * It's the normal behaviour for pathname to start with a slash\n */\n if (pathname.startsWith(\"/\")) {\n pathParts.shift();\n }\n\n return pathParts;\n};\n\n/**\n * Safely (with proper encodeURIComponent) creates a URL pathname from parts\n * @example\n * partsToPathName(['a', 'b']) => '/a/b'\n * partsToPathName(['a', 'b/c']) => '/a/b%2Fc'\n */\nexport const partsToPathname = (parts: string[]): string => {\n if (!parts || parts.length === 0) {\n return \"/\";\n }\n return `/${parts.filter(Boolean).map(encodeURIComponent).join(\"/\")}`;\n};\n\n/**\n * Removes query string from a pathname and returns a URL class pathname\n * @example\n * getPurePathname('/a/b?c=d') => '/a/b'\n */\nexport const getPurePathname = (dirtyPathname: string): string => {\n return new URL(dirtyPathname, \"https://www.example.com\").pathname;\n};\n\n// Re-export so we don't require the whole url-join package\nexport { default as urlJoin } from \"url-join\";\n"],"names":["slugify","str","_slugify","replacement","lower","strict","e","strings","placeholders","result","i","l","length","placeholder","encodeURIComponent","createUrlBuilder","base","pathname","searchParams","overrideBasePathname","hash","returnType","url","URL","urlJoin","search","objectToSearchParams","toString","urlString","replace","origin","urlSearchParamsInstance","URLSearchParams","key","value","Object","entries","Array","isArray","forEach","v","append","searchParamsToObject","urlSearchParams","storedValue","push","pathnameToParts","dirtyPathname","getPurePathname","pathParts","split","startsWith","shift","partsToPathname","parts","filter","Boolean","map","join"],"mappings":";;;;AAmBO,MAAMA,OAAA,GAAWC,GAAA,IACtBC,QAAA,CAASD,GAAA,EAAK;EACZE,WAAA,EAAa,GAAA;EACbC,KAAA,EAAO,IAAA;EACPC,MAAA,EAAQ;AACV,CAAC,CAAA;AASI,SAASC,CAAAA,CACdC,YACGC,YAAA,EACK;EACR,IAAIC,MAAA,GAAS,EAAA;EACb,KAAA,IAASC,IAAI,CAAA,EAAGC,CAAA,GAAIJ,QAAQK,MAAA,EAAQF,CAAA,GAAIC,GAAGD,CAAA,EAAA,EAAK;IAC9CD,MAAA,IAAUF,QAAQG,CAAC,CAAA;IACnB,IAAIA,CAAA,GAAIF,aAAaI,MAAA,EAAQ;MAC3B,MAAMC,WAAA,GAAcL,aAAaE,CAAC,CAAA;MAClC,IAAIG,eAAe,IAAA,EAAM;QAEvBJ,MAAA,IAAUK,mBAAmBD,WAAW,CAAA;MAC1C;IACF;EACF;EACA,OAAOJ,MAAA;AACT;AAuCO,MAAMM,gBAAA,GACVC,IAAA,IACD,CAAC;EACCC,QAAA;EACAC,YAAA;EACAC,oBAAA;EACAC,IAAA;EACAC;AACF,CAAA,KAAuC;EACrC,MAAMC,GAAA,GAAM,IAAIC,GAAA,CAAIP,IAAI,CAAA;EAExB,IAAIC,QAAA,EAAU;IACZK,GAAA,CAAIL,WAAWE,oBAAA,GACXF,QAAA,GACAO,OAAA,CAAQF,GAAA,CAAIL,UAAUA,QAAQ,CAAA;EACpC;EAEA,IAAIC,YAAA,EAAc;IAEhBI,GAAA,CAAIG,MAAA,GAASC,oBAAA,CACXR,YAAA,EACAI,GAAA,CAAIJ,YAAA,EACJS,QAAA,CAAA,CAAS;EACb;EAGA,IAAIP,IAAA,EAAM;IACRE,GAAA,CAAIF,IAAA,GAAOA,IAAA;EACb;EAEA,MAAMQ,SAAA,GAAYN,IAAI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MAAA,CAAOwB,GAAG,CAAA,GAAIC,KAAA;IAChB;EACF,CAAC,CAAA;EAED,OAAOzB,MAAA;AACT,CAAA;AAMO,MAAMqC,eAAA,GAAmBC,aAAA,IAAoC;EAClE,MAAM9B,QAAA,GAAW+B,gBAAgBD,aAAa,CAAA;EAG9C,IAAI,CAAC9B,QAAA,IAAYA,QAAA,KAAa,GAAA,EAAK;IACjC,OAAO,EAAC;EACV;EAEA,MAAMgC,SAAA,GAAYhC,QAAA,CAASiC,KAAA,CAAM,GAAG,CAAA;EAMpC,IAAIjC,QAAA,CAASkC,UAAA,CAAW,GAAG,CAAA,EAAG;IAC5BF,SAAA,CAAUG,KAAA,CAAA,CAAM;EAClB;EAEA,OAAOH,SAAA;AACT,CAAA;AAQO,MAAMI,eAAA,GAAmBC,KAAA,IAA4B;EAC1D,IAAI,CAACA,KAAA,IAASA,KAAA,CAAM1C,MAAA,KAAW,CAAA,EAAG;IAChC,OAAO,GAAA;EACT;EACA,OAAO,IAAI0C,KAAA,CAAMC,MAAA,CAAOC,OAAO,CAAA,CAAEC,IAAI3C,kBAAkB,CAAA,CAAE4C,IAAA,CAAK,GAAG,CAAC,EAAA;AACpE,CAAA;AAOO,MAAMV,eAAA,GAAmBD,aAAA,IAAkC;EAChE,OAAO,IAAIxB,GAAA,CAAIwB,aAAA,EAAe,yBAAyB,CAAA,CAAE9B,QAAA;AAC3D,CAAA;"}
|
|
@@ -8,7 +8,7 @@ const address2SuperRefinement = ({
|
|
|
8
8
|
address2,
|
|
9
9
|
country
|
|
10
10
|
}, ctx) => {
|
|
11
|
-
if (
|
|
11
|
+
if ([generated.CountryCode.Br, generated.CountryCode.Ph].includes(country) && !address2) {
|
|
12
12
|
return ctx.addIssue({
|
|
13
13
|
path: ["address2"],
|
|
14
14
|
code: zod.ZodIssueCode.too_small,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"address2SuperRefinement.js","sources":["../../../src/utils/validations/address2SuperRefinement.ts"],"sourcesContent":["import { type SuperRefinement, ZodIssueCode } from \"zod\";\n\nimport type { Nullish } from \"../../types\";\nimport { CountryCode } from \"../../types\";\n\nexport const address2SuperRefinement: SuperRefinement<{\n address2?: Nullish<string>;\n country: CountryCode;\n}> = ({ address2, country }, ctx) => {\n // Should be set for Brazil.\n if (
|
|
1
|
+
{"version":3,"file":"address2SuperRefinement.js","sources":["../../../src/utils/validations/address2SuperRefinement.ts"],"sourcesContent":["import { type SuperRefinement, ZodIssueCode } from \"zod\";\n\nimport type { Nullish } from \"../../types\";\nimport { CountryCode } from \"../../types\";\n\nexport const address2SuperRefinement: SuperRefinement<{\n address2?: Nullish<string>;\n country: CountryCode;\n}> = ({ address2, country }, ctx) => {\n // Should be set for Brazil and Philippines.\n if ([CountryCode.Br, CountryCode.Ph].includes(country) && !address2) {\n return ctx.addIssue({\n path: [\"address2\"],\n code: ZodIssueCode.too_small,\n type: \"string\",\n minimum: 1,\n inclusive: true,\n });\n }\n};\n"],"names":["address2SuperRefinement","address2","country","ctx","CountryCode","Br","Ph","includes","addIssue","path","code","ZodIssueCode","too_small","type","minimum","inclusive"],"mappings":";;;;;;AAKO,MAAMA,0BAGRA,CAAC;EAAEC,QAAA;EAAUC;AAAA,GAAWC,GAAA,KAAQ;EAEnC,IAAI,CAACC,SAAAA,CAAAA,WAAA,CAAYC,EAAA,EAAID,qBAAA,CAAYE,EAAE,EAAEC,QAAA,CAASL,OAAO,CAAA,IAAK,CAACD,QAAA,EAAU;IACnE,OAAOE,IAAIK,QAAA,CAAS;MAClBC,IAAA,EAAM,CAAC,UAAU,CAAA;MACjBC,MAAMC,GAAAA,CAAAA,YAAA,CAAaC,SAAA;MACnBC,IAAA,EAAM,QAAA;MACNC,OAAA,EAAS,CAAA;MACTC,SAAA,EAAW;IAAA,CACZ,CAAA;EACH;AACF,CAAA;"}
|
|
@@ -6,7 +6,7 @@ const address2SuperRefinement = ({
|
|
|
6
6
|
address2,
|
|
7
7
|
country
|
|
8
8
|
}, ctx) => {
|
|
9
|
-
if (
|
|
9
|
+
if ([CountryCode.Br, CountryCode.Ph].includes(country) && !address2) {
|
|
10
10
|
return ctx.addIssue({
|
|
11
11
|
path: ["address2"],
|
|
12
12
|
code: ZodIssueCode.too_small,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"address2SuperRefinement.mjs","sources":["../../../src/utils/validations/address2SuperRefinement.ts"],"sourcesContent":["import { type SuperRefinement, ZodIssueCode } from \"zod\";\n\nimport type { Nullish } from \"../../types\";\nimport { CountryCode } from \"../../types\";\n\nexport const address2SuperRefinement: SuperRefinement<{\n address2?: Nullish<string>;\n country: CountryCode;\n}> = ({ address2, country }, ctx) => {\n // Should be set for Brazil.\n if (
|
|
1
|
+
{"version":3,"file":"address2SuperRefinement.mjs","sources":["../../../src/utils/validations/address2SuperRefinement.ts"],"sourcesContent":["import { type SuperRefinement, ZodIssueCode } from \"zod\";\n\nimport type { Nullish } from \"../../types\";\nimport { CountryCode } from \"../../types\";\n\nexport const address2SuperRefinement: SuperRefinement<{\n address2?: Nullish<string>;\n country: CountryCode;\n}> = ({ address2, country }, ctx) => {\n // Should be set for Brazil and Philippines.\n if ([CountryCode.Br, CountryCode.Ph].includes(country) && !address2) {\n return ctx.addIssue({\n path: [\"address2\"],\n code: ZodIssueCode.too_small,\n type: \"string\",\n minimum: 1,\n inclusive: true,\n });\n }\n};\n"],"names":["address2SuperRefinement","address2","country","ctx","CountryCode","Br","Ph","includes","addIssue","path","code","ZodIssueCode","too_small","type","minimum","inclusive"],"mappings":";;;;AAKO,MAAMA,0BAGRA,CAAC;EAAEC,QAAA;EAAUC;AAAA,GAAWC,GAAA,KAAQ;EAEnC,IAAI,CAACC,WAAA,CAAYC,EAAA,EAAID,WAAA,CAAYE,EAAE,EAAEC,QAAA,CAASL,OAAO,CAAA,IAAK,CAACD,QAAA,EAAU;IACnE,OAAOE,IAAIK,QAAA,CAAS;MAClBC,IAAA,EAAM,CAAC,UAAU,CAAA;MACjBC,MAAMC,YAAA,CAAaC,SAAA;MACnBC,IAAA,EAAM,QAAA;MACNC,OAAA,EAAS,CAAA;MACTC,SAAA,EAAW;IAAA,CACZ,CAAA;EACH;AACF,CAAA;"}
|
|
@@ -56,7 +56,7 @@ const createTaxIdSuperRefinement = ({
|
|
|
56
56
|
type: "specific",
|
|
57
57
|
addressType: type
|
|
58
58
|
});
|
|
59
|
-
const shouldUppercaseTaxLabel = [types.TaxLabelType.Gst, types.TaxLabelType.Vat, types.TaxLabelType.Cpf, types.TaxLabelType.Cnpj].includes(taxLabelType);
|
|
59
|
+
const shouldUppercaseTaxLabel = [types.TaxLabelType.Gst, types.TaxLabelType.Vat, types.TaxLabelType.Cpf, types.TaxLabelType.Cnpj, types.TaxLabelType.Psn, types.TaxLabelType.Tin].includes(taxLabelType);
|
|
60
60
|
const label = shouldUppercaseTaxLabel ? taxLabelType.toUpperCase() : taxLabelType;
|
|
61
61
|
if (!index.isValidTaxNumberPattern(type, taxCountryCode, taxId)) {
|
|
62
62
|
return zod$1.addCustomZodIssue(ctx, "taxId", zod$1.CustomZodIssueCode.TaxFormat, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"taxIdSuperRefinement.js","sources":["../../../src/utils/validations/taxIdSuperRefinement.ts"],"sourcesContent":["import type { SuperRefinement } from \"zod\";\nimport { ZodIssueCode } from \"zod\";\n\nimport type { AddressType, CountryCode, Nullish } from \"../../types\";\nimport { AddressUsageType, ValidateTaxStatus } from \"../../types\";\nimport { assertUnreachable, checkUnreachable } from \"../errors\";\nimport {\n getTaxCountry,\n getTaxLabelTypeByCountry,\n getValidationMappingByType,\n hasTaxNumberPatternValidation,\n isValidTaxNumberPattern,\n} from \"../tax\";\nimport { TaxLabelType } from \"../tax/types\";\nimport { addCustomZodIssue, CustomZodIssueCode } from \"../zod\";\n\nexport type ValidateTaxIdExternally = (opts: {\n type: AddressType;\n countryCode: CountryCode;\n province: Nullish<string>;\n taxId: string;\n}) => Promise<Nullish<ValidateTaxStatus>>;\n\nexport const createTaxIdSuperRefinement =\n ({\n addressUsageType,\n validateTaxIdExternally,\n }: {\n addressUsageType: AddressUsageType;\n validateTaxIdExternally?: ValidateTaxIdExternally;\n }): SuperRefinement<{\n type: AddressType;\n country: CountryCode;\n province?: Nullish<string>;\n taxId?: Nullish<string>;\n }> =>\n async ({ type, country, province, taxId }, ctx) => {\n switch (addressUsageType) {\n // Exit early because tax ID is generally not needed for billing addresses.\n case AddressUsageType.Billing:\n return;\n // Don't do anything and proceed with the validation logic below.\n case AddressUsageType.ReturnShipping:\n case AddressUsageType.Shipping:\n break;\n default:\n assertUnreachable(addressUsageType);\n }\n\n const taxCountryCode = getTaxCountry({\n country,\n province,\n });\n\n // Doesn't require strict validation\n if (!hasTaxNumberPatternValidation(type, taxCountryCode)) {\n return;\n }\n\n const mapping = getValidationMappingByType(type);\n const { isOptional } = mapping[taxCountryCode];\n\n if (!taxId && isOptional) {\n return;\n }\n\n // Ensure that we have tax ID before passing it to the validation\n if (!taxId) {\n return ctx.addIssue({\n path: [\"taxId\"],\n code: ZodIssueCode.too_small,\n type: \"string\",\n minimum: 1,\n inclusive: true,\n });\n }\n\n const taxLabelType = getTaxLabelTypeByCountry(country, {\n type: \"specific\",\n addressType: type,\n });\n const shouldUppercaseTaxLabel = [\n TaxLabelType.Gst,\n TaxLabelType.Vat,\n TaxLabelType.Cpf,\n TaxLabelType.Cnpj,\n ].includes(taxLabelType);\n const label = shouldUppercaseTaxLabel\n ? taxLabelType.toUpperCase()\n : taxLabelType;\n\n // If the pattern is invalid, error\n if (!isValidTaxNumberPattern(type, taxCountryCode, taxId)) {\n return addCustomZodIssue(ctx, \"taxId\", CustomZodIssueCode.TaxFormat, {\n label,\n });\n }\n\n if (!validateTaxIdExternally) {\n return;\n }\n\n // Additionally validate tax id externally depending on environment\n const taxStatus = await validateTaxIdExternally({\n type,\n countryCode: country,\n province,\n taxId,\n });\n\n switch (taxStatus) {\n case ValidateTaxStatus.Invalid: {\n addCustomZodIssue(ctx, \"taxId\", CustomZodIssueCode.TaxInvalid, {\n label,\n });\n break;\n }\n\n case ValidateTaxStatus.Unavailable: {\n addCustomZodIssue(ctx, \"taxId\", CustomZodIssueCode.TaxUnavailable, {\n label,\n });\n break;\n }\n\n case ValidateTaxStatus.Valid:\n case ValidateTaxStatus.NotValidated:\n case null:\n case undefined: {\n // Do nothing;\n break;\n }\n\n default: {\n // Do nothing as well (but in a type safe way)\n checkUnreachable(taxStatus);\n }\n }\n };\n"],"names":["createTaxIdSuperRefinement","addressUsageType","validateTaxIdExternally","type","country","province","taxId","ctx","AddressUsageType","Billing","ReturnShipping","Shipping","assertUnreachable","taxCountryCode","getTaxCountry","hasTaxNumberPatternValidation","mapping","getValidationMappingByType","isOptional","addIssue","path","code","ZodIssueCode","too_small","minimum","inclusive","taxLabelType","getTaxLabelTypeByCountry","addressType","shouldUppercaseTaxLabel","TaxLabelType","Gst","Vat","Cpf","Cnpj","includes","label","toUpperCase","isValidTaxNumberPattern","addCustomZodIssue","CustomZodIssueCode","TaxFormat","taxStatus","countryCode","ValidateTaxStatus","Invalid","TaxInvalid","Unavailable","TaxUnavailable","Valid","NotValidated","checkUnreachable"],"mappings":";;;;;;;;;;;AAuBO,MAAMA,6BACXA,CAAC;EACCC,gBAAA;EACAC;AACF,CAAA,KASA,OAAO;EAAEC,IAAA;EAAMC;EAASC,QAAA;EAAUC;AAAA,GAASC,GAAA,KAAQ;EACjD,QAAQN,gBAAA;IAAkB;IAExB,KAAKO,SAAAA,CAAAA,gBAAA,CAAiBC,OAAA;MACpB;IAAA;IAEF,KAAKD,SAAAA,CAAAA,gBAAA,CAAiBE,cAAA;IACtB,KAAKF,SAAAA,CAAAA,gBAAA,CAAiBG,QAAA;MACpB;IACF;MACEC,MAAAA,CAAAA,iBAAA,CAAkBX,gBAAgB,CAAA;EAAA;EAGtC,MAAMY,iBAAiBC,KAAAA,CAAAA,aAAA,CAAc;IACnCV,OAAA;IACAC;EAAA,CACD,CAAA;EAGD,IAAI,CAACU,KAAAA,CAAAA,6BAAA,CAA8BZ,IAAA,EAAMU,cAAc,CAAA,EAAG;IACxD;EACF;EAEA,MAAMG,OAAA,GAAUC,KAAAA,CAAAA,2BAA2Bd,IAAI,CAAA;EAC/C,MAAM;IAAEe;EAAA,CAAW,GAAIF,OAAA,CAAQH,cAAc,CAAA;EAE7C,IAAI,CAACP,SAASY,UAAA,EAAY;IACxB;EACF;EAGA,IAAI,CAACZ,KAAA,EAAO;IACV,OAAOC,IAAIY,QAAA,CAAS;MAClBC,IAAA,EAAM,CAAC,OAAO,CAAA;MACdC,MAAMC,GAAAA,CAAAA,YAAA,CAAaC,SAAA;MACnBpB,IAAA,EAAM,QAAA;MACNqB,OAAA,EAAS,CAAA;MACTC,SAAA,EAAW;IAAA,CACZ,CAAA;EACH;EAEA,MAAMC,YAAA,GAAeC,KAAAA,CAAAA,yBAAyBvB,OAAA,EAAS;IACrDD,IAAA,EAAM,UAAA;IACNyB,WAAA,EAAazB;EAAA,CACd,CAAA;EACD,MAAM0B,uBAAA,GAA0B,CAC9BC,KAAAA,CAAAA,YAAA,CAAaC,GAAA,EACbD,KAAAA,CAAAA,YAAA,CAAaE,GAAA,EACbF,KAAAA,CAAAA,YAAA,CAAaG,GAAA,EACbH,
|
|
1
|
+
{"version":3,"file":"taxIdSuperRefinement.js","sources":["../../../src/utils/validations/taxIdSuperRefinement.ts"],"sourcesContent":["import type { SuperRefinement } from \"zod\";\nimport { ZodIssueCode } from \"zod\";\n\nimport type { AddressType, CountryCode, Nullish } from \"../../types\";\nimport { AddressUsageType, ValidateTaxStatus } from \"../../types\";\nimport { assertUnreachable, checkUnreachable } from \"../errors\";\nimport {\n getTaxCountry,\n getTaxLabelTypeByCountry,\n getValidationMappingByType,\n hasTaxNumberPatternValidation,\n isValidTaxNumberPattern,\n} from \"../tax\";\nimport { TaxLabelType } from \"../tax/types\";\nimport { addCustomZodIssue, CustomZodIssueCode } from \"../zod\";\n\nexport type ValidateTaxIdExternally = (opts: {\n type: AddressType;\n countryCode: CountryCode;\n province: Nullish<string>;\n taxId: string;\n}) => Promise<Nullish<ValidateTaxStatus>>;\n\nexport const createTaxIdSuperRefinement =\n ({\n addressUsageType,\n validateTaxIdExternally,\n }: {\n addressUsageType: AddressUsageType;\n validateTaxIdExternally?: ValidateTaxIdExternally;\n }): SuperRefinement<{\n type: AddressType;\n country: CountryCode;\n province?: Nullish<string>;\n taxId?: Nullish<string>;\n }> =>\n async ({ type, country, province, taxId }, ctx) => {\n switch (addressUsageType) {\n // Exit early because tax ID is generally not needed for billing addresses.\n case AddressUsageType.Billing:\n return;\n // Don't do anything and proceed with the validation logic below.\n case AddressUsageType.ReturnShipping:\n case AddressUsageType.Shipping:\n break;\n default:\n assertUnreachable(addressUsageType);\n }\n\n const taxCountryCode = getTaxCountry({\n country,\n province,\n });\n\n // Doesn't require strict validation\n if (!hasTaxNumberPatternValidation(type, taxCountryCode)) {\n return;\n }\n\n const mapping = getValidationMappingByType(type);\n const { isOptional } = mapping[taxCountryCode];\n\n if (!taxId && isOptional) {\n return;\n }\n\n // Ensure that we have tax ID before passing it to the validation\n if (!taxId) {\n return ctx.addIssue({\n path: [\"taxId\"],\n code: ZodIssueCode.too_small,\n type: \"string\",\n minimum: 1,\n inclusive: true,\n });\n }\n\n const taxLabelType = getTaxLabelTypeByCountry(country, {\n type: \"specific\",\n addressType: type,\n });\n const shouldUppercaseTaxLabel = [\n TaxLabelType.Gst,\n TaxLabelType.Vat,\n TaxLabelType.Cpf,\n TaxLabelType.Cnpj,\n TaxLabelType.Psn,\n TaxLabelType.Tin,\n ].includes(taxLabelType);\n const label = shouldUppercaseTaxLabel\n ? taxLabelType.toUpperCase()\n : taxLabelType;\n\n // If the pattern is invalid, error\n if (!isValidTaxNumberPattern(type, taxCountryCode, taxId)) {\n return addCustomZodIssue(ctx, \"taxId\", CustomZodIssueCode.TaxFormat, {\n label,\n });\n }\n\n if (!validateTaxIdExternally) {\n return;\n }\n\n // Additionally validate tax id externally depending on environment\n const taxStatus = await validateTaxIdExternally({\n type,\n countryCode: country,\n province,\n taxId,\n });\n\n switch (taxStatus) {\n case ValidateTaxStatus.Invalid: {\n addCustomZodIssue(ctx, \"taxId\", CustomZodIssueCode.TaxInvalid, {\n label,\n });\n break;\n }\n\n case ValidateTaxStatus.Unavailable: {\n addCustomZodIssue(ctx, \"taxId\", CustomZodIssueCode.TaxUnavailable, {\n label,\n });\n break;\n }\n\n case ValidateTaxStatus.Valid:\n case ValidateTaxStatus.NotValidated:\n case null:\n case undefined: {\n // Do nothing;\n break;\n }\n\n default: {\n // Do nothing as well (but in a type safe way)\n checkUnreachable(taxStatus);\n }\n }\n };\n"],"names":["createTaxIdSuperRefinement","addressUsageType","validateTaxIdExternally","type","country","province","taxId","ctx","AddressUsageType","Billing","ReturnShipping","Shipping","assertUnreachable","taxCountryCode","getTaxCountry","hasTaxNumberPatternValidation","mapping","getValidationMappingByType","isOptional","addIssue","path","code","ZodIssueCode","too_small","minimum","inclusive","taxLabelType","getTaxLabelTypeByCountry","addressType","shouldUppercaseTaxLabel","TaxLabelType","Gst","Vat","Cpf","Cnpj","Psn","Tin","includes","label","toUpperCase","isValidTaxNumberPattern","addCustomZodIssue","CustomZodIssueCode","TaxFormat","taxStatus","countryCode","ValidateTaxStatus","Invalid","TaxInvalid","Unavailable","TaxUnavailable","Valid","NotValidated","checkUnreachable"],"mappings":";;;;;;;;;;;AAuBO,MAAMA,6BACXA,CAAC;EACCC,gBAAA;EACAC;AACF,CAAA,KASA,OAAO;EAAEC,IAAA;EAAMC;EAASC,QAAA;EAAUC;AAAA,GAASC,GAAA,KAAQ;EACjD,QAAQN,gBAAA;IAAkB;IAExB,KAAKO,SAAAA,CAAAA,gBAAA,CAAiBC,OAAA;MACpB;IAAA;IAEF,KAAKD,SAAAA,CAAAA,gBAAA,CAAiBE,cAAA;IACtB,KAAKF,SAAAA,CAAAA,gBAAA,CAAiBG,QAAA;MACpB;IACF;MACEC,MAAAA,CAAAA,iBAAA,CAA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A,CAAAA,iBAAA,CAAkBlC,GAAA,EAAK,OAAA,EAASmC,KAAAA,CAAAA,kBAAA,CAAmBC,SAAA,EAAW;MACnEL;IAAA,CACD,CAAA;EACH;EAEA,IAAI,CAACpC,uBAAA,EAAyB;IAC5B;EACF;EAGA,MAAM0C,SAAA,GAAY,MAAM1C,uBAAA,CAAwB;IAC9CC,IAAA;IACA0C,WAAA,EAAazC,OAAA;IACbC,QAAA;IACAC;EAAA,CACD,CAAA;EAED,QAAQsC,SAAA;IACN,KAAKE,SAAAA,CAAAA,kBAAkBC,OAAA;MAAS;QAC9BN,KAAAA,CAAAA,iBAAA,CAAkBlC,GAAA,EAAK,OAAA,EAASmC,KAAAA,CAAAA,kBAAA,CAAmBM,UAAA,EAAY;UAC7DV;QAAA,CACD,CAAA;QACD;MACF;IAEA,KAAKQ,SAAAA,CAAAA,kBAAkBG,WAAA;MAAa;QAClCR,KAAAA,CAAAA,iBAAA,CAAkBlC,GAAA,EAAK,OAAA,EAASmC,KAAAA,CAAAA,kBAAA,CAAmBQ,cAAA,EAAgB;UACjEZ;QAAA,CACD,CAAA;QACD;MACF;IAEA,KAAKQ,SAAAA,CAAAA,iBAAA,CAAkBK,KAAA;IACvB,KAAKL,SAAAA,CAAAA,iBAAA,CAAkBM,YAAA;IACvB,KAAK,IAAA;IACL,KAAK,KAAA,CAAA;MAAW;QAEd;MACF;IAEA;MAAS;QAEPC,MAAAA,CAAAA,gBAAA,CAAiBT,SAAS,CAAA;MAC5B;EAAA;AAEJ,CAAA;"}
|
|
@@ -54,7 +54,7 @@ const createTaxIdSuperRefinement = ({
|
|
|
54
54
|
type: "specific",
|
|
55
55
|
addressType: type
|
|
56
56
|
});
|
|
57
|
-
const shouldUppercaseTaxLabel = [TaxLabelType.Gst, TaxLabelType.Vat, TaxLabelType.Cpf, TaxLabelType.Cnpj].includes(taxLabelType);
|
|
57
|
+
const shouldUppercaseTaxLabel = [TaxLabelType.Gst, TaxLabelType.Vat, TaxLabelType.Cpf, TaxLabelType.Cnpj, TaxLabelType.Psn, TaxLabelType.Tin].includes(taxLabelType);
|
|
58
58
|
const label = shouldUppercaseTaxLabel ? taxLabelType.toUpperCase() : taxLabelType;
|
|
59
59
|
if (!isValidTaxNumberPattern(type, taxCountryCode, taxId)) {
|
|
60
60
|
return addCustomZodIssue(ctx, "taxId", CustomZodIssueCode.TaxFormat, {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"taxIdSuperRefinement.mjs","sources":["../../../src/utils/validations/taxIdSuperRefinement.ts"],"sourcesContent":["import type { SuperRefinement } from \"zod\";\nimport { ZodIssueCode } from \"zod\";\n\nimport type { AddressType, CountryCode, Nullish } from \"../../types\";\nimport { AddressUsageType, ValidateTaxStatus } from \"../../types\";\nimport { assertUnreachable, checkUnreachable } from \"../errors\";\nimport {\n getTaxCountry,\n getTaxLabelTypeByCountry,\n getValidationMappingByType,\n hasTaxNumberPatternValidation,\n isValidTaxNumberPattern,\n} from \"../tax\";\nimport { TaxLabelType } from \"../tax/types\";\nimport { addCustomZodIssue, CustomZodIssueCode } from \"../zod\";\n\nexport type ValidateTaxIdExternally = (opts: {\n type: AddressType;\n countryCode: CountryCode;\n province: Nullish<string>;\n taxId: string;\n}) => Promise<Nullish<ValidateTaxStatus>>;\n\nexport const createTaxIdSuperRefinement =\n ({\n addressUsageType,\n validateTaxIdExternally,\n }: {\n addressUsageType: AddressUsageType;\n validateTaxIdExternally?: ValidateTaxIdExternally;\n }): SuperRefinement<{\n type: AddressType;\n country: CountryCode;\n province?: Nullish<string>;\n taxId?: Nullish<string>;\n }> =>\n async ({ type, country, province, taxId }, ctx) => {\n switch (addressUsageType) {\n // Exit early because tax ID is generally not needed for billing addresses.\n case AddressUsageType.Billing:\n return;\n // Don't do anything and proceed with the validation logic below.\n case AddressUsageType.ReturnShipping:\n case AddressUsageType.Shipping:\n break;\n default:\n assertUnreachable(addressUsageType);\n }\n\n const taxCountryCode = getTaxCountry({\n country,\n province,\n });\n\n // Doesn't require strict validation\n if (!hasTaxNumberPatternValidation(type, taxCountryCode)) {\n return;\n }\n\n const mapping = getValidationMappingByType(type);\n const { isOptional } = mapping[taxCountryCode];\n\n if (!taxId && isOptional) {\n return;\n }\n\n // Ensure that we have tax ID before passing it to the validation\n if (!taxId) {\n return ctx.addIssue({\n path: [\"taxId\"],\n code: ZodIssueCode.too_small,\n type: \"string\",\n minimum: 1,\n inclusive: true,\n });\n }\n\n const taxLabelType = getTaxLabelTypeByCountry(country, {\n type: \"specific\",\n addressType: type,\n });\n const shouldUppercaseTaxLabel = [\n TaxLabelType.Gst,\n TaxLabelType.Vat,\n TaxLabelType.Cpf,\n TaxLabelType.Cnpj,\n ].includes(taxLabelType);\n const label = shouldUppercaseTaxLabel\n ? taxLabelType.toUpperCase()\n : taxLabelType;\n\n // If the pattern is invalid, error\n if (!isValidTaxNumberPattern(type, taxCountryCode, taxId)) {\n return addCustomZodIssue(ctx, \"taxId\", CustomZodIssueCode.TaxFormat, {\n label,\n });\n }\n\n if (!validateTaxIdExternally) {\n return;\n }\n\n // Additionally validate tax id externally depending on environment\n const taxStatus = await validateTaxIdExternally({\n type,\n countryCode: country,\n province,\n taxId,\n });\n\n switch (taxStatus) {\n case ValidateTaxStatus.Invalid: {\n addCustomZodIssue(ctx, \"taxId\", CustomZodIssueCode.TaxInvalid, {\n label,\n });\n break;\n }\n\n case ValidateTaxStatus.Unavailable: {\n addCustomZodIssue(ctx, \"taxId\", CustomZodIssueCode.TaxUnavailable, {\n label,\n });\n break;\n }\n\n case ValidateTaxStatus.Valid:\n case ValidateTaxStatus.NotValidated:\n case null:\n case undefined: {\n // Do nothing;\n break;\n }\n\n default: {\n // Do nothing as well (but in a type safe way)\n checkUnreachable(taxStatus);\n }\n }\n };\n"],"names":["createTaxIdSuperRefinement","addressUsageType","validateTaxIdExternally","type","country","province","taxId","ctx","AddressUsageType","Billing","ReturnShipping","Shipping","assertUnreachable","taxCountryCode","getTaxCountry","hasTaxNumberPatternValidation","mapping","getValidationMappingByType","isOptional","addIssue","path","code","ZodIssueCode","too_small","minimum","inclusive","taxLabelType","getTaxLabelTypeByCountry","addressType","shouldUppercaseTaxLabel","TaxLabelType","Gst","Vat","Cpf","Cnpj","includes","label","toUpperCase","isValidTaxNumberPattern","addCustomZodIssue","CustomZodIssueCode","TaxFormat","taxStatus","countryCode","ValidateTaxStatus","Invalid","TaxInvalid","Unavailable","TaxUnavailable","Valid","NotValidated","checkUnreachable"],"mappings":";;;;;;;;;AAuBO,MAAMA,6BACXA,CAAC;EACCC,gBAAA;EACAC;AACF,CAAA,KASA,OAAO;EAAEC,IAAA;EAAMC;EAASC,QAAA;EAAUC;AAAA,GAASC,GAAA,KAAQ;EACjD,QAAQN,gBAAA;IAAkB;IAExB,KAAKO,gBAAA,CAAiBC,OAAA;MACpB;IAAA;IAEF,KAAKD,gBAAA,CAAiBE,cAAA;IACtB,KAAKF,gBAAA,CAAiBG,QAAA;MACpB;IACF;MACEC,iBAAA,CAAkBX,gBAAgB,CAAA;EAAA;EAGtC,MAAMY,iBAAiBC,aAAA,CAAc;IACnCV,OAAA;IACAC;EAAA,CACD,CAAA;EAGD,IAAI,CAACU,6BAAA,CAA8BZ,IAAA,EAAMU,cAAc,CAAA,EAAG;IACxD;EACF;EAEA,MAAMG,OAAA,GAAUC,2BAA2Bd,IAAI,CAAA;EAC/C,MAAM;IAAEe;EAAA,CAAW,GAAIF,OAAA,CAAQH,cAAc,CAAA;EAE7C,IAAI,CAACP,SAASY,UAAA,EAAY;IACxB;EACF;EAGA,IAAI,CAACZ,KAAA,EAAO;IACV,OAAOC,IAAIY,QAAA,CAAS;MAClBC,IAAA,EAAM,CAAC,OAAO,CAAA;MACdC,MAAMC,YAAA,CAAaC,SAAA;MACnBpB,IAAA,EAAM,QAAA;MACNqB,OAAA,EAAS,CAAA;MACTC,SAAA,EAAW;IAAA,CACZ,CAAA;EACH;EAEA,MAAMC,YAAA,GAAeC,yBAAyBvB,OAAA,EAAS;IACrDD,IAAA,EAAM,UAAA;IACNyB,WAAA,EAAazB;EAAA,CACd,CAAA;EACD,MAAM0B,uBAAA,GAA0B,CAC9BC,YAAA,CAAaC,GAAA,EACbD,YAAA,CAAaE,GAAA,EACbF,YAAA,CAAaG,GAAA,EACbH,YAAA,CAAaI,IAAA,CACf,CAAEC,
|
|
1
|
+
{"version":3,"file":"taxIdSuperRefinement.mjs","sources":["../../../src/utils/validations/taxIdSuperRefinement.ts"],"sourcesContent":["import type { SuperRefinement } from \"zod\";\nimport { ZodIssueCode } from \"zod\";\n\nimport type { AddressType, CountryCode, Nullish } from \"../../types\";\nimport { AddressUsageType, ValidateTaxStatus } from \"../../types\";\nimport { assertUnreachable, checkUnreachable } from \"../errors\";\nimport {\n getTaxCountry,\n getTaxLabelTypeByCountry,\n getValidationMappingByType,\n hasTaxNumberPatternValidation,\n isValidTaxNumberPattern,\n} from \"../tax\";\nimport { TaxLabelType } from \"../tax/types\";\nimport { addCustomZodIssue, CustomZodIssueCode } from \"../zod\";\n\nexport type ValidateTaxIdExternally = (opts: {\n type: AddressType;\n countryCode: CountryCode;\n province: Nullish<string>;\n taxId: string;\n}) => Promise<Nullish<ValidateTaxStatus>>;\n\nexport const createTaxIdSuperRefinement =\n ({\n addressUsageType,\n validateTaxIdExternally,\n }: {\n addressUsageType: AddressUsageType;\n validateTaxIdExternally?: ValidateTaxIdExternally;\n }): SuperRefinement<{\n type: AddressType;\n country: CountryCode;\n province?: Nullish<string>;\n taxId?: Nullish<string>;\n }> =>\n async ({ type, country, province, taxId }, ctx) => {\n switch (addressUsageType) {\n // Exit early because tax ID is generally not needed for billing addresses.\n case AddressUsageType.Billing:\n return;\n // Don't do anything and proceed with the validation logic below.\n case AddressUsageType.ReturnShipping:\n case AddressUsageType.Shipping:\n break;\n default:\n assertUnreachable(addressUsageType);\n }\n\n const taxCountryCode = getTaxCountry({\n country,\n province,\n });\n\n // Doesn't require strict validation\n if (!hasTaxNumberPatternValidation(type, taxCountryCode)) {\n return;\n }\n\n const mapping = getValidationMappingByType(type);\n const { isOptional } = mapping[taxCountryCode];\n\n if (!taxId && isOptional) {\n return;\n }\n\n // Ensure that we have tax ID before passing it to the validation\n if (!taxId) {\n return ctx.addIssue({\n path: [\"taxId\"],\n code: ZodIssueCode.too_small,\n type: \"string\",\n minimum: 1,\n inclusive: true,\n });\n }\n\n const taxLabelType = getTaxLabelTypeByCountry(country, {\n type: \"specific\",\n addressType: type,\n });\n const shouldUppercaseTaxLabel = [\n TaxLabelType.Gst,\n TaxLabelType.Vat,\n TaxLabelType.Cpf,\n TaxLabelType.Cnpj,\n TaxLabelType.Psn,\n TaxLabelType.Tin,\n ].includes(taxLabelType);\n const label = shouldUppercaseTaxLabel\n ? taxLabelType.toUpperCase()\n : taxLabelType;\n\n // If the pattern is invalid, error\n if (!isValidTaxNumberPattern(type, taxCountryCode, taxId)) {\n return addCustomZodIssue(ctx, \"taxId\", CustomZodIssueCode.TaxFormat, {\n label,\n });\n }\n\n if (!validateTaxIdExternally) {\n return;\n }\n\n // Additionally validate tax id externally depending on environment\n const taxStatus = await validateTaxIdExternally({\n type,\n countryCode: country,\n province,\n taxId,\n });\n\n switch (taxStatus) {\n case ValidateTaxStatus.Invalid: {\n addCustomZodIssue(ctx, \"taxId\", CustomZodIssueCode.TaxInvalid, {\n label,\n });\n break;\n }\n\n case ValidateTaxStatus.Unavailable: {\n addCustomZodIssue(ctx, \"taxId\", CustomZodIssueCode.TaxUnavailable, {\n label,\n });\n break;\n }\n\n case ValidateTaxStatus.Valid:\n case ValidateTaxStatus.NotValidated:\n case null:\n case undefined: {\n // Do nothing;\n break;\n }\n\n default: {\n // Do nothing as well (but in a type safe way)\n checkUnreachable(taxStatus);\n }\n }\n };\n"],"names":["createTaxIdSuperRefinement","addressUsageType","validateTaxIdExternally","type","country","province","taxId","ctx","AddressUsageType","Billing","ReturnShipping","Shipping","assertUnreachable","taxCountryCode","getTaxCountry","hasTaxNumberPatternValidation","mapping","getValidationMappingByType","isOptional","addIssue","path","code","ZodIssueCode","too_small","minimum","inclusive","taxLabelType","getTaxLabelTypeByCountry","addressType","shouldUppercaseTaxLabel","TaxLabelType","Gst","Vat","Cpf","Cnpj","Psn","Tin","includes","label","toUpperCase","isValidTaxNumberPattern","addCustomZodIssue","CustomZodIssueCode","TaxFormat","taxStatus","countryCode","ValidateTaxStatus","Invalid","TaxInvalid","Unavailable","TaxUnavailable","Valid","NotValidated","checkUnreachable"],"mappings":";;;;;;;;;AAuBO,MAAMA,6BACXA,CAAC;EACCC,gBAAA;EACAC;AACF,CAAA,KASA,OAAO;EAAEC,IAAA;EAAMC;EAASC,QAAA;EAAUC;AAAA,GAASC,GAAA,KAAQ;EACjD,QAAQN,gBAAA;IAAkB;IAExB,KAAKO,gBAAA,CAAiBC,OAAA;MACpB;IAAA;IAEF,KAAKD,gBAAA,CAAiBE,cAAA;IACtB,KAAKF,gBAAA,CAAiBG,QAAA;MACpB;IACF;MACEC,iBAAA,CAAkBX,gBAAgB,CAAA;EAAA;EAGtC,MAAMY,iBAAiBC,aAAA,CAAc;IACnCV,OAAA;IACAC;EAAA,CACD,CAAA;EAGD,IAAI,CAACU,6BAAA,CAA8BZ,IAAA,EAAMU,cAAc,CAAA,EAAG;IACxD;EACF;EAEA,MAAMG,OAAA,GAAUC,2BAA2Bd,IAAI,CAAA;EAC/C,MAAM;IAAEe;EAAA,CAAW,GAAIF,OAAA,CAAQH,cAAc,CAAA;EAE7C,IAAI,CAACP,SAASY,UAAA,EAAY;IACxB;EACF;EAGA,IAAI,CAACZ,KAAA,EAAO;IACV,OAAOC,IAAIY,QAAA,CAAS;MAClBC,IAAA,EAAM,CAAC,OAAO,CAAA;MACdC,MAAMC,YAAA,CAAaC,SAAA;MACnBpB,IAAA,EAAM,QAAA;MACNqB,OAAA,EAAS,CAAA;MACTC,SAAA,EAAW;IAAA,CACZ,CAAA;EACH;EAEA,MAAMC,YAAA,GAAeC,yBAAyBvB,OAAA,EAAS;IACrDD,IAAA,EAAM,UAAA;IACNyB,WAA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