load-google-maps-api
Advanced tools
Comparing version 0.0.3 to 1.0.0
77
index.js
@@ -1,55 +0,48 @@ | ||
export default ({ | ||
client, | ||
key, | ||
language, | ||
libraries = [], | ||
region, | ||
timeout = 10000, | ||
v | ||
} = {}) => { | ||
var CALLBACK_NAME = '__googleMapsApiOnLoadCallback' | ||
const callbackName = '__googleMapsApiOnLoadCallback'; | ||
var OPTIONS_KEYS = ['client', 'key', 'language', 'region', 'v'] | ||
return new Promise((resolve, reject) => { | ||
module.exports = function(options) { | ||
options = options || {} | ||
return new Promise(function(resolve, reject) { | ||
// Exit if not running inside a browser. | ||
if (typeof window === 'undefined') { | ||
return reject(new Error('Can only load the Google Maps API in the browser')); | ||
return reject( | ||
new Error('Can only load the Google Maps API in the browser') | ||
) | ||
} | ||
// Prepare the `script` tag to be inserted into the page. | ||
const scriptElement = document.createElement('script'); | ||
const params = [`callback=${callbackName}`]; | ||
if (client) params.push(`client=${client}`); | ||
if (key) params.push(`key=${key}`); | ||
if (language) params.push(`language=${language}`); | ||
libraries = [].concat(libraries); // Ensure that `libraries` is an array | ||
if (libraries.length) params.push(`libraries=${libraries.join(',')}`); | ||
if (region) params.push(`region=${region}`); | ||
if (v) params.push(`v=${v}`); | ||
scriptElement.src = `https://maps.googleapis.com/maps/api/js?${params.join('&')}`; | ||
// Reject the promise after a timeout. | ||
var timeoutId = setTimeout(function() { | ||
window[CALLBACK_NAME] = function() {} // Set the on load callback to a no-op. | ||
reject(new Error('Could not load the Google Maps API')) | ||
}, options.timeout || 10000) | ||
// Timeout if necessary. | ||
let timeoutId = null; | ||
if (timeout) { | ||
timeoutId = setTimeout(() => { | ||
window[callbackName] = () => {}; // Set the on load callback to a no-op. | ||
reject(new Error('Could not load the Google Maps API')); | ||
}, timeout); | ||
} | ||
// Hook up the on load callback. | ||
window[callbackName] = () => { | ||
window[CALLBACK_NAME] = function() { | ||
if (timeoutId !== null) { | ||
clearTimeout(timeoutId); | ||
clearTimeout(timeoutId) | ||
} | ||
resolve(window.google.maps); | ||
delete window[callbackName]; | ||
}; | ||
resolve(window.google.maps) | ||
delete window[CALLBACK_NAME] | ||
} | ||
// Prepare the `script` tag to be inserted into the page. | ||
var scriptElement = document.createElement('script') | ||
var params = ['callback=' + CALLBACK_NAME] | ||
OPTIONS_KEYS.forEach(function(key) { | ||
if (options[key]) { | ||
params.push(key + '=' + options[key]) | ||
} | ||
}) | ||
if (options.libraries && options.libraries.length) { | ||
params.push('libraries=' + options.libraries.join(',')) | ||
} | ||
scriptElement.src = | ||
'https://maps.googleapis.com/maps/api/js?' + params.join('&') | ||
// Insert the `script` tag. | ||
document.body.appendChild(scriptElement); | ||
}); | ||
}; | ||
document.body.appendChild(scriptElement) | ||
}) | ||
} |
The MIT License (MIT) | ||
Copyright (c) 2016 Lim Yuan Qing | ||
Copyright (c) 2017 Lim Yuan Qing | ||
@@ -5,0 +5,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy |
{ | ||
"name": "load-google-maps-api", | ||
"version": "0.0.3", | ||
"version": "1.0.0", | ||
"description": "A thin, Promise-returning helper for loading the Google Maps JavaScript API.", | ||
@@ -11,28 +11,12 @@ "author": "Lim Yuan Qing", | ||
}, | ||
"main": "lib", | ||
"devDependencies": { | ||
"babel-cli": "^6.3.17", | ||
"babel-core": "^6.3.26", | ||
"babel-eslint": "^5.0.0-beta6", | ||
"babel-preset-es2015": "^6.3.13", | ||
"babelify": "^7.2.0", | ||
"eslint": "^1.10.3", | ||
"karma": "^0.13.16", | ||
"karma-browserify": "^4.4.2", | ||
"karma-chrome-launcher": "^0.2.2", | ||
"karma-cli": "0.1.2", | ||
"karma-phantomjs-launcher": "^0.2.2", | ||
"karma-tap": "^1.0.3", | ||
"karma-tape-reporter": "^1.0.3", | ||
"phantomjs": "^1.9.19", | ||
"rimraf": "^2.5.0", | ||
"tape": "^4.4.0" | ||
"browserify": "^14.3.0", | ||
"prettier": "^1.2.2", | ||
"tape": "^4.6.3", | ||
"tape-run": "^3.0.0" | ||
}, | ||
"scripts": { | ||
"build": "babel index.js --out-dir lib", | ||
"clean": "rimraf lib", | ||
"lint": "eslint index.js karma.conf.js", | ||
"prepublish": "npm run clean && npm run build", | ||
"test": "karma start karma.conf.js --single-run" | ||
"lint": "prettier *.js --no-semi --single-quote --write", | ||
"test": "browserify test.js | tape-run" | ||
} | ||
} |
@@ -8,9 +8,9 @@ # load-google-maps-api [![npm Version](http://img.shields.io/npm/v/load-google-maps-api.svg?style=flat)](https://www.npmjs.com/package/load-google-maps-api) [![Build Status](https://img.shields.io/travis/yuanqing/load-google-maps-api.svg?branch=master&style=flat)](https://travis-ci.org/yuanqing/load-google-maps-api) | ||
```js | ||
import loadGoogleMapsAPI from 'load-google-maps-api'; | ||
const loadGoogleMapsAPI = require('load-google-maps-api') | ||
loadGoogleMapsAPI().then((googleMaps) => { | ||
console.log(googleMaps); //=> Object { Animation: Object, ... | ||
loadGoogleMapsAPI().then(function(googleMaps) { | ||
console.log(googleMaps) //=> Object { Animation: Object, ... | ||
}).catch((err) => { | ||
console.error(err); | ||
}); | ||
console.error(err) | ||
}) | ||
``` | ||
@@ -40,3 +40,3 @@ | ||
```js | ||
import loadGoogleMapsAPI from 'load-google-maps-api'; | ||
const loadGoogleMapsAPI = require('load-google-maps-api') | ||
``` | ||
@@ -65,5 +65,11 @@ | ||
Install via [npm](https://npmjs.com): | ||
Install via [yarn](https://yarnpkg.com): | ||
```bash | ||
$ yarn add load-google-maps-api | ||
``` | ||
Or [npm](https://npmjs.com): | ||
```bash | ||
$ npm install --save load-google-maps-api | ||
@@ -70,0 +76,0 @@ ``` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
78124
4
1
78
8
47
1