Skip to content

Commit

Permalink
feat(s3-service): add custom config option (#240)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisowebb authored Jul 11, 2023
1 parent 4ed0676 commit 732f2a7
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 18 additions & 1 deletion __tests__/sendFile.unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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'],
Expand All @@ -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) }))
Expand Down Expand Up @@ -393,8 +408,10 @@ describe('SendFile Tests:', function() {
})
}) // end it


afterEach(function() {
stub.restore()
setConfigSpy.restore();
})

}) // end sendFile tests
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -46,6 +47,8 @@ class API {
? props.compression
: false;

this._s3Config = props && props.s3Config;

this._sampleCounts = {};

this._requestCount = 0;
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions lib/s3-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 732f2a7

Please sign in to comment.