All files sns.ts

100% Statements 9/9
100% Branches 2/2
100% Functions 3/3
100% Lines 9/9

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44    3x                                           3x     2x       2x   2x 2x 2x 2x       2x      
import * as sns from '@aws-cdk/aws-sns';
import * as cdk from '@aws-cdk/core';
import {GenericRestApiAwsIntegration, GenericRestApiAwsIntegrationProps} from "./generic-rest-api-aws-integration"
 
/**
 * Integrates an SNS Topic with ApiGateway(RestApi) by publishing Topic messages on http request
 */
export interface SnsRestApiIntegrationProps extends GenericRestApiAwsIntegrationProps {
    /**
     * The target SNS Topic to which messages will be published by the RestApi Integration
     */
    topic: sns.Topic;
 
    /**
     *  Integration request mapping templates
     *
     *  reference: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html
     *
     *  @default {"application/json": "Action=Publish&TopicArn=$util.urlEncode('" + props.topic.topicArn + "')&Message=$util.urlEncode($input.body)"}
     */
    requestTemplates?: { [contentType: string]: string; };
 
}
 
export class SnsRestApiIntegration extends GenericRestApiAwsIntegration {
 
    constructor(scope: cdk.Construct, id: string, props: SnsRestApiIntegrationProps) {
        super(scope, id, props);
    }
 
    init(id: string, props: SnsRestApiIntegrationProps) : void {
        this.requestTemplates = props.requestTemplates
            || {"application/json": "Action=Publish&TopicArn=$util.urlEncode('" + props.topic.topicArn + "')&Message=$util.urlEncode($input.body)"}
        this.successResponseTemplates["application/json"] = "{\"status\":\"message received\", \"messageId\": $input.json('PublishResponse.PublishResult.MessageId')}"
        this.failureResponseTemplates["application/json"] = "{\"status\":\"failed to process message\")}"
        this.integrationPath="/"
        this.awsService = "sns"
    }
 
    configureAwsService(id: string, props: SnsRestApiIntegrationProps): void {
        props.topic.grantPublish(this.integrationRole)
    }
}