@pretto/places 0.2.0 → 0.3.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.
@@ -20,7 +20,7 @@ interface Options {
20
20
  country: string;
21
21
  limit: number;
22
22
  }
23
- declare type AddressSearch = (search: string, options?: Options) => Promise<AddressSearchResult[]>;
23
+ declare type AddressSearch = (search: string, options?: Options) => Promise<AddressSearchResult[] | string>;
24
24
  export declare const get: AddressSearch;
25
25
  export {};
26
26
  //# sourceMappingURL=addressSearch.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"addressSearch.d.ts","sourceRoot":"","sources":["../src/addressSearch.ts"],"names":[],"mappings":"AAAA,UAAU,mBAAmB;IAC3B,KAAK,EAAE,MAAM,CAAA;IACb,KAAK,EAAE;QACL,IAAI,EAAE,MAAM,CAAA;QACZ,OAAO,EAAE,MAAM,CAAA;QACf,MAAM,EAAE,MAAM,CAAA;QACd,OAAO,EAAE,MAAM,CAAA;KAChB,CAAA;CACF;AAED,MAAM,WAAW,gBAAgB;IAC/B,UAAU,EAAE;QACV,KAAK,EAAE,MAAM,CAAA;QACb,QAAQ,EAAE,MAAM,CAAA;QAChB,IAAI,EAAE,MAAM,CAAA;QACZ,WAAW,EAAE,MAAM,CAAA;QACnB,MAAM,EAAE,MAAM,CAAA;KACf,CAAA;CACF;AAED,UAAU,OAAO;IACf,OAAO,EAAE,MAAM,CAAA;IACf,KAAK,EAAE,MAAM,CAAA;CACd;AAED,aAAK,aAAa,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,KAAK,OAAO,CAAC,mBAAmB,EAAE,CAAC,CAAA;AAE1F,eAAO,MAAM,GAAG,EAAE,aAmBjB,CAAA"}
1
+ {"version":3,"file":"addressSearch.d.ts","sourceRoot":"","sources":["../src/addressSearch.ts"],"names":[],"mappings":"AAAA,UAAU,mBAAmB;IAC3B,KAAK,EAAE,MAAM,CAAA;IACb,KAAK,EAAE;QACL,IAAI,EAAE,MAAM,CAAA;QACZ,OAAO,EAAE,MAAM,CAAA;QACf,MAAM,EAAE,MAAM,CAAA;QACd,OAAO,EAAE,MAAM,CAAA;KAChB,CAAA;CACF;AAED,MAAM,WAAW,gBAAgB;IAC/B,UAAU,EAAE;QACV,KAAK,EAAE,MAAM,CAAA;QACb,QAAQ,EAAE,MAAM,CAAA;QAChB,IAAI,EAAE,MAAM,CAAA;QACZ,WAAW,EAAE,MAAM,CAAA;QACnB,MAAM,EAAE,MAAM,CAAA;KACf,CAAA;CACF;AAED,UAAU,OAAO;IACf,OAAO,EAAE,MAAM,CAAA;IACf,KAAK,EAAE,MAAM,CAAA;CACd;AAED,aAAK,aAAa,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,KAAK,OAAO,CAAC,mBAAmB,EAAE,GAAG,MAAM,CAAC,CAAA;AAEnG,eAAO,MAAM,GAAG,EAAE,aA4BjB,CAAA"}
@@ -12,7 +12,7 @@ export interface CountriesApiResult {
12
12
  interface Options {
13
13
  limit: number;
14
14
  }
15
- export declare const init: (appId: string, apiKey: string, options?: AlgoliaSearchOptions | undefined) => {
15
+ export declare const init: (appId: string, apiKey: string, options?: AlgoliaSearchOptions | undefined) => "An error occurred." | {
16
16
  get(search: string, options?: Options | undefined): Promise<CountriesSearchResult[]>;
17
17
  };
18
18
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"countrySearch.d.ts","sourceRoot":"","sources":["../src/countrySearch.ts"],"names":[],"mappings":"AAAA,OAAsB,EAAE,oBAAoB,EAAgB,MAAM,eAAe,CAAA;AAIjF,UAAU,qBAAqB;IAC7B,KAAK,EAAE,MAAM,CAAA;IACb,KAAK,EAAE,MAAM,CAAA;CACd;AAED,MAAM,WAAW,kBAAkB;IACjC,QAAQ,EAAE,MAAM,CAAA;IAChB,GAAG,EAAE,MAAM,CAAA;IACX,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,EAAE,MAAM,CAAA;CACf;AAED,UAAU,OAAO;IACf,KAAK,EAAE,MAAM,CAAA;CACd;AAsBD,eAAO,MAAM,IAAI,UAAW,MAAM,UAAU,MAAM;gBAK1B,MAAM;CAO7B,CAAA"}
1
+ {"version":3,"file":"countrySearch.d.ts","sourceRoot":"","sources":["../src/countrySearch.ts"],"names":[],"mappings":"AAAA,OAAsB,EAAE,oBAAoB,EAAgB,MAAM,eAAe,CAAA;AAIjF,UAAU,qBAAqB;IAC7B,KAAK,EAAE,MAAM,CAAA;IACb,KAAK,EAAE,MAAM,CAAA;CACd;AAED,MAAM,WAAW,kBAAkB;IACjC,QAAQ,EAAE,MAAM,CAAA;IAChB,GAAG,EAAE,MAAM,CAAA;IACX,MAAM,EAAE,MAAM,CAAA;IACd,MAAM,EAAE,MAAM,CAAA;CACf;AAED,UAAU,OAAO;IACf,KAAK,EAAE,MAAM,CAAA;CACd;AAsBD,eAAO,MAAM,IAAI,UAAW,MAAM,UAAU,MAAM;gBAK1B,MAAM;CAS7B,CAAA"}
package/dist/index.js CHANGED
@@ -80,23 +80,34 @@ const get$2 = (search, options) => __awaiter(void 0, void 0, void 0, function* (
80
80
  const isNumber = !isNaN(parseInt(search, 10));
81
81
  if (!search || (isNumber && search.length < 2))
82
82
  return [];
83
- if (isNumber && search.length > 2 && search.length < 5) {
84
- return Array.from(SEARCH_CACHE).filter(i => i.value.zipcode.indexOf(search) !== -1);
83
+ if (isNumber && search.length > 2 && search.length < 5 && SEARCH_CACHE.length > 0) {
84
+ return SEARCH_CACHE.filter(i => i.value.zipcode.includes(search));
85
85
  }
86
- const { country = 'fr', limit = 5 } = options || {};
87
- const response = yield fetch(getUri(search, limit));
88
- const result = yield response.json();
89
- const formatedResponse = responseFormat(result, country, search);
90
- if (isEqual__default["default"](PREVIOUS_SEARCH, formatedResponse)) {
91
- const cleanValue = search.replace(' ', '').replace(PREVIOUS_VALUE, '');
92
- return Array.from(PREVIOUS_SEARCH).filter(i => i.label.indexOf(cleanValue) !== -1);
86
+ try {
87
+ const { country = 'fr', limit = 5 } = options || {};
88
+ const response = yield fetch(getUri(search, limit));
89
+ if (!response.ok)
90
+ return Promise.reject(response);
91
+ const result = yield response.json();
92
+ const formatedResponse = responseFormat(result, country, search);
93
+ if (isEqual__default["default"](PREVIOUS_SEARCH, formatedResponse)) {
94
+ const cleanValue = search.replace(' ', '').replace(PREVIOUS_VALUE, '');
95
+ return PREVIOUS_SEARCH.filter(i => i.label.includes(cleanValue));
96
+ }
97
+ PREVIOUS_VALUE = search;
98
+ PREVIOUS_SEARCH = formatedResponse;
99
+ if (isNumber && search.length !== 5) {
100
+ SEARCH_CACHE = formatedResponse;
101
+ }
102
+ return formatedResponse;
93
103
  }
94
- PREVIOUS_VALUE = search;
95
- PREVIOUS_SEARCH = formatedResponse;
96
- if (isNumber && search.length !== 5) {
97
- SEARCH_CACHE = formatedResponse;
104
+ catch (error) {
105
+ if (typeof error === 'string')
106
+ throw new Error(error);
107
+ if (error instanceof Error)
108
+ throw new Error((error === null || error === void 0 ? void 0 : error.message) || 'An error occurred.');
109
+ return 'An error occurred.';
98
110
  }
99
- return formatedResponse;
100
111
  });
101
112
 
102
113
  var municipalitySearch = /*#__PURE__*/Object.freeze({
@@ -107,21 +118,32 @@ var municipalitySearch = /*#__PURE__*/Object.freeze({
107
118
  const get$1 = (search, options) => __awaiter(void 0, void 0, void 0, function* () {
108
119
  if (!search)
109
120
  return [];
110
- const { country = 'fr', limit = 5 } = options || {};
111
- const format = encodeURIComponent(search);
112
- const response = yield fetch(`https://api-adresse.data.gouv.fr/search/?q=${format}&autocomplete=1&limit=${limit}`);
113
- const result = yield response.json();
114
- return result.features.map(({ properties }) => {
115
- return {
116
- label: `${properties.label} (${properties.postcode})`,
117
- value: {
118
- city: properties.city,
119
- country,
120
- street: `${properties.housenumber} ${properties.street}`,
121
- zipcode: properties.postcode,
122
- },
123
- };
124
- });
121
+ try {
122
+ const { country = 'fr', limit = 5 } = options || {};
123
+ const format = encodeURIComponent(search);
124
+ const response = yield fetch(`https://api-adresse.data.gouv.fr/search/?q=${format}&autocomplete=1&limit=${limit}`);
125
+ if (!response.ok)
126
+ return Promise.reject(response);
127
+ const result = yield response.json();
128
+ return result.features.map(({ properties }) => {
129
+ return {
130
+ label: `${properties.label} (${properties.postcode})`,
131
+ value: {
132
+ city: properties.city,
133
+ country,
134
+ street: `${properties.housenumber} ${properties.street}`,
135
+ zipcode: properties.postcode,
136
+ },
137
+ };
138
+ });
139
+ }
140
+ catch (error) {
141
+ if (typeof error === 'string')
142
+ throw new Error(error);
143
+ if (error instanceof Error)
144
+ throw new Error((error === null || error === void 0 ? void 0 : error.message) || 'An error occurred.');
145
+ return 'An error occurred.';
146
+ }
125
147
  });
126
148
 
127
149
  var addressSearch = /*#__PURE__*/Object.freeze({
@@ -139,8 +161,8 @@ const get = (instance, search, options) => __awaiter(void 0, void 0, void 0, fun
139
161
  return data.hits.reduce((acc, { codeiso2, cog, libcog, libenr }) => {
140
162
  if (codeiso2 === '')
141
163
  return acc;
142
- const formatCountryName = convertFirstLetterToUpperCase(libcog.indexOf('(') !== -1 ? libenr : libcog);
143
- const formatCog = cog.indexOf('X') !== -1 ? `(${cog})` : '';
164
+ const formatCountryName = convertFirstLetterToUpperCase(libcog.includes('(') ? libenr : libcog);
165
+ const formatCog = cog.includes('X') ? `(${cog})` : '';
144
166
  const label = `${formatCountryName} ${formatCog}`;
145
167
  return [...acc, { label, value: codeiso2.toLowerCase() }];
146
168
  }, []);
@@ -157,7 +179,11 @@ const init = (appId, apiKey, options) => {
157
179
  };
158
180
  }
159
181
  catch (error) {
160
- throw new Error((error === null || error === void 0 ? void 0 : error.message) || 'Invalid Application-ID or API key');
182
+ if (typeof error === 'string')
183
+ throw new Error(error);
184
+ if (error instanceof Error)
185
+ throw new Error((error === null || error === void 0 ? void 0 : error.message) || 'An error occurred.');
186
+ return 'An error occurred.';
161
187
  }
162
188
  };
163
189
 
package/dist/module.js CHANGED
@@ -70,23 +70,34 @@ const get$2 = (search, options) => __awaiter(void 0, void 0, void 0, function* (
70
70
  const isNumber = !isNaN(parseInt(search, 10));
71
71
  if (!search || (isNumber && search.length < 2))
72
72
  return [];
73
- if (isNumber && search.length > 2 && search.length < 5) {
74
- return Array.from(SEARCH_CACHE).filter(i => i.value.zipcode.indexOf(search) !== -1);
73
+ if (isNumber && search.length > 2 && search.length < 5 && SEARCH_CACHE.length > 0) {
74
+ return SEARCH_CACHE.filter(i => i.value.zipcode.includes(search));
75
75
  }
76
- const { country = 'fr', limit = 5 } = options || {};
77
- const response = yield fetch(getUri(search, limit));
78
- const result = yield response.json();
79
- const formatedResponse = responseFormat(result, country, search);
80
- if (isEqual(PREVIOUS_SEARCH, formatedResponse)) {
81
- const cleanValue = search.replace(' ', '').replace(PREVIOUS_VALUE, '');
82
- return Array.from(PREVIOUS_SEARCH).filter(i => i.label.indexOf(cleanValue) !== -1);
76
+ try {
77
+ const { country = 'fr', limit = 5 } = options || {};
78
+ const response = yield fetch(getUri(search, limit));
79
+ if (!response.ok)
80
+ return Promise.reject(response);
81
+ const result = yield response.json();
82
+ const formatedResponse = responseFormat(result, country, search);
83
+ if (isEqual(PREVIOUS_SEARCH, formatedResponse)) {
84
+ const cleanValue = search.replace(' ', '').replace(PREVIOUS_VALUE, '');
85
+ return PREVIOUS_SEARCH.filter(i => i.label.includes(cleanValue));
86
+ }
87
+ PREVIOUS_VALUE = search;
88
+ PREVIOUS_SEARCH = formatedResponse;
89
+ if (isNumber && search.length !== 5) {
90
+ SEARCH_CACHE = formatedResponse;
91
+ }
92
+ return formatedResponse;
83
93
  }
84
- PREVIOUS_VALUE = search;
85
- PREVIOUS_SEARCH = formatedResponse;
86
- if (isNumber && search.length !== 5) {
87
- SEARCH_CACHE = formatedResponse;
94
+ catch (error) {
95
+ if (typeof error === 'string')
96
+ throw new Error(error);
97
+ if (error instanceof Error)
98
+ throw new Error((error === null || error === void 0 ? void 0 : error.message) || 'An error occurred.');
99
+ return 'An error occurred.';
88
100
  }
89
- return formatedResponse;
90
101
  });
91
102
 
92
103
  var municipalitySearch = /*#__PURE__*/Object.freeze({
@@ -97,21 +108,32 @@ var municipalitySearch = /*#__PURE__*/Object.freeze({
97
108
  const get$1 = (search, options) => __awaiter(void 0, void 0, void 0, function* () {
98
109
  if (!search)
99
110
  return [];
100
- const { country = 'fr', limit = 5 } = options || {};
101
- const format = encodeURIComponent(search);
102
- const response = yield fetch(`https://api-adresse.data.gouv.fr/search/?q=${format}&autocomplete=1&limit=${limit}`);
103
- const result = yield response.json();
104
- return result.features.map(({ properties }) => {
105
- return {
106
- label: `${properties.label} (${properties.postcode})`,
107
- value: {
108
- city: properties.city,
109
- country,
110
- street: `${properties.housenumber} ${properties.street}`,
111
- zipcode: properties.postcode,
112
- },
113
- };
114
- });
111
+ try {
112
+ const { country = 'fr', limit = 5 } = options || {};
113
+ const format = encodeURIComponent(search);
114
+ const response = yield fetch(`https://api-adresse.data.gouv.fr/search/?q=${format}&autocomplete=1&limit=${limit}`);
115
+ if (!response.ok)
116
+ return Promise.reject(response);
117
+ const result = yield response.json();
118
+ return result.features.map(({ properties }) => {
119
+ return {
120
+ label: `${properties.label} (${properties.postcode})`,
121
+ value: {
122
+ city: properties.city,
123
+ country,
124
+ street: `${properties.housenumber} ${properties.street}`,
125
+ zipcode: properties.postcode,
126
+ },
127
+ };
128
+ });
129
+ }
130
+ catch (error) {
131
+ if (typeof error === 'string')
132
+ throw new Error(error);
133
+ if (error instanceof Error)
134
+ throw new Error((error === null || error === void 0 ? void 0 : error.message) || 'An error occurred.');
135
+ return 'An error occurred.';
136
+ }
115
137
  });
116
138
 
117
139
  var addressSearch = /*#__PURE__*/Object.freeze({
@@ -129,8 +151,8 @@ const get = (instance, search, options) => __awaiter(void 0, void 0, void 0, fun
129
151
  return data.hits.reduce((acc, { codeiso2, cog, libcog, libenr }) => {
130
152
  if (codeiso2 === '')
131
153
  return acc;
132
- const formatCountryName = convertFirstLetterToUpperCase(libcog.indexOf('(') !== -1 ? libenr : libcog);
133
- const formatCog = cog.indexOf('X') !== -1 ? `(${cog})` : '';
154
+ const formatCountryName = convertFirstLetterToUpperCase(libcog.includes('(') ? libenr : libcog);
155
+ const formatCog = cog.includes('X') ? `(${cog})` : '';
134
156
  const label = `${formatCountryName} ${formatCog}`;
135
157
  return [...acc, { label, value: codeiso2.toLowerCase() }];
136
158
  }, []);
@@ -147,7 +169,11 @@ const init = (appId, apiKey, options) => {
147
169
  };
148
170
  }
149
171
  catch (error) {
150
- throw new Error((error === null || error === void 0 ? void 0 : error.message) || 'Invalid Application-ID or API key');
172
+ if (typeof error === 'string')
173
+ throw new Error(error);
174
+ if (error instanceof Error)
175
+ throw new Error((error === null || error === void 0 ? void 0 : error.message) || 'An error occurred.');
176
+ return 'An error occurred.';
151
177
  }
152
178
  };
153
179
 
@@ -14,7 +14,7 @@ interface Options {
14
14
  country: string;
15
15
  limit: number;
16
16
  }
17
- declare type MunicipalitySearch = (search: string, options?: Options) => Promise<MunicipalitySearchResult[]>;
17
+ declare type MunicipalitySearch = (search: string, options?: Options) => Promise<MunicipalitySearchResult[] | string>;
18
18
  export declare const get: MunicipalitySearch;
19
19
  export {};
20
20
  //# sourceMappingURL=municipalitySearch.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"municipalitySearch.d.ts","sourceRoot":"","sources":["../src/municipalitySearch.ts"],"names":[],"mappings":"AAKA,UAAU,wBAAwB;IAChC,KAAK,EAAE,MAAM,CAAA;IACb,KAAK,EAAE;QACL,IAAI,EAAE,MAAM,CAAA;QACZ,OAAO,EAAE,MAAM,CAAA;QACf,OAAO,EAAE,MAAM,CAAA;KAChB,CAAA;CACF;AAED,MAAM,WAAW,qBAAqB;IACpC,GAAG,EAAE,MAAM,CAAA;IACX,YAAY,EAAE,MAAM,EAAE,CAAA;CACvB;AAED,UAAU,OAAO;IACf,OAAO,EAAE,MAAM,CAAA;IACf,KAAK,EAAE,MAAM,CAAA;CACd;AAED,aAAK,kBAAkB,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,KAAK,OAAO,CAAC,wBAAwB,EAAE,CAAC,CAAA;AA0BpG,eAAO,MAAM,GAAG,EAAE,kBA6BjB,CAAA"}
1
+ {"version":3,"file":"municipalitySearch.d.ts","sourceRoot":"","sources":["../src/municipalitySearch.ts"],"names":[],"mappings":"AAKA,UAAU,wBAAwB;IAChC,KAAK,EAAE,MAAM,CAAA;IACb,KAAK,EAAE;QACL,IAAI,EAAE,MAAM,CAAA;QACZ,OAAO,EAAE,MAAM,CAAA;QACf,OAAO,EAAE,MAAM,CAAA;KAChB,CAAA;CACF;AAED,MAAM,WAAW,qBAAqB;IACpC,GAAG,EAAE,MAAM,CAAA;IACX,YAAY,EAAE,MAAM,EAAE,CAAA;CACvB;AAED,UAAU,OAAO;IACf,OAAO,EAAE,MAAM,CAAA;IACf,KAAK,EAAE,MAAM,CAAA;CACd;AAED,aAAK,kBAAkB,GAAG,CAAC,MAAM,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,OAAO,KAAK,OAAO,CAAC,wBAAwB,EAAE,GAAG,MAAM,CAAC,CAAA;AA0B7G,eAAO,MAAM,GAAG,EAAE,kBAsCjB,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pretto/places",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -15,15 +15,16 @@
15
15
  "dist/**/*"
16
16
  ],
17
17
  "dependencies": {
18
- "@types/array.prototype.flat": "^1.2.1",
19
18
  "algoliasearch": "^4.13.0",
20
19
  "array.prototype.flat": "^1.3.0",
21
20
  "lodash.isequal": "^4.5.0"
22
21
  },
23
22
  "devDependencies": {
24
23
  "@rollup/plugin-typescript": "^8.3.2",
24
+ "@types/array.prototype.flat": "^1.2.1",
25
25
  "@types/lodash.isequal": "^4.5.5",
26
26
  "deepmerge": "^4.2.2",
27
+ "eslint": "^8.14.0",
27
28
  "rimraf": "^3.0.2",
28
29
  "rollup": "^2.70.2",
29
30
  "tslib": "^2.3.1",