Skip to content

Commit

Permalink
CMR-147: Addressing PR comments, adding common getBaseUrl function
Browse files Browse the repository at this point in the history
  • Loading branch information
william-valencia committed Oct 15, 2024
1 parent 1b37e99 commit bf65b2f
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 12 deletions.
2 changes: 0 additions & 2 deletions src/__tests__/providerCatalog.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,6 @@ describe("GET /:provider", () => {

describe(`given the provider has a collection`, () => {
it("has a child link for that collection without query parameters", async function () {
this.timeout(5000);

sandbox
.stub(Provider, "getProviders")
.resolves([null, [{ "provider-id": "TEST", "short-name": "TEST" }]]);
Expand Down
9 changes: 3 additions & 6 deletions src/routes/browse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Links } from "../@types/StacCatalog";
import { getCollections } from "../domains/collections";
import { buildQuery, stringifyQuery } from "../domains/stac";
import { ItemNotFound } from "../models/errors";
import { mergeMaybe, stacContext } from "../utils";
import { getBaseUrl, mergeMaybe, stacContext } from "../utils";

const collectionLinks = (req: Request, nextCursor?: string | null): Links => {
const { stacRoot, self, path } = stacContext(req);
Expand Down Expand Up @@ -64,13 +64,10 @@ export const collectionsHandler = async (req: Request, res: Response): Promise<v

const { stacRoot, self } = stacContext(req);

// Remove query parameters from the URL, keeping only the base path
const baseUrl = self.replace(/\?.*$/, "");

collections.forEach((collection) => {
collection.links.push({
rel: "self",
href: `${baseUrl}/${encodeURIComponent(collection.id)}`,
href: `${getBaseUrl(self)}/${encodeURIComponent(collection.id)}`,
type: "application/json",
});
collection.links.push({
Expand All @@ -92,7 +89,7 @@ export const collectionsHandler = async (req: Request, res: Response): Promise<v
if (!itemsLink) {
collection.links.push({
rel: "items",
href: `${baseUrl}/${encodeURIComponent(collection.id)}/items`,
href: `${getBaseUrl(self)}/${encodeURIComponent(collection.id)}/items`,
type: "application/json",
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/routes/catalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Links, STACCatalog } from "../@types/StacCatalog";
import { getAllCollectionIds } from "../domains/collections";
import { conformance } from "../domains/providers";
import { ServiceUnavailableError } from "../models/errors";
import { mergeMaybe, stacContext } from "../utils";
import { getBaseUrl, mergeMaybe, stacContext } from "../utils";

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

Expand Down Expand Up @@ -105,7 +105,7 @@ export const providerCatalogHandler = async (req: Request, res: Response) => {
const selfLinks = generateSelfLinks(req);
const childLinks = (collections ?? []).map(({ id, title }) => ({
rel: "child",
href: `${self.replace(/\?.*$/, "")}/collections/${encodeURIComponent(id)}`,
href: `${getBaseUrl(self)}/collections/${encodeURIComponent(id)}`,
title,
type: "application/json",
}));
Expand Down
4 changes: 2 additions & 2 deletions src/routes/rootCatalog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Link, STACCatalog } from "../@types/StacCatalog";

import { conformance } from "../domains/providers";
import { Provider } from "../models/CmrModels";
import { stacContext } from "../utils";
import { getBaseUrl, stacContext } from "../utils";

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

Expand Down Expand Up @@ -45,7 +45,7 @@ const providerLinks = (req: Request, providers: Provider[]): Link[] => {
rel: "child",
title,
type: "application/json",
href: `${self.replace(/\?.*$/, "")}/${providerId}`,
href: `${getBaseUrl(self)}/${providerId}`,
}));
};

Expand Down
8 changes: 8 additions & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,11 @@ export const generatePossibleCollectionIds = (id: string, separator: string, rep

return tokensCopy.join(separator);
});

/**
* Returns the base URL
* This is used to remove Query Parameters that may have been added to the URL.
*/
export const getBaseUrl = (url: string) => {
return url.replace(/\?.*$/, "");
};

0 comments on commit bf65b2f

Please sign in to comment.