-
Notifications
You must be signed in to change notification settings - Fork 4k
/
url.ts
63 lines (56 loc) · 1.9 KB
/
url.ts
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import sns = require('@aws-cdk/aws-sns');
import { Token } from '@aws-cdk/core';
import { SubscriptionProps } from './subscription';
/**
* Options for URL subscriptions.
*/
export interface UrlSubscriptionProps extends SubscriptionProps {
/**
* The message to the queue is the same as it was sent to the topic
*
* If false, the message will be wrapped in an SNS envelope.
*
* @default false
*/
readonly rawMessageDelivery?: boolean;
/**
* The subscription's protocol.
*
* @default - Protocol is derived from url
*/
readonly protocol?: sns.SubscriptionProtocol;
}
/**
* Use a URL as a subscription target
*
* The message will be POSTed to the given URL.
*
* @see https://docs.aws.amazon.com/sns/latest/dg/sns-http-https-endpoint-as-subscriber.html
*/
export class UrlSubscription implements sns.ITopicSubscription {
private readonly protocol: sns.SubscriptionProtocol;
private readonly unresolvedUrl: boolean;
constructor(private readonly url: string, private readonly props: UrlSubscriptionProps = {}) {
this.unresolvedUrl = Token.isUnresolved(url);
if (!this.unresolvedUrl && !url.startsWith('http://') && !url.startsWith('https://')) {
throw new Error('URL must start with either http:// or https://');
}
if (this.unresolvedUrl && props.protocol === undefined) {
throw new Error('Must provide protocol if url is unresolved');
}
if (this.unresolvedUrl) {
this.protocol = props.protocol!;
} else {
this.protocol = this.url.startsWith('https:') ? sns.SubscriptionProtocol.HTTPS : sns.SubscriptionProtocol.HTTP;
}
}
public bind(_topic: sns.ITopic): sns.TopicSubscriptionConfig {
return {
subscriberId: this.unresolvedUrl ? 'UnresolvedUrl' : this.url,
endpoint: this.url,
protocol: this.protocol,
rawMessageDelivery: this.props.rawMessageDelivery,
filterPolicy: this.props.filterPolicy,
};
}
}