Skip to content

Commit 7c0e474

Browse files
committed
feat: use serverless v3 logger and error class
1 parent f355c41 commit 7c0e474

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

src/index.ts

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
import { SecretsManagerClient, GetSecretValueCommand } from '@aws-sdk/client-secrets-manager';
22

3-
import type { Serverless, ServerlessSecretHooks, ServerlessSecretOptions } from './index.types';
3+
import type { Serverless, ServerlessSecretHooks, ServerlessSecretOptions, ServerlessOptions } from './index.types';
44

55
class ServerlessAWSSecret {
6+
Error: ErrorConstructor;
67
hooks: ServerlessSecretHooks;
8+
log: NonNullable<ServerlessOptions['log']>;
79
options: ServerlessSecretOptions;
810
serverless: Serverless;
911

10-
constructor(serverless: Serverless) {
12+
constructor(serverless: Serverless, cliOptions: unknown, options: ServerlessOptions) {
1113
this.setOptions(serverless);
14+
this.setLogger(options);
1215

1316
this.serverless = serverless;
17+
this.Error = serverless.classes?.Error ?? Error;
1418

1519
this.hooks = {
1620
'before:package:initialize': this.loadSecrets.bind(this),
@@ -25,7 +29,7 @@ class ServerlessAWSSecret {
2529
const { SecretString } = await client.send(command);
2630

2731
if (!SecretString) {
28-
throw new this.serverless.classes.Error(`Failed to retrieve the secret: ${this.options.secretId}`);
32+
throw new this.Error(`Failed to retrieve the secret: ${this.options.secretId}`);
2933
}
3034

3135
const secrets = JSON.parse(SecretString);
@@ -36,20 +40,18 @@ class ServerlessAWSSecret {
3640
const secretKey = value.replace(this.options.secretPrefix!, '');
3741

3842
if (!secrets[secretKey]) {
39-
throw new Error(`Secret ${secretKey} do not exist`);
43+
throw new this.Error(`Secret ${secretKey} do not exist`);
4044
}
4145

42-
if (this.options.verbose) {
43-
console.log(`[serverless-aws-secrets]: Replacing ${key} with secret of ${secretKey}`);
44-
}
46+
this.log.verbose(`[serverless-aws-secrets]: Replacing ${key} with secret of ${secretKey}`);
4547

4648
this.serverless.service.provider.environment[key] = secrets[secretKey];
4749

4850
++replaceCount;
4951
}
5052
}
5153

52-
console.log(`[serverless-aws-secrets]: Replaced ${replaceCount} secrets in environment variables`);
54+
this.log.success(`[serverless-aws-secrets]: Replaced ${replaceCount} secrets in environment variables`);
5355
}
5456

5557
setOptions(serverless: Serverless) {
@@ -60,6 +62,13 @@ class ServerlessAWSSecret {
6062
this.options.verbose = this.options.verbose ?? false;
6163
}
6264

65+
setLogger(options: ServerlessOptions) {
66+
this.log = {
67+
verbose: options.log?.verbose ?? this.options.verbose ? console.log : () => {},
68+
success: options.log?.success ?? console.log,
69+
};
70+
}
71+
6372
getSecretId(serverless: Serverless) {
6473
if (this.options?.secretId) {
6574
return this.options.secretId;

src/index.types.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export interface Serverless {
1313
};
1414
};
1515
};
16-
classes: {
16+
classes?: {
1717
Error: ErrorConstructor;
1818
};
1919
}
@@ -22,6 +22,13 @@ export interface ServerlessSecretHooks {
2222
[key: string]: () => void;
2323
}
2424

25+
export interface ServerlessOptions {
26+
log?: {
27+
verbose: (message: string) => void;
28+
success: (message: string) => void;
29+
};
30+
}
31+
2532
export interface ServerlessSecretOptions {
2633
/**
2734
* @dev Environment variables with values that start with this prefix are treated as secrets

0 commit comments

Comments
 (0)