Skip to content

Commit

Permalink
Fix: Error: Could not dynamically require "../data/countries/ad.json"…
Browse files Browse the repository at this point in the history
…. Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option
  • Loading branch information
classyrazy committed Apr 13, 2024
1 parent b27178d commit b6b3d10
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ The `getStates()` method will return an array of states, which contains all the
- cities: The cities of the state.
```typescript
import { CountriesAtlas } from '@amplifiedhq/countries-atlas'
const states = CountriesAtlas.getStates('AD')
const states = await CountriesAtlas.getStates('AD')
// [
// {
// "name": "Andorra la Vella",
Expand All @@ -255,7 +255,7 @@ const states = CountriesAtlas.getStates('AD')
The `state()` method is used to get the state by the given `iso2` and `state_code` properties, it will return the state if it exists, otherwise it will return `undefined`. For example, if you want to get the state, you can do the following:
```typescript
import { CountriesAtlas } from '@amplifiedhq/countries-atlas'
const state = CountriesAtlas.state('AD', '07')
const state = await CountriesAtlas.state('AD', '07')
//{
// "name": "Andorra la Vella",
// "state_code": "07",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@amplifiedhq/countries-atlas",
"version": "1.1.2",
"version": "1.2.1",
"description": "Uncover the world with a single lightweight library - countries, codes, currencies, flags, languages, cities, and more 🌎",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
25 changes: 16 additions & 9 deletions src/helpers/CountriesAtlas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,17 @@ export class CountriesAtlas {
* @param {string} iso2 - ISO2 code of the country.
* @returns {State[] | undefined} - Array of state objects or undefined if not found.
*/
getStates(iso2: string): State[] | undefined {
async getStates(iso2: string): Promise<State[] | undefined> {
const country = this.find(iso2)
if (country) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const statesData = require(`../data/countries/${country.iso2?.toLowerCase()}.json`)
return statesData.states
try {
const statesData = await import(`../data/countries/${country.iso2?.toLowerCase()}.json`);
return statesData.states as State[];
} catch (err) {
return undefined;
}
}
return undefined
return undefined;
}

// state(iso2: string, stateCode: string): Promise<State | undefined> | undefined {
Expand All @@ -102,13 +105,17 @@ export class CountriesAtlas {
* @param {string} stateCode - State code of the state to find.
* @returns {State | undefined} - State object or undefined if not found.
*/
state(iso2: string, stateCode: string): State | undefined {
async state(iso2: string, stateCode: string): Promise<State | undefined> {
const country = this.find(iso2);
if (country) {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const statesData = require(`../data/countries/${country.iso2?.toLowerCase()}.json`) as StateData
const state = statesData.states.find((s: State) => s.state_code?.toUpperCase() === stateCode);
return state ? state : undefined;
try {
const statesData = await import(`../data/countries/${country.iso2?.toLowerCase()}.json`) as StateData;
const state = statesData.states.find((s: State) => s.state_code?.toUpperCase() === stateCode);
return state ? state : undefined;
} catch (err) {
return undefined;
}
}
return undefined;
}
Expand Down

0 comments on commit b6b3d10

Please sign in to comment.