@airhornjs/aws 5.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +298 -0
- package/biome.json +35 -0
- package/coverage/coverage-final.json +2 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +116 -0
- package/coverage/lcov-report/index.ts.html +817 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +210 -0
- package/coverage/lcov.info +265 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.js +184 -0
- package/package.json +60 -0
- package/src/index.ts +244 -0
- package/test/index.test.ts +486 -0
- package/test/setup.ts +23 -0
- package/tsconfig.json +22 -0
- package/vitest.config.ts +12 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { SESClient, SendEmailCommand } from "@aws-sdk/client-ses";
|
|
3
|
+
import { PublishCommand, SNSClient } from "@aws-sdk/client-sns";
|
|
4
|
+
import {
|
|
5
|
+
AirhornSendType
|
|
6
|
+
} from "airhorn";
|
|
7
|
+
var AirhornAws = class {
|
|
8
|
+
name = "aws";
|
|
9
|
+
capabilities;
|
|
10
|
+
snsClient;
|
|
11
|
+
sesClient;
|
|
12
|
+
constructor(options) {
|
|
13
|
+
if (!options.region) {
|
|
14
|
+
throw new Error("AirhornAws requires region");
|
|
15
|
+
}
|
|
16
|
+
this.capabilities = options.capabilities || [
|
|
17
|
+
AirhornSendType.SMS,
|
|
18
|
+
AirhornSendType.MobilePush,
|
|
19
|
+
AirhornSendType.Email
|
|
20
|
+
];
|
|
21
|
+
const credentials = options.accessKeyId && options.secretAccessKey ? {
|
|
22
|
+
accessKeyId: options.accessKeyId,
|
|
23
|
+
secretAccessKey: options.secretAccessKey,
|
|
24
|
+
sessionToken: options.sessionToken
|
|
25
|
+
} : void 0;
|
|
26
|
+
if (this.capabilities.includes(AirhornSendType.SMS)) {
|
|
27
|
+
this.snsClient = new SNSClient({
|
|
28
|
+
region: options.region,
|
|
29
|
+
credentials
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
if (this.capabilities.includes(AirhornSendType.Email)) {
|
|
33
|
+
this.sesClient = new SESClient({
|
|
34
|
+
region: options.region,
|
|
35
|
+
credentials
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async send(message, options) {
|
|
40
|
+
const result = {
|
|
41
|
+
success: false,
|
|
42
|
+
response: null,
|
|
43
|
+
errors: []
|
|
44
|
+
};
|
|
45
|
+
try {
|
|
46
|
+
if (message.type === AirhornSendType.SMS || message.type === AirhornSendType.MobilePush) {
|
|
47
|
+
return this.sendSMS(message, options);
|
|
48
|
+
}
|
|
49
|
+
if (message.type === AirhornSendType.Email) {
|
|
50
|
+
return this.sendEmail(message, options);
|
|
51
|
+
}
|
|
52
|
+
throw new Error(
|
|
53
|
+
`AirhornAws does not support message type: ${message.type}`
|
|
54
|
+
);
|
|
55
|
+
} catch (error) {
|
|
56
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
57
|
+
result.errors.push(err);
|
|
58
|
+
result.response = {
|
|
59
|
+
error: err.message,
|
|
60
|
+
details: error
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
return result;
|
|
64
|
+
}
|
|
65
|
+
async sendSMS(message, options) {
|
|
66
|
+
const result = {
|
|
67
|
+
success: false,
|
|
68
|
+
response: null,
|
|
69
|
+
errors: []
|
|
70
|
+
};
|
|
71
|
+
try {
|
|
72
|
+
if (!this.snsClient) {
|
|
73
|
+
throw new Error("SNS is not configured");
|
|
74
|
+
}
|
|
75
|
+
if (!message.from) {
|
|
76
|
+
throw new Error(
|
|
77
|
+
"From identifier is required for SMS/MobilePush messages"
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
let command;
|
|
81
|
+
if (message.type === AirhornSendType.MobilePush || message.to.startsWith("arn:")) {
|
|
82
|
+
command = new PublishCommand({
|
|
83
|
+
TargetArn: message.to,
|
|
84
|
+
Message: message.content,
|
|
85
|
+
MessageAttributes: options?.MessageAttributes,
|
|
86
|
+
MessageStructure: options?.MessageStructure,
|
|
87
|
+
...options
|
|
88
|
+
});
|
|
89
|
+
} else {
|
|
90
|
+
command = new PublishCommand({
|
|
91
|
+
PhoneNumber: message.to,
|
|
92
|
+
Message: message.content,
|
|
93
|
+
MessageAttributes: {
|
|
94
|
+
"AWS.SNS.SMS.SenderID": {
|
|
95
|
+
DataType: "String",
|
|
96
|
+
StringValue: message.from
|
|
97
|
+
},
|
|
98
|
+
"AWS.SNS.SMS.SMSType": {
|
|
99
|
+
DataType: "String",
|
|
100
|
+
StringValue: options?.smsType || "Transactional"
|
|
101
|
+
},
|
|
102
|
+
...options?.MessageAttributes
|
|
103
|
+
},
|
|
104
|
+
...options
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
const response = await this.snsClient.send(command);
|
|
108
|
+
result.success = true;
|
|
109
|
+
result.response = {
|
|
110
|
+
messageId: response.MessageId,
|
|
111
|
+
sequenceNumber: response.SequenceNumber,
|
|
112
|
+
metadata: response.$metadata
|
|
113
|
+
};
|
|
114
|
+
} catch (error) {
|
|
115
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
116
|
+
result.errors.push(err);
|
|
117
|
+
result.response = {
|
|
118
|
+
error: err.message,
|
|
119
|
+
details: error
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
return result;
|
|
123
|
+
}
|
|
124
|
+
async sendEmail(message, options) {
|
|
125
|
+
const result = {
|
|
126
|
+
success: false,
|
|
127
|
+
response: null,
|
|
128
|
+
errors: []
|
|
129
|
+
};
|
|
130
|
+
try {
|
|
131
|
+
if (!this.sesClient) {
|
|
132
|
+
throw new Error("SES is not configured");
|
|
133
|
+
}
|
|
134
|
+
if (!message.from) {
|
|
135
|
+
throw new Error("From email address is required for email messages");
|
|
136
|
+
}
|
|
137
|
+
const command = new SendEmailCommand({
|
|
138
|
+
Source: message.from,
|
|
139
|
+
Destination: {
|
|
140
|
+
ToAddresses: [message.to],
|
|
141
|
+
CcAddresses: options?.ccAddresses,
|
|
142
|
+
BccAddresses: options?.bccAddresses
|
|
143
|
+
},
|
|
144
|
+
Message: {
|
|
145
|
+
Subject: {
|
|
146
|
+
Data: message.subject || "Notification",
|
|
147
|
+
Charset: "UTF-8"
|
|
148
|
+
},
|
|
149
|
+
Body: {
|
|
150
|
+
Text: {
|
|
151
|
+
Data: message.content,
|
|
152
|
+
Charset: "UTF-8"
|
|
153
|
+
},
|
|
154
|
+
Html: {
|
|
155
|
+
Data: message.content,
|
|
156
|
+
Charset: "UTF-8"
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
},
|
|
160
|
+
ReplyToAddresses: options?.replyToAddresses,
|
|
161
|
+
ReturnPath: options?.returnPath,
|
|
162
|
+
ConfigurationSetName: options?.configurationSetName,
|
|
163
|
+
Tags: options?.tags
|
|
164
|
+
});
|
|
165
|
+
const response = await this.sesClient.send(command);
|
|
166
|
+
result.success = true;
|
|
167
|
+
result.response = {
|
|
168
|
+
messageId: response.MessageId,
|
|
169
|
+
metadata: response.$metadata
|
|
170
|
+
};
|
|
171
|
+
} catch (error) {
|
|
172
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
173
|
+
result.errors.push(err);
|
|
174
|
+
result.response = {
|
|
175
|
+
error: err.message,
|
|
176
|
+
details: error
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
return result;
|
|
180
|
+
}
|
|
181
|
+
};
|
|
182
|
+
export {
|
|
183
|
+
AirhornAws
|
|
184
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@airhornjs/aws",
|
|
3
|
+
"version": "5.0.1",
|
|
4
|
+
"description": "AWS SNS and SES provider for Airhorn",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.js",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"import": "./dist/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"airhorn",
|
|
16
|
+
"notifications",
|
|
17
|
+
"aws",
|
|
18
|
+
"sns",
|
|
19
|
+
"ses",
|
|
20
|
+
"sms",
|
|
21
|
+
"email",
|
|
22
|
+
"provider"
|
|
23
|
+
],
|
|
24
|
+
"homepage": "https://github.com/jaredwray/airhorn/tree/main/packages/aws",
|
|
25
|
+
"repository": {
|
|
26
|
+
"type": "git",
|
|
27
|
+
"url": "https://github.com/jaredwray/airhorn.git",
|
|
28
|
+
"directory": "packages/aws"
|
|
29
|
+
},
|
|
30
|
+
"bugs": {
|
|
31
|
+
"url": "https://github.com/jaredwray/airhorn/issues"
|
|
32
|
+
},
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"author": "Jared Wray <me@jaredwray.com>",
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@aws-sdk/client-ses": "^3.873.0",
|
|
37
|
+
"@aws-sdk/client-sns": "^3.873.0"
|
|
38
|
+
},
|
|
39
|
+
"devDependencies": {
|
|
40
|
+
"@biomejs/biome": "^2.2.2",
|
|
41
|
+
"@types/node": "^24.3.0",
|
|
42
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
43
|
+
"rimraf": "^6.0.1",
|
|
44
|
+
"tsup": "^8.5.0",
|
|
45
|
+
"tsx": "^4.20.5",
|
|
46
|
+
"typescript": "^5.9.2",
|
|
47
|
+
"vitest": "^3.2.4"
|
|
48
|
+
},
|
|
49
|
+
"peerDependencies": {
|
|
50
|
+
"airhorn": "^5.0.1"
|
|
51
|
+
},
|
|
52
|
+
"scripts": {
|
|
53
|
+
"lint": "biome check --write --error-on-warnings",
|
|
54
|
+
"test": "pnpm lint && vitest run --coverage",
|
|
55
|
+
"test:ci": "biome check --error-on-warnings && vitest run --coverage",
|
|
56
|
+
"clean": "rimraf ./dist ./coverage ./node_modules ./package-lock.json ./pnpm-lock.yaml",
|
|
57
|
+
"build:publish": "pnpm build && pnpm publish --access public",
|
|
58
|
+
"build": "rimraf ./dist && tsup src/index.ts --format esm --dts --clean"
|
|
59
|
+
}
|
|
60
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
import { SESClient, SendEmailCommand } from "@aws-sdk/client-ses";
|
|
2
|
+
import { PublishCommand, SNSClient } from "@aws-sdk/client-sns";
|
|
3
|
+
import {
|
|
4
|
+
type AirhornProvider,
|
|
5
|
+
type AirhornProviderMessage,
|
|
6
|
+
type AirhornProviderSendResult,
|
|
7
|
+
AirhornSendType,
|
|
8
|
+
} from "airhorn";
|
|
9
|
+
|
|
10
|
+
export type AirhornAwsOptions = {
|
|
11
|
+
region: string;
|
|
12
|
+
accessKeyId?: string;
|
|
13
|
+
secretAccessKey?: string;
|
|
14
|
+
sessionToken?: string;
|
|
15
|
+
capabilities?: AirhornSendType[];
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
export class AirhornAws implements AirhornProvider {
|
|
19
|
+
public name = "aws";
|
|
20
|
+
public capabilities: AirhornSendType[];
|
|
21
|
+
|
|
22
|
+
private snsClient?: SNSClient;
|
|
23
|
+
private sesClient?: SESClient;
|
|
24
|
+
|
|
25
|
+
constructor(options: AirhornAwsOptions) {
|
|
26
|
+
if (!options.region) {
|
|
27
|
+
throw new Error("AirhornAws requires region");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Set capabilities from options or default to SMS, MobilePush, and Email
|
|
31
|
+
this.capabilities = options.capabilities || [
|
|
32
|
+
AirhornSendType.SMS,
|
|
33
|
+
AirhornSendType.MobilePush,
|
|
34
|
+
AirhornSendType.Email,
|
|
35
|
+
];
|
|
36
|
+
|
|
37
|
+
const credentials =
|
|
38
|
+
options.accessKeyId && options.secretAccessKey
|
|
39
|
+
? {
|
|
40
|
+
accessKeyId: options.accessKeyId,
|
|
41
|
+
secretAccessKey: options.secretAccessKey,
|
|
42
|
+
sessionToken: options.sessionToken,
|
|
43
|
+
}
|
|
44
|
+
: undefined;
|
|
45
|
+
|
|
46
|
+
// Configure SNS if SMS capability is enabled
|
|
47
|
+
if (this.capabilities.includes(AirhornSendType.SMS)) {
|
|
48
|
+
this.snsClient = new SNSClient({
|
|
49
|
+
region: options.region,
|
|
50
|
+
credentials,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Configure SES if Email capability is enabled
|
|
55
|
+
if (this.capabilities.includes(AirhornSendType.Email)) {
|
|
56
|
+
this.sesClient = new SESClient({
|
|
57
|
+
region: options.region,
|
|
58
|
+
credentials,
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async send(
|
|
64
|
+
message: AirhornProviderMessage,
|
|
65
|
+
// biome-ignore lint/suspicious/noExplicitAny: expected
|
|
66
|
+
options?: any,
|
|
67
|
+
): Promise<AirhornProviderSendResult> {
|
|
68
|
+
const result: AirhornProviderSendResult = {
|
|
69
|
+
success: false,
|
|
70
|
+
response: null,
|
|
71
|
+
errors: [],
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
try {
|
|
75
|
+
if (
|
|
76
|
+
message.type === AirhornSendType.SMS ||
|
|
77
|
+
message.type === AirhornSendType.MobilePush
|
|
78
|
+
) {
|
|
79
|
+
return this.sendSMS(message, options);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
if (message.type === AirhornSendType.Email) {
|
|
83
|
+
return this.sendEmail(message, options);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
throw new Error(
|
|
87
|
+
`AirhornAws does not support message type: ${message.type}`,
|
|
88
|
+
);
|
|
89
|
+
} catch (error) {
|
|
90
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
91
|
+
result.errors.push(err);
|
|
92
|
+
result.response = {
|
|
93
|
+
error: err.message,
|
|
94
|
+
details: error,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return result;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
private async sendSMS(
|
|
102
|
+
message: AirhornProviderMessage,
|
|
103
|
+
// biome-ignore lint/suspicious/noExplicitAny: expected
|
|
104
|
+
options?: any,
|
|
105
|
+
): Promise<AirhornProviderSendResult> {
|
|
106
|
+
const result: AirhornProviderSendResult = {
|
|
107
|
+
success: false,
|
|
108
|
+
response: null,
|
|
109
|
+
errors: [],
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
try {
|
|
113
|
+
if (!this.snsClient) {
|
|
114
|
+
throw new Error("SNS is not configured");
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (!message.from) {
|
|
118
|
+
throw new Error(
|
|
119
|
+
"From identifier is required for SMS/MobilePush messages",
|
|
120
|
+
);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Build command based on message type and destination
|
|
124
|
+
let command: PublishCommand;
|
|
125
|
+
if (
|
|
126
|
+
message.type === AirhornSendType.MobilePush ||
|
|
127
|
+
message.to.startsWith("arn:")
|
|
128
|
+
) {
|
|
129
|
+
// Mobile push to endpoint ARN or topic ARN
|
|
130
|
+
command = new PublishCommand({
|
|
131
|
+
TargetArn: message.to,
|
|
132
|
+
Message: message.content,
|
|
133
|
+
MessageAttributes: options?.MessageAttributes,
|
|
134
|
+
MessageStructure: options?.MessageStructure,
|
|
135
|
+
...options,
|
|
136
|
+
});
|
|
137
|
+
} else {
|
|
138
|
+
// Regular SMS to phone number
|
|
139
|
+
command = new PublishCommand({
|
|
140
|
+
PhoneNumber: message.to,
|
|
141
|
+
Message: message.content,
|
|
142
|
+
MessageAttributes: {
|
|
143
|
+
"AWS.SNS.SMS.SenderID": {
|
|
144
|
+
DataType: "String",
|
|
145
|
+
StringValue: message.from,
|
|
146
|
+
},
|
|
147
|
+
"AWS.SNS.SMS.SMSType": {
|
|
148
|
+
DataType: "String",
|
|
149
|
+
StringValue: options?.smsType || "Transactional",
|
|
150
|
+
},
|
|
151
|
+
...options?.MessageAttributes,
|
|
152
|
+
},
|
|
153
|
+
...options,
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const response = await this.snsClient.send(command);
|
|
158
|
+
|
|
159
|
+
result.success = true;
|
|
160
|
+
result.response = {
|
|
161
|
+
messageId: response.MessageId,
|
|
162
|
+
sequenceNumber: response.SequenceNumber,
|
|
163
|
+
metadata: response.$metadata,
|
|
164
|
+
};
|
|
165
|
+
} catch (error) {
|
|
166
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
167
|
+
result.errors.push(err);
|
|
168
|
+
result.response = {
|
|
169
|
+
error: err.message,
|
|
170
|
+
details: error,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return result;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
private async sendEmail(
|
|
178
|
+
message: AirhornProviderMessage,
|
|
179
|
+
// biome-ignore lint/suspicious/noExplicitAny: expected
|
|
180
|
+
options?: any,
|
|
181
|
+
): Promise<AirhornProviderSendResult> {
|
|
182
|
+
const result: AirhornProviderSendResult = {
|
|
183
|
+
success: false,
|
|
184
|
+
response: null,
|
|
185
|
+
errors: [],
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
try {
|
|
189
|
+
if (!this.sesClient) {
|
|
190
|
+
throw new Error("SES is not configured");
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
if (!message.from) {
|
|
194
|
+
throw new Error("From email address is required for email messages");
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const command = new SendEmailCommand({
|
|
198
|
+
Source: message.from,
|
|
199
|
+
Destination: {
|
|
200
|
+
ToAddresses: [message.to],
|
|
201
|
+
CcAddresses: options?.ccAddresses,
|
|
202
|
+
BccAddresses: options?.bccAddresses,
|
|
203
|
+
},
|
|
204
|
+
Message: {
|
|
205
|
+
Subject: {
|
|
206
|
+
Data: message.subject || "Notification",
|
|
207
|
+
Charset: "UTF-8",
|
|
208
|
+
},
|
|
209
|
+
Body: {
|
|
210
|
+
Text: {
|
|
211
|
+
Data: message.content,
|
|
212
|
+
Charset: "UTF-8",
|
|
213
|
+
},
|
|
214
|
+
Html: {
|
|
215
|
+
Data: message.content,
|
|
216
|
+
Charset: "UTF-8",
|
|
217
|
+
},
|
|
218
|
+
},
|
|
219
|
+
},
|
|
220
|
+
ReplyToAddresses: options?.replyToAddresses,
|
|
221
|
+
ReturnPath: options?.returnPath,
|
|
222
|
+
ConfigurationSetName: options?.configurationSetName,
|
|
223
|
+
Tags: options?.tags,
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
const response = await this.sesClient.send(command);
|
|
227
|
+
|
|
228
|
+
result.success = true;
|
|
229
|
+
result.response = {
|
|
230
|
+
messageId: response.MessageId,
|
|
231
|
+
metadata: response.$metadata,
|
|
232
|
+
};
|
|
233
|
+
} catch (error) {
|
|
234
|
+
const err = error instanceof Error ? error : new Error(String(error));
|
|
235
|
+
result.errors.push(err);
|
|
236
|
+
result.response = {
|
|
237
|
+
error: err.message,
|
|
238
|
+
details: error,
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return result;
|
|
243
|
+
}
|
|
244
|
+
}
|