diff --git a/src/api/response/iiif/collection.js b/src/api/response/iiif/collection.js index b071141f..5f4e0723 100644 --- a/src/api/response/iiif/collection.js +++ b/src/api/response/iiif/collection.js @@ -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; diff --git a/src/helpers.js b/src/helpers.js index 7a5d6bf3..f5b6f384 100644 --- a/src/helpers.js +++ b/src/helpers.js @@ -37,6 +37,19 @@ 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) { @@ -44,8 +57,9 @@ function addCorsHeaders(event, response) { 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 = {}; @@ -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 diff --git a/test/unit/api/response/iiif/collection.test.js b/test/unit/api/response/iiif/collection.test.js index 034c548f..0883408c 100644 --- a/test/unit/api/response/iiif/collection.test.js +++ b/test/unit/api/response/iiif/collection.test.js @@ -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"); + }); });