Skip to content

Commit

Permalink
MP20200107 - Typescript update related to shukerullah#24
Browse files Browse the repository at this point in the history
  • Loading branch information
mpouncy-panda committed Jan 7, 2020
1 parent 49eeda3 commit b2d82bf
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 5 deletions.
16 changes: 16 additions & 0 deletions lib/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Type definitions for react-geocode 0.2.1
// Project: https://github.com/shukerullah/react-geocode/
// Definitions by: Mike Pouncy <https://github.com/pouncyisdead>

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<any>;
fromAddress(address: string, apiKey?: string, language?: string, region?: string): Promise<any>;
};

export = reactGeocode;
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
28 changes: 23 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,22 @@
* @package react-geocode
* @author Pir Shukarulalh Shah <[email protected]> (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) {
Expand All @@ -20,6 +30,10 @@ function log(message, warn = false) {
}
}

/**
* @param {RequestInfo} url
* @returns {Promise<any>}
*/
async function handleUrl(url) {
const response = await fetch(url).catch(() =>
Promise.reject(new Error("Error fetching data"))
Expand All @@ -45,7 +59,7 @@ async function handleUrl(url) {
);
}

export default {
const reactGeocode = {
/**
*
*
Expand Down Expand Up @@ -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<any>}
*/
async fromLatLng(lat, lng, apiKey, language, region) {
if (!lat || !lng) {
Expand All @@ -113,6 +127,7 @@ export default {

if (region || REGION) {
REGION = region || REGION;
// @ts-ignore
url += `&region=${encodeURIComponent(REGION)}`;
}

Expand All @@ -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<any>}
*/
async fromAddress(address, apiKey, language, region) {
if (!address) {
Expand All @@ -148,9 +163,12 @@ export default {

if (region || REGION) {
REGION = region || REGION;
// @ts-ignore
url += `&region=${encodeURIComponent(REGION)}`;
}

return handleUrl(url);
}
};

export default reactGeocode;
15 changes: 15 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -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/"
}
}

0 comments on commit b2d82bf

Please sign in to comment.