@shopify/address
Advanced tools
Comparing version 4.0.8 to 4.1.0
@@ -27,13 +27,22 @@ 'use strict'; | ||
async getCountry(countryCode) { | ||
const country = this.loadCountryFromCache(countryCode); | ||
async getCountry(countryCode, { | ||
includeHiddenZones = false | ||
} = {}) { | ||
const country = this.loadCountryFromCache(countryCode, includeHiddenZones); | ||
if (country) return country; | ||
return loader.loadCountry(this.locale, countryCode); | ||
return loader.loadCountry(this.locale, countryCode, { | ||
includeHiddenZones | ||
}); | ||
} | ||
async getCountries() { | ||
const cachedCountries = ORDERED_COUNTRIES_CACHE.get(this.locale); | ||
async getCountries({ | ||
includeHiddenZones = false | ||
} = {}) { | ||
const cacheKey = this.cacheKey(this.locale, includeHiddenZones); | ||
const cachedCountries = ORDERED_COUNTRIES_CACHE.get(cacheKey); | ||
if (cachedCountries) return cachedCountries; | ||
const countries = await loader.loadCountries(this.locale); | ||
ORDERED_COUNTRIES_CACHE.set(this.locale, countries); | ||
const countries = await loader.loadCountries(this.locale, { | ||
includeHiddenZones | ||
}); | ||
ORDERED_COUNTRIES_CACHE.set(cacheKey, countries); | ||
return countries; | ||
@@ -78,4 +87,9 @@ } | ||
loadCountryFromCache(countryCode) { | ||
const cachedCountries = ORDERED_COUNTRIES_CACHE.get(this.locale); | ||
cacheKey(locale, includeHiddenZones) { | ||
/* Cache list of countries per locale, both with and without hidden zones included */ | ||
return `${locale}-${includeHiddenZones}`; | ||
} | ||
loadCountryFromCache(countryCode, includeHiddenZones) { | ||
const cachedCountries = ORDERED_COUNTRIES_CACHE.get(this.cacheKey(this.locale, includeHiddenZones)); | ||
if (!cachedCountries) return null; | ||
@@ -82,0 +96,0 @@ return cachedCountries.find(({ |
@@ -8,3 +8,5 @@ 'use strict'; | ||
const loadCountries = memoizeAsync(async locale => { | ||
const loadCountries = memoizeAsync(async (locale, { | ||
includeHiddenZones = false | ||
} = {}) => { | ||
const response = await fetch(addressConsts.GRAPHQL_ENDPOINT, { | ||
@@ -17,3 +19,4 @@ method: 'POST', | ||
variables: { | ||
locale: locale.replace(/-/, '_').toUpperCase() | ||
locale: locale.replace(/-/, '_').toUpperCase(), | ||
includeHiddenZones | ||
} | ||
@@ -30,3 +33,5 @@ }) | ||
}); | ||
const loadCountry = memoizeAsync(async (locale, countryCode) => { | ||
const loadCountry = memoizeAsync(async (locale, countryCode, { | ||
includeHiddenZones = false | ||
} = {}) => { | ||
const response = await fetch(addressConsts.GRAPHQL_ENDPOINT, { | ||
@@ -40,3 +45,4 @@ method: 'POST', | ||
countryCode, | ||
locale: locale.replace(/-/, '_').toUpperCase() | ||
locale: locale.replace(/-/, '_').toUpperCase(), | ||
includeHiddenZones | ||
} | ||
@@ -43,0 +49,0 @@ }) |
@@ -11,8 +11,13 @@ import type { Address, FieldName, Country } from '@shopify/address-consts'; | ||
updateLocale(locale: string): void; | ||
getCountry(countryCode: string): Promise<Country>; | ||
getCountries(): Promise<Country[]>; | ||
getCountry(countryCode: string, { includeHiddenZones }?: { | ||
includeHiddenZones?: boolean | undefined; | ||
}): Promise<Country>; | ||
getCountries({ includeHiddenZones }?: { | ||
includeHiddenZones?: boolean | undefined; | ||
}): Promise<Country[]>; | ||
format(address: Address): Promise<string[]>; | ||
getOrderedFields(countryCode: string): Promise<FieldName[][]>; | ||
private cacheKey; | ||
private loadCountryFromCache; | ||
} | ||
//# sourceMappingURL=AddressFormatter.d.ts.map |
import type { Country, ResponseError } from '@shopify/address-consts'; | ||
export declare const loadCountries: (locale: string) => Promise<Country[]>; | ||
export declare const loadCountry: (locale: string, countryCode: string) => Promise<Country>; | ||
export declare const loadCountries: (locale: string, options?: { | ||
includeHiddenZones?: boolean; | ||
}) => Promise<Country[]>; | ||
export declare const loadCountry: (locale: string, countryCode: string, options?: { | ||
includeHiddenZones?: boolean; | ||
}) => Promise<Country>; | ||
export declare class CountryLoaderError extends Error { | ||
@@ -5,0 +9,0 @@ constructor(errors: ResponseError); |
{ | ||
"name": "@shopify/address", | ||
"version": "4.0.8", | ||
"version": "4.1.0", | ||
"license": "MIT", | ||
@@ -26,3 +26,3 @@ "description": "Address utilities for formatting addresses", | ||
"devDependencies": { | ||
"@shopify/address-mocks": "^3.1.7", | ||
"@shopify/address-mocks": "^3.2.0", | ||
"@shopify/jest-dom-mocks": "^4.1.2" | ||
@@ -29,0 +29,0 @@ }, |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
39836
746