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

Ensure event object has expected members #33

Merged
merged 1 commit into from
Oct 20, 2022
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
105 changes: 36 additions & 69 deletions docs/docs/spec/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,8 @@ paths:
tags:
- Collection
parameters:
- name: page
in: query
required: false
description: Page number of results to retrieve
schema:
type: integer
minimum: 1
- $ref: "./types.yaml#/components/parameters/page"
- $ref: "./types.yaml#/components/parameters/size"
responses:
200:
$ref: "./types.yaml#/components/responses/SearchResponse"
Expand All @@ -43,6 +38,7 @@ paths:
operationId: getCollectionById
parameters:
- $ref: "./types.yaml#/components/parameters/id"
- $ref: "./types.yaml#/components/parameters/as"
responses:
200:
$ref: "./types.yaml#/components/responses/DocumentResponse"
Expand All @@ -53,23 +49,9 @@ paths:
- Collection
parameters:
- $ref: "./types.yaml#/components/parameters/id"
- name: aspect
in: query
required: false
description: Desired aspect ratio
schema:
type: string
enum:
- full
- square
- name: size
in: query
required: false
description: Size of largest dimension
schema:
type: integer
minimum: 1
maximum: 300
- $ref: "./types.yaml#/components/parameters/id"
- $ref: "./types.yaml#/components/parameters/thumbnailAspect"
- $ref: "./types.yaml#/components/parameters/thumbnailSize"
responses:
200:
description: A thumbnail image for the given collection
Expand Down Expand Up @@ -98,30 +80,25 @@ paths:
responses:
200:
$ref: "./types.yaml#/components/responses/DocumentResponse"
/works/{id}/similar:
get:
operationId: getSimilarWorks
tags:
- Work
parameters:
- $ref: "./types.yaml#/components/parameters/id"
- $ref: "./types.yaml#/components/parameters/page"
- $ref: "./types.yaml#/components/parameters/size"
- $ref: "./types.yaml#/components/parameters/as"
/works/{id}/thumbnail:
get:
operationId: getWorkThumbnail
tags:
- Work
parameters:
- $ref: "./types.yaml#/components/parameters/id"
- name: aspect
in: query
required: false
description: Desired aspect ratio
schema:
type: string
enum:
- full
- square
- name: size
in: query
required: false
description: Size of largest dimension
schema:
type: integer
minimum: 1
maximum: 300
- $ref: "./types.yaml#/components/parameters/thumbnailAspect"
- $ref: "./types.yaml#/components/parameters/thumbnailSize"
responses:
200:
description: A thumbnail image for the given work
Expand All @@ -136,34 +113,11 @@ paths:
tags:
- Search
parameters:
- name: query
in: query
required: false
description: Keyword query
schema:
type: string
- name: searchToken
in: query
required: false
description: Search token from previous search response
schema:
type: string
- name: page
in: query
required: false
description: Page number of results to retrieve
schema:
type: integer
minimum: 1
- name: as
in: query
required: false
description: Desired output format
schema:
type: string
enum:
- iiif
- opensearch
- $ref: "./types.yaml#/components/parameters/query"
- $ref: "./types.yaml#/components/parameters/searchToken"
- $ref: "./types.yaml#/components/parameters/page"
- $ref: "./types.yaml#/components/parameters/size"
- $ref: "./types.yaml#/components/parameters/as"
responses:
200:
$ref: "./types.yaml#/components/responses/SearchResponse"
Expand All @@ -181,6 +135,19 @@ paths:
200:
$ref: "./types.yaml#/components/responses/SearchResponse"
/search/{models}:
get:
operationId: getSearch
tags:
- Search
parameters:
- $ref: "./types.yaml#/components/parameters/query"
- $ref: "./types.yaml#/components/parameters/searchToken"
- $ref: "./types.yaml#/components/parameters/page"
- $ref: "./types.yaml#/components/parameters/size"
- $ref: "./types.yaml#/components/parameters/as"
responses:
200:
$ref: "./types.yaml#/components/responses/SearchResponse"
post:
operationId: postSearchWithModels
tags:
Expand Down
59 changes: 59 additions & 0 deletions docs/docs/spec/types.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,65 @@ components:
schema:
type: string
format: uuid
query:
name: query
in: query
required: false
description: Keyword query
schema:
type: string
searchToken:
name: searchToken
in: query
required: false
description: Search token from previous search response
schema:
type: string
page:
name: page
in: query
required: false
description: Page number of results to retrieve
schema:
type: integer
minimum: 1
size:
name: size
in: query
required: false
description: Maximum number of results per page
schema:
type: integer
minimum: 0
as:
name: as
in: query
required: false
description: Desired output format
schema:
type: string
enum:
- iiif
- opensearch
thumbnailAspect:
name: aspect
in: query
required: false
description: Desired aspect ratio
schema:
type: string
enum:
- full
- square
thumbnailSize:
name: size
in: query
required: false
description: Size of largest dimension
schema:
type: integer
minimum: 1
maximum: 300
responses:
DocumentResponse:
description: A single document response
Expand Down
4 changes: 3 additions & 1 deletion src/handlers/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ const {
decodeEventBody,
normalizeHeaders,
objectifyCookies,
stubEventMembers,
} = require("../helpers");

const processRequest = function (event) {
let result = normalizeHeaders(event);
let result = stubEventMembers(event);
result = normalizeHeaders(event);
result = objectifyCookies(result);
result = decodeEventBody(result);
return result;
Expand Down
10 changes: 10 additions & 0 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,21 @@ function isFromReadingRoom(event) {
return AllowedIPs.includes(sourceIp);
}

function stubEventMembers(event) {
event.headers ||= {};
event.pathParameters ||= {};
event.queryStringParameters ||= {};
event.stageVariables ||= {};
event.cookies ||= [];
return event;
}

module.exports = {
addCorsHeaders,
baseUrl,
decodeEventBody,
isFromReadingRoom,
normalizeHeaders,
objectifyCookies,
stubEventMembers,
};
3 changes: 0 additions & 3 deletions test/test-helpers/event-builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ module.exports = class {
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https",
},
queryStringParameters: {},
requestContext: {
accountId: "123456789012",
apiId: "pewpew",
Expand All @@ -35,9 +34,7 @@ module.exports = class {
timeEpoch: Number(now),
},
body: "",
pathParameters: {},
isBase64Encoded: false,
stageVariables: {},
};
}

Expand Down
10 changes: 10 additions & 0 deletions test/unit/api/helpers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
isFromReadingRoom,
normalizeHeaders,
objectifyCookies,
stubEventMembers,
} = require("../../../src/helpers");
const chai = require("chai");
const expect = chai.expect;
Expand Down Expand Up @@ -200,4 +201,13 @@ describe("helpers", () => {
});
});
});

describe("stubEventMembers", () => {
it("makes sure the event has all expected members", () => {
const result = stubEventMembers({});
expect(result.cookies).to.eql([]);
expect(result.pathParameters).to.eql({});
expect(result.queryStringParameters).to.eql({});
});
});
});