Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Production push #162

Merged
merged 5 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
});
});
Loading