diff --git a/lib/index.d.ts b/lib/index.d.ts new file mode 100644 index 0000000..8f1534b --- /dev/null +++ b/lib/index.d.ts @@ -0,0 +1,16 @@ +// Type definitions for react-geocode 0.2.1 +// Project: https://github.com/shukerullah/react-geocode/ +// Definitions by: Mike Pouncy + +declare module 'react-geocode' { + const reactGeocode: { + setApiKey(apiKey: string): void; + setLanguage(language: string): void; + setRegion(region: string): void; + enableDebug(flag?: boolean): void; + fromLatLng(lat: string, lng: string, apiKey?: string, language?: string, region?: string): Promise; + fromAddress(address: string, apiKey?: string, language?: string, region?: string): Promise; + }; + + export = reactGeocode; +} \ No newline at end of file diff --git a/package.json b/package.json index c1665ec..137c778 100644 --- a/package.json +++ b/package.json @@ -3,6 +3,7 @@ "version": "0.2.1", "description": "A module to transform a description of a location (i.e. street address, town name, etc.) into geographic coordinates (i.e. latitude and longitude) and vice versa.", "main": "./lib/index.js", + "types": "./lib/index.d.ts", "repository": { "type": "git", "url": "git+https://github.com/shukerullah/react-geocode.git" diff --git a/src/index.js b/src/index.js index c4f61f6..695f669 100644 --- a/src/index.js +++ b/src/index.js @@ -4,12 +4,22 @@ * @package react-geocode * @author Pir Shukarulalh Shah (http://www.shukarullah.com) */ + +/** @type {boolean | null} */ let DEBUG = false; +/** @type {string | null} */ let API_KEY = null; +/** @type {string | null} */ let LANGUAGE = "en"; +/** @type {string | number | boolean | null} */ let REGION = null; +/** @type {string | null} */ const GOOGLE_API = "https://maps.google.com/maps/api/geocode/json"; +/** + * @param {string} message + * @param {boolean} warn + */ function log(message, warn = false) { if (DEBUG) { if (warn) { @@ -20,6 +30,10 @@ function log(message, warn = false) { } } +/** + * @param {RequestInfo} url + * @returns {Promise} + */ async function handleUrl(url) { const response = await fetch(url).catch(() => Promise.reject(new Error("Error fetching data")) @@ -45,7 +59,7 @@ async function handleUrl(url) { ); } -export default { +const reactGeocode = { /** * * @@ -89,8 +103,8 @@ export default { * @param {string} lng * @param {string} [apiKey] * @param {string} [language] - * @param {string} [region] - * @returns {Promise} + * @param {string | null} [region] + * @returns {Promise} */ async fromLatLng(lat, lng, apiKey, language, region) { if (!lat || !lng) { @@ -113,6 +127,7 @@ export default { if (region || REGION) { REGION = region || REGION; + // @ts-ignore url += `®ion=${encodeURIComponent(REGION)}`; } @@ -125,8 +140,8 @@ export default { * @param {string} address * @param {string} [apiKey] * @param {string} [language] - * @param {string} [region] - * @returns {Promise} + * @param {string | null} [region] + * @returns {Promise} */ async fromAddress(address, apiKey, language, region) { if (!address) { @@ -148,9 +163,12 @@ export default { if (region || REGION) { REGION = region || REGION; + // @ts-ignore url += `®ion=${encodeURIComponent(REGION)}`; } return handleUrl(url); } }; + +export default reactGeocode; diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..4c41a2e --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,15 @@ +{ + "compilerOptions": { /* Enable incremental compilation */ + "target": "ES5" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */, + "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */, + "lib": [ "ES2015", "DOM" ] /* Specify library files to be included in the compilation. */, + "allowJs": true /* Allow javascript files to be compiled. */, + "checkJs": true /* Report errors in .js files. */, + "declaration": true /* Generates corresponding '.d.ts' file. */, + "noEmit": false /* Do not emit outputs. */, + "strict": true /* Enable all strict type-checking options. */, + "esModuleInterop": true /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */, + "forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */, + "outDir": "lib/" + } +}