From b6b3d10cc514f78f1ee4b20d582429191a89e924 Mon Sep 17 00:00:00 2001 From: Adewale Abdulrazaq Date: Sun, 14 Apr 2024 00:59:43 +0100 Subject: [PATCH] Fix: Error: Could not dynamically require "../data/countries/ad.json". Please configure the dynamicRequireTargets or/and ignoreDynamicRequires option --- README.md | 4 ++-- package.json | 2 +- src/helpers/CountriesAtlas.ts | 25 ++++++++++++++++--------- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 5627793..d60e9a3 100644 --- a/README.md +++ b/README.md @@ -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", @@ -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", diff --git a/package.json b/package.json index acfaf48..89d520c 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/helpers/CountriesAtlas.ts b/src/helpers/CountriesAtlas.ts index ce33bfc..4c709fc 100644 --- a/src/helpers/CountriesAtlas.ts +++ b/src/helpers/CountriesAtlas.ts @@ -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 { 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 | undefined { @@ -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 { 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; }