Skip to content

Commit b09fee4

Browse files
committed
add links and featureCount to output
1 parent acaeef4 commit b09fee4

File tree

4 files changed

+24
-11
lines changed

4 files changed

+24
-11
lines changed

src/ngd.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { BBox, Config } from "./types.js";
99
import { initialiseConfig } from "./utils/config.js";
1010
import fetch from "node-fetch";
1111

12-
export { ngd };
12+
export { ngd, NGDLink };
1313

1414
// Types
1515
interface NGDExtent {
@@ -109,7 +109,7 @@ const ngd = {
109109
filterCRS?: null | string | number;
110110
} = {}
111111
): Promise<FeatureCollection> => {
112-
validateParams({bbox, datetime})
112+
validateParams({ bbox, datetime });
113113
const config = initialiseConfig(apiKey, offset, limit);
114114
config.url = buildUrl(collectionId, {
115115
bbox,

src/utils/ngd/request.ts

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,16 @@ import { continuePaging, logEndConditions } from "../request.js";
44
import { logging } from "../logging.js";
55

66
import fetch, { type Response } from "node-fetch"; // not required in Node17.5 (LTS) onwards
7-
7+
import { type NGDLink } from "../../ngd.js";
88
import { type FeatureCollection } from "geojson";
99

1010
export { request, get };
1111

12+
interface NGDOutput extends FeatureCollection {
13+
links: NGDLink[];
14+
numberReturned: number;
15+
}
16+
1217
async function get(endpoint: string, key: string): Promise<Response> {
1318
logging.info("🔍 " + endpoint);
1419
return fetch(endpoint, {
@@ -37,12 +42,20 @@ function getOffsetEndpointNGD(config: Config, featureCount: number): string {
3742
return config.url + "&offset=" + config.paging.position + "&limit=" + limit;
3843
}
3944

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> {
4152
let endpoint: string;
4253
let featureCount = 0;
43-
let output: FeatureCollection | undefined = {
54+
let output: NGDOutput | undefined = {
4455
type: "FeatureCollection",
4556
features: [],
57+
numberReturned: 0,
58+
links: [],
4659
};
4760

4861
const getEndpoint = config.paging.enabled
@@ -54,11 +67,10 @@ async function request(config: Config): Promise<FeatureCollection> {
5467

5568
let response: Response = await get(endpoint, config.key);
5669

57-
const responseJson: FeatureCollection = <FeatureCollection>(
58-
await response.json()
59-
);
70+
const responseJson: NGDOutput = <NGDOutput>await response.json();
6071

6172
output.features = output.features.concat(responseJson.features);
73+
output.links = output.links.concat(filterSelfLink(responseJson.links));
6274

6375
if (responseJson.features && responseJson.features.length == 100) {
6476
config.paging.position += 100;
@@ -75,5 +87,6 @@ async function request(config: Config): Promise<FeatureCollection> {
7587
throw Error("There is no output at the end of request");
7688
}
7789

90+
output.numberReturned = featureCount;
7891
return output;
7992
}

src/utils/ngd/url.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// src/utils/ngd/url.ts
2-
import { BBox } from "../../types";
3-
import { getCRS } from "../crs";
2+
import { BBox } from "../../types.js";
3+
import { getCRS } from "../crs.js";
44

55
export { buildUrl, root };
66

src/utils/ngd/validate.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// src/utils/validate.ts
22

3-
import { BBox } from "../../types";
3+
import { BBox } from "../../types.js";
44

55
export { validateParams, datetimeError };
66

0 commit comments

Comments
 (0)