@mountainpass/addressr 2.1.0 → 2.1.1

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.
@@ -9,6 +9,8 @@ exports.fetchGnafFile = fetchGnafFile;
9
9
  exports.getAddress = getAddress;
10
10
  exports.getAddresses = getAddresses;
11
11
  exports.getLocality = getLocality;
12
+ exports.getPostcode = getPostcode;
13
+ exports.getState = getState;
12
14
  exports.loadGnaf = loadGnaf;
13
15
  exports.mapAddressDetails = mapAddressDetails;
14
16
  exports.searchForAddress = searchForAddress;
@@ -934,20 +936,29 @@ async function getLocality(pid) {
934
936
  return resp;
935
937
  }
936
938
  async function searchForPostcode(searchString) {
939
+ const query = searchString && searchString.length > 0 ? {
940
+ bool: {
941
+ filter: [{
942
+ prefix: {
943
+ postcodes: searchString
944
+ }
945
+ }]
946
+ }
947
+ } : {
948
+ bool: {
949
+ filter: [{
950
+ exists: {
951
+ field: 'postcodes'
952
+ }
953
+ }]
954
+ }
955
+ };
937
956
  const searchResp = await globalThis.esClient.search({
938
957
  index: _elasticsearch.ES_LOCALITY_INDEX_NAME,
939
958
  body: {
940
959
  from: 0,
941
960
  size: 0,
942
- query: {
943
- bool: {
944
- filter: [{
945
- prefix: {
946
- postcodes: searchString
947
- }
948
- }]
949
- }
950
- },
961
+ query,
951
962
  aggs: {
952
963
  postcodes: {
953
964
  terms: {
@@ -969,6 +980,62 @@ async function searchForPostcode(searchString) {
969
980
  logger('postcode hits', JSON.stringify(searchResp.body.aggregations, undefined, 2));
970
981
  return searchResp;
971
982
  }
983
+ async function getPostcode(postcode) {
984
+ const searchResp = await globalThis.esClient.search({
985
+ index: _elasticsearch.ES_LOCALITY_INDEX_NAME,
986
+ body: {
987
+ size: 0,
988
+ query: {
989
+ term: {
990
+ postcodes: postcode
991
+ }
992
+ },
993
+ aggs: {
994
+ localities: {
995
+ terms: {
996
+ field: 'locality_name.raw',
997
+ size: 100
998
+ }
999
+ }
1000
+ }
1001
+ }
1002
+ });
1003
+ return searchResp;
1004
+ }
1005
+ async function getState(abbreviation) {
1006
+ const searchResp = await globalThis.esClient.search({
1007
+ index: _elasticsearch.ES_LOCALITY_INDEX_NAME,
1008
+ body: {
1009
+ size: 0,
1010
+ query: {
1011
+ term: {
1012
+ state_abbreviation: abbreviation.toUpperCase()
1013
+ }
1014
+ },
1015
+ aggs: {
1016
+ state_name: {
1017
+ terms: {
1018
+ field: 'state_name',
1019
+ size: 1
1020
+ }
1021
+ },
1022
+ localities: {
1023
+ terms: {
1024
+ field: 'locality_name.raw',
1025
+ size: 1000
1026
+ }
1027
+ },
1028
+ postcodes: {
1029
+ terms: {
1030
+ field: 'postcodes',
1031
+ size: 1000
1032
+ }
1033
+ }
1034
+ }
1035
+ }
1036
+ });
1037
+ return searchResp;
1038
+ }
972
1039
  async function searchForState(searchString) {
973
1040
  const query = searchString ? {
974
1041
  bool: {
@@ -177,38 +177,49 @@ function startRest2Server() {
177
177
  }]
178
178
  });
179
179
  const postcodesType = waycharter.registerCollection({
180
+ itemPath: '/:postcode',
181
+ itemLoader: async ({
182
+ postcode
183
+ }) => {
184
+ const result = await (0, _addressService.getPostcode)(postcode);
185
+ const localities = result.body.aggregations.localities.buckets.map(l => ({
186
+ name: l.key
187
+ }));
188
+ const body = {
189
+ postcode,
190
+ localities
191
+ };
192
+ const hash = _nodeCrypto.default.createHash('md5').update(JSON.stringify(body)).digest('hex');
193
+ return {
194
+ body,
195
+ headers: {
196
+ etag: `"${_version.version}-${hash}"`,
197
+ 'cache-control': `public, max-age=${ONE_WEEK}`
198
+ },
199
+ status: 200
200
+ };
201
+ },
180
202
  collectionPath: '/postcodes',
181
203
  collectionLoader: async ({
182
204
  q
183
205
  }) => {
184
- if (q && q.length > 2) {
185
- const result = await (0, _addressService.searchForPostcode)(q);
186
- const buckets = result.body.aggregations.postcodes.buckets;
187
- const body = buckets.map(bucket => ({
188
- postcode: bucket.key,
189
- localities: bucket.localities.buckets.map(l => ({
190
- name: l.key
191
- }))
192
- }));
193
- const responseHash = _nodeCrypto.default.createHash('md5').update(JSON.stringify(body)).digest('hex');
194
- return {
195
- body,
196
- hasMore: false,
197
- headers: {
198
- etag: `"${_version.version}-${responseHash}"`,
199
- 'cache-control': `public, max-age=${ONE_WEEK}`
200
- }
201
- };
202
- } else {
203
- return {
204
- body: [],
205
- hasMore: false,
206
- headers: {
207
- etag: `"${_version.version}"`,
208
- 'cache-control': `public, max-age=${ONE_WEEK}`
209
- }
210
- };
211
- }
206
+ const result = await (0, _addressService.searchForPostcode)(q || '');
207
+ const buckets = result.body.aggregations.postcodes.buckets;
208
+ const body = buckets.map(bucket => ({
209
+ postcode: bucket.key,
210
+ localities: bucket.localities.buckets.map(l => ({
211
+ name: l.key
212
+ }))
213
+ }));
214
+ const responseHash = _nodeCrypto.default.createHash('md5').update(JSON.stringify(body)).digest('hex');
215
+ return {
216
+ body,
217
+ hasMore: false,
218
+ headers: {
219
+ etag: `"${_version.version}-${responseHash}"`,
220
+ 'cache-control': `public, max-age=${ONE_WEEK}`
221
+ }
222
+ };
212
223
  },
213
224
  filters: [{
214
225
  rel: 'https://addressr.io/rels/postcode-search',
@@ -216,11 +227,37 @@ function startRest2Server() {
216
227
  }]
217
228
  });
218
229
  const statesType = waycharter.registerCollection({
230
+ itemPath: '/:abbreviation',
231
+ itemLoader: async ({
232
+ abbreviation
233
+ }) => {
234
+ const result = await (0, _addressService.getState)(abbreviation);
235
+ const stateName = result.body.aggregations.state_name.buckets[0]?.key || abbreviation.toUpperCase();
236
+ const localities = result.body.aggregations.localities.buckets.map(l => ({
237
+ name: l.key
238
+ }));
239
+ const postcodes = result.body.aggregations.postcodes.buckets.map(p => p.key);
240
+ const body = {
241
+ abbreviation: abbreviation.toUpperCase(),
242
+ name: stateName,
243
+ localities,
244
+ postcodes
245
+ };
246
+ const hash = _nodeCrypto.default.createHash('md5').update(JSON.stringify(body)).digest('hex');
247
+ return {
248
+ body,
249
+ headers: {
250
+ etag: `"${_version.version}-${hash}"`,
251
+ 'cache-control': `public, max-age=${ONE_WEEK}`
252
+ },
253
+ status: 200
254
+ };
255
+ },
219
256
  collectionPath: '/states',
220
257
  collectionLoader: async ({
221
258
  q
222
259
  }) => {
223
- const result = await (0, _addressService.searchForState)(q && q.length > 1 ? q : undefined);
260
+ const result = await (0, _addressService.searchForState)(q || undefined);
224
261
  const buckets = result.body.aggregations.states.buckets;
225
262
  const body = buckets.map(bucket => ({
226
263
  abbreviation: bucket.key,
package/lib/version.js CHANGED
@@ -5,4 +5,4 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.version = void 0;
7
7
  // Generated by genversion.
8
- const version = exports.version = '2.1.0';
8
+ const version = exports.version = '2.1.1';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mountainpass/addressr",
3
- "version": "2.1.0",
3
+ "version": "2.1.1",
4
4
  "description": "Australian Address Validation, Search and Autocomplete",
5
5
  "author": {
6
6
  "name": "Mountain Pass",