@pretto/places 0.20.0 → 0.22.0
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +16 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +71 -23
- package/dist/module.js +71 -24
- package/dist/reverseGeolocSearch.d.ts +21 -0
- package/dist/reverseGeolocSearch.d.ts.map +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
@@ -143,6 +143,22 @@ const results = await geolocSearch.get('75010')
|
|
143
143
|
// }]
|
144
144
|
```
|
145
145
|
|
146
|
+
For reverse geolocalisation
|
147
|
+
|
148
|
+
```typescript
|
149
|
+
import { reverseGeolocSearch } from '@pretto/places'
|
150
|
+
|
151
|
+
const results = await reverseGeolocSearch.get({ latitude: 48.8932244326416, longitude: 2.289395548568507 })
|
152
|
+
// result object format :
|
153
|
+
// [{
|
154
|
+
// center: {latitude: 48.8946, longitude: 2.2874}
|
155
|
+
// city: 'Levallois-Perret'
|
156
|
+
// code: '92044',
|
157
|
+
// coordinates: [2.2874, 48.8946],
|
158
|
+
// zipcode: 92300,
|
159
|
+
// }]
|
160
|
+
```
|
161
|
+
|
146
162
|
### Debounce
|
147
163
|
|
148
164
|
`addressSearch`, `municipalitySearch` and `geolocSearch` have defaut debounce value fixed to **300ms**\
|
package/dist/index.d.ts
CHANGED
@@ -2,4 +2,5 @@ export * as municipalitySearch from './municipalitySearch';
|
|
2
2
|
export * as addressSearch from './addressSearch';
|
3
3
|
export * as countrySearch from './countrySearch';
|
4
4
|
export * as geolocSearch from './geolocSearch';
|
5
|
+
export * as reverseGeolocSearch from './reverseGeolocSearch';
|
5
6
|
//# sourceMappingURL=index.d.ts.map
|
package/dist/index.d.ts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,kBAAkB,MAAM,sBAAsB,CAAA;AAC1D,OAAO,KAAK,aAAa,MAAM,iBAAiB,CAAA;AAChD,OAAO,KAAK,aAAa,MAAM,iBAAiB,CAAA;AAChD,OAAO,KAAK,YAAY,MAAM,gBAAgB,CAAA"}
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,kBAAkB,MAAM,sBAAsB,CAAA;AAC1D,OAAO,KAAK,aAAa,MAAM,iBAAiB,CAAA;AAChD,OAAO,KAAK,aAAa,MAAM,iBAAiB,CAAA;AAChD,OAAO,KAAK,YAAY,MAAM,gBAAgB,CAAA;AAC9C,OAAO,KAAK,mBAAmB,MAAM,uBAAuB,CAAA"}
|
package/dist/index.js
CHANGED
@@ -111,7 +111,7 @@ const responseFormat = (places, country, search, departmentOnly) => {
|
|
111
111
|
});
|
112
112
|
return flat__default["default"](allPlaces);
|
113
113
|
};
|
114
|
-
const getData$
|
114
|
+
const getData$3 = (search, options) => __awaiter(void 0, void 0, void 0, function* () {
|
115
115
|
const { country = 'fr', limit = 5, departmentOnly = false } = options || {};
|
116
116
|
const isNumber = !isNaN(parseInt(search, 10));
|
117
117
|
if (!search || (isNumber && search.length < 2))
|
@@ -144,23 +144,23 @@ const getData$2 = (search, options) => __awaiter(void 0, void 0, void 0, functio
|
|
144
144
|
return 'An error occurred.';
|
145
145
|
}
|
146
146
|
});
|
147
|
-
let getDelayedData$
|
148
|
-
const get$
|
149
|
-
clearTimeout(getDelayedData$
|
147
|
+
let getDelayedData$3;
|
148
|
+
const get$4 = (search, options, debounceValue = 300) => {
|
149
|
+
clearTimeout(getDelayedData$3);
|
150
150
|
return new Promise((resolve, reject) => {
|
151
|
-
getDelayedData$
|
152
|
-
resolve(getData$
|
151
|
+
getDelayedData$3 = setTimeout(() => {
|
152
|
+
resolve(getData$3(search, options));
|
153
153
|
}, debounceValue);
|
154
154
|
});
|
155
155
|
};
|
156
156
|
|
157
157
|
var municipalitySearch = /*#__PURE__*/Object.freeze({
|
158
158
|
__proto__: null,
|
159
|
-
getData: getData$
|
160
|
-
get: get$
|
159
|
+
getData: getData$3,
|
160
|
+
get: get$4
|
161
161
|
});
|
162
162
|
|
163
|
-
const getData$
|
163
|
+
const getData$2 = (search, options) => __awaiter(void 0, void 0, void 0, function* () {
|
164
164
|
if (!search || search.length < 2)
|
165
165
|
return [];
|
166
166
|
try {
|
@@ -193,22 +193,22 @@ const getData$1 = (search, options) => __awaiter(void 0, void 0, void 0, functio
|
|
193
193
|
return 'An error occurred.';
|
194
194
|
}
|
195
195
|
});
|
196
|
-
let getDelayedData$
|
197
|
-
const get$
|
198
|
-
clearTimeout(getDelayedData$
|
196
|
+
let getDelayedData$2;
|
197
|
+
const get$3 = (search, options, debounceValue = 300) => {
|
198
|
+
clearTimeout(getDelayedData$2);
|
199
199
|
return new Promise((resolve, reject) => {
|
200
|
-
getDelayedData$
|
201
|
-
resolve(getData$
|
200
|
+
getDelayedData$2 = setTimeout(() => {
|
201
|
+
resolve(getData$2(search, options));
|
202
202
|
}, debounceValue);
|
203
203
|
});
|
204
204
|
};
|
205
205
|
|
206
206
|
var addressSearch = /*#__PURE__*/Object.freeze({
|
207
207
|
__proto__: null,
|
208
|
-
get: get$
|
208
|
+
get: get$3
|
209
209
|
});
|
210
210
|
|
211
|
-
const get$
|
211
|
+
const get$2 = (instance, search, options) => __awaiter(void 0, void 0, void 0, function* () {
|
212
212
|
if (!search)
|
213
213
|
return [];
|
214
214
|
const { limit = 5 } = options || {};
|
@@ -230,7 +230,7 @@ const init = (appId, apiKey, options) => {
|
|
230
230
|
return {
|
231
231
|
get(search, options) {
|
232
232
|
return __awaiter(this, void 0, void 0, function* () {
|
233
|
-
return yield get$
|
233
|
+
return yield get$2(ALGOLIA_COUNTRIES_API, search, options);
|
234
234
|
});
|
235
235
|
},
|
236
236
|
};
|
@@ -249,7 +249,7 @@ var countrySearch = /*#__PURE__*/Object.freeze({
|
|
249
249
|
init: init
|
250
250
|
});
|
251
251
|
|
252
|
-
const formatData = (results) => results.map(result => ({
|
252
|
+
const formatData$1 = (results) => results.map(result => ({
|
253
253
|
city: result.nom,
|
254
254
|
code: result.code,
|
255
255
|
coordinates: result.centre.coordinates,
|
@@ -257,7 +257,7 @@ const formatData = (results) => results.map(result => ({
|
|
257
257
|
// All french zipcode are only numbers, even particular cases like Corse (2A, 2B > 20), Finistère (29N, 29S > 29x), etc..
|
258
258
|
// For more information: https://fr.wikipedia.org/wiki/Code_postal_en_France
|
259
259
|
// Kept as string for consistency with geo.api.gouv.fr
|
260
|
-
const getData = (search) => __awaiter(void 0, void 0, void 0, function* () {
|
260
|
+
const getData$1 = (search) => __awaiter(void 0, void 0, void 0, function* () {
|
261
261
|
if (!search || !/(\d){5}/.test(search))
|
262
262
|
return [];
|
263
263
|
try {
|
@@ -266,7 +266,54 @@ const getData = (search) => __awaiter(void 0, void 0, void 0, function* () {
|
|
266
266
|
if (!response.ok)
|
267
267
|
return Promise.reject(response);
|
268
268
|
const results = yield response.json();
|
269
|
-
return formatData(results);
|
269
|
+
return formatData$1(results);
|
270
|
+
}
|
271
|
+
catch (error) {
|
272
|
+
if (typeof error === 'string')
|
273
|
+
throw new Error(error);
|
274
|
+
throw new Error(error instanceof Error && error.message ? error.message : 'An error occurred.');
|
275
|
+
}
|
276
|
+
});
|
277
|
+
let getDelayedData$1;
|
278
|
+
const get$1 = (search, debounceValue = 300) => {
|
279
|
+
clearTimeout(getDelayedData$1);
|
280
|
+
return new Promise((resolve, reject) => {
|
281
|
+
getDelayedData$1 = setTimeout(() => {
|
282
|
+
resolve(getData$1(search));
|
283
|
+
}, debounceValue);
|
284
|
+
});
|
285
|
+
};
|
286
|
+
|
287
|
+
var geolocSearch = /*#__PURE__*/Object.freeze({
|
288
|
+
__proto__: null,
|
289
|
+
get: get$1
|
290
|
+
});
|
291
|
+
|
292
|
+
const formatData = (geoResults, addressResults) => {
|
293
|
+
const district = addressResults.features.length > 0 ? addressResults.features[0].properties.district : null;
|
294
|
+
const postcode = addressResults.features.length > 0 ? addressResults.features[0].properties.postcode : null;
|
295
|
+
return geoResults.map(result => ({
|
296
|
+
center: { latitude: result.centre.coordinates[1], longitude: result.centre.coordinates[0] },
|
297
|
+
city: result.nom,
|
298
|
+
code: result.code,
|
299
|
+
coordinates: result.centre.coordinates,
|
300
|
+
zipcode: result.codesPostaux[0],
|
301
|
+
outline: result.contour.coordinates[0].map(([longitude, latitude]) => ({ longitude, latitude })),
|
302
|
+
district,
|
303
|
+
postcode,
|
304
|
+
}));
|
305
|
+
};
|
306
|
+
const getData = (search, signal) => __awaiter(void 0, void 0, void 0, function* () {
|
307
|
+
try {
|
308
|
+
const [geoResponse, addressResponse] = yield Promise.all([
|
309
|
+
fetch(`https://geo.api.gouv.fr/communes?lat=${search.latitude}&lon=${search.longitude}&fields=departement,codesPostaux,centre,contour`, { signal }),
|
310
|
+
fetch(`https://api-adresse.data.gouv.fr/reverse/?lat=${search.latitude}&lon=${search.longitude}&limit=1&&autocomplete=0`, { signal }),
|
311
|
+
]);
|
312
|
+
if (!geoResponse.ok)
|
313
|
+
return Promise.reject(geoResponse);
|
314
|
+
const geoResults = yield geoResponse.json();
|
315
|
+
const addressResults = yield addressResponse.json();
|
316
|
+
return formatData(geoResults, addressResults);
|
270
317
|
}
|
271
318
|
catch (error) {
|
272
319
|
if (typeof error === 'string')
|
@@ -275,16 +322,16 @@ const getData = (search) => __awaiter(void 0, void 0, void 0, function* () {
|
|
275
322
|
}
|
276
323
|
});
|
277
324
|
let getDelayedData;
|
278
|
-
const get = (search, debounceValue = 300) => {
|
325
|
+
const get = (search, debounceValue = 300, signal) => {
|
279
326
|
clearTimeout(getDelayedData);
|
280
327
|
return new Promise((resolve, reject) => {
|
281
328
|
getDelayedData = setTimeout(() => {
|
282
|
-
resolve(getData(search));
|
329
|
+
resolve(getData(search, signal));
|
283
330
|
}, debounceValue);
|
284
331
|
});
|
285
332
|
};
|
286
333
|
|
287
|
-
var
|
334
|
+
var reverseGeolocSearch = /*#__PURE__*/Object.freeze({
|
288
335
|
__proto__: null,
|
289
336
|
get: get
|
290
337
|
});
|
@@ -293,3 +340,4 @@ exports.addressSearch = addressSearch;
|
|
293
340
|
exports.countrySearch = countrySearch;
|
294
341
|
exports.geolocSearch = geolocSearch;
|
295
342
|
exports.municipalitySearch = municipalitySearch;
|
343
|
+
exports.reverseGeolocSearch = reverseGeolocSearch;
|
package/dist/module.js
CHANGED
@@ -101,7 +101,7 @@ const responseFormat = (places, country, search, departmentOnly) => {
|
|
101
101
|
});
|
102
102
|
return flat(allPlaces);
|
103
103
|
};
|
104
|
-
const getData$
|
104
|
+
const getData$3 = (search, options) => __awaiter(void 0, void 0, void 0, function* () {
|
105
105
|
const { country = 'fr', limit = 5, departmentOnly = false } = options || {};
|
106
106
|
const isNumber = !isNaN(parseInt(search, 10));
|
107
107
|
if (!search || (isNumber && search.length < 2))
|
@@ -134,23 +134,23 @@ const getData$2 = (search, options) => __awaiter(void 0, void 0, void 0, functio
|
|
134
134
|
return 'An error occurred.';
|
135
135
|
}
|
136
136
|
});
|
137
|
-
let getDelayedData$
|
138
|
-
const get$
|
139
|
-
clearTimeout(getDelayedData$
|
137
|
+
let getDelayedData$3;
|
138
|
+
const get$4 = (search, options, debounceValue = 300) => {
|
139
|
+
clearTimeout(getDelayedData$3);
|
140
140
|
return new Promise((resolve, reject) => {
|
141
|
-
getDelayedData$
|
142
|
-
resolve(getData$
|
141
|
+
getDelayedData$3 = setTimeout(() => {
|
142
|
+
resolve(getData$3(search, options));
|
143
143
|
}, debounceValue);
|
144
144
|
});
|
145
145
|
};
|
146
146
|
|
147
147
|
var municipalitySearch = /*#__PURE__*/Object.freeze({
|
148
148
|
__proto__: null,
|
149
|
-
getData: getData$
|
150
|
-
get: get$
|
149
|
+
getData: getData$3,
|
150
|
+
get: get$4
|
151
151
|
});
|
152
152
|
|
153
|
-
const getData$
|
153
|
+
const getData$2 = (search, options) => __awaiter(void 0, void 0, void 0, function* () {
|
154
154
|
if (!search || search.length < 2)
|
155
155
|
return [];
|
156
156
|
try {
|
@@ -183,22 +183,22 @@ const getData$1 = (search, options) => __awaiter(void 0, void 0, void 0, functio
|
|
183
183
|
return 'An error occurred.';
|
184
184
|
}
|
185
185
|
});
|
186
|
-
let getDelayedData$
|
187
|
-
const get$
|
188
|
-
clearTimeout(getDelayedData$
|
186
|
+
let getDelayedData$2;
|
187
|
+
const get$3 = (search, options, debounceValue = 300) => {
|
188
|
+
clearTimeout(getDelayedData$2);
|
189
189
|
return new Promise((resolve, reject) => {
|
190
|
-
getDelayedData$
|
191
|
-
resolve(getData$
|
190
|
+
getDelayedData$2 = setTimeout(() => {
|
191
|
+
resolve(getData$2(search, options));
|
192
192
|
}, debounceValue);
|
193
193
|
});
|
194
194
|
};
|
195
195
|
|
196
196
|
var addressSearch = /*#__PURE__*/Object.freeze({
|
197
197
|
__proto__: null,
|
198
|
-
get: get$
|
198
|
+
get: get$3
|
199
199
|
});
|
200
200
|
|
201
|
-
const get$
|
201
|
+
const get$2 = (instance, search, options) => __awaiter(void 0, void 0, void 0, function* () {
|
202
202
|
if (!search)
|
203
203
|
return [];
|
204
204
|
const { limit = 5 } = options || {};
|
@@ -220,7 +220,7 @@ const init = (appId, apiKey, options) => {
|
|
220
220
|
return {
|
221
221
|
get(search, options) {
|
222
222
|
return __awaiter(this, void 0, void 0, function* () {
|
223
|
-
return yield get$
|
223
|
+
return yield get$2(ALGOLIA_COUNTRIES_API, search, options);
|
224
224
|
});
|
225
225
|
},
|
226
226
|
};
|
@@ -239,7 +239,7 @@ var countrySearch = /*#__PURE__*/Object.freeze({
|
|
239
239
|
init: init
|
240
240
|
});
|
241
241
|
|
242
|
-
const formatData = (results) => results.map(result => ({
|
242
|
+
const formatData$1 = (results) => results.map(result => ({
|
243
243
|
city: result.nom,
|
244
244
|
code: result.code,
|
245
245
|
coordinates: result.centre.coordinates,
|
@@ -247,7 +247,7 @@ const formatData = (results) => results.map(result => ({
|
|
247
247
|
// All french zipcode are only numbers, even particular cases like Corse (2A, 2B > 20), Finistère (29N, 29S > 29x), etc..
|
248
248
|
// For more information: https://fr.wikipedia.org/wiki/Code_postal_en_France
|
249
249
|
// Kept as string for consistency with geo.api.gouv.fr
|
250
|
-
const getData = (search) => __awaiter(void 0, void 0, void 0, function* () {
|
250
|
+
const getData$1 = (search) => __awaiter(void 0, void 0, void 0, function* () {
|
251
251
|
if (!search || !/(\d){5}/.test(search))
|
252
252
|
return [];
|
253
253
|
try {
|
@@ -256,7 +256,54 @@ const getData = (search) => __awaiter(void 0, void 0, void 0, function* () {
|
|
256
256
|
if (!response.ok)
|
257
257
|
return Promise.reject(response);
|
258
258
|
const results = yield response.json();
|
259
|
-
return formatData(results);
|
259
|
+
return formatData$1(results);
|
260
|
+
}
|
261
|
+
catch (error) {
|
262
|
+
if (typeof error === 'string')
|
263
|
+
throw new Error(error);
|
264
|
+
throw new Error(error instanceof Error && error.message ? error.message : 'An error occurred.');
|
265
|
+
}
|
266
|
+
});
|
267
|
+
let getDelayedData$1;
|
268
|
+
const get$1 = (search, debounceValue = 300) => {
|
269
|
+
clearTimeout(getDelayedData$1);
|
270
|
+
return new Promise((resolve, reject) => {
|
271
|
+
getDelayedData$1 = setTimeout(() => {
|
272
|
+
resolve(getData$1(search));
|
273
|
+
}, debounceValue);
|
274
|
+
});
|
275
|
+
};
|
276
|
+
|
277
|
+
var geolocSearch = /*#__PURE__*/Object.freeze({
|
278
|
+
__proto__: null,
|
279
|
+
get: get$1
|
280
|
+
});
|
281
|
+
|
282
|
+
const formatData = (geoResults, addressResults) => {
|
283
|
+
const district = addressResults.features.length > 0 ? addressResults.features[0].properties.district : null;
|
284
|
+
const postcode = addressResults.features.length > 0 ? addressResults.features[0].properties.postcode : null;
|
285
|
+
return geoResults.map(result => ({
|
286
|
+
center: { latitude: result.centre.coordinates[1], longitude: result.centre.coordinates[0] },
|
287
|
+
city: result.nom,
|
288
|
+
code: result.code,
|
289
|
+
coordinates: result.centre.coordinates,
|
290
|
+
zipcode: result.codesPostaux[0],
|
291
|
+
outline: result.contour.coordinates[0].map(([longitude, latitude]) => ({ longitude, latitude })),
|
292
|
+
district,
|
293
|
+
postcode,
|
294
|
+
}));
|
295
|
+
};
|
296
|
+
const getData = (search, signal) => __awaiter(void 0, void 0, void 0, function* () {
|
297
|
+
try {
|
298
|
+
const [geoResponse, addressResponse] = yield Promise.all([
|
299
|
+
fetch(`https://geo.api.gouv.fr/communes?lat=${search.latitude}&lon=${search.longitude}&fields=departement,codesPostaux,centre,contour`, { signal }),
|
300
|
+
fetch(`https://api-adresse.data.gouv.fr/reverse/?lat=${search.latitude}&lon=${search.longitude}&limit=1&&autocomplete=0`, { signal }),
|
301
|
+
]);
|
302
|
+
if (!geoResponse.ok)
|
303
|
+
return Promise.reject(geoResponse);
|
304
|
+
const geoResults = yield geoResponse.json();
|
305
|
+
const addressResults = yield addressResponse.json();
|
306
|
+
return formatData(geoResults, addressResults);
|
260
307
|
}
|
261
308
|
catch (error) {
|
262
309
|
if (typeof error === 'string')
|
@@ -265,18 +312,18 @@ const getData = (search) => __awaiter(void 0, void 0, void 0, function* () {
|
|
265
312
|
}
|
266
313
|
});
|
267
314
|
let getDelayedData;
|
268
|
-
const get = (search, debounceValue = 300) => {
|
315
|
+
const get = (search, debounceValue = 300, signal) => {
|
269
316
|
clearTimeout(getDelayedData);
|
270
317
|
return new Promise((resolve, reject) => {
|
271
318
|
getDelayedData = setTimeout(() => {
|
272
|
-
resolve(getData(search));
|
319
|
+
resolve(getData(search, signal));
|
273
320
|
}, debounceValue);
|
274
321
|
});
|
275
322
|
};
|
276
323
|
|
277
|
-
var
|
324
|
+
var reverseGeolocSearch = /*#__PURE__*/Object.freeze({
|
278
325
|
__proto__: null,
|
279
326
|
get: get
|
280
327
|
});
|
281
328
|
|
282
|
-
export { addressSearch, countrySearch, geolocSearch, municipalitySearch };
|
329
|
+
export { addressSearch, countrySearch, geolocSearch, municipalitySearch, reverseGeolocSearch };
|
@@ -0,0 +1,21 @@
|
|
1
|
+
export declare type ReverseGeolocSearchResult = {
|
2
|
+
center: {
|
3
|
+
latitude: number;
|
4
|
+
longitude: number;
|
5
|
+
};
|
6
|
+
city: string;
|
7
|
+
code: string;
|
8
|
+
coordinates: [number, number];
|
9
|
+
outline: Array<{
|
10
|
+
latitude: number;
|
11
|
+
longitude: number;
|
12
|
+
}>;
|
13
|
+
zipcode: string;
|
14
|
+
postcode: string | null;
|
15
|
+
district: string | null;
|
16
|
+
};
|
17
|
+
export declare const get: (search: {
|
18
|
+
latitude: number;
|
19
|
+
longitude: number;
|
20
|
+
}, debounceValue?: number, signal?: AbortSignal | undefined) => Promise<ReverseGeolocSearchResult[]>;
|
21
|
+
//# sourceMappingURL=reverseGeolocSearch.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"reverseGeolocSearch.d.ts","sourceRoot":"","sources":["../src/reverseGeolocSearch.ts"],"names":[],"mappings":"AAAA,oBAAY,yBAAyB,GAAG;IACtC,MAAM,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAA;KAAE,CAAA;IAC/C,IAAI,EAAE,MAAM,CAAA;IACZ,IAAI,EAAE,MAAM,CAAA;IACZ,WAAW,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;IAC7B,OAAO,EAAE,KAAK,CAAC;QAAE,QAAQ,EAAE,MAAM,CAAC;QAAC,SAAS,EAAE,MAAM,CAAA;KAAE,CAAC,CAAA;IACvD,OAAO,EAAE,MAAM,CAAA;IACf,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAA;IACvB,QAAQ,EAAE,MAAM,GAAG,IAAI,CAAA;CACxB,CAAA;AAuED,eAAO,MAAM,GAAG,WACN;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,kBAChC,MAAM,uCAEpB,QAAQ,yBAAyB,EAAE,CAQrC,CAAA"}
|