From 732f2a7c0478716a83eb9fb704c4288b8ba4172c Mon Sep 17 00:00:00 2001 From: Chris Webb <86809221+chrisowebb@users.noreply.github.com> Date: Tue, 11 Jul 2023 22:06:48 +0100 Subject: [PATCH] feat(s3-service): add custom config option (#240) --- README.md | 1 + __tests__/sendFile.unit.js | 19 ++++++++++++++++++- index.js | 6 ++++++ lib/s3-service.js | 1 + 4 files changed, 26 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5fc03a5..24a499b 100644 --- a/README.md +++ b/README.md @@ -152,6 +152,7 @@ Require the `lambda-api` module into your Lambda handler script and instantiate | serializer | `Function` | Optional object serializer function. This function receives the `body` of a response and must return a string. Defaults to `JSON.stringify` | | version | `String` | Version number accessible via the `REQUEST` object | | errorHeaderWhitelist | `Array` | Array of headers to maintain on errors | +| s3Config | `Object` | Optional object to provide as config to S3 sdk. [S3ClientConfig](https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-s3/interfaces/s3clientconfig.html) | ```javascript // Require the framework and instantiate it with optional version and base parameters diff --git a/__tests__/sendFile.unit.js b/__tests__/sendFile.unit.js index 92bb110..6400e3b 100644 --- a/__tests__/sendFile.unit.js +++ b/__tests__/sendFile.unit.js @@ -10,7 +10,7 @@ const sinon = require('sinon') const S3 = require('../lib/s3-service'); // Init S3 Service // Init API instance -const api = require('../index')({ version: 'v1.0', mimeTypes: { test: 'text/test' } }) +const api = require('../index')({ version: 'v1.0', mimeTypes: { test: 'text/test' }}) let event = { httpMethod: 'get', @@ -184,10 +184,12 @@ api.use(function(err,req,res,next) { let stub describe('SendFile Tests:', function() { + let setConfigSpy; beforeEach(function() { // Stub getObjectAsync stub = sinon.stub(S3,'getObject') + setConfigSpy = sinon.spy(S3, 'setConfig'); }) it('Bad path', async function() { @@ -345,6 +347,7 @@ describe('SendFile Tests:', function() { it('S3 file', async function() { let _event = Object.assign({},event,{ path: '/sendfile/s3' }) let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) })) + sinon.assert.notCalled(setConfigSpy); expect(result).toEqual({ multiValueHeaders: { 'content-type': ['text/plain'], @@ -356,6 +359,18 @@ describe('SendFile Tests:', function() { }) }) // end it + it('S3 file w/ custom config',async function() { + const s3Config = { + endpoint: "http://test" + } + const apiWithConfig = require('../index')({ version: 'v1.0', mimeTypes: { test: 'text/test' }, s3Config}) + let _event = Object.assign({},event,{ path: '/sendfile/s3' }) + await new Promise(r => apiWithConfig.run(_event,{ + s3Config + },(e,res) => { r(res) })) + sinon.assert.calledWith(setConfigSpy, s3Config); + }) // end it + it('S3 file w/ nested path', async function() { let _event = Object.assign({},event,{ path: '/sendfile/s3path' }) let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) })) @@ -393,8 +408,10 @@ describe('SendFile Tests:', function() { }) }) // end it + afterEach(function() { stub.restore() + setConfigSpy.restore(); }) }) // end sendFile tests diff --git a/index.js b/index.js index e3f0c4a..cdd9b43 100644 --- a/index.js +++ b/index.js @@ -9,6 +9,7 @@ const REQUEST = require('./lib/request'); const RESPONSE = require('./lib/response'); const UTILS = require('./lib/utils'); const LOGGER = require('./lib/logger'); +const S3 = require('./lib/s3-service'); const prettyPrint = require('./lib/prettyPrint'); const { ConfigurationError } = require('./lib/errors'); @@ -46,6 +47,8 @@ class API { ? props.compression : false; + this._s3Config = props && props.s3Config; + this._sampleCounts = {}; this._requestCount = 0; @@ -284,6 +287,9 @@ class API { this._context = this.context = typeof context === 'object' ? context : {}; this._cb = cb ? cb : undefined; + // Set S3 Client + if (this._s3Config) S3.setConfig(this._s3Config); + // Initalize request and response objects let request = new REQUEST(this); let response = new RESPONSE(this, request); diff --git a/lib/s3-service.js b/lib/s3-service.js index 9f860fc..0ed5ece 100644 --- a/lib/s3-service.js +++ b/lib/s3-service.js @@ -12,6 +12,7 @@ const { streamToBuffer } = require('./utils'); // Export exports.client = new S3Client(); +exports.setConfig = (config) => (exports.client = new S3Client(config)); exports.getObject = (params) => { return {