Skip to content

Commit 45f4802

Browse files
authored
Merge pull request #165 from mgifford/copilot/update-core-url-support
fix: forward url_limit as query param to DAP API endpoint
2 parents 3c16f2f + da4b5b0 commit 45f4802

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

src/ingest/dap-source.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,17 @@ export async function fetchDapRecords({ endpoint, fetchImpl = fetch }) {
105105
return extractArrayPayload(payload);
106106
}
107107

108-
function buildDapEndpoint(endpoint, apiKey) {
108+
function buildDapEndpoint(endpoint, apiKey, { limit } = {}) {
109109
const url = new URL(endpoint);
110110

111111
if (apiKey && !url.searchParams.has('api_key')) {
112112
url.searchParams.set('api_key', apiKey);
113113
}
114114

115+
if (limit != null && !url.searchParams.has('limit')) {
116+
url.searchParams.set('limit', String(limit));
117+
}
118+
115119
return url.toString();
116120
}
117121

@@ -133,7 +137,7 @@ export async function getNormalizedTopPages({
133137
if (sourceFile) {
134138
rawRecords = await readDapRecordsFromFile(sourceFile);
135139
} else {
136-
const resolvedEndpoint = buildDapEndpoint(endpoint, dapApiKey);
140+
const resolvedEndpoint = buildDapEndpoint(endpoint, dapApiKey, { limit });
137141
rawRecords = await fetchDapRecords({ endpoint: resolvedEndpoint, fetchImpl });
138142
}
139143

tests/unit/dap-ingest.test.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,45 @@ test('normalizeDapRecords filters out DAP placeholder entries like (other)', ()
5959
assert.equal(normalized.records[0].url, 'https://example.gov');
6060
assert.equal(normalized.excluded.filter((e) => e.reason === 'placeholder_url').length, 2, 'Should exclude both placeholder entries');
6161
});
62+
63+
test('getNormalizedTopPages forwards limit as a query param to the API endpoint', async () => {
64+
let capturedEndpoint;
65+
const mockFetch = async (url) => {
66+
capturedEndpoint = url;
67+
return {
68+
ok: true,
69+
json: async () => [{ url: 'https://example.gov', page_load_count: 1000 }]
70+
};
71+
};
72+
73+
await getNormalizedTopPages({
74+
endpoint: 'https://api.gsa.gov/analytics/dap/v2.0.0/reports/site/data',
75+
limit: 50,
76+
sourceDate: '2026-03-01',
77+
fetchImpl: mockFetch
78+
});
79+
80+
const resolved = new URL(capturedEndpoint);
81+
assert.equal(resolved.searchParams.get('limit'), '50', 'limit should be forwarded as a query param');
82+
});
83+
84+
test('getNormalizedTopPages does not override a limit already present in the endpoint URL', async () => {
85+
let capturedEndpoint;
86+
const mockFetch = async (url) => {
87+
capturedEndpoint = url;
88+
return {
89+
ok: true,
90+
json: async () => [{ url: 'https://example.gov', page_load_count: 1000 }]
91+
};
92+
};
93+
94+
await getNormalizedTopPages({
95+
endpoint: 'https://api.gsa.gov/analytics/dap/v2.0.0/reports/site/data?limit=200',
96+
limit: 50,
97+
sourceDate: '2026-03-01',
98+
fetchImpl: mockFetch
99+
});
100+
101+
const resolved = new URL(capturedEndpoint);
102+
assert.equal(resolved.searchParams.get('limit'), '200', 'pre-set limit in endpoint URL should not be overridden');
103+
});

0 commit comments

Comments
 (0)