Skip to content

Commit

Permalink
Merge pull request #162 from nulib/deploy/staging
Browse files Browse the repository at this point in the history
Production push
  • Loading branch information
adamjarling authored Aug 31, 2023
2 parents 1d15a1c + 1a7c242 commit 612f228
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/api/response/iiif/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,19 @@ function homepageUrl(pageInfo) {
result = new URL(`/collections/${collectionId}`, dcUrl());
} else {
result = new URL("/search", dcUrl());
if (pageInfo.options?.queryStringParameters?.query)
if (pageInfo.options?.queryStringParameters?.query) {
result.searchParams.set(
"q",
pageInfo.options.queryStringParameters.query
);
}

if (pageInfo.query_url.includes("similar")) {
// Grab the work id from the query_url and add it to the search params
const regex = /works\/(.*)\/similar/;
const found = pageInfo.query_url.match(regex);
result.searchParams.set("similar", found[1]);
}
}

return result;
Expand Down
21 changes: 20 additions & 1 deletion src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,29 @@ const AcceptableHeaders = [
"X-Forwarded-Port",
"X-Requested-With",
];

const ExposedHeaders = [
"Cache-Control",
"Content-Language",
"Content-Length",
"Content-Type",
"Date",
"ETag",
"Expires",
"Last-Modified",
"Pragma",
];

const TextTypes = new RegExp(/^(application\/(json|(.+\+)?xml)$|text\/)/);

function addCorsHeaders(event, response) {
const allowOrigin = event?.headers?.origin || "*";
const corsHeaders = {
"Access-Control-Allow-Origin": allowOrigin,
"Access-Control-Allow-Headers": AcceptableHeaders.join(", "),
"Access-Control-Allow-Methods": "POST, GET, OPTIONS",
"Access-Control-Allow-Methods": "POST, GET, HEAD, OPTIONS",
"Access-Control-Allow-Credentials": "true",
"Access-Control-Expose-Headers": ExposedHeaders.join(", "),
"Access-Control-Max-Age": "600",
};
if (!response.headers) response.headers = {};
Expand Down Expand Up @@ -169,6 +183,11 @@ function normalizeHeaders(event) {

function baseUrl(event) {
event = normalizeHeaders(event);

// For use with the local https-proxy in dev mode
if (event.headers["x-forwarded-base"])
return event.headers["x-forwarded-base"];

const scheme = event.headers["x-forwarded-proto"];

// The localhost check only matters in dev mode, but it's
Expand Down
27 changes: 27 additions & 0 deletions test/unit/api/response/iiif/collection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,31 @@ describe("IIIF Collection response transformer", () => {
expect(body.status).to.eq(404);
expect(body.error).to.be.a("string");
});

it("handles a request including /similar route", async () => {
let pagerWorkSimilar = new Paginator(
"http://dcapi.library.northwestern.edu/api/v2/",
"works/1234/similar",
["works"],
{ query: { query_string: { query: "genre.label:architecture" } } },
"iiif",
{
includeToken: false,
queryStringParameters: {
collectionLabel: "The collection label",
collectionSummary: "The collection Summary",
},
}
);

const response = {
statusCode: 200,
body: helpers.testFixture("mocks/search.json"),
};
const result = await transformer.transform(response, pagerWorkSimilar);
expect(result.statusCode).to.eq(200);

const body = JSON.parse(result.body);
expect(body.homepage[0].id).to.contain("search?similar=1234");
});
});

0 comments on commit 612f228

Please sign in to comment.