...

/

Request Processor and Concrete Adapters

Request Processor and Concrete Adapters

In this lesson, you'll see how to go about implementing the request processor along with concrete adapters using the Ports and Adapters design pattern.

For the ShowFormFunction handler, the core business logic is to validate that a requested upload has an extension and that the extension belongs to a list of approved file types. For valid extensions, the function needs to create a unique upload file name based on the request ID and the extension, and produce a signed upload policy and a signed download policy. This business core will have three ports (shown in the figure provided below):

  1. The first port should provide the request ID and the extension, starting the process, and format the resulting signatures accordingly.
  2. The second port should sign upload policies based on a key.
  3. The third port should sign download policies based on a key.

The core business logic should not care about AWS interfaces, but instead describe the workflow using concepts important for a specific use case, such as signing uploads and downloads. You can create a new file called request-processor.js in the user-form directory with the code from the following listing.

Press + to interact
module.exports = class RequestProcessor {
constructor(uploadSigner, downloadSigner, uploadLimitInMB, allowedExtensions) {
this.uploadSigner = uploadSigner;
this.downloadSigner = downloadSigner;
this.uploadLimitInMB = uploadLimitInMB;
this.allowedExtensions = allowedExtensions;
};
processRequest(requestId, extension) {
if (!extension) {
throw `no extension provided`;
}
const normalisedExtension = extension.toLowerCase();
const isImage = this.allowedExtensions.includes(normalisedExtension);
if (!isImage) {
throw `extension ${extension} is not supported`;
}
const fileKey = `${requestId}.${normalisedExtension}`;
return {
upload: this.uploadSigner.signUpload(fileKey, this.uploadLimitInMB),
download: this.downloadSigner.signDownload(fileKey)
};
};
};
...
Access this course and 1400+ top-rated courses and projects.