geocodio-library-node 1.3.3 → 1.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # geocod.io Node library [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url]
1
+ # geocod.io Node library [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url]
2
2
  > Library for performing forward and reverse address geocoding for addresses or coordinates in the US and Canada.
3
3
 
4
4
  <!-- toc -->
@@ -10,6 +10,12 @@
10
10
  * [Field appends](#field-appends)
11
11
  * [Address components](#address-components)
12
12
  * [Limit results](#limit-results)
13
+ * [Lists](#lists)
14
+ * [Create A List](#create-a-list)
15
+ * [Get List Status](#get-list-status)
16
+ * [Get All Lists](#get-all-lists)
17
+ * [Download A List](#download-a-list)
18
+ * [Delete A List](#delete-a-list)
13
19
  - [Testing](#testing)
14
20
  - [Changelog](#changelog)
15
21
  - [Security](#security)
@@ -225,6 +231,68 @@ geocoder.reverse('38.9002898,-76.9990361', ['timezone'], 5)
225
231
  .catch(err => { ... });
226
232
  ```
227
233
 
234
+ ### Lists
235
+
236
+ List methods are nested within `.list`. To access list methods, be sure to to run `geocoder.list` and then include the task method you would like to utilize.
237
+
238
+ #### Create A List
239
+
240
+ To create and upload a new list using an existing .CSV file, run `geocoder.list.create(...)` and pass in your filename/file path.
241
+
242
+ You may also need to pass in some additional parameters:
243
+ * Direction: Use the default string `"forward"`.
244
+ * Format: Use the default string `"{{A}} {{B}} {{C}} {{D}}"`
245
+ * Callback: A callback URL.
246
+
247
+ ```javascript
248
+ geocoder.list.create(
249
+ `${__dirname}/stubs/sample_list.csv`,
250
+ "forward",
251
+ "{{A}} {{B}} {{C}} {{D}}",
252
+ "https://example.com/my-callback"
253
+ )
254
+ ```
255
+
256
+ #### Get List Status
257
+
258
+ To retrieve the current status of your list, pass your list's ID into `geocoder.list.status(...)`.
259
+
260
+ ```javascript
261
+ geocoder.list.status(1234567)
262
+ .then(response => { ... })
263
+ .catch(err => { ... });
264
+ ```
265
+
266
+ #### Get All Lists
267
+
268
+ To retrieve all available lists, run `geocoder.list.all()`. You do not need to pass anything into this function.
269
+
270
+ ```javascript
271
+ geocoder.list.all()
272
+ .then(response => { ... })
273
+ .catch(err => { ... });
274
+ ```
275
+
276
+ #### Download A List
277
+
278
+ To download a list, run `geocoder.list.download(...)` and pass in the ID of the list you'd like to download, as well as a string that includes a filename. Be sure to include a `.csv` file extension.
279
+
280
+ ```javascript
281
+ geocoder.list.download(1234567, "geocoded_file.csv")
282
+ .then(response => { ...})
283
+ .catch(err => { ... });
284
+ ```
285
+
286
+ #### Delete A List
287
+
288
+ To delete a list, run `geocoder.list.deleteList(...)` and pass in the ID of the list you'd like to delete.
289
+
290
+ ```javascript
291
+ geocoder.list.delete(1234567)
292
+ .then(response => { ... })
293
+ .catch(err => { ... });
294
+ ```
295
+
228
296
  ## Testing
229
297
 
230
298
  ```bash
@@ -247,5 +315,3 @@ The MIT License (MIT). Please see [License File](LICENSE.md) for more informatio
247
315
  [npm-url]: https://npmjs.org/package/geocodio-library-node
248
316
  [travis-image]: https://travis-ci.org/Geocodio/geocodio-library-node.svg?branch=master
249
317
  [travis-url]: https://travis-ci.org/Geocodio/geocodio-library-node
250
- [daviddm-image]: https://david-dm.org/Geocodio/geocodio-library-node.svg?theme=shields.io
251
- [daviddm-url]: https://david-dm.org/Geocodio/geocodio-library-node
@@ -200,4 +200,40 @@ describe("geocoder", () => {
200
200
  expect(response.results.length).toEqual(1);
201
201
  });
202
202
  });
203
+
204
+ // LIST TESTS
205
+
206
+ it("Can create a new list, check status, download list and delete list", () => {
207
+ expect.assertions(1);
208
+
209
+ // Upload and create list
210
+ let newList = geocoder.list
211
+ .create(
212
+ `${__dirname}/stubs/sample_list.csv`,
213
+ "forward",
214
+ "{{A}} {{B}} {{C}} {{D}}",
215
+ "https://example.com/my-callback"
216
+ )
217
+ .then(response => {
218
+ expect(response.file.filename).toEqual("sample_list.csv");
219
+
220
+ geocoder.list.status(response.id).then(
221
+ setTimeout(resp => {
222
+ expect(resp.id).toEqual(response.id);
223
+
224
+ geocoder.list
225
+ .download(response.id, "geocoded_file.csv")
226
+ .then(resp => {
227
+ expect(resp.file.filename).toEqual("geocoded_file.csv");
228
+
229
+ geocoder.list.delete(response.id).then(resp => {
230
+ expect(resp.id).toEqual(null);
231
+ });
232
+ });
233
+ }, 3000)
234
+ );
235
+ });
236
+
237
+ return newList;
238
+ });
203
239
  });
@@ -0,0 +1,25 @@
1
+ address,city,state,zip
2
+ 660 Pennsylvania Ave SE,Washington,DC,20003
3
+ 1718 14th St NW,Washington,DC,20009
4
+ 1309 5th St NE,,,20002
5
+ 2150 P St NW,,,20037
6
+ 201 F Street NE,,,20002
7
+ 1001 2nd St SE,,,20003
8
+ 1645 Wisconsin Avenue NW,Washington,DC,20007
9
+ 820 East Baltimore Street,Baltimore,MD,21202
10
+ 800 F St NW,Washington,DC,20001
11
+ 700 Constitution Avenue NW,Washington,DC,20565
12
+ 1123 Pennsylvania Ave SE,Washington,DC,20003
13
+ 621 Pennsylvania Ave SE,Washington,DC,20003
14
+ 1702 G Street NW,Washington,DC,20006
15
+ 701 8th St SE,Washington,DC,20003
16
+ 12187 Darnestown Rd,Gaithersburg,MD,20878
17
+ 4961 Elm Street,Bethesda,MD,
18
+ 3064 Mount Pleasant St NW,Washington,DC,
19
+ 1052 Thomas Jefferson Street NW,Washington,DC,
20
+ 475 H St NW,Washington,DC,
21
+ 1301 U St NW,Washington,DC,
22
+ "1726 20th Street, NW",Washington,DC,
23
+ "1916 I Street, NW",Washington,DC,
24
+ 107 Church St NE,Vienna,VA,
25
+ 4817 Bethesda Ave,Bethesda,MD,20814
package/lib/index.js CHANGED
@@ -1,13 +1,15 @@
1
1
  "use strict";
2
2
 
3
3
  const axios = require("axios");
4
+ const fs = require("fs");
5
+ const FormData = require("form-data");
4
6
 
5
7
  class Geocodio {
6
8
  constructor(apiKey, hostname, apiVersion) {
7
9
  this.apiKey = apiKey || process.env.GEOCODIO_API_KEY || null;
8
10
  this.hostname =
9
11
  hostname || process.env.GEOCODIO_HOSTNAME || "api.geocod.io";
10
- this.apiVersion = apiVersion || process.env.GEOCODIO_API_VERSION || "v1.6";
12
+ this.apiVersion = apiVersion || process.env.GEOCODIO_API_VERSION || "v1.7";
11
13
 
12
14
  this.SINGLE_TIMEOUT_MS = 5000;
13
15
  this.BATCH_TIMEOUT_MS = 30 * 60 * 1000;
@@ -23,6 +25,8 @@ class Geocodio {
23
25
  "postal_code",
24
26
  "country"
25
27
  ];
28
+
29
+ this.list = this.list();
26
30
  }
27
31
 
28
32
  geocode(query, fields = [], limit = null) {
@@ -55,11 +59,16 @@ class Geocodio {
55
59
  response = this.performBatchRequest(url, query, queryParameters);
56
60
  }
57
61
 
62
+ return this.handleResponse(response);
63
+ }
64
+
65
+ handleResponse(response) {
58
66
  return response
59
67
  .then(response => response.data)
60
68
  .catch(error => {
61
69
  if (error.response) {
62
- const errorMessage = error.response.data.error || "Error";
70
+ const errorMessage =
71
+ error.response.data.message || error.response.data.error || "Error";
63
72
  const code = error.response.status || 0;
64
73
 
65
74
  const decoratedError = new Error(errorMessage);
@@ -144,6 +153,94 @@ class Geocodio {
144
153
  isNumeric(n) {
145
154
  return !isNaN(parseFloat(n)) && isFinite(n);
146
155
  }
156
+
157
+ list() {
158
+ const create = (filename, direction, format, callback) => {
159
+ const form = new FormData();
160
+ form.append("file", fs.createReadStream(filename));
161
+
162
+ return this.handleResponse(
163
+ axios.post(this.formatUrl("lists"), form, {
164
+ params: {
165
+ api_key: this.apiKey,
166
+ direction,
167
+ format,
168
+ callback
169
+ },
170
+ headers: form.getHeaders()
171
+ })
172
+ );
173
+ };
174
+
175
+ const status = listId => {
176
+ return this.handleResponse(
177
+ axios.get(this.formatUrl(`lists/${listId}`), {
178
+ params: {
179
+ api_key: this.apiKey
180
+ }
181
+ })
182
+ );
183
+ };
184
+
185
+ const all = () => {
186
+ return this.handleRequest(
187
+ axios.get(this.formatUrl(`lists`), {
188
+ params: {
189
+ api_key: this.apiKey
190
+ }
191
+ })
192
+ );
193
+ };
194
+
195
+ const download = (listId, output) => {
196
+ const writer = fs.createWriteStream(output);
197
+
198
+ return this.handleRequest(
199
+ axios({
200
+ method: "get",
201
+ url: this.formatUrl(`lists/${listId}/download`),
202
+ params: {
203
+ api_key: this.apiKey
204
+ },
205
+ responseType: "stream"
206
+ }).then(response => {
207
+ return new Promise((resolve, reject) => {
208
+ response.data.pipe(writer);
209
+ let error = null;
210
+ writer.on("error", err => {
211
+ error = err;
212
+ writer.close();
213
+ reject(err);
214
+ });
215
+ writer.on("close", () => {
216
+ if (!error) {
217
+ resolve(true);
218
+ }
219
+ });
220
+ });
221
+ })
222
+ );
223
+ };
224
+
225
+ const deleteList = listId => {
226
+ return this.handleRequest(
227
+ axios.delete(this.formatUrl(`lists/${listId}`), {
228
+ params: {
229
+ api_key: this.apiKey
230
+ }
231
+ })
232
+ );
233
+ };
234
+
235
+ return {
236
+ create,
237
+ status,
238
+ all,
239
+ download,
240
+ deleteList, // Kept for backwards compatibility
241
+ delete: deleteList
242
+ };
243
+ }
147
244
  }
148
245
 
149
246
  module.exports = Geocodio;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "geocodio-library-node",
3
- "version": "1.3.3",
3
+ "version": "1.5.0",
4
4
  "description": "geocod.io geocoding API library",
5
5
  "homepage": "https://github.com/Geocodio/geocodio-library-node",
6
6
  "author": {
@@ -73,6 +73,7 @@
73
73
  },
74
74
  "license": "MIT",
75
75
  "dependencies": {
76
- "axios": "^0.21.1"
76
+ "axios": "^0.27.2",
77
+ "form-data": "^4.0.0"
77
78
  }
78
79
  }
package/CHANGELOG.md DELETED
@@ -1,32 +0,0 @@
1
- # Changelog
2
-
3
- All notable changes to `geocodio-library-node` will be documented in this file
4
-
5
- ## 1.3.3 - 2021-04-16
6
- ## 1.3.2
7
-
8
- - Updated dependencies and fixed package.json configuration error
9
-
10
- ## 1.3.1 - 2021-01-05
11
-
12
- - Updated dependencies
13
-
14
- ## 1.3.0 - 2020-06-03
15
-
16
- - Added support for Geocodio API v1.6
17
-
18
- ## 1.2.1 - 2020-05-14
19
-
20
- - Updated npm dependencies
21
-
22
- ## 1.2.0 - 2020-05-14
23
-
24
- - Added support for Geocodio API v1.5
25
-
26
- ## 1.1.0 - 2020-02-24
27
-
28
- **Fix:** Fixed issue where batch requests did not allow custom keys
29
-
30
- ## 1.0.0 - 2019-09-19
31
-
32
- - First official release