Skip to content

Commit

Permalink
properly pass through api key (#278)
Browse files Browse the repository at this point in the history
* properly pass through api key

* added tests and changelog for token/apikey issue

* proper naming and formatting

* Update src/Tasks/Geocode.js
  • Loading branch information
gavinr committed Jul 29, 2021
1 parent 7f6f3a2 commit a7982cc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,10 @@

## [Upcoming changes][Unreleased]

### Fixed

- Properly pass through token and apikey when calling `L.esri.Geocoding.geocode()` ([#278](https://github.com/Esri/esri-leaflet-geocoder/pull/278))

## [3.1.0] - 2021-07-20

### Fixed
Expand Down
20 changes: 20 additions & 0 deletions spec/Tasks/GeocodeSpec.js
Expand Up @@ -333,5 +333,25 @@ describe('L.esri.Geocode', function () {

request.respond(200, { 'Content-Type': 'text/plain; charset=utf-8' }, sampleFindAddressCandidatesResponse);
});

it('should pass through a token', function (done) {
var request = L.esri.Geocoding.geocode({token: 'testToken'}).text('380 New York St, Redlands, California, 92373').run(function (err, response) {
done();
});

expect(request.url).to.contain('token=testToken');

request.respond(200, { 'Content-Type': 'text/plain; charset=utf-8' }, sampleFindAddressCandidatesResponse);
});

it('should pass through an apikey', function (done) {
var request = L.esri.Geocoding.geocode({apikey: 'testApiKey'}).text('380 New York St, Redlands, California, 92373').run(function (err, response) {
done();
});

expect(request.url).to.contain('token=testApiKey');

request.respond(200, { 'Content-Type': 'text/plain; charset=utf-8' }, sampleFindAddressCandidatesResponse);
});
});
/* eslint-disable handle-callback-err */
7 changes: 7 additions & 0 deletions src/Tasks/Geocode.js
Expand Up @@ -26,6 +26,7 @@ export var Geocode = Task.extend({
'text': 'singleLine',
'category': 'category',
'token': 'token',
'apikey': 'apikey',
'key': 'magicKey',
'fields': 'outFields',
'forStorage': 'forStorage',
Expand Down Expand Up @@ -56,6 +57,12 @@ export var Geocode = Task.extend({
},

run: function (callback, context) {
if (this.options.token) {
this.params.token = this.options.token;
}
if (this.options.apikey) {
this.params.token = this.options.apikey;
}
if (this.options.customParam) {
this.params[this.options.customParam] = this.params.singleLine;
delete this.params.singleLine;
Expand Down

0 comments on commit a7982cc

Please sign in to comment.