Skip to content

Commit fc0ad38

Browse files
authored
Merge pull request #54 from rschick/response-template
feat: allow custom response template
2 parents dd87f19 + 4175bf8 commit fc0ad38

File tree

4 files changed

+80
-5
lines changed

4 files changed

+80
-5
lines changed

README.md

+33
Original file line numberDiff line numberDiff line change
@@ -627,3 +627,36 @@ custom:
627627
> It is important that the mapping template will return a valid `application/x-www-form-urlencoded` string
628628

629629
Source: [Connect AWS API Gateway directly to SNS using a service integration](https://www.alexdebrie.com/posts/aws-api-gateway-service-proxy/)
630+
631+
### Custom response body mapping templates
632+
633+
You can customize the response body by providing mapping templates for success, server errors (5xx) and client errors (4xx).
634+
635+
> Templates must be in JSON format. If a template isn't provided, the integration response will be returned as-is to the client.
636+
637+
#### Kinesis Example
638+
639+
```yml
640+
custom:
641+
apiGatewayServiceProxies:
642+
- kinesis:
643+
path: /kinesis
644+
method: post
645+
streamName: { Ref: 'MyStream' }
646+
response:
647+
template:
648+
success: |
649+
{
650+
"success": true
651+
}
652+
serverError: |
653+
{
654+
"success": false,
655+
"errorMessage": "Server Error"
656+
}
657+
clientError: |
658+
{
659+
"success": false,
660+
"errorMessage": "Client Error"
661+
}
662+
```

lib/apiGateway/schema.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,19 @@ const request = Joi.object({
167167
template: Joi.object().required()
168168
})
169169

170+
const response = Joi.object({
171+
template: Joi.object().keys({
172+
success: Joi.string(),
173+
clientError: Joi.string(),
174+
serverError: Joi.string()
175+
})
176+
})
177+
170178
const allowedProxies = ['kinesis', 'sqs', 's3', 'sns', 'dynamodb']
171179

172180
const proxiesSchemas = {
173181
kinesis: Joi.object({
174-
kinesis: proxy.append({ streamName: stringOrRef.required(), partitionKey, request })
182+
kinesis: proxy.append({ streamName: stringOrRef.required(), partitionKey, request, response })
175183
}),
176184
s3: Joi.object({
177185
s3: proxy.append({

lib/package/kinesis/compileMethodsToKinesis.js

+8-3
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,19 @@ module.exports = {
6565
StatusCode: 200,
6666
SelectionPattern: 200,
6767
ResponseParameters: {},
68-
ResponseTemplates: {}
68+
ResponseTemplates: this.getKinesisIntegrationResponseTemplate(http, 'success')
6969
},
7070
{
7171
StatusCode: 400,
7272
SelectionPattern: 400,
7373
ResponseParameters: {},
74-
ResponseTemplates: {}
74+
ResponseTemplates: this.getKinesisIntegrationResponseTemplate(http, 'clientError')
7575
},
7676
{
7777
StatusCode: 500,
7878
SelectionPattern: 500,
7979
ResponseParameters: {},
80-
ResponseTemplates: {}
80+
ResponseTemplates: this.getKinesisIntegrationResponseTemplate(http, 'serverError')
8181
}
8282
]
8383
}
@@ -98,6 +98,11 @@ module.exports = {
9898
return Object.assign(defaultRequestTemplates, _.get(http, ['request', 'template']))
9999
},
100100

101+
getKinesisIntegrationResponseTemplate(http, statusType) {
102+
const template = _.get(http, ['response', 'template', statusType])
103+
return Object.assign({}, template && { 'application/json': template })
104+
},
105+
101106
getDefaultKinesisRequestTemplates(http) {
102107
return {
103108
'application/json': this.buildDefaultKinesisRequestTemplate(http),

lib/package/kinesis/compileMethodsToKinesis.test.js

+30-1
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ describe('#compileMethodsToKinesis()', () => {
532532
])
533533
})
534534

535-
it('should return a custom template for application/json when one is given', () => {
535+
it('should return a custom request template for application/json when one is given', () => {
536536
const httpWithRequestTemplate = {
537537
path: 'foo/bar1',
538538
method: 'post',
@@ -553,6 +553,35 @@ describe('#compileMethodsToKinesis()', () => {
553553
)
554554
})
555555

556+
it('should return a custom response template for application/json when one is given', () => {
557+
const httpWithRequestTemplate = {
558+
path: 'foo/bar1',
559+
method: 'post',
560+
response: {
561+
template: {
562+
success: 'success template',
563+
clientError: 'client error template',
564+
serverError: 'server error template'
565+
}
566+
},
567+
auth: {
568+
authorizationType: 'NONE'
569+
}
570+
}
571+
const resource = serverlessApigatewayServiceProxy.getKinesisMethodIntegration(
572+
httpWithRequestTemplate
573+
)
574+
expect(
575+
resource.Properties.Integration.IntegrationResponses[0].ResponseTemplates['application/json']
576+
).to.be.equal('success template')
577+
expect(
578+
resource.Properties.Integration.IntegrationResponses[1].ResponseTemplates['application/json']
579+
).to.be.equal('client error template')
580+
expect(
581+
resource.Properties.Integration.IntegrationResponses[2].ResponseTemplates['application/json']
582+
).to.be.equal('server error template')
583+
})
584+
556585
it('should return the default for application/x-www-form-urlencoded when one is not given', () => {
557586
const httpWithoutRequestTemplate = {
558587
streamName: 'myStream',

0 commit comments

Comments
 (0)