@pretto/places 0.21.0 → 0.22.0

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.js CHANGED
@@ -289,22 +289,31 @@ var geolocSearch = /*#__PURE__*/Object.freeze({
289
289
  get: get$1
290
290
  });
291
291
 
292
- const formatData = (results) => {
293
- return results.map(result => ({
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 => ({
294
296
  center: { latitude: result.centre.coordinates[1], longitude: result.centre.coordinates[0] },
295
297
  city: result.nom,
296
298
  code: result.code,
297
299
  coordinates: result.centre.coordinates,
298
300
  zipcode: result.codesPostaux[0],
301
+ outline: result.contour.coordinates[0].map(([longitude, latitude]) => ({ longitude, latitude })),
302
+ district,
303
+ postcode,
299
304
  }));
300
305
  };
301
- const getData = (search) => __awaiter(void 0, void 0, void 0, function* () {
306
+ const getData = (search, signal) => __awaiter(void 0, void 0, void 0, function* () {
302
307
  try {
303
- const response = yield fetch(`https://geo.api.gouv.fr/communes?lat=${search.latitude}&lon=${search.longitude}&fields=departement,codesPostaux,centre`);
304
- if (!response.ok)
305
- return Promise.reject(response);
306
- const results = yield response.json();
307
- return formatData(results);
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);
308
317
  }
309
318
  catch (error) {
310
319
  if (typeof error === 'string')
@@ -313,11 +322,11 @@ const getData = (search) => __awaiter(void 0, void 0, void 0, function* () {
313
322
  }
314
323
  });
315
324
  let getDelayedData;
316
- const get = (search, debounceValue = 300) => {
325
+ const get = (search, debounceValue = 300, signal) => {
317
326
  clearTimeout(getDelayedData);
318
327
  return new Promise((resolve, reject) => {
319
328
  getDelayedData = setTimeout(() => {
320
- resolve(getData(search));
329
+ resolve(getData(search, signal));
321
330
  }, debounceValue);
322
331
  });
323
332
  };
package/dist/module.js CHANGED
@@ -279,22 +279,31 @@ var geolocSearch = /*#__PURE__*/Object.freeze({
279
279
  get: get$1
280
280
  });
281
281
 
282
- const formatData = (results) => {
283
- return results.map(result => ({
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 => ({
284
286
  center: { latitude: result.centre.coordinates[1], longitude: result.centre.coordinates[0] },
285
287
  city: result.nom,
286
288
  code: result.code,
287
289
  coordinates: result.centre.coordinates,
288
290
  zipcode: result.codesPostaux[0],
291
+ outline: result.contour.coordinates[0].map(([longitude, latitude]) => ({ longitude, latitude })),
292
+ district,
293
+ postcode,
289
294
  }));
290
295
  };
291
- const getData = (search) => __awaiter(void 0, void 0, void 0, function* () {
296
+ const getData = (search, signal) => __awaiter(void 0, void 0, void 0, function* () {
292
297
  try {
293
- const response = yield fetch(`https://geo.api.gouv.fr/communes?lat=${search.latitude}&lon=${search.longitude}&fields=departement,codesPostaux,centre`);
294
- if (!response.ok)
295
- return Promise.reject(response);
296
- const results = yield response.json();
297
- return formatData(results);
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);
298
307
  }
299
308
  catch (error) {
300
309
  if (typeof error === 'string')
@@ -303,11 +312,11 @@ const getData = (search) => __awaiter(void 0, void 0, void 0, function* () {
303
312
  }
304
313
  });
305
314
  let getDelayedData;
306
- const get = (search, debounceValue = 300) => {
315
+ const get = (search, debounceValue = 300, signal) => {
307
316
  clearTimeout(getDelayedData);
308
317
  return new Promise((resolve, reject) => {
309
318
  getDelayedData = setTimeout(() => {
310
- resolve(getData(search));
319
+ resolve(getData(search, signal));
311
320
  }, debounceValue);
312
321
  });
313
322
  };
@@ -6,10 +6,16 @@ export declare type ReverseGeolocSearchResult = {
6
6
  city: string;
7
7
  code: string;
8
8
  coordinates: [number, number];
9
+ outline: Array<{
10
+ latitude: number;
11
+ longitude: number;
12
+ }>;
9
13
  zipcode: string;
14
+ postcode: string | null;
15
+ district: string | null;
10
16
  };
11
17
  export declare const get: (search: {
12
18
  latitude: number;
13
19
  longitude: number;
14
- }, debounceValue?: number) => Promise<ReverseGeolocSearchResult[]>;
20
+ }, debounceValue?: number, signal?: AbortSignal | undefined) => Promise<ReverseGeolocSearchResult[]>;
15
21
  //# sourceMappingURL=reverseGeolocSearch.d.ts.map
@@ -1 +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,MAAM,CAAA;CAChB,CAAA;AAyCD,eAAO,MAAM,GAAG,WACN;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,kBAChC,MAAM,KACpB,QAAQ,yBAAyB,EAAE,CAQrC,CAAA"}
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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pretto/places",
3
- "version": "0.21.0",
3
+ "version": "0.22.0",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",