Skip to content

feat!: type marketplace lookups with literal unions - #507

Merged
tusbar merged 1 commit into
masterfrom
tusbar/typed-marketplace-lookups
Jul 31, 2026
Merged

feat!: type marketplace lookups with literal unions#507
tusbar merged 1 commit into
masterfrom
tusbar/typed-marketplace-lookups

Conversation

@tusbar

@tusbar tusbar commented Jul 31, 2026

Copy link
Copy Markdown
Member

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 arbitrary string still returns Marketplace | undefined.

getMarketplaceByCode('fr').domain      // Marketplace — no optional chaining
getMarketplaceByCode(someString)?.domain // Marketplace | undefined

How

  • Data moved to TypeScriptsrc/marketplaces.jsonsrc/marketplaces.ts, declared as const satisfies readonly Marketplace[]. JSON module imports widen every field to string, so no union could be derived from them; the as const keeps the shape validated and preserves the literals. resolveJsonModule dropped from tsconfig.
  • Derived unionsMarketplaceCode, MarketplaceId, MarketplaceRegion, MarketplaceDomain are derived from the data and re-exported from index.ts.
  • Overloads — each lookup gets a literal overload returning Marketplace and a string overload returning Marketplace | undefined. Codes accept Uppercase<>; domains accept Uppercase<> and a www. prefix, mirroring the runtime normalization.
  • MarketplaceCode / MarketplaceDomain cover 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 returns Marketplace | undefined. MarketplaceId still spans all 34.
  • Type teststests/index.test-d.ts holds type-level assertions, checked by a tests/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, matching selling-partner-api-sdk. Dependabot's stale @types/jest entry removed (@jest/* already covers @jest/globals).

Breaking change

marketplaces is now typed readonly Marketplace[]. Callers that mutate it (sort, push) or pass it where Marketplace[] 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.

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]`.
Copilot AI review requested due to automatic review settings July 31, 2026 12:45
@codecov

codecov Bot commented Jul 31, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (e20e944) to head (a7fb2aa).
✅ All tests successful. No failed tests found.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.json to src/marketplaces.ts and derive/export MarketplaceCode, MarketplaceId, MarketplaceRegion, and MarketplaceDomain.
  • Add overloads to getMarketplaceById / getMarketplaceByCode / getMarketplaceByDomain for 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.

Comment thread src/index.ts
Comment thread tests/index.test-d.ts
@tusbar
tusbar merged commit dd5cb8f into master Jul 31, 2026
7 checks passed
@tusbar
tusbar deleted the tusbar/typed-marketplace-lookups branch July 31, 2026 12:50
@bizon-bot

Copy link
Copy Markdown
Member

🎉 This PR is included in version 6.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

3 participants