All files sqs.ts

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

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  2x 2x                                         2x     2x         2x   2x 2x 2x 2x       2x      
import * as sqs from '@aws-cdk/aws-sqs';
import * as cdk from '@aws-cdk/core';
import {GenericRestApiAwsIntegration, GenericRestApiAwsIntegrationProps} from "./generic-rest-api-aws-integration"
 
/**
 * Integrates an SQS Queue with ApiGateway (RestApi) by publishing Queue messages on http request
 */
export interface SqsRestApiIntegrationProps extends GenericRestApiAwsIntegrationProps {
    /**
     * The target SQS Queue to which messages will be sent by the RestApi Integration
     */
    queue: sqs.IQueue;
 
    /**
     *  Integration request mapping templates
     *
     *  reference: https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html
     *
     *  @default {"application/json": "Action=SendMessage&MessageBody=$util.urlEncode(\"$input.body\")"}
     */
    requestTemplates?: { [contentType: string]: string; };
}
 
export class SqsRestApiIntegration extends GenericRestApiAwsIntegration {
 
    constructor(scope: cdk.Construct, id: string, props: SqsRestApiIntegrationProps) {
        super(scope, id, props);
    }
 
    init(id: string, props: SqsRestApiIntegrationProps) : void {
 
        this.requestTemplates = props.requestTemplates
            || {"application/json": "Action=SendMessage&MessageBody=$util.urlEncode(\"$input.body\")"}
        this.successResponseTemplates["application/json"] = "{\"status\":\"message received\", \"messageId\": $input.json('SendMessageResponse.SendMessageResult.MessageId')}"
        this.failureResponseTemplates["application/json"] = "{\"status\":\"failed to process message\")}"
        this.integrationPath = cdk.Aws.ACCOUNT_ID + "/" + props.queue.queueName
        this.awsService = "sqs"
    }
 
    configureAwsService(id: string, props: SqsRestApiIntegrationProps) : void {
        props.queue.grantSendMessages(this.integrationRole)
    }
}