Skip to content

Commit bd51680

Browse files
authored
Merge pull request #96 from sambauers/master
feat(sqs): full backwards compatibility for sqs without a custom body
2 parents ff2ff8c + cce0406 commit bd51680

File tree

3 files changed

+54
-53
lines changed

3 files changed

+54
-53
lines changed

README.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ custom:
157157
'integration.request.querystring.MessageAttribute.2.Value.DataType': "'String'"
158158
```
159159

160-
The preferred way to pass `MessageAttribute` parameters is via a request body mapping template. Any `requestParameters` keys that begin with `integration.request.querystring.` will be automatically placed in the request body to maintain backward compatibility with existing implementations.
160+
The alternative way to pass `MessageAttribute` parameters is via a request body mapping template.
161161

162162
#### Customizing request body mapping templates
163163

@@ -714,7 +714,7 @@ Source: [How to connect SNS to Kinesis for cross-account delivery via API Gatewa
714714

715715
#### SQS
716716

717-
Customizing SQS request templates requires us to force all requests to use an `application/x-www-form-urlencoded` style body. The plugin sets the `Content-Type` to `application/x-www-form-urlencoded` for you, but API Gateway will still look for the template under the `application/json` request template type, so that is where you need to configure you request body in `serverless.yml`:
717+
Customizing SQS request templates requires us to force all requests to use an `application/x-www-form-urlencoded` style body. The plugin sets the `Content-Type` header to `application/x-www-form-urlencoded` for you, but API Gateway will still look for the template under the `application/json` request template type, so that is where you need to configure you request body in `serverless.yml`:
718718

719719
```yml
720720
custom:
@@ -742,6 +742,8 @@ Be careful when mixing additional `requestParameters` into your SQS endpoint as
742742

743743
Your custom template must also set the `Action` and `MessageBody` parameters, as these will not be added for you by the plugin.
744744

745+
When using a custom request body, headers sent by a client will no longer be passed through to the SQS queue (`PassthroughBehavior` is automatically set to `NEVER`). You will need to pass through headers sent by the client explicitly in the request body. Also, any custom querystring parameters in the `requestParameters` array will be ignored. These also need to be added via the custom request body.
746+
745747
#### SNS
746748

747749
Similar to the [Kinesis](#kinesis-1) support, you can customize the default request mapping templates in `serverless.yml` like so:

lib/package/sqs/compileMethodsToSqs.js

+19-28
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,29 @@ module.exports = {
6060
'arn:aws:apigateway:${AWS::Region}:sqs:path//${AWS::AccountId}/${queueName}',
6161
{ queueName: http.queueName }
6262
]
63-
},
64-
PassthroughBehavior: 'NEVER',
65-
RequestParameters: _.merge(
63+
}
64+
}
65+
66+
const customRequestTemplates = _.get(http, ['request', 'template'])
67+
68+
if (_.isEmpty(customRequestTemplates)) {
69+
integration.RequestParameters = _.merge(
70+
{
71+
'integration.request.querystring.Action': "'SendMessage'",
72+
'integration.request.querystring.MessageBody': 'method.request.body'
73+
},
74+
http.requestParameters
75+
)
76+
integration.RequestTemplates = { 'application/json': '{statusCode:200}' }
77+
} else {
78+
integration.PassthroughBehavior = 'NEVER'
79+
integration.RequestParameters = _.merge(
6680
{
6781
'integration.request.header.Content-Type': "'application/x-www-form-urlencoded'"
6882
},
6983
_.omitBy(http.requestParameters, requestParameterIsQuerystring)
70-
),
71-
RequestTemplates: this.getSqsIntegrationRequestTemplates(http)
84+
)
85+
integration.RequestTemplates = this.getSqsIntegrationRequestTemplates(http)
7286
}
7387

7488
let integrationResponse
@@ -146,29 +160,6 @@ module.exports = {
146160
getSqsIntegrationRequestTemplates(http) {
147161
const defaultRequestTemplates = this.getDefaultSqsRequestTemplates(http)
148162
const customRequestTemplates = _.get(http, ['request', 'template'])
149-
let requestParametersQuerystrings = _.pickBy(
150-
http.requestParameters,
151-
requestParameterIsQuerystring
152-
)
153-
154-
if (_.isEmpty(customRequestTemplates) && !_.isEmpty(requestParametersQuerystrings)) {
155-
requestParametersQuerystrings = _.map(requestParametersQuerystrings, (value, parameter) => {
156-
return [
157-
parameter.trim().replace('integration.request.querystring.', ''),
158-
value
159-
.trim()
160-
.replace(/^context\.(.+)$/, '$$util.urlEncode($$context.$1)')
161-
.replace(/(^')|('$)/g, '')
162-
].join('=')
163-
})
164-
165-
return {
166-
'application/json': `${
167-
defaultRequestTemplates['application/json']
168-
}&${requestParametersQuerystrings.join('&')}`
169-
}
170-
}
171-
172163
return Object.assign(defaultRequestTemplates, customRequestTemplates)
173164
},
174165

lib/package/sqs/compileMethodsToSqs.test.js

+31-23
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,12 @@ describe('#compileMethodsToSqs()', () => {
7373
}
7474
]
7575
},
76-
PassthroughBehavior: 'NEVER',
7776
RequestParameters: {
78-
'integration.request.header.Content-Type': "'application/x-www-form-urlencoded'"
77+
'integration.request.querystring.Action': "'SendMessage'",
78+
'integration.request.querystring.MessageBody': 'method.request.body'
7979
},
8080
RequestTemplates: {
81-
'application/json': 'Action=SendMessage&MessageBody=$util.urlEncode($input.body)'
81+
'application/json': '{statusCode:200}'
8282
},
8383
IntegrationResponses: [
8484
{
@@ -175,12 +175,12 @@ describe('#compileMethodsToSqs()', () => {
175175
}
176176
]
177177
},
178-
PassthroughBehavior: 'NEVER',
179178
RequestParameters: {
180-
'integration.request.header.Content-Type': "'application/x-www-form-urlencoded'"
179+
'integration.request.querystring.Action': "'SendMessage'",
180+
'integration.request.querystring.MessageBody': 'method.request.body'
181181
},
182182
RequestTemplates: {
183-
'application/json': 'Action=SendMessage&MessageBody=$util.urlEncode($input.body)'
183+
'application/json': '{statusCode:200}'
184184
},
185185
IntegrationResponses: [
186186
{
@@ -276,12 +276,12 @@ describe('#compileMethodsToSqs()', () => {
276276
}
277277
]
278278
},
279-
PassthroughBehavior: 'NEVER',
280279
RequestParameters: {
281-
'integration.request.header.Content-Type': "'application/x-www-form-urlencoded'"
280+
'integration.request.querystring.Action': "'SendMessage'",
281+
'integration.request.querystring.MessageBody': 'method.request.body'
282282
},
283283
RequestTemplates: {
284-
'application/json': 'Action=SendMessage&MessageBody=$util.urlEncode($input.body)'
284+
'application/json': '{statusCode:200}'
285285
},
286286
IntegrationResponses: [
287287
{
@@ -405,12 +405,12 @@ describe('#compileMethodsToSqs()', () => {
405405
}
406406
]
407407
},
408-
PassthroughBehavior: 'NEVER',
409408
RequestParameters: {
410-
'integration.request.header.Content-Type': "'application/x-www-form-urlencoded'"
409+
'integration.request.querystring.Action': "'SendMessage'",
410+
'integration.request.querystring.MessageBody': 'method.request.body'
411411
},
412412
RequestTemplates: {
413-
'application/json': 'Action=SendMessage&MessageBody=$util.urlEncode($input.body)'
413+
'application/json': '{statusCode:200}'
414414
},
415415
IntegrationResponses: [
416416
{
@@ -494,12 +494,12 @@ describe('#compileMethodsToSqs()', () => {
494494
}
495495
]
496496
},
497-
PassthroughBehavior: 'NEVER',
498497
RequestParameters: {
499-
'integration.request.header.Content-Type': "'application/x-www-form-urlencoded'"
498+
'integration.request.querystring.Action': "'SendMessage'",
499+
'integration.request.querystring.MessageBody': 'method.request.body'
500500
},
501501
RequestTemplates: {
502-
'application/json': 'Action=SendMessage&MessageBody=$util.urlEncode($input.body)'
502+
'application/json': '{statusCode:200}'
503503
},
504504
IntegrationResponses: [
505505
{
@@ -583,14 +583,14 @@ describe('#compileMethodsToSqs()', () => {
583583
}
584584
]
585585
},
586-
PassthroughBehavior: 'NEVER',
587586
RequestParameters: {
588-
'integration.request.header.Content-Type': "'application/x-www-form-urlencoded'",
587+
'integration.request.querystring.Action': "'SendMessage'",
588+
'integration.request.querystring.MessageBody': 'method.request.body',
589589
key1: 'value1',
590590
key2: 'value2'
591591
},
592592
RequestTemplates: {
593-
'application/json': 'Action=SendMessage&MessageBody=$util.urlEncode($input.body)'
593+
'application/json': '{statusCode:200}'
594594
},
595595
IntegrationResponses: [
596596
{
@@ -880,14 +880,22 @@ describe('#compileMethodsToSqs()', () => {
880880
}
881881
]
882882
},
883-
PassthroughBehavior: 'NEVER',
884883
RequestParameters: {
885-
'integration.request.header.Content-Type': "'application/x-www-form-urlencoded'",
886-
'integration.request.header.x-my-custom-header': "'foobar'"
884+
'integration.request.header.x-my-custom-header': "'foobar'",
885+
'integration.request.querystring.Action': "'SendMessage'",
886+
'integration.request.querystring.MessageAttribute.1.Name': "'cognitoIdentityId'",
887+
'integration.request.querystring.MessageAttribute.1.Value.DataType': "'String'",
888+
'integration.request.querystring.MessageAttribute.1.Value.StringValue':
889+
'context.identity.cognitoIdentityId',
890+
'integration.request.querystring.MessageAttribute.2.Name':
891+
"'cognitoAuthenticationProvider'",
892+
'integration.request.querystring.MessageAttribute.2.Value.DataType': "'String'",
893+
'integration.request.querystring.MessageAttribute.2.Value.StringValue':
894+
'context.identity.cognitoAuthenticationProvider',
895+
'integration.request.querystring.MessageBody': 'method.request.body'
887896
},
888897
RequestTemplates: {
889-
'application/json':
890-
'Action=SendMessage&MessageBody=$util.urlEncode($input.body)&MessageAttribute.1.Name=cognitoIdentityId&MessageAttribute.1.Value.StringValue=$util.urlEncode($context.identity.cognitoIdentityId)&MessageAttribute.1.Value.DataType=String&MessageAttribute.2.Name=cognitoAuthenticationProvider&MessageAttribute.2.Value.StringValue=$util.urlEncode($context.identity.cognitoAuthenticationProvider)&MessageAttribute.2.Value.DataType=String'
898+
'application/json': '{statusCode:200}'
891899
},
892900
IntegrationResponses: [
893901
{

0 commit comments

Comments
 (0)