@@ -4,11 +4,16 @@ import { continuePaging, logEndConditions } from "../request.js";
4
4
import { logging } from "../logging.js" ;
5
5
6
6
import fetch , { type Response } from "node-fetch" ; // not required in Node17.5 (LTS) onwards
7
-
7
+ import { type NGDLink } from "../../ngd.js" ;
8
8
import { type FeatureCollection } from "geojson" ;
9
9
10
10
export { request , get } ;
11
11
12
+ interface NGDOutput extends FeatureCollection {
13
+ links : NGDLink [ ] ;
14
+ numberReturned : number ;
15
+ }
16
+
12
17
async function get ( endpoint : string , key : string ) : Promise < Response > {
13
18
logging . info ( "🔍 " + endpoint ) ;
14
19
return fetch ( endpoint , {
@@ -37,12 +42,20 @@ function getOffsetEndpointNGD(config: Config, featureCount: number): string {
37
42
return config . url + "&offset=" + config . paging . position + "&limit=" + limit ;
38
43
}
39
44
40
- async function request ( config : Config ) : Promise < FeatureCollection > {
45
+ function filterSelfLink ( links : NGDLink [ ] ) : NGDLink {
46
+ return links . filter ( ( link ) => {
47
+ return link . rel == "self" ;
48
+ } ) [ 0 ] ;
49
+ }
50
+
51
+ async function request ( config : Config ) : Promise < NGDOutput > {
41
52
let endpoint : string ;
42
53
let featureCount = 0 ;
43
- let output : FeatureCollection | undefined = {
54
+ let output : NGDOutput | undefined = {
44
55
type : "FeatureCollection" ,
45
56
features : [ ] ,
57
+ numberReturned : 0 ,
58
+ links : [ ] ,
46
59
} ;
47
60
48
61
const getEndpoint = config . paging . enabled
@@ -54,11 +67,10 @@ async function request(config: Config): Promise<FeatureCollection> {
54
67
55
68
let response : Response = await get ( endpoint , config . key ) ;
56
69
57
- const responseJson : FeatureCollection = < FeatureCollection > (
58
- await response . json ( )
59
- ) ;
70
+ const responseJson : NGDOutput = < NGDOutput > await response . json ( ) ;
60
71
61
72
output . features = output . features . concat ( responseJson . features ) ;
73
+ output . links = output . links . concat ( filterSelfLink ( responseJson . links ) ) ;
62
74
63
75
if ( responseJson . features && responseJson . features . length == 100 ) {
64
76
config . paging . position += 100 ;
@@ -75,5 +87,6 @@ async function request(config: Config): Promise<FeatureCollection> {
75
87
throw Error ( "There is no output at the end of request" ) ;
76
88
}
77
89
90
+ output . numberReturned = featureCount ;
78
91
return output ;
79
92
}
0 commit comments