feat!: type marketplace lookups with literal unions - #507
Conversation
Move the marketplace data from marketplaces.json into a .ts module declared `as const satisfies readonly Marketplace[]`, so the literal codes, ids, regions and domains survive as types. Derive and export MarketplaceCode, MarketplaceId, MarketplaceRegion and MarketplaceDomain from it. Overload each lookup: a known literal (or a value typed with one of the unions) returns `Marketplace`, an arbitrary string returns `Marketplace | undefined`. Codes accept their uppercase form, domains accept uppercase and a `www.` prefix. MarketplaceCode and MarketplaceDomain only cover the marketplaces served by an Amazon storefront; the multi-channel, invoicing and Amazon Pay entries have no domain. Type-level assertions live in tests/index.test-d.ts, checked by tests/tsconfig.json. Tests now pull globals from @jest/globals instead of @types/jest. BREAKING CHANGE: `marketplaces` is now typed `readonly Marketplace[]`; callers that mutate it (sort, push) or pass it where `Marketplace[]` is expected must copy it first with `[...marketplaces]`.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #507 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 1 2 +1
Lines 11 12 +1
Branches 1 1
=========================================
+ Hits 11 12 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
This PR strengthens the TypeScript surface of @bizon/amazon-ids by moving marketplace data to a as const TypeScript module and deriving literal unions from it, enabling lookup overloads to return Marketplace for known literals/union-typed inputs while keeping Marketplace | undefined for arbitrary string.
Changes:
- Migrate
src/marketplaces.jsontosrc/marketplaces.tsand derive/exportMarketplaceCode,MarketplaceId,MarketplaceRegion, andMarketplaceDomain. - Add overloads to
getMarketplaceById/getMarketplaceByCode/getMarketplaceByDomainfor precise return types on known inputs. - Add type-level assertion tests (
tests/index.test-d.ts) and update Jest tests to import globals from@jest/globals.
Reviewed changes
Copilot reviewed 10 out of 12 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
tsconfig.json |
Drops resolveJsonModule after moving marketplace data out of JSON. |
tests/tsconfig.json |
Adds a dedicated tsconfig for type-level test assertions. |
tests/index.test-d.ts |
Introduces compile-time assertions validating overload return types and exported unions. |
tests/index.spec.ts |
Imports Jest globals explicitly via @jest/globals. |
src/marketplaces.ts |
New TS data module with as const marketplaces data and derived union types. |
src/marketplaces.json |
Removes the legacy JSON marketplace dataset. |
src/index.ts |
Re-exports derived types/data and adds overloaded lookup signatures. |
README.md |
Documents the new overload behavior and the exported unions. |
package.json |
Runs an extra tsc pass for the test tsconfig; adds @jest/globals; removes @types/jest. |
pnpm-lock.yaml |
Lockfile updates reflecting dependency changes. |
CLAUDE.md |
Updates project structure/docs to reflect TS data + type-test additions. |
.github/dependabot.yml |
Adjusts Jest dependency grouping patterns to @jest/*. |
Files not reviewed (1)
- pnpm-lock.yaml: Generated file
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
🎉 This PR is included in version 6.0.0 🎉 The release is available on: Your semantic-release bot 📦🚀 |
What
Make the marketplace lookups return precise types: passing a known literal (or a value typed with one of the exported unions) now returns
Marketplace, while an arbitrarystringstill returnsMarketplace | undefined.How
src/marketplaces.json→src/marketplaces.ts, declaredas const satisfies readonly Marketplace[]. JSON module imports widen every field tostring, so no union could be derived from them; theas constkeeps the shape validated and preserves the literals.resolveJsonModuledropped from tsconfig.MarketplaceCode,MarketplaceId,MarketplaceRegion,MarketplaceDomainare derived from the data and re-exported fromindex.ts.Marketplaceand astringoverload returningMarketplace | undefined. Codes acceptUppercase<>; domains acceptUppercase<>and awww.prefix, mirroring the runtime normalization.MarketplaceCode/MarketplaceDomaincover only domain-backed marketplaces (23 of 34). The multi-channel (*-non-amazon), invoicing and Amazon Pay entries have no domain, so their codes aren't in the union and looking them up returnsMarketplace | undefined.MarketplaceIdstill spans all 34.tests/index.test-d.tsholds type-level assertions, checked by atests/tsconfig.json(Jest ignores.test-d.ts). Verified they actually fail when an assertion is wrong.@jest/globals— tests import their globals explicitly instead of relying on@types/jest, matchingselling-partner-api-sdk. Dependabot's stale@types/jestentry removed (@jest/*already covers@jest/globals).Breaking change
marketplacesis now typedreadonly Marketplace[]. Callers that mutate it (sort,push) or pass it whereMarketplace[]is expected must copy first:[...marketplaces]. This also stops the exported array from being mutated as shared library state.The narrowed return types are source-compatible — existing
?.just becomes redundant on known lookups.