@pretto/places 0.40.0 → 0.41.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.
Files changed (2) hide show
  1. package/README.md +106 -76
  2. package/package.json +1 -1
package/README.md CHANGED
@@ -5,6 +5,8 @@
5
5
  Allows you to search for an address, a postcode or a French department.
6
6
  You can also search a country.
7
7
 
8
+ The library is typed but you can retrieve the types in the `dist/types` folder if needed
9
+
8
10
  ## FAQ
9
11
 
10
12
  ### How to implement the library?
@@ -16,28 +18,32 @@ yarn add @pretto/places
16
18
  For municipality and zipcode
17
19
 
18
20
  ```jsx
19
- import { municipalitySearch } from '@pretto/places'
21
+ import { searchPlaces } from '@pretto/places';
22
+ import { MunicipalitySearchResult } from '@pretto/places/dist/types';
20
23
 
21
- const result = await municipalitySearch.get("paris", { limit: 21 })
24
+ const result = await searchPlaces("paris", { limit: 21 })
22
25
 
23
- // expected result : Paris (75001), Paris (75002), Paris (75003), ..., Paris (75020)
24
26
  // result object format :
25
27
  [
26
28
  {
27
- "label": "Paris (75001)",
28
- "value": {
29
- "city": "Paris",
30
- "country": "fr",
31
- "zipcode": "75001"
32
- }
29
+ label: "Paris 1er Arrondissement 75001",
30
+ value: {
31
+ center: [2.3359, 48.862],
32
+ city: "Paris 1er Arrondissement",
33
+ country: "fr",
34
+ outline: [[{"latitude": 48.870688, "longitude": 2.347918},...]],
35
+ zipcode: "75001",
36
+ },
33
37
  },
34
38
  {
35
- "label": "Paris (75002)",
36
- "value": {
37
- "city": "Paris",
38
- "country": "fr",
39
- "zipcode": "75002"
40
- }
39
+ label: "Paris 2e Arrondissement 75002",
40
+ value: {
41
+ center: [2.3411, 48.8677],
42
+ city: "Paris 2e Arrondissement",
43
+ country: "fr",
44
+ outline: [Array],
45
+ zipcode: "75002",
46
+ },
41
47
  },
42
48
  ...
43
49
  ]
@@ -46,28 +52,36 @@ const result = await municipalitySearch.get("paris", { limit: 21 })
46
52
  For department only
47
53
 
48
54
  ```jsx
49
- import { municipalitySearch } from '@pretto/places'
55
+ import { searchDepartment } from '@pretto/places'
56
+ import { DepartmentSearchResult } from '@pretto/places/dist/types';
50
57
 
51
- const result = await municipalitySearch.get("paris", { departmentOnly: true, limit: 21 })
58
+ const result = await searchDepartment("pa", { limit: 21 })
52
59
 
53
- // expected result : Paris (75), Parisot(81), Parisot (82), Cormeilles-en-Parisis (95), ...
54
60
  // result object format :
55
61
  [
56
62
  {
57
- "label": "Paris (75)",
58
- "value": {
59
- "city": "Paris",
60
- "country": "fr",
61
- "zipcode": "75"
62
- }
63
+ label: "Paris (75)",
64
+ value: {
65
+ code: "75",
66
+ codeRegion: "11",
67
+ country: "fr",
68
+ region: {
69
+ code: "11",
70
+ nom: "Île-de-France"
71
+ }
72
+ },
63
73
  },
64
74
  {
65
- "label": "Parisot (81)",
66
- "value": {
67
- "city": "Parisot",
68
- "country": "fr",
69
- "zipcode": "81"
70
- }
75
+ label: "Pas-de-Calais (62)",
76
+ value: {
77
+ code: "62",
78
+ codeRegion: "32",
79
+ country: "fr",
80
+ region: {
81
+ code: "32",
82
+ nom: "Hauts-de-France"
83
+ }
84
+ },
71
85
  },
72
86
  ...
73
87
  ]
@@ -77,30 +91,30 @@ For Address (France only)
77
91
 
78
92
  ```jsx
79
93
  import { addressSearch } from '@pretto/places'
94
+ import { AddressSearchResult } from '@pretto/places/dist/types';
80
95
 
81
96
  const result = await addressSearch.get("55 rue de paradis", { limit: 10 })
82
97
 
83
- // expected result : 55 Rue de Paradis 75010 Paris (75010), 55 Rue de Paradis 51160 Hautvillers (51160)...
84
98
  // result object format :
85
99
  [
86
- {
87
- "label": "55 Rue de Paradis 75010 Paris (75010)",
88
- "value": {
89
- "city": "Paris",
90
- "country": "fr",
91
- "street": "55 Rue de Paradis",
92
- "zipcode": "75010"
93
- }
100
+ {
101
+ label: "42 Rue de Paradis 75010, Paris",
102
+ value: {
103
+ city: "Paris",
104
+ country: "fr",
105
+ street: "42 Rue de Paradis",
106
+ zipcode: "75010",
94
107
  },
95
- {
96
- "label": "55 Rue de Paradis 51160 Hautvillers (51160)",
97
- "value": {
98
- "city": "Hautvillers",
99
- "country": "fr",
100
- "street": "55 Rue de Paradis",
101
- "zipcode": "51160"
102
- }
108
+ },
109
+ {
110
+ label: "42 Rue de Paradis 95100, Argenteuil",
111
+ value: {
112
+ city: "Argenteuil",
113
+ country: "fr",
114
+ street: "42 Rue de Paradis",
115
+ zipcode: "95100",
103
116
  },
117
+ },
104
118
  ...
105
119
  ]
106
120
  ```
@@ -108,11 +122,10 @@ const result = await addressSearch.get("55 rue de paradis", { limit: 10 })
108
122
  For country
109
123
 
110
124
  ```jsx
111
- import { countrySearch } from '@pretto/places'
125
+ import { searchCountry } from '@pretto/places'
112
126
 
113
- const countriesApi = countrySearch.init(ALGOLIA_COUNTRIES_APP_ID, ALGOLIA_COUNTRIES_API_KEY)
127
+ const countriesApi = searchCountry.init(ALGOLIA_COUNTRIES_APP_ID, ALGOLIA_COUNTRIES_API_KEY)
114
128
  const results = await countriesApi.get('al', { limit: 10 })[
115
- // expected result : Allemagne (99109), Albanie (99125), Algerie (99352)
116
129
  // result object format :
117
130
  ({
118
131
  label: 'Allemagne (99109)',
@@ -129,44 +142,61 @@ const results = await countriesApi.get('al', { limit: 10 })[
129
142
  ]
130
143
  ```
131
144
 
132
- For geolocalisation
145
+ For reverse geolocalisation
133
146
 
134
147
  ```typescript
135
- import { geolocSearch } from '@pretto/places'
148
+ import { searchByCoordinates } from '@pretto/places'
149
+ import { MunicipalitySearchResult } from '@pretto/places/dist/types';
150
+
151
+ const results = await searchByCoordinates({ latitude: 48.87571729159758, longitude: 2.3503000982950883 })
136
152
 
137
- const results = await geolocSearch.get('75010')
138
153
  // result object format :
139
- // [{
140
- // city: 'Paris'
141
- // code: '75010',
142
- // coordinates: [2.347, 48.8589],
143
- // }]
154
+ [
155
+ {
156
+ label: "Paris 10e Arrondissement 75010",
157
+ value: {
158
+ center: [Object],
159
+ city: "Paris 10e Arrondissement",
160
+ country: "fr",
161
+ outline: [Array],
162
+ zipcode: "75010",
163
+ },
164
+ },
165
+ ];
144
166
  ```
145
167
 
146
- For reverse geolocalisation
168
+ ### Options
147
169
 
148
- ```typescript
149
- import { reverseGeolocSearch } from '@pretto/places'
170
+ You can pass options to the search function.
150
171
 
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
- ```
172
+ | Option | Type | Default Value |
173
+ | ------------ | ----------- | --------------- |
174
+ | debouncetime | number | 300 |
175
+ | limit | number | 10 |
176
+ | signal | AbortSignal | AbortController |
161
177
 
162
- ### Debounce
178
+ ```jsx
179
+ const debounceValue = 1000 // --- 1000 = 1s
180
+ const results = await searchPlaces('paris', { debouncetime: debounceValue, limit: 100 })
181
+ ```
163
182
 
164
- `addressSearch`, `municipalitySearch` and `geolocSearch` have defaut debounce value fixed to **300ms**\
165
- You can override this value by passing debounce value as last argument
183
+ The signal allows you to abort a request if you no longer want to wait for the response. If it's not defined in the options, the library will use a locally managed AbortController(). If you need to manage your own abort, you can do it like this:
166
184
 
167
185
  ```jsx
168
- const debounceValue = 1000 // --- 1000 = 1s
169
- const results = await countriesApi.get('al', { limit: 10 }, debounceValue)
186
+ let controller = null
187
+ ...
188
+ if(!search) {
189
+ controller.abort()
190
+ setResults([])
191
+ return
192
+ }
193
+
194
+ if(controller) {
195
+ controller.abort()
196
+ }
197
+
198
+ controller = new AbortController()
199
+ const places = await searchPlaces(search, {signal: controller.signal})
170
200
  ```
171
201
 
172
202
  ### How to publish a new version?
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pretto/places",
3
- "version": "0.40.0",
3
+ "version": "0.41.0",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",