@johnmmackey/ms-utils 4.2.1 → 4.4.0
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/CHANGELOG.md +6 -0
- package/README.md +1 -1
- package/index.d.ts +175 -0
- package/lib/config.js +5 -0
- package/package.json +3 -3
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -47,7 +47,7 @@ sendmail(
|
|
|
47
47
|
* uses ```dotenv``` to load a .env file in the current working directory into the environment
|
|
48
48
|
* pushes all items loaded from etcd into the environment.
|
|
49
49
|
|
|
50
|
-
The order of precedence:
|
|
50
|
+
The order of precedence: .env > ETCD > externally set environment
|
|
51
51
|
|
|
52
52
|
## Future
|
|
53
53
|
* Add local queueing for log events when AMQP is not operative.
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
// Type definitions for @johnmmackey/ms-utils
|
|
2
|
+
// Project: https://bitbucket.org/52westlabs/ms-utils
|
|
3
|
+
// Definitions by: GitHub Copilot
|
|
4
|
+
|
|
5
|
+
/// <reference types="node" />
|
|
6
|
+
|
|
7
|
+
import { SESv2Client, SendEmailCommandOutput } from '@aws-sdk/client-sesv2';
|
|
8
|
+
|
|
9
|
+
// ============================================================================
|
|
10
|
+
// Utils Module
|
|
11
|
+
// ============================================================================
|
|
12
|
+
|
|
13
|
+
export namespace Utils {
|
|
14
|
+
/**
|
|
15
|
+
* Masks sensitive keys in configuration objects
|
|
16
|
+
* @param config - Configuration object to mask
|
|
17
|
+
* @returns A new object with masked sensitive values
|
|
18
|
+
*/
|
|
19
|
+
function maskKeys(config: Record<string, any>): Record<string, any>;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// ============================================================================
|
|
23
|
+
// Config Module
|
|
24
|
+
// ============================================================================
|
|
25
|
+
|
|
26
|
+
export namespace Config {
|
|
27
|
+
/**
|
|
28
|
+
* Loads configuration from etcd for the specified service
|
|
29
|
+
* @param serviceName - Name of the service to load configuration for
|
|
30
|
+
* @returns Promise that resolves to the configuration object
|
|
31
|
+
*/
|
|
32
|
+
function load(serviceName: string): Promise<Record<string, string>>;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Gets a configuration value by key or all configuration if no key provided
|
|
36
|
+
* @param k - Optional configuration key
|
|
37
|
+
* @returns Configuration value or entire configuration object
|
|
38
|
+
*/
|
|
39
|
+
function get(): Record<string, string>;
|
|
40
|
+
function get(k: string): string;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Gets a configuration value as a parsed JSON object
|
|
44
|
+
* @param k - Configuration key
|
|
45
|
+
* @returns Parsed JSON object or null if parsing fails
|
|
46
|
+
*/
|
|
47
|
+
function getObj(k: string): any | null;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
// ============================================================================
|
|
51
|
+
// Winston MSLogger Factory
|
|
52
|
+
// ============================================================================
|
|
53
|
+
|
|
54
|
+
export interface MSLoggerOptions {
|
|
55
|
+
level?: string;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export interface ConnectionManager {
|
|
59
|
+
addConfig(callback: (channel: any) => void): void;
|
|
60
|
+
sendToQueue(queue: string, content: Buffer, options?: any): void;
|
|
61
|
+
ch?: any;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface WinstonTransport {
|
|
65
|
+
name: string;
|
|
66
|
+
level: string;
|
|
67
|
+
log(level: string, msg: string, meta: any, callback: (error: Error | null, success: boolean) => void): void;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
export interface Winston {
|
|
71
|
+
transports: {
|
|
72
|
+
MSLogger?: any;
|
|
73
|
+
};
|
|
74
|
+
Transport: any;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Factory function to create a Winston MSLogger transport
|
|
79
|
+
* @param winston - Winston instance
|
|
80
|
+
* @param serviceName - Name of the service
|
|
81
|
+
* @param cm - Connection manager instance
|
|
82
|
+
* @returns MSLogger constructor
|
|
83
|
+
*/
|
|
84
|
+
export function winstonMSLoggerFactory(
|
|
85
|
+
winston: Winston,
|
|
86
|
+
serviceName: string,
|
|
87
|
+
cm: ConnectionManager
|
|
88
|
+
): new (options: MSLoggerOptions) => WinstonTransport;
|
|
89
|
+
|
|
90
|
+
// ============================================================================
|
|
91
|
+
// Mailer Module
|
|
92
|
+
// ============================================================================
|
|
93
|
+
|
|
94
|
+
export interface MailRecipient {
|
|
95
|
+
email: string;
|
|
96
|
+
name: string;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export interface MailAttachment {
|
|
100
|
+
originalname: string;
|
|
101
|
+
mimetype: string;
|
|
102
|
+
path?: string;
|
|
103
|
+
content?: Buffer;
|
|
104
|
+
contentDisposition?: string;
|
|
105
|
+
cid?: string;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export interface MailOptions {
|
|
109
|
+
from?: string;
|
|
110
|
+
subject?: string;
|
|
111
|
+
replyTo?: string;
|
|
112
|
+
textBody?: string;
|
|
113
|
+
htmlBody?: string;
|
|
114
|
+
attachments?: MailAttachment[];
|
|
115
|
+
includeUnsubscribeLink?: boolean;
|
|
116
|
+
mailingLists?: any;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export class Mailer {
|
|
120
|
+
/**
|
|
121
|
+
* Creates a new Mailer instance
|
|
122
|
+
* @param connectionManager - AMQP connection manager
|
|
123
|
+
*/
|
|
124
|
+
constructor(connectionManager: ConnectionManager);
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Sends an email message
|
|
128
|
+
* @param recipients - Array of recipients
|
|
129
|
+
* @param options - Mail options
|
|
130
|
+
* @returns Promise that resolves when email is queued
|
|
131
|
+
*/
|
|
132
|
+
send(recipients: MailRecipient[], options: MailOptions): Promise<void>;
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Writes a key-value pair to Redis
|
|
136
|
+
* @param v - Value to store
|
|
137
|
+
* @param type - Type identifier
|
|
138
|
+
* @returns Promise that resolves to the generated key
|
|
139
|
+
*/
|
|
140
|
+
writeKey(v: string, type: string): Promise<string>;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// ============================================================================
|
|
144
|
+
// AWS SES Send Mail Module
|
|
145
|
+
// ============================================================================
|
|
146
|
+
|
|
147
|
+
export interface AwsMailMessage {
|
|
148
|
+
from: string;
|
|
149
|
+
to: string | string[];
|
|
150
|
+
replyTo?: string;
|
|
151
|
+
subject: string;
|
|
152
|
+
textBody?: string;
|
|
153
|
+
htmlBody?: string;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Sends an email via AWS SES v2
|
|
158
|
+
* @param message - Email message details
|
|
159
|
+
* @returns Promise that resolves to AWS SES response
|
|
160
|
+
*/
|
|
161
|
+
export function awsSendMail(message: AwsMailMessage): Promise<SendEmailCommandOutput>;
|
|
162
|
+
|
|
163
|
+
// ============================================================================
|
|
164
|
+
// Main Module Exports
|
|
165
|
+
// ============================================================================
|
|
166
|
+
|
|
167
|
+
declare const _default: {
|
|
168
|
+
Utils: typeof Utils;
|
|
169
|
+
Config: typeof Config;
|
|
170
|
+
winstonMSLoggerFactory: typeof winstonMSLoggerFactory;
|
|
171
|
+
Mailer: typeof Mailer;
|
|
172
|
+
awsSendMail: typeof awsSendMail;
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
export default _default;
|
package/lib/config.js
CHANGED
|
@@ -33,6 +33,11 @@ async function load(serviceName) {
|
|
|
33
33
|
|
|
34
34
|
// as a side-effect, load these into process.env
|
|
35
35
|
dotenv.populate(process.env, config, { override: true });
|
|
36
|
+
|
|
37
|
+
// load any .env file that may be present into the environment
|
|
38
|
+
// again to override
|
|
39
|
+
// why? the first one may be necessary to find an etcd server...
|
|
40
|
+
dotenv.config({ override: true });
|
|
36
41
|
return config;
|
|
37
42
|
}
|
|
38
43
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@johnmmackey/ms-utils",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.4.0",
|
|
4
4
|
"description": "Utility functions for Microservice Architecture",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -14,8 +14,8 @@
|
|
|
14
14
|
"license": "ISC",
|
|
15
15
|
"homepage": "https://bitbucket.org/52westlabs/ms-utils#readme",
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"@aws-sdk/client-sesv2": "^3.
|
|
18
|
-
"dotenv": "^
|
|
17
|
+
"@aws-sdk/client-sesv2": "^3.987.0",
|
|
18
|
+
"dotenv": "^17.2.4",
|
|
19
19
|
"etcd3": "^1.1.2",
|
|
20
20
|
"redis": "^4.7.0",
|
|
21
21
|
"uuid": "^11.1.0"
|