Skip to content

Commit 732f2a7

Browse files
authored
feat(s3-service): add custom config option (#240)
1 parent 4ed0676 commit 732f2a7

File tree

4 files changed

+26
-1
lines changed

4 files changed

+26
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ Require the `lambda-api` module into your Lambda handler script and instantiate
152152
| serializer | `Function` | Optional object serializer function. This function receives the `body` of a response and must return a string. Defaults to `JSON.stringify` |
153153
| version | `String` | Version number accessible via the `REQUEST` object |
154154
| errorHeaderWhitelist | `Array` | Array of headers to maintain on errors |
155+
| 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) |
155156

156157
```javascript
157158
// Require the framework and instantiate it with optional version and base parameters

__tests__/sendFile.unit.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const sinon = require('sinon')
1010
const S3 = require('../lib/s3-service'); // Init S3 Service
1111

1212
// Init API instance
13-
const api = require('../index')({ version: 'v1.0', mimeTypes: { test: 'text/test' } })
13+
const api = require('../index')({ version: 'v1.0', mimeTypes: { test: 'text/test' }})
1414

1515
let event = {
1616
httpMethod: 'get',
@@ -184,10 +184,12 @@ api.use(function(err,req,res,next) {
184184
let stub
185185

186186
describe('SendFile Tests:', function() {
187+
let setConfigSpy;
187188

188189
beforeEach(function() {
189190
// Stub getObjectAsync
190191
stub = sinon.stub(S3,'getObject')
192+
setConfigSpy = sinon.spy(S3, 'setConfig');
191193
})
192194

193195
it('Bad path', async function() {
@@ -345,6 +347,7 @@ describe('SendFile Tests:', function() {
345347
it('S3 file', async function() {
346348
let _event = Object.assign({},event,{ path: '/sendfile/s3' })
347349
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
350+
sinon.assert.notCalled(setConfigSpy);
348351
expect(result).toEqual({
349352
multiValueHeaders: {
350353
'content-type': ['text/plain'],
@@ -356,6 +359,18 @@ describe('SendFile Tests:', function() {
356359
})
357360
}) // end it
358361

362+
it('S3 file w/ custom config',async function() {
363+
const s3Config = {
364+
endpoint: "http://test"
365+
}
366+
const apiWithConfig = require('../index')({ version: 'v1.0', mimeTypes: { test: 'text/test' }, s3Config})
367+
let _event = Object.assign({},event,{ path: '/sendfile/s3' })
368+
await new Promise(r => apiWithConfig.run(_event,{
369+
s3Config
370+
},(e,res) => { r(res) }))
371+
sinon.assert.calledWith(setConfigSpy, s3Config);
372+
}) // end it
373+
359374
it('S3 file w/ nested path', async function() {
360375
let _event = Object.assign({},event,{ path: '/sendfile/s3path' })
361376
let result = await new Promise(r => api.run(_event,{},(e,res) => { r(res) }))
@@ -393,8 +408,10 @@ describe('SendFile Tests:', function() {
393408
})
394409
}) // end it
395410

411+
396412
afterEach(function() {
397413
stub.restore()
414+
setConfigSpy.restore();
398415
})
399416

400417
}) // end sendFile tests

index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const REQUEST = require('./lib/request');
99
const RESPONSE = require('./lib/response');
1010
const UTILS = require('./lib/utils');
1111
const LOGGER = require('./lib/logger');
12+
const S3 = require('./lib/s3-service');
1213
const prettyPrint = require('./lib/prettyPrint');
1314
const { ConfigurationError } = require('./lib/errors');
1415

@@ -46,6 +47,8 @@ class API {
4647
? props.compression
4748
: false;
4849

50+
this._s3Config = props && props.s3Config;
51+
4952
this._sampleCounts = {};
5053

5154
this._requestCount = 0;
@@ -284,6 +287,9 @@ class API {
284287
this._context = this.context = typeof context === 'object' ? context : {};
285288
this._cb = cb ? cb : undefined;
286289

290+
// Set S3 Client
291+
if (this._s3Config) S3.setConfig(this._s3Config);
292+
287293
// Initalize request and response objects
288294
let request = new REQUEST(this);
289295
let response = new RESPONSE(this, request);

lib/s3-service.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const { streamToBuffer } = require('./utils');
1212

1313
// Export
1414
exports.client = new S3Client();
15+
exports.setConfig = (config) => (exports.client = new S3Client(config));
1516

1617
exports.getObject = (params) => {
1718
return {

0 commit comments

Comments
 (0)