@@ -6,7 +6,11 @@ import {
6
6
HttpOptions ,
7
7
Paginateable ,
8
8
} from "./types" ;
9
- import { IdpcPostcodeNotFoundError , IdpcUdprnNotFoundError } from "./error" ;
9
+ import {
10
+ IdpcPostcodeNotFoundError ,
11
+ IdpcUmprnNotFoundError ,
12
+ IdpcUdprnNotFoundError ,
13
+ } from "./error" ;
10
14
import { Address } from "@ideal-postcodes/api-typings" ;
11
15
import {
12
16
appendAuthorization ,
@@ -15,7 +19,12 @@ import {
15
19
appendFilter ,
16
20
appendTags ,
17
21
} from "./util" ;
18
- import { Request } from "./resources/resource" ;
22
+ import { Request as BaseRequest } from "./resources/resource" ;
23
+
24
+ interface Request extends BaseRequest {
25
+ query : StringMap ;
26
+ header : StringMap ;
27
+ }
19
28
20
29
type Protocol = "http" | "https" ;
21
30
@@ -82,11 +91,25 @@ import {
82
91
UmprnResource ,
83
92
} from "./resources/umprn" ;
84
93
85
- interface LookupPostcodeOptions
94
+ interface LookupIdOptions
95
+ extends Authenticable ,
96
+ Filterable ,
97
+ Taggable ,
98
+ HttpOptions { }
99
+
100
+ interface LookupAddressOptions
86
101
extends Authenticable ,
87
102
Filterable ,
88
103
Taggable ,
104
+ Paginateable ,
89
105
HttpOptions {
106
+ /**
107
+ * Query for address
108
+ */
109
+ query : string ;
110
+ }
111
+
112
+ interface LookupPostcodeOptions extends LookupIdOptions {
90
113
/**
91
114
* Postcode to query for. Space and case insensitive
92
115
*/
@@ -99,27 +122,18 @@ interface LookupPostcodeOptions
99
122
page ?: number ;
100
123
}
101
124
102
- interface LookupAddressOptions
103
- extends Authenticable ,
104
- Filterable ,
105
- Taggable ,
106
- Paginateable ,
107
- HttpOptions {
125
+ interface LookupUdprnOptions extends LookupIdOptions {
108
126
/**
109
- * Query for address
127
+ * UDPRN to query for
110
128
*/
111
- query : string ;
129
+ udprn : number ;
112
130
}
113
131
114
- interface LookupUdprnOptions
115
- extends Authenticable ,
116
- Filterable ,
117
- Taggable ,
118
- HttpOptions {
132
+ interface LookupUmprnOptions extends LookupIdOptions {
119
133
/**
120
- * UDPRN to query for
134
+ * UMPRN to query for
121
135
*/
122
- udprn : number ;
136
+ umprn : number ;
123
137
}
124
138
125
139
export class Client {
@@ -200,19 +214,10 @@ export class Client {
200
214
* [API Documentation for /postcodes](https://ideal-postcodes.co.uk/documentation/postcodes#postcode)
201
215
*/
202
216
lookupPostcode ( options : LookupPostcodeOptions ) : Promise < Address [ ] > {
203
- const header : StringMap = { } ;
204
- const query : StringMap = { } ;
205
-
206
- appendAuthorization ( { client : this , header, options } ) ;
207
- appendIp ( { header, options } ) ;
208
- appendFilter ( { query, options } ) ;
209
- appendTags ( { query, options } ) ;
217
+ const queryOptions = this . toAddressIdQuery ( options ) ;
210
218
211
219
const { page } = options ;
212
- if ( page !== undefined ) query . page = page . toString ( ) ;
213
-
214
- const queryOptions : Request = { header, query } ;
215
- if ( options . timeout !== undefined ) queryOptions . timeout = options . timeout ;
220
+ if ( page !== undefined ) queryOptions . query . page = page . toString ( ) ;
216
221
217
222
return this . postcodes
218
223
. retrieve ( options . postcode , queryOptions )
@@ -249,15 +254,15 @@ export class Client {
249
254
}
250
255
251
256
/**
252
- * lookupUdprn
253
- *
254
- * Search for an address given a UDPRN
257
+ * toAddressIdQuery
255
258
*
256
- * Invalid UDPRN returns `null`
257
- *
258
- * [API Documentation for /udprn](https://ideal-postcodes.co.uk/documentation/udprn)
259
+ * Generates a request object. Bundles together commonly used header/query extractions:
260
+ * - Authorization (api_key, licensee, user_token)
261
+ * - Source IP forwarding
262
+ * - Result filtering
263
+ * - Tagging
259
264
*/
260
- lookupUdprn ( options : LookupUdprnOptions ) : Promise < Address | null > {
265
+ private toAddressIdQuery ( options : LookupIdOptions ) : Request {
261
266
const header : StringMap = { } ;
262
267
const query : StringMap = { } ;
263
268
@@ -266,9 +271,23 @@ export class Client {
266
271
appendFilter ( { query, options } ) ;
267
272
appendTags ( { query, options } ) ;
268
273
269
- const queryOptions : Request = { header, query } ;
270
- if ( options . timeout !== undefined ) queryOptions . timeout = options . timeout ;
274
+ const request : Request = { header, query } ;
275
+ if ( options . timeout !== undefined ) request . timeout = options . timeout ;
276
+
277
+ return request ;
278
+ }
271
279
280
+ /**
281
+ * lookupUdprn
282
+ *
283
+ * Search for an address given a UDPRN
284
+ *
285
+ * Invalid UDPRN returns `null`
286
+ *
287
+ * [API Documentation for /udprn](https://ideal-postcodes.co.uk/documentation/udprn)
288
+ */
289
+ lookupUdprn ( options : LookupUdprnOptions ) : Promise < Address | null > {
290
+ const queryOptions = this . toAddressIdQuery ( options ) ;
272
291
return this . udprn
273
292
. retrieve ( options . udprn . toString ( ) , queryOptions )
274
293
. then ( response => response . body . result )
@@ -277,4 +296,24 @@ export class Client {
277
296
throw error ;
278
297
} ) ;
279
298
}
299
+
300
+ /**
301
+ * lookupUdprn
302
+ *
303
+ * Search for an address given a UDPRN
304
+ *
305
+ * Invalid UDPRN returns `null`
306
+ *
307
+ * [API Documentation for /udprn](https://ideal-postcodes.co.uk/documentation/udprn)
308
+ */
309
+ lookupUmprn ( options : LookupUmprnOptions ) : Promise < Address | null > {
310
+ const queryOptions = this . toAddressIdQuery ( options ) ;
311
+ return this . umprn
312
+ . retrieve ( options . umprn . toString ( ) , queryOptions )
313
+ . then ( response => response . body . result )
314
+ . catch ( error => {
315
+ if ( error instanceof IdpcUmprnNotFoundError ) return null ;
316
+ throw error ;
317
+ } ) ;
318
+ }
280
319
}
0 commit comments