All files sns.ts

100% Statements 9/9
100% Branches 0/0
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    1x                       1x     1x       1x 1x 1x 1x 1x       1x      
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;
}
 
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["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)
    }
}