@pretto/places 0.15.0 → 0.17.0

Sign up to get free protection for your applications and to get access to all the features.
package/README.md CHANGED
@@ -129,9 +129,23 @@ const results = await countriesApi.get('al', { limit: 10 })[
129
129
  ]
130
130
  ```
131
131
 
132
+ For geolocalisation
133
+
134
+ ```typescript
135
+ import { geolocSearch } from '@pretto/places'
136
+
137
+ const results = await geolocSearch.get('75010')
138
+ // result object format :
139
+ // [{
140
+ // city: 'Paris'
141
+ // code: '75010',
142
+ // coordinates: [2.347, 48.8589],
143
+ // }]
144
+ ```
145
+
132
146
  ### Debounce
133
147
 
134
- `addressSearch` and `municipalitySearch` have defaut debounce value fixed to **300ms**\
148
+ `addressSearch`, `municipalitySearch` and `geolocSearch` have defaut debounce value fixed to **300ms**\
135
149
  You can override this value by passing debounce value as last argument
136
150
 
137
151
  ```jsx
@@ -0,0 +1,7 @@
1
+ declare type GeolocSearchResult = {
2
+ code: string;
3
+ coordinates: [number, number];
4
+ };
5
+ export declare const get: (search: string, debounceValue?: number) => Promise<GeolocSearchResult[]>;
6
+ export {};
7
+ //# sourceMappingURL=geolocSearch.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"geolocSearch.d.ts","sourceRoot":"","sources":["../src/geolocSearch.ts"],"names":[],"mappings":"AAAA,aAAK,kBAAkB,GAAG;IACxB,IAAI,EAAE,MAAM,CAAA;IACZ,WAAW,EAAE,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CAC9B,CAAA;AAyCD,eAAO,MAAM,GAAG,WAAY,MAAM,kBAAiB,MAAM,KAAS,QAAQ,kBAAkB,EAAE,CAQ7F,CAAA"}
package/dist/index.d.ts CHANGED
@@ -1,4 +1,5 @@
1
1
  export * as municipalitySearch from './municipalitySearch';
2
2
  export * as addressSearch from './addressSearch';
3
3
  export * as countrySearch from './countrySearch';
4
+ export * as geolocSearch from './geolocSearch';
4
5
  //# sourceMappingURL=index.d.ts.map
@@ -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"}
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"}
package/dist/index.js CHANGED
@@ -100,7 +100,7 @@ const responseFormat = (places, country, search, departmentOnly) => {
100
100
  });
101
101
  return flat__default["default"](allPlaces);
102
102
  };
103
- const getData$1 = (search, options) => __awaiter(void 0, void 0, void 0, function* () {
103
+ const getData$2 = (search, options) => __awaiter(void 0, void 0, void 0, function* () {
104
104
  const { country = 'fr', limit = 5, departmentOnly = false } = options || {};
105
105
  const isNumber = !isNaN(parseInt(search, 10));
106
106
  if (!search || (isNumber && search.length < 2))
@@ -133,23 +133,23 @@ const getData$1 = (search, options) => __awaiter(void 0, void 0, void 0, functio
133
133
  return 'An error occurred.';
134
134
  }
135
135
  });
136
- let getDelayedData$1;
137
- const get$2 = (search, options, debounceValue = 300) => {
138
- clearTimeout(getDelayedData$1);
136
+ let getDelayedData$2;
137
+ const get$3 = (search, options, debounceValue = 300) => {
138
+ clearTimeout(getDelayedData$2);
139
139
  return new Promise((resolve, reject) => {
140
- getDelayedData$1 = setTimeout(() => {
141
- resolve(getData$1(search, options));
140
+ getDelayedData$2 = setTimeout(() => {
141
+ resolve(getData$2(search, options));
142
142
  }, debounceValue);
143
143
  });
144
144
  };
145
145
 
146
146
  var municipalitySearch = /*#__PURE__*/Object.freeze({
147
147
  __proto__: null,
148
- getData: getData$1,
149
- get: get$2
148
+ getData: getData$2,
149
+ get: get$3
150
150
  });
151
151
 
152
- const getData = (search, options) => __awaiter(void 0, void 0, void 0, function* () {
152
+ const getData$1 = (search, options) => __awaiter(void 0, void 0, void 0, function* () {
153
153
  if (!search || search.length < 2)
154
154
  return [];
155
155
  try {
@@ -182,22 +182,22 @@ const getData = (search, options) => __awaiter(void 0, void 0, void 0, function*
182
182
  return 'An error occurred.';
183
183
  }
184
184
  });
185
- let getDelayedData;
186
- const get$1 = (search, options, debounceValue = 300) => {
187
- clearTimeout(getDelayedData);
185
+ let getDelayedData$1;
186
+ const get$2 = (search, options, debounceValue = 300) => {
187
+ clearTimeout(getDelayedData$1);
188
188
  return new Promise((resolve, reject) => {
189
- getDelayedData = setTimeout(() => {
190
- resolve(getData(search, options));
189
+ getDelayedData$1 = setTimeout(() => {
190
+ resolve(getData$1(search, options));
191
191
  }, debounceValue);
192
192
  });
193
193
  };
194
194
 
195
195
  var addressSearch = /*#__PURE__*/Object.freeze({
196
196
  __proto__: null,
197
- get: get$1
197
+ get: get$2
198
198
  });
199
199
 
200
- const get = (instance, search, options) => __awaiter(void 0, void 0, void 0, function* () {
200
+ const get$1 = (instance, search, options) => __awaiter(void 0, void 0, void 0, function* () {
201
201
  if (!search)
202
202
  return [];
203
203
  const { limit = 5 } = options || {};
@@ -219,7 +219,7 @@ const init = (appId, apiKey, options) => {
219
219
  return {
220
220
  get(search, options) {
221
221
  return __awaiter(this, void 0, void 0, function* () {
222
- return yield get(ALGOLIA_COUNTRIES_API, search, options);
222
+ return yield get$1(ALGOLIA_COUNTRIES_API, search, options);
223
223
  });
224
224
  },
225
225
  };
@@ -238,6 +238,47 @@ var countrySearch = /*#__PURE__*/Object.freeze({
238
238
  init: init
239
239
  });
240
240
 
241
+ const formatData = (results) => results.map(result => ({
242
+ city: result.nom,
243
+ code: result.code,
244
+ coordinates: result.centre.coordinates,
245
+ }));
246
+ // All french zipcode are only numbers, even particular cases like Corse (2A, 2B > 20), Finistère (29N, 29S > 29x), etc..
247
+ // For more information: https://fr.wikipedia.org/wiki/Code_postal_en_France
248
+ // Kept as string for consistency with geo.api.gouv.fr
249
+ const getData = (search) => __awaiter(void 0, void 0, void 0, function* () {
250
+ if (!search || !/(\d){5}/.test(search))
251
+ return [];
252
+ try {
253
+ const formattedSearch = encodeURIComponent(search);
254
+ const response = yield fetch(`https://geo.api.gouv.fr/communes?codePostal=${formattedSearch}&fields=centre`);
255
+ if (!response.ok)
256
+ return Promise.reject(response);
257
+ const results = yield response.json();
258
+ return formatData(results);
259
+ }
260
+ catch (error) {
261
+ if (typeof error === 'string')
262
+ throw new Error(error);
263
+ throw new Error(error instanceof Error && error.message ? error.message : 'An error occurred.');
264
+ }
265
+ });
266
+ let getDelayedData;
267
+ const get = (search, debounceValue = 300) => {
268
+ clearTimeout(getDelayedData);
269
+ return new Promise((resolve, reject) => {
270
+ getDelayedData = setTimeout(() => {
271
+ resolve(getData(search));
272
+ }, debounceValue);
273
+ });
274
+ };
275
+
276
+ var geolocSearch = /*#__PURE__*/Object.freeze({
277
+ __proto__: null,
278
+ get: get
279
+ });
280
+
241
281
  exports.addressSearch = addressSearch;
242
282
  exports.countrySearch = countrySearch;
283
+ exports.geolocSearch = geolocSearch;
243
284
  exports.municipalitySearch = municipalitySearch;
package/dist/module.js CHANGED
@@ -90,7 +90,7 @@ const responseFormat = (places, country, search, departmentOnly) => {
90
90
  });
91
91
  return flat(allPlaces);
92
92
  };
93
- const getData$1 = (search, options) => __awaiter(void 0, void 0, void 0, function* () {
93
+ const getData$2 = (search, options) => __awaiter(void 0, void 0, void 0, function* () {
94
94
  const { country = 'fr', limit = 5, departmentOnly = false } = options || {};
95
95
  const isNumber = !isNaN(parseInt(search, 10));
96
96
  if (!search || (isNumber && search.length < 2))
@@ -123,23 +123,23 @@ const getData$1 = (search, options) => __awaiter(void 0, void 0, void 0, functio
123
123
  return 'An error occurred.';
124
124
  }
125
125
  });
126
- let getDelayedData$1;
127
- const get$2 = (search, options, debounceValue = 300) => {
128
- clearTimeout(getDelayedData$1);
126
+ let getDelayedData$2;
127
+ const get$3 = (search, options, debounceValue = 300) => {
128
+ clearTimeout(getDelayedData$2);
129
129
  return new Promise((resolve, reject) => {
130
- getDelayedData$1 = setTimeout(() => {
131
- resolve(getData$1(search, options));
130
+ getDelayedData$2 = setTimeout(() => {
131
+ resolve(getData$2(search, options));
132
132
  }, debounceValue);
133
133
  });
134
134
  };
135
135
 
136
136
  var municipalitySearch = /*#__PURE__*/Object.freeze({
137
137
  __proto__: null,
138
- getData: getData$1,
139
- get: get$2
138
+ getData: getData$2,
139
+ get: get$3
140
140
  });
141
141
 
142
- const getData = (search, options) => __awaiter(void 0, void 0, void 0, function* () {
142
+ const getData$1 = (search, options) => __awaiter(void 0, void 0, void 0, function* () {
143
143
  if (!search || search.length < 2)
144
144
  return [];
145
145
  try {
@@ -172,22 +172,22 @@ const getData = (search, options) => __awaiter(void 0, void 0, void 0, function*
172
172
  return 'An error occurred.';
173
173
  }
174
174
  });
175
- let getDelayedData;
176
- const get$1 = (search, options, debounceValue = 300) => {
177
- clearTimeout(getDelayedData);
175
+ let getDelayedData$1;
176
+ const get$2 = (search, options, debounceValue = 300) => {
177
+ clearTimeout(getDelayedData$1);
178
178
  return new Promise((resolve, reject) => {
179
- getDelayedData = setTimeout(() => {
180
- resolve(getData(search, options));
179
+ getDelayedData$1 = setTimeout(() => {
180
+ resolve(getData$1(search, options));
181
181
  }, debounceValue);
182
182
  });
183
183
  };
184
184
 
185
185
  var addressSearch = /*#__PURE__*/Object.freeze({
186
186
  __proto__: null,
187
- get: get$1
187
+ get: get$2
188
188
  });
189
189
 
190
- const get = (instance, search, options) => __awaiter(void 0, void 0, void 0, function* () {
190
+ const get$1 = (instance, search, options) => __awaiter(void 0, void 0, void 0, function* () {
191
191
  if (!search)
192
192
  return [];
193
193
  const { limit = 5 } = options || {};
@@ -209,7 +209,7 @@ const init = (appId, apiKey, options) => {
209
209
  return {
210
210
  get(search, options) {
211
211
  return __awaiter(this, void 0, void 0, function* () {
212
- return yield get(ALGOLIA_COUNTRIES_API, search, options);
212
+ return yield get$1(ALGOLIA_COUNTRIES_API, search, options);
213
213
  });
214
214
  },
215
215
  };
@@ -228,4 +228,44 @@ var countrySearch = /*#__PURE__*/Object.freeze({
228
228
  init: init
229
229
  });
230
230
 
231
- export { addressSearch, countrySearch, municipalitySearch };
231
+ const formatData = (results) => results.map(result => ({
232
+ city: result.nom,
233
+ code: result.code,
234
+ coordinates: result.centre.coordinates,
235
+ }));
236
+ // All french zipcode are only numbers, even particular cases like Corse (2A, 2B > 20), Finistère (29N, 29S > 29x), etc..
237
+ // For more information: https://fr.wikipedia.org/wiki/Code_postal_en_France
238
+ // Kept as string for consistency with geo.api.gouv.fr
239
+ const getData = (search) => __awaiter(void 0, void 0, void 0, function* () {
240
+ if (!search || !/(\d){5}/.test(search))
241
+ return [];
242
+ try {
243
+ const formattedSearch = encodeURIComponent(search);
244
+ const response = yield fetch(`https://geo.api.gouv.fr/communes?codePostal=${formattedSearch}&fields=centre`);
245
+ if (!response.ok)
246
+ return Promise.reject(response);
247
+ const results = yield response.json();
248
+ return formatData(results);
249
+ }
250
+ catch (error) {
251
+ if (typeof error === 'string')
252
+ throw new Error(error);
253
+ throw new Error(error instanceof Error && error.message ? error.message : 'An error occurred.');
254
+ }
255
+ });
256
+ let getDelayedData;
257
+ const get = (search, debounceValue = 300) => {
258
+ clearTimeout(getDelayedData);
259
+ return new Promise((resolve, reject) => {
260
+ getDelayedData = setTimeout(() => {
261
+ resolve(getData(search));
262
+ }, debounceValue);
263
+ });
264
+ };
265
+
266
+ var geolocSearch = /*#__PURE__*/Object.freeze({
267
+ __proto__: null,
268
+ get: get
269
+ });
270
+
271
+ export { addressSearch, countrySearch, geolocSearch, municipalitySearch };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pretto/places",
3
- "version": "0.15.0",
3
+ "version": "0.17.0",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",