@tomassabol/aws-services 1.8.2 → 1.8.4-beta.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/.eslintignore +1 -0
- package/.eslintrc.json +75 -0
- package/.husky/pre-commit +4 -0
- package/.lintstagedrc.json +9 -0
- package/.nvmrc +1 -0
- package/.prettierignore +2 -0
- package/.prettierrc.json +3 -0
- package/package.json +16 -18
- package/src/appconfig.ts +20 -0
- package/src/dynamodb.ts +7 -0
- package/src/eventbridge.ts +47 -0
- package/src/index.ts +34 -0
- package/src/logger.ts +14 -0
- package/src/s3.ts +56 -0
- package/src/secrets-manager.ts +37 -0
- package/src/sns.ts +24 -0
- package/src/sqs.ts +32 -0
- package/src/ssm.ts +70 -0
- package/tsconfig.build.json +4 -0
- package/tsconfig.json +10 -0
- package/typedoc.json +4 -0
- package/README.md +0 -28
- package/appconfig.d.ts +0 -3
- package/appconfig.js +0 -18
- package/dynamodb.d.ts +0 -4
- package/dynamodb.js +0 -7
- package/dynamodb.mock.d.ts +0 -8
- package/dynamodb.mock.js +0 -12
- package/eventbridge.d.ts +0 -8
- package/eventbridge.js +0 -37
- package/eventbridge.mock.d.ts +0 -3
- package/eventbridge.mock.js +0 -7
- package/logger.d.ts +0 -2
- package/logger.js +0 -14
- package/s3.d.ts +0 -13
- package/s3.js +0 -46
- package/secrets-manager.d.ts +0 -4
- package/secrets-manager.js +0 -37
- package/secrets-manager.mock.d.ts +0 -4
- package/secrets-manager.mock.js +0 -8
- package/sns.d.ts +0 -7
- package/sns.js +0 -21
- package/sqs.d.ts +0 -6
- package/sqs.js +0 -24
- package/sqs.mock.d.ts +0 -3
- package/sqs.mock.js +0 -7
- package/ssm.d.ts +0 -19
- package/ssm.js +0 -48
- package/ssm.mock.d.ts +0 -3
- package/ssm.mock.js +0 -7
- package/tsconfig.build.tsbuildinfo +0 -1
package/.eslintignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
dist
|
package/.eslintrc.json
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
{
|
|
2
|
+
"root": true,
|
|
3
|
+
"env": {
|
|
4
|
+
"node": true
|
|
5
|
+
},
|
|
6
|
+
"overrides": [
|
|
7
|
+
{
|
|
8
|
+
"files": ["**/*.ts", "**/*.tsx"],
|
|
9
|
+
"parser": "@typescript-eslint/parser",
|
|
10
|
+
"parserOptions": {
|
|
11
|
+
"project": "./tsconfig.json"
|
|
12
|
+
},
|
|
13
|
+
"plugins": ["@typescript-eslint"],
|
|
14
|
+
"extends": [
|
|
15
|
+
"eslint:recommended",
|
|
16
|
+
"plugin:import/recommended",
|
|
17
|
+
"plugin:import/typescript",
|
|
18
|
+
"plugin:@typescript-eslint/eslint-recommended",
|
|
19
|
+
"plugin:@typescript-eslint/recommended",
|
|
20
|
+
"prettier"
|
|
21
|
+
],
|
|
22
|
+
"rules": {
|
|
23
|
+
"@typescript-eslint/no-empty-interface": "warn",
|
|
24
|
+
"@typescript-eslint/no-unused-vars": [
|
|
25
|
+
"warn",
|
|
26
|
+
{ "argsIgnorePattern": "^_" }
|
|
27
|
+
],
|
|
28
|
+
"@typescript-eslint/no-floating-promises": "error",
|
|
29
|
+
"require-await": "warn",
|
|
30
|
+
"no-useless-catch": "warn",
|
|
31
|
+
"no-console": "warn",
|
|
32
|
+
"no-new-object": "warn",
|
|
33
|
+
"object-shorthand": ["warn", "always"],
|
|
34
|
+
"quote-props": ["warn", "as-needed"],
|
|
35
|
+
"prefer-object-spread": "warn",
|
|
36
|
+
"prefer-destructuring": [
|
|
37
|
+
"warn",
|
|
38
|
+
{
|
|
39
|
+
"VariableDeclarator": {
|
|
40
|
+
"array": false,
|
|
41
|
+
"object": true
|
|
42
|
+
},
|
|
43
|
+
"AssignmentExpression": {
|
|
44
|
+
"array": false,
|
|
45
|
+
"object": false
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
],
|
|
49
|
+
"default-param-last": "warn",
|
|
50
|
+
"no-param-reassign": [
|
|
51
|
+
"warn",
|
|
52
|
+
{ "props": true, "ignorePropertyModificationsFor": ["acc"] }
|
|
53
|
+
],
|
|
54
|
+
"prefer-arrow-callback": "warn",
|
|
55
|
+
"no-duplicate-imports": "warn",
|
|
56
|
+
"import/no-mutable-exports": "warn",
|
|
57
|
+
"import/first": "warn",
|
|
58
|
+
"no-iterator": "warn",
|
|
59
|
+
"dot-notation": "warn",
|
|
60
|
+
"one-var": ["warn", { "initialized": "never" }],
|
|
61
|
+
"no-multi-assign": "warn",
|
|
62
|
+
"no-plusplus": ["warn", { "allowForLoopAfterthoughts": true }],
|
|
63
|
+
"eqeqeq": "warn"
|
|
64
|
+
}
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
"files": ["**/*.md"],
|
|
68
|
+
"extends": "plugin:markdown/recommended"
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
"files": ["**/*.json"],
|
|
72
|
+
"extends": "plugin:json/recommended"
|
|
73
|
+
}
|
|
74
|
+
]
|
|
75
|
+
}
|
package/.nvmrc
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
v18.18.2
|
package/.prettierignore
ADDED
package/.prettierrc.json
ADDED
package/package.json
CHANGED
|
@@ -1,17 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tomassabol/aws-services",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.4-beta.1",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "AWS Services",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"import": "./index.js",
|
|
9
|
+
"require": "./index.js",
|
|
10
|
+
"types": "./index.d.ts"
|
|
11
|
+
},
|
|
12
|
+
"./*": {
|
|
13
|
+
"import": "./*.js",
|
|
14
|
+
"require": "./*.js",
|
|
15
|
+
"types": "./*.d.ts"
|
|
16
|
+
}
|
|
17
|
+
},
|
|
6
18
|
"scripts": {
|
|
7
19
|
"build": "tsc -b tsconfig.build.json",
|
|
8
|
-
"clean": "rimraf dist
|
|
9
|
-
"
|
|
10
|
-
"dist": "tsc -b tsconfig.build.json && cp package.json dist && cp README.md dist && cd dist && npm publish",
|
|
20
|
+
"clean": "rimraf dist",
|
|
21
|
+
"dist": "tsc -b tsconfig.build.json && cp package.json dist && cd dist && npm publish",
|
|
11
22
|
"format": "prettier --check .",
|
|
12
23
|
"husky-install": "husky install",
|
|
13
|
-
"lint": "eslint . --max-warnings 0"
|
|
14
|
-
"test": "jest"
|
|
24
|
+
"lint": "eslint . --max-warnings 0"
|
|
15
25
|
},
|
|
16
26
|
"peerDependencies": {
|
|
17
27
|
"@aws-lambda-powertools/logger": "^2.2.0",
|
|
@@ -26,24 +36,16 @@
|
|
|
26
36
|
"@aws-sdk/lib-dynamodb": "^3.388.0"
|
|
27
37
|
},
|
|
28
38
|
"devDependencies": {
|
|
29
|
-
"@aws-sdk/util-stream-node": "^3.374.0",
|
|
30
39
|
"@tsconfig/node22": "^22.0.2",
|
|
31
|
-
"@types/aws-sdk": "^2.7.4",
|
|
32
|
-
"@types/jest": "^29.4.0",
|
|
33
40
|
"@types/node": "^22.18.12",
|
|
34
41
|
"@typescript-eslint/eslint-plugin": "^5.25.0",
|
|
35
42
|
"@typescript-eslint/parser": "^5.25.0",
|
|
36
|
-
"aws-sdk-client-mock": "^3.0.0",
|
|
37
|
-
"esbuild-jest": "^0.4.0",
|
|
38
43
|
"eslint": "^8.11.0",
|
|
39
44
|
"eslint-config-prettier": "^8.3.0",
|
|
40
45
|
"eslint-plugin-import": "^2.25.4",
|
|
41
46
|
"eslint-plugin-json": "^3.1.0",
|
|
42
47
|
"eslint-plugin-markdown": "^3.0.0",
|
|
43
48
|
"husky": "^8.0.1",
|
|
44
|
-
"jest": "^29.4.3",
|
|
45
|
-
"jest-cli": "^29.4.3",
|
|
46
|
-
"jest-sonar-reporter": "^2.0.0",
|
|
47
49
|
"lint-staged": "^13.1.2",
|
|
48
50
|
"prettier": "^2.6.2",
|
|
49
51
|
"prettier-plugin-sh": "^0.12.8",
|
|
@@ -52,10 +54,6 @@
|
|
|
52
54
|
"typedoc-plugin-markdown": "^3.14.0",
|
|
53
55
|
"typescript": "^5.9.3"
|
|
54
56
|
},
|
|
55
|
-
"jestSonar": {
|
|
56
|
-
"reportPath": ".sonar",
|
|
57
|
-
"reportFile": "test-report.xml"
|
|
58
|
-
},
|
|
59
57
|
"publishConfig": {
|
|
60
58
|
"access": "public"
|
|
61
59
|
}
|
package/src/appconfig.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AppConfigClient,
|
|
3
|
+
GetApplicationCommand,
|
|
4
|
+
} from "@aws-sdk/client-appconfig"
|
|
5
|
+
import { logger } from "./logger"
|
|
6
|
+
|
|
7
|
+
export const appConfigClient = new AppConfigClient({})
|
|
8
|
+
|
|
9
|
+
export const getApplication = async (applicationId: string) => {
|
|
10
|
+
try {
|
|
11
|
+
return await appConfigClient.send(
|
|
12
|
+
new GetApplicationCommand({
|
|
13
|
+
ApplicationId: applicationId,
|
|
14
|
+
})
|
|
15
|
+
)
|
|
16
|
+
} catch (error) {
|
|
17
|
+
logger.error("getApplication", { error, applicationId })
|
|
18
|
+
throw error
|
|
19
|
+
}
|
|
20
|
+
}
|
package/src/dynamodb.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { DynamoDBClient } from "@aws-sdk/client-dynamodb"
|
|
2
|
+
import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb"
|
|
3
|
+
|
|
4
|
+
export const client: DynamoDBClient = new DynamoDBClient({})
|
|
5
|
+
|
|
6
|
+
export const documentClient: DynamoDBDocumentClient =
|
|
7
|
+
DynamoDBDocumentClient.from(client)
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import {
|
|
3
|
+
EventBridgeClient,
|
|
4
|
+
PutEventsCommand,
|
|
5
|
+
} from "@aws-sdk/client-eventbridge"
|
|
6
|
+
import { logger } from "./logger"
|
|
7
|
+
|
|
8
|
+
export const client = new EventBridgeClient({})
|
|
9
|
+
|
|
10
|
+
export async function sendEvent(params: {
|
|
11
|
+
source: string
|
|
12
|
+
eventBusName: string
|
|
13
|
+
eventType: string
|
|
14
|
+
event: any
|
|
15
|
+
}) {
|
|
16
|
+
const { eventBusName, eventType, event, source } = params
|
|
17
|
+
|
|
18
|
+
const input = {
|
|
19
|
+
Entries: [
|
|
20
|
+
{
|
|
21
|
+
EventBusName: eventBusName,
|
|
22
|
+
Source: source,
|
|
23
|
+
DetailType: eventType,
|
|
24
|
+
Detail: JSON.stringify(event),
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const command = new PutEventsCommand(input)
|
|
30
|
+
|
|
31
|
+
try {
|
|
32
|
+
const result = await client.send(command)
|
|
33
|
+
|
|
34
|
+
/* Check if there are any failed events */
|
|
35
|
+
if (result.FailedEntryCount) {
|
|
36
|
+
const errorDetail = result.Entries?.length
|
|
37
|
+
? `${result.Entries[0].ErrorCode}: ${result.Entries[0].ErrorMessage}}`
|
|
38
|
+
: "unknown"
|
|
39
|
+
throw new Error(`PutEventsCommand error ${errorDetail}`)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
logger.debug("sendEvent success", { input, result })
|
|
43
|
+
} catch (error) {
|
|
44
|
+
logger.error("sendEvent error", { input, error })
|
|
45
|
+
throw error
|
|
46
|
+
}
|
|
47
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// AppConfig
|
|
2
|
+
export { appConfigClient, getApplication } from "./appconfig"
|
|
3
|
+
|
|
4
|
+
// DynamoDB
|
|
5
|
+
export { client as dynamodbClient, documentClient } from "./dynamodb"
|
|
6
|
+
|
|
7
|
+
// EventBridge
|
|
8
|
+
export { client as eventBridgeClient, sendEvent } from "./eventbridge"
|
|
9
|
+
|
|
10
|
+
// Logger
|
|
11
|
+
export { logger } from "./logger"
|
|
12
|
+
|
|
13
|
+
// S3
|
|
14
|
+
export { client as s3Client, putObjectToS3, getObjectFromS3 } from "./s3"
|
|
15
|
+
|
|
16
|
+
// Secrets Manager
|
|
17
|
+
export {
|
|
18
|
+
client as secretsManagerClient,
|
|
19
|
+
getSecret,
|
|
20
|
+
getSecretAsObject,
|
|
21
|
+
} from "./secrets-manager"
|
|
22
|
+
|
|
23
|
+
// SNS
|
|
24
|
+
export { snsClient, publishMessageToSNS } from "./sns"
|
|
25
|
+
|
|
26
|
+
// SQS
|
|
27
|
+
export { client as sqsClient, sendSqsMessage } from "./sqs"
|
|
28
|
+
|
|
29
|
+
// SSM
|
|
30
|
+
export {
|
|
31
|
+
client as ssmClient,
|
|
32
|
+
getSsmParameters,
|
|
33
|
+
type SSMParameters,
|
|
34
|
+
} from "./ssm"
|
package/src/logger.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Logger } from "@aws-lambda-powertools/logger"
|
|
2
|
+
|
|
3
|
+
const noLogger = {
|
|
4
|
+
log: () => undefined,
|
|
5
|
+
debug: () => undefined,
|
|
6
|
+
info: () => undefined,
|
|
7
|
+
error: () => undefined,
|
|
8
|
+
warn: () => undefined,
|
|
9
|
+
} as typeof console
|
|
10
|
+
|
|
11
|
+
export const logger =
|
|
12
|
+
process.env.JEST_WORKER_ID === undefined || process.env.TEST_LOGGER
|
|
13
|
+
? new Logger({ logLevel: "DEBUG" })
|
|
14
|
+
: noLogger
|
package/src/s3.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { S3, PutObjectCommandOutput } from "@aws-sdk/client-s3"
|
|
2
|
+
import { logger } from "./logger"
|
|
3
|
+
|
|
4
|
+
export const client = new S3({})
|
|
5
|
+
|
|
6
|
+
export async function putObjectToS3(params: {
|
|
7
|
+
bucket: string
|
|
8
|
+
key: string
|
|
9
|
+
body: string | Uint8Array | Buffer
|
|
10
|
+
contentEncoding: string
|
|
11
|
+
contentType: string
|
|
12
|
+
}): Promise<PutObjectCommandOutput> {
|
|
13
|
+
const { bucket, key, body, contentEncoding, contentType } = params
|
|
14
|
+
|
|
15
|
+
try {
|
|
16
|
+
return await client.putObject({
|
|
17
|
+
Bucket: bucket,
|
|
18
|
+
Key: key,
|
|
19
|
+
Body: body,
|
|
20
|
+
ContentEncoding: contentEncoding,
|
|
21
|
+
ContentType: contentType,
|
|
22
|
+
})
|
|
23
|
+
} catch (error) {
|
|
24
|
+
logger.error("putObjectToS3", {
|
|
25
|
+
error,
|
|
26
|
+
params: { bucket, key, contentEncoding, contentType },
|
|
27
|
+
})
|
|
28
|
+
throw error
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export async function getObjectFromS3(params: {
|
|
33
|
+
bucket: string
|
|
34
|
+
key: string
|
|
35
|
+
}): Promise<Uint8Array> {
|
|
36
|
+
try {
|
|
37
|
+
const { bucket, key } = params
|
|
38
|
+
|
|
39
|
+
const result = await client.getObject({
|
|
40
|
+
Bucket: bucket,
|
|
41
|
+
Key: key,
|
|
42
|
+
})
|
|
43
|
+
|
|
44
|
+
const body = result.Body
|
|
45
|
+
|
|
46
|
+
if (body === undefined) {
|
|
47
|
+
logger.error("Body not found", { params })
|
|
48
|
+
throw new Error("Body not found")
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return await body.transformToByteArray()
|
|
52
|
+
} catch (error) {
|
|
53
|
+
logger.error("getObjectFromS3", { error, params })
|
|
54
|
+
throw error
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SecretsManagerClient,
|
|
3
|
+
GetSecretValueCommand,
|
|
4
|
+
} from "@aws-sdk/client-secrets-manager"
|
|
5
|
+
import assert from "assert"
|
|
6
|
+
import { logger } from "./logger"
|
|
7
|
+
|
|
8
|
+
export const client = new SecretsManagerClient({})
|
|
9
|
+
|
|
10
|
+
export async function getSecret(secretName: string): Promise<string> {
|
|
11
|
+
try {
|
|
12
|
+
const response = await client.send(
|
|
13
|
+
new GetSecretValueCommand({
|
|
14
|
+
SecretId: secretName,
|
|
15
|
+
})
|
|
16
|
+
)
|
|
17
|
+
const value = response.SecretString
|
|
18
|
+
assert(value, "SecretString value not found")
|
|
19
|
+
return value
|
|
20
|
+
} catch (error) {
|
|
21
|
+
logger.error("getSecret", { error, secretName })
|
|
22
|
+
// For a list of exceptions thrown, see
|
|
23
|
+
// https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
|
|
24
|
+
throw error
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export async function getSecretAsObject<T = unknown>(
|
|
29
|
+
secretName: string
|
|
30
|
+
): Promise<T> {
|
|
31
|
+
const value = await getSecret(secretName)
|
|
32
|
+
try {
|
|
33
|
+
return JSON.parse(value)
|
|
34
|
+
} catch (error) {
|
|
35
|
+
throw Error(`Cannot parse secret ${secretName}. Expected JSON.`)
|
|
36
|
+
}
|
|
37
|
+
}
|
package/src/sns.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { SNSClient, PublishCommand } from "@aws-sdk/client-sns"
|
|
2
|
+
import { logger } from "./logger"
|
|
3
|
+
|
|
4
|
+
export const snsClient = new SNSClient({})
|
|
5
|
+
|
|
6
|
+
export const publishMessageToSNS = async (params: {
|
|
7
|
+
topicArn: string
|
|
8
|
+
message: string
|
|
9
|
+
options?: object
|
|
10
|
+
}) => {
|
|
11
|
+
try {
|
|
12
|
+
const { topicArn, message, options } = params
|
|
13
|
+
return await snsClient.send(
|
|
14
|
+
new PublishCommand({
|
|
15
|
+
TopicArn: topicArn,
|
|
16
|
+
Message: message,
|
|
17
|
+
...options,
|
|
18
|
+
})
|
|
19
|
+
)
|
|
20
|
+
} catch (error) {
|
|
21
|
+
logger.error("publishMessageToSNS", { error, params })
|
|
22
|
+
throw error
|
|
23
|
+
}
|
|
24
|
+
}
|
package/src/sqs.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import {
|
|
2
|
+
SQSClient,
|
|
3
|
+
SendMessageCommand,
|
|
4
|
+
SendMessageCommandInput,
|
|
5
|
+
} from "@aws-sdk/client-sqs"
|
|
6
|
+
import { logger } from "./logger"
|
|
7
|
+
|
|
8
|
+
export const client = new SQSClient({})
|
|
9
|
+
|
|
10
|
+
export async function sendSqsMessage(params: {
|
|
11
|
+
queueUrl: string
|
|
12
|
+
message: string | object
|
|
13
|
+
}): Promise<string | undefined> {
|
|
14
|
+
const { queueUrl, message } = params
|
|
15
|
+
|
|
16
|
+
try {
|
|
17
|
+
const body = typeof message === "object" ? JSON.stringify(message) : message
|
|
18
|
+
|
|
19
|
+
const input: SendMessageCommandInput = {
|
|
20
|
+
QueueUrl: queueUrl,
|
|
21
|
+
MessageBody: body,
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const command = new SendMessageCommand(input)
|
|
25
|
+
|
|
26
|
+
const response = await client.send(command)
|
|
27
|
+
return response.MessageId
|
|
28
|
+
} catch (error) {
|
|
29
|
+
logger.error("sendSqsMessage", { error, params })
|
|
30
|
+
throw error
|
|
31
|
+
}
|
|
32
|
+
}
|
package/src/ssm.ts
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { SSMClient, GetParametersCommand } from "@aws-sdk/client-ssm"
|
|
2
|
+
import assert = require("assert")
|
|
3
|
+
|
|
4
|
+
export const client = new SSMClient({})
|
|
5
|
+
|
|
6
|
+
export type SSMParameters = {
|
|
7
|
+
[name: string]: string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Get a list of parameters from SSM Parameter Store
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```typescript
|
|
15
|
+
* const params = await getSsmParameters({
|
|
16
|
+
* first: "/my-params/first",
|
|
17
|
+
* second: "/my-params/second",
|
|
18
|
+
* })
|
|
19
|
+
*
|
|
20
|
+
* // params = { first: "value1", second: "value2" }
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
export async function getSsmParameters<T extends SSMParameters>(
|
|
25
|
+
parameters: T
|
|
26
|
+
): Promise<T> {
|
|
27
|
+
/*
|
|
28
|
+
* Fetch parameters from SSM
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
const command = new GetParametersCommand({
|
|
32
|
+
Names: Object.values(parameters),
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
const output = await client.send(command)
|
|
36
|
+
|
|
37
|
+
/*
|
|
38
|
+
* Map returned parameters to result object
|
|
39
|
+
*/
|
|
40
|
+
|
|
41
|
+
const result: Record<string, string> = {}
|
|
42
|
+
output.Parameters?.forEach((outputParam) => {
|
|
43
|
+
assert(
|
|
44
|
+
typeof outputParam.Value === "string",
|
|
45
|
+
`Received invalid value of SSM parameter ${outputParam.Name}`
|
|
46
|
+
)
|
|
47
|
+
const entry = Object.entries(parameters).find(
|
|
48
|
+
([_key, value]) => value === outputParam.Name
|
|
49
|
+
)
|
|
50
|
+
assert(entry, `Received invalid SSM parameter ${outputParam.Name}`)
|
|
51
|
+
const [key] = entry
|
|
52
|
+
result[key] = outputParam.Value
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
/*
|
|
56
|
+
* Check that all parameters were received
|
|
57
|
+
*/
|
|
58
|
+
|
|
59
|
+
const notFoundParams = Object.keys(parameters).filter(
|
|
60
|
+
(key) => false === Object.hasOwn(result, key)
|
|
61
|
+
)
|
|
62
|
+
|
|
63
|
+
if (notFoundParams.length > 0) {
|
|
64
|
+
throw new Error(
|
|
65
|
+
`Cannot obtain SSM parameters: ${notFoundParams.join(", ")}`
|
|
66
|
+
)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return result as T
|
|
70
|
+
}
|
package/tsconfig.json
ADDED
package/typedoc.json
ADDED
package/README.md
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
# AWS Services
|
|
2
|
-
|
|
3
|
-
## How to publish
|
|
4
|
-
|
|
5
|
-
To be able to import from path `@tomassabol/aws-services/s3` and not `@tomassabol/aws-services/dist/s3`, you have to publish the package with following command:
|
|
6
|
-
|
|
7
|
-
```sh
|
|
8
|
-
npm run dist
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## How to use it
|
|
12
|
-
|
|
13
|
-
```sh
|
|
14
|
-
npm install @tomassabol/aws-services
|
|
15
|
-
```
|
|
16
|
-
|
|
17
|
-
```typescript
|
|
18
|
-
import { getFromS3 } from "@tomassabol/aws-services/s3"
|
|
19
|
-
|
|
20
|
-
async function getMyObject() {
|
|
21
|
-
const params = {
|
|
22
|
-
bucket: "my-bucket-name",
|
|
23
|
-
key: "my-object-key",
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const data = await getFromS3(params)
|
|
27
|
-
}
|
|
28
|
-
```
|
package/appconfig.d.ts
DELETED
package/appconfig.js
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getApplication = exports.appConfigClient = void 0;
|
|
4
|
-
const client_appconfig_1 = require("@aws-sdk/client-appconfig");
|
|
5
|
-
const logger_1 = require("./logger");
|
|
6
|
-
exports.appConfigClient = new client_appconfig_1.AppConfigClient({});
|
|
7
|
-
const getApplication = async (applicationId) => {
|
|
8
|
-
try {
|
|
9
|
-
return await exports.appConfigClient.send(new client_appconfig_1.GetApplicationCommand({
|
|
10
|
-
ApplicationId: applicationId,
|
|
11
|
-
}));
|
|
12
|
-
}
|
|
13
|
-
catch (error) {
|
|
14
|
-
logger_1.logger.error("getApplication", { error, applicationId });
|
|
15
|
-
throw error;
|
|
16
|
-
}
|
|
17
|
-
};
|
|
18
|
-
exports.getApplication = getApplication;
|
package/dynamodb.d.ts
DELETED
package/dynamodb.js
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.documentClient = exports.client = void 0;
|
|
4
|
-
const client_dynamodb_1 = require("@aws-sdk/client-dynamodb");
|
|
5
|
-
const lib_dynamodb_1 = require("@aws-sdk/lib-dynamodb");
|
|
6
|
-
exports.client = new client_dynamodb_1.DynamoDBClient({});
|
|
7
|
-
exports.documentClient = lib_dynamodb_1.DynamoDBDocumentClient.from(exports.client);
|
package/dynamodb.mock.d.ts
DELETED
package/dynamodb.mock.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.mockDynamodbService = void 0;
|
|
4
|
-
exports.mockDynamodbService = {
|
|
5
|
-
client: {
|
|
6
|
-
send: jest.fn(),
|
|
7
|
-
},
|
|
8
|
-
documentClient: {
|
|
9
|
-
send: jest.fn(),
|
|
10
|
-
},
|
|
11
|
-
};
|
|
12
|
-
jest.mock("@tomassabol/aws-services/dynamodb", () => exports.mockDynamodbService);
|
package/eventbridge.d.ts
DELETED
package/eventbridge.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.client = void 0;
|
|
4
|
-
exports.sendEvent = sendEvent;
|
|
5
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
6
|
-
const client_eventbridge_1 = require("@aws-sdk/client-eventbridge");
|
|
7
|
-
const logger_1 = require("./logger");
|
|
8
|
-
exports.client = new client_eventbridge_1.EventBridgeClient({});
|
|
9
|
-
async function sendEvent(params) {
|
|
10
|
-
const { eventBusName, eventType, event, source } = params;
|
|
11
|
-
const input = {
|
|
12
|
-
Entries: [
|
|
13
|
-
{
|
|
14
|
-
EventBusName: eventBusName,
|
|
15
|
-
Source: source,
|
|
16
|
-
DetailType: eventType,
|
|
17
|
-
Detail: JSON.stringify(event),
|
|
18
|
-
},
|
|
19
|
-
],
|
|
20
|
-
};
|
|
21
|
-
const command = new client_eventbridge_1.PutEventsCommand(input);
|
|
22
|
-
try {
|
|
23
|
-
const result = await exports.client.send(command);
|
|
24
|
-
/* Check if there are any failed events */
|
|
25
|
-
if (result.FailedEntryCount) {
|
|
26
|
-
const errorDetail = result.Entries?.length
|
|
27
|
-
? `${result.Entries[0].ErrorCode}: ${result.Entries[0].ErrorMessage}}`
|
|
28
|
-
: "unknown";
|
|
29
|
-
throw new Error(`PutEventsCommand error ${errorDetail}`);
|
|
30
|
-
}
|
|
31
|
-
logger_1.logger.debug("sendEvent success", { input, result });
|
|
32
|
-
}
|
|
33
|
-
catch (error) {
|
|
34
|
-
logger_1.logger.error("sendEvent error", { input, error });
|
|
35
|
-
throw error;
|
|
36
|
-
}
|
|
37
|
-
}
|
package/eventbridge.mock.d.ts
DELETED
package/eventbridge.mock.js
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.mockEventbridgeService = void 0;
|
|
4
|
-
exports.mockEventbridgeService = {
|
|
5
|
-
sendEvent: jest.fn(),
|
|
6
|
-
};
|
|
7
|
-
jest.mock("@tomassabol/aws-services/eventbridge", () => exports.mockEventbridgeService);
|
package/logger.d.ts
DELETED
package/logger.js
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.logger = void 0;
|
|
4
|
-
const logger_1 = require("@aws-lambda-powertools/logger");
|
|
5
|
-
const noLogger = {
|
|
6
|
-
log: () => undefined,
|
|
7
|
-
debug: () => undefined,
|
|
8
|
-
info: () => undefined,
|
|
9
|
-
error: () => undefined,
|
|
10
|
-
warn: () => undefined,
|
|
11
|
-
};
|
|
12
|
-
exports.logger = process.env.JEST_WORKER_ID === undefined || process.env.TEST_LOGGER
|
|
13
|
-
? new logger_1.Logger({ logLevel: "DEBUG" })
|
|
14
|
-
: noLogger;
|
package/s3.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
import { S3, PutObjectCommandOutput } from "@aws-sdk/client-s3";
|
|
2
|
-
export declare const client: S3;
|
|
3
|
-
export declare function putObjectToS3(params: {
|
|
4
|
-
bucket: string;
|
|
5
|
-
key: string;
|
|
6
|
-
body: string | Uint8Array | Buffer;
|
|
7
|
-
contentEncoding: string;
|
|
8
|
-
contentType: string;
|
|
9
|
-
}): Promise<PutObjectCommandOutput>;
|
|
10
|
-
export declare function getObjectFromS3(params: {
|
|
11
|
-
bucket: string;
|
|
12
|
-
key: string;
|
|
13
|
-
}): Promise<Uint8Array>;
|
package/s3.js
DELETED
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.client = void 0;
|
|
4
|
-
exports.putObjectToS3 = putObjectToS3;
|
|
5
|
-
exports.getObjectFromS3 = getObjectFromS3;
|
|
6
|
-
const client_s3_1 = require("@aws-sdk/client-s3");
|
|
7
|
-
const logger_1 = require("./logger");
|
|
8
|
-
exports.client = new client_s3_1.S3({});
|
|
9
|
-
async function putObjectToS3(params) {
|
|
10
|
-
const { bucket, key, body, contentEncoding, contentType } = params;
|
|
11
|
-
try {
|
|
12
|
-
return await exports.client.putObject({
|
|
13
|
-
Bucket: bucket,
|
|
14
|
-
Key: key,
|
|
15
|
-
Body: body,
|
|
16
|
-
ContentEncoding: contentEncoding,
|
|
17
|
-
ContentType: contentType,
|
|
18
|
-
});
|
|
19
|
-
}
|
|
20
|
-
catch (error) {
|
|
21
|
-
logger_1.logger.error("putObjectToS3", {
|
|
22
|
-
error,
|
|
23
|
-
params: { bucket, key, contentEncoding, contentType },
|
|
24
|
-
});
|
|
25
|
-
throw error;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
async function getObjectFromS3(params) {
|
|
29
|
-
try {
|
|
30
|
-
const { bucket, key } = params;
|
|
31
|
-
const result = await exports.client.getObject({
|
|
32
|
-
Bucket: bucket,
|
|
33
|
-
Key: key,
|
|
34
|
-
});
|
|
35
|
-
const body = result.Body;
|
|
36
|
-
if (body === undefined) {
|
|
37
|
-
logger_1.logger.error("Body not found", { params });
|
|
38
|
-
throw new Error("Body not found");
|
|
39
|
-
}
|
|
40
|
-
return await body.transformToByteArray();
|
|
41
|
-
}
|
|
42
|
-
catch (error) {
|
|
43
|
-
logger_1.logger.error("getObjectFromS3", { error, params });
|
|
44
|
-
throw error;
|
|
45
|
-
}
|
|
46
|
-
}
|
package/secrets-manager.d.ts
DELETED
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import { SecretsManagerClient } from "@aws-sdk/client-secrets-manager";
|
|
2
|
-
export declare const client: SecretsManagerClient;
|
|
3
|
-
export declare function getSecret(secretName: string): Promise<string>;
|
|
4
|
-
export declare function getSecretAsObject<T = unknown>(secretName: string): Promise<T>;
|
package/secrets-manager.js
DELETED
|
@@ -1,37 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
-
};
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.client = void 0;
|
|
7
|
-
exports.getSecret = getSecret;
|
|
8
|
-
exports.getSecretAsObject = getSecretAsObject;
|
|
9
|
-
const client_secrets_manager_1 = require("@aws-sdk/client-secrets-manager");
|
|
10
|
-
const assert_1 = __importDefault(require("assert"));
|
|
11
|
-
const logger_1 = require("./logger");
|
|
12
|
-
exports.client = new client_secrets_manager_1.SecretsManagerClient({});
|
|
13
|
-
async function getSecret(secretName) {
|
|
14
|
-
try {
|
|
15
|
-
const response = await exports.client.send(new client_secrets_manager_1.GetSecretValueCommand({
|
|
16
|
-
SecretId: secretName,
|
|
17
|
-
}));
|
|
18
|
-
const value = response.SecretString;
|
|
19
|
-
(0, assert_1.default)(value, "SecretString value not found");
|
|
20
|
-
return value;
|
|
21
|
-
}
|
|
22
|
-
catch (error) {
|
|
23
|
-
logger_1.logger.error("getSecret", { error, secretName });
|
|
24
|
-
// For a list of exceptions thrown, see
|
|
25
|
-
// https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html
|
|
26
|
-
throw error;
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
async function getSecretAsObject(secretName) {
|
|
30
|
-
const value = await getSecret(secretName);
|
|
31
|
-
try {
|
|
32
|
-
return JSON.parse(value);
|
|
33
|
-
}
|
|
34
|
-
catch (error) {
|
|
35
|
-
throw Error(`Cannot parse secret ${secretName}. Expected JSON.`);
|
|
36
|
-
}
|
|
37
|
-
}
|
package/secrets-manager.mock.js
DELETED
|
@@ -1,8 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.mockSecretsManagerService = void 0;
|
|
4
|
-
exports.mockSecretsManagerService = {
|
|
5
|
-
getSecret: jest.fn(),
|
|
6
|
-
getSecretAsObject: jest.fn(),
|
|
7
|
-
};
|
|
8
|
-
jest.mock("@tomassabol/aws-services/secrets-manager", () => exports.mockSecretsManagerService);
|
package/sns.d.ts
DELETED
|
@@ -1,7 +0,0 @@
|
|
|
1
|
-
import { SNSClient } from "@aws-sdk/client-sns";
|
|
2
|
-
export declare const snsClient: SNSClient;
|
|
3
|
-
export declare const publishMessageToSNS: (params: {
|
|
4
|
-
topicArn: string;
|
|
5
|
-
message: string;
|
|
6
|
-
options?: object;
|
|
7
|
-
}) => Promise<import("@aws-sdk/client-sns").PublishCommandOutput>;
|
package/sns.js
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.publishMessageToSNS = exports.snsClient = void 0;
|
|
4
|
-
const client_sns_1 = require("@aws-sdk/client-sns");
|
|
5
|
-
const logger_1 = require("./logger");
|
|
6
|
-
exports.snsClient = new client_sns_1.SNSClient({});
|
|
7
|
-
const publishMessageToSNS = async (params) => {
|
|
8
|
-
try {
|
|
9
|
-
const { topicArn, message, options } = params;
|
|
10
|
-
return await exports.snsClient.send(new client_sns_1.PublishCommand({
|
|
11
|
-
TopicArn: topicArn,
|
|
12
|
-
Message: message,
|
|
13
|
-
...options,
|
|
14
|
-
}));
|
|
15
|
-
}
|
|
16
|
-
catch (error) {
|
|
17
|
-
logger_1.logger.error("publishMessageToSNS", { error, params });
|
|
18
|
-
throw error;
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
exports.publishMessageToSNS = publishMessageToSNS;
|
package/sqs.d.ts
DELETED
package/sqs.js
DELETED
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.client = void 0;
|
|
4
|
-
exports.sendSqsMessage = sendSqsMessage;
|
|
5
|
-
const client_sqs_1 = require("@aws-sdk/client-sqs");
|
|
6
|
-
const logger_1 = require("./logger");
|
|
7
|
-
exports.client = new client_sqs_1.SQSClient({});
|
|
8
|
-
async function sendSqsMessage(params) {
|
|
9
|
-
const { queueUrl, message } = params;
|
|
10
|
-
try {
|
|
11
|
-
const body = typeof message === "object" ? JSON.stringify(message) : message;
|
|
12
|
-
const input = {
|
|
13
|
-
QueueUrl: queueUrl,
|
|
14
|
-
MessageBody: body,
|
|
15
|
-
};
|
|
16
|
-
const command = new client_sqs_1.SendMessageCommand(input);
|
|
17
|
-
const response = await exports.client.send(command);
|
|
18
|
-
return response.MessageId;
|
|
19
|
-
}
|
|
20
|
-
catch (error) {
|
|
21
|
-
logger_1.logger.error("sendSqsMessage", { error, params });
|
|
22
|
-
throw error;
|
|
23
|
-
}
|
|
24
|
-
}
|
package/sqs.mock.d.ts
DELETED
package/sqs.mock.js
DELETED
package/ssm.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { SSMClient } from "@aws-sdk/client-ssm";
|
|
2
|
-
export declare const client: SSMClient;
|
|
3
|
-
export type SSMParameters = {
|
|
4
|
-
[name: string]: string;
|
|
5
|
-
};
|
|
6
|
-
/**
|
|
7
|
-
* Get a list of parameters from SSM Parameter Store
|
|
8
|
-
*
|
|
9
|
-
* @example
|
|
10
|
-
* ```typescript
|
|
11
|
-
* const params = await getSsmParameters({
|
|
12
|
-
* first: "/my-params/first",
|
|
13
|
-
* second: "/my-params/second",
|
|
14
|
-
* })
|
|
15
|
-
*
|
|
16
|
-
* // params = { first: "value1", second: "value2" }
|
|
17
|
-
* ```
|
|
18
|
-
*/
|
|
19
|
-
export declare function getSsmParameters<T extends SSMParameters>(parameters: T): Promise<T>;
|
package/ssm.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.client = void 0;
|
|
4
|
-
exports.getSsmParameters = getSsmParameters;
|
|
5
|
-
const client_ssm_1 = require("@aws-sdk/client-ssm");
|
|
6
|
-
const assert = require("assert");
|
|
7
|
-
exports.client = new client_ssm_1.SSMClient({});
|
|
8
|
-
/**
|
|
9
|
-
* Get a list of parameters from SSM Parameter Store
|
|
10
|
-
*
|
|
11
|
-
* @example
|
|
12
|
-
* ```typescript
|
|
13
|
-
* const params = await getSsmParameters({
|
|
14
|
-
* first: "/my-params/first",
|
|
15
|
-
* second: "/my-params/second",
|
|
16
|
-
* })
|
|
17
|
-
*
|
|
18
|
-
* // params = { first: "value1", second: "value2" }
|
|
19
|
-
* ```
|
|
20
|
-
*/
|
|
21
|
-
async function getSsmParameters(parameters) {
|
|
22
|
-
/*
|
|
23
|
-
* Fetch parameters from SSM
|
|
24
|
-
*/
|
|
25
|
-
const command = new client_ssm_1.GetParametersCommand({
|
|
26
|
-
Names: Object.values(parameters),
|
|
27
|
-
});
|
|
28
|
-
const output = await exports.client.send(command);
|
|
29
|
-
/*
|
|
30
|
-
* Map returned parameters to result object
|
|
31
|
-
*/
|
|
32
|
-
const result = {};
|
|
33
|
-
output.Parameters?.forEach((outputParam) => {
|
|
34
|
-
assert(typeof outputParam.Value === "string", `Received invalid value of SSM parameter ${outputParam.Name}`);
|
|
35
|
-
const entry = Object.entries(parameters).find(([_key, value]) => value === outputParam.Name);
|
|
36
|
-
assert(entry, `Received invalid SSM parameter ${outputParam.Name}`);
|
|
37
|
-
const [key] = entry;
|
|
38
|
-
result[key] = outputParam.Value;
|
|
39
|
-
});
|
|
40
|
-
/*
|
|
41
|
-
* Check that all parameters were received
|
|
42
|
-
*/
|
|
43
|
-
const notFoundParams = Object.keys(parameters).filter((key) => false === Object.hasOwn(result, key));
|
|
44
|
-
if (notFoundParams.length > 0) {
|
|
45
|
-
throw new Error(`Cannot obtain SSM parameters: ${notFoundParams.join(", ")}`);
|
|
46
|
-
}
|
|
47
|
-
return result;
|
|
48
|
-
}
|
package/ssm.mock.d.ts
DELETED
package/ssm.mock.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"root":["../src/appconfig.ts","../src/dynamodb.mock.ts","../src/dynamodb.ts","../src/eventbridge.mock.ts","../src/eventbridge.ts","../src/logger.ts","../src/s3.ts","../src/secrets-manager.mock.ts","../src/secrets-manager.ts","../src/sns.ts","../src/sqs.mock.ts","../src/sqs.ts","../src/ssm.mock.ts","../src/ssm.ts"],"version":"5.9.3"}
|