Skip to content

Commit

Permalink
CMR-10118: Address PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dmistry1 committed Oct 17, 2024
1 parent 1432e10 commit 4694cbb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
6 changes: 4 additions & 2 deletions src/__tests__/providerCatalog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import ItemSpec from "../../resources/item-spec/json-schema/item.json";

import { Link } from "../@types/StacCatalog";
import { createApp } from "../app";
import * as Provider from "../domains/providers";
import * as Collections from "../domains/collections";
import * as Provider from "../domains/providers";
import * as stac from "../domains/stac";
import { generateSTACCollections } from "../utils/testUtils";

const stacApp = createApp();
Expand Down Expand Up @@ -167,6 +168,7 @@ describe("GET /:provider", () => {

describe("when there are more results available", () => {
it("includes a 'next' link with the correct query parameters", async () => {
sandbox.stub(stac, 'CMR_QUERY_MAX').value(100);
sandbox
.stub(Provider, "getProviders")
.resolves([null, [{ "provider-id": "TEST", "short-name": "TEST" }]]);
Expand All @@ -182,7 +184,6 @@ describe("GET /:provider", () => {

const { body: catalog } = await request(stacApp).get(`/stac/TEST`);

console.log("🚀 ~ it.only ~ catalog:", catalog);
const nextLink = catalog.links.find((l: Link) => l.rel === "next");
expect(nextLink).to.exist;
expect(nextLink.rel).to.equal("next");
Expand All @@ -196,6 +197,7 @@ describe("GET /:provider", () => {

describe("when there are no more results available", () => {
it("does not include a 'next' link", async () => {
sandbox.stub(stac, 'CMR_QUERY_MAX').value(100);
sandbox
.stub(Provider, "getProviders")
.resolves([null, [{ "provider-id": "TEST", "short-name": "TEST" }]]);
Expand Down
17 changes: 11 additions & 6 deletions src/routes/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { getAllCollectionIds } from "../domains/collections";
import { conformance } from "../domains/providers";
import { ServiceUnavailableError } from "../models/errors";
import { getBaseUrl, mergeMaybe, stacContext } from "../utils";
import { stringifyQuery } from "../domains/stac";
import { CMR_QUERY_MAX, stringifyQuery } from "../domains/stac";

const STAC_VERSION = process.env.STAC_VERSION ?? "1.0.0";

const generateSelfLinks = (req: Request, nextCursor?: string | null): Links => {
const generateSelfLinks = (req: Request, nextCursor?: string | null, count?: number): Links => {
const { stacRoot, path, self } = stacContext(req);

const links = [
Expand Down Expand Up @@ -76,7 +76,12 @@ const generateSelfLinks = (req: Request, nextCursor?: string | null): Links => {

const originalQuery = mergeMaybe(req.query, req.body);

if (nextCursor) {
// Add a 'next' link if there are more results available
// This is determined by:
// 1. The presence of a nextCursor (indicating more results)
// 2. The number of collection equaling CMR_QUERY_MAX (100)
// The 'next' link includes the original query parameters plus the new cursor
if (nextCursor && count === CMR_QUERY_MAX) {
const nextResultsQuery = { ...originalQuery, cursor: nextCursor };

links.push({
Expand All @@ -97,13 +102,13 @@ const providerCollections = async (

const cloudOnly = headers["cloud-stac"] === "true" ? { cloudHosted: true } : {};

const query2 = mergeMaybe(
const mergedQuery = mergeMaybe(
{ provider: provider?.["provider-id"], cursor: query?.cursor },
{ ...cloudOnly }
);

try {
const { items, cursor } = await getAllCollectionIds(query2, { headers });
const { items, cursor } = await getAllCollectionIds(mergedQuery, { headers });
return [null, items, cursor];
} catch (err) {
console.error("A problem occurred querying for collections.", err);
Expand All @@ -122,7 +127,7 @@ export const providerCatalogHandler = async (req: Request, res: Response) => {

const { self } = stacContext(req);

const selfLinks = generateSelfLinks(req, cursor);
const selfLinks = generateSelfLinks(req, cursor, collections?.length);

const childLinks = (collections ?? []).map(({ id, title }) => ({
rel: "child",
Expand Down

0 comments on commit 4694cbb

Please sign in to comment.