@pretto/places 0.14.0 → 0.16.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
@@ -129,10 +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
+ // code: '75010',
141
+ // coordinates: [2.347, 48.8589],
142
+ // }]
143
+ ```
144
+
132
145
  ### Debounce
133
146
 
134
- `addressSearch` and `municipalitySearch` have defaut debounce value fixed to **300ms**\
135
- You can override this value by passing `debounceValue` as last argument
147
+ `addressSearch`, `municipalitySearch` and `geolocSearch` have defaut debounce value fixed to **300ms**\
148
+ You can override this value by passing debounce value as last argument
136
149
 
137
150
  ```jsx
138
151
  const debounceValue = 1000 // --- 1000 = 1s
@@ -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;AAwCD,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,46 @@ var countrySearch = /*#__PURE__*/Object.freeze({
238
238
  init: init
239
239
  });
240
240
 
241
+ const formatData = (results) => results.map(result => ({
242
+ code: result.code,
243
+ coordinates: result.centre.coordinates,
244
+ }));
245
+ // All french zipcode are only numbers, even particular cases like Corse (2A, 2B > 20), Finistère (29N, 29S > 29x), etc..
246
+ // For more information: https://fr.wikipedia.org/wiki/Code_postal_en_France
247
+ // Kept as string for consistency with geo.api.gouv.fr
248
+ const getData = (search) => __awaiter(void 0, void 0, void 0, function* () {
249
+ if (!search || !/(\d){5}/.test(search))
250
+ return [];
251
+ try {
252
+ const formattedSearch = encodeURIComponent(search);
253
+ const response = yield fetch(`https://geo.api.gouv.fr/communes?codePostal=${formattedSearch}&fields=centre`);
254
+ if (!response.ok)
255
+ return Promise.reject(response);
256
+ const results = yield response.json();
257
+ return formatData(results);
258
+ }
259
+ catch (error) {
260
+ if (typeof error === 'string')
261
+ throw new Error(error);
262
+ throw new Error(error instanceof Error && error.message ? error.message : 'An error occurred.');
263
+ }
264
+ });
265
+ let getDelayedData;
266
+ const get = (search, debounceValue = 300) => {
267
+ clearTimeout(getDelayedData);
268
+ return new Promise((resolve, reject) => {
269
+ getDelayedData = setTimeout(() => {
270
+ resolve(getData(search));
271
+ }, debounceValue);
272
+ });
273
+ };
274
+
275
+ var geolocSearch = /*#__PURE__*/Object.freeze({
276
+ __proto__: null,
277
+ get: get
278
+ });
279
+
241
280
  exports.addressSearch = addressSearch;
242
281
  exports.countrySearch = countrySearch;
282
+ exports.geolocSearch = geolocSearch;
243
283
  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,43 @@ 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
+ code: result.code,
233
+ coordinates: result.centre.coordinates,
234
+ }));
235
+ // All french zipcode are only numbers, even particular cases like Corse (2A, 2B > 20), Finistère (29N, 29S > 29x), etc..
236
+ // For more information: https://fr.wikipedia.org/wiki/Code_postal_en_France
237
+ // Kept as string for consistency with geo.api.gouv.fr
238
+ const getData = (search) => __awaiter(void 0, void 0, void 0, function* () {
239
+ if (!search || !/(\d){5}/.test(search))
240
+ return [];
241
+ try {
242
+ const formattedSearch = encodeURIComponent(search);
243
+ const response = yield fetch(`https://geo.api.gouv.fr/communes?codePostal=${formattedSearch}&fields=centre`);
244
+ if (!response.ok)
245
+ return Promise.reject(response);
246
+ const results = yield response.json();
247
+ return formatData(results);
248
+ }
249
+ catch (error) {
250
+ if (typeof error === 'string')
251
+ throw new Error(error);
252
+ throw new Error(error instanceof Error && error.message ? error.message : 'An error occurred.');
253
+ }
254
+ });
255
+ let getDelayedData;
256
+ const get = (search, debounceValue = 300) => {
257
+ clearTimeout(getDelayedData);
258
+ return new Promise((resolve, reject) => {
259
+ getDelayedData = setTimeout(() => {
260
+ resolve(getData(search));
261
+ }, debounceValue);
262
+ });
263
+ };
264
+
265
+ var geolocSearch = /*#__PURE__*/Object.freeze({
266
+ __proto__: null,
267
+ get: get
268
+ });
269
+
270
+ export { addressSearch, countrySearch, geolocSearch, municipalitySearch };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pretto/places",
3
- "version": "0.14.0",
3
+ "version": "0.16.0",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",