@creator.co/wapi 1.2.5 → 1.2.6
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/README.md +216 -5
- package/dist/index.d.ts +15 -0
- package/dist/index.js.map +1 -1
- package/dist/package.json +6 -1
- package/dist/src/API/Request.d.ts +45 -82
- package/dist/src/API/Request.js +49 -77
- package/dist/src/API/Request.js.map +1 -1
- package/dist/src/API/Response.d.ts +94 -163
- package/dist/src/API/Response.js +101 -161
- package/dist/src/API/Response.js.map +1 -1
- package/dist/src/API/Utils.d.ts +21 -42
- package/dist/src/API/Utils.js +22 -43
- package/dist/src/API/Utils.js.map +1 -1
- package/dist/src/BaseEvent/EventProcessor.d.ts +32 -55
- package/dist/src/BaseEvent/EventProcessor.js +30 -38
- package/dist/src/BaseEvent/EventProcessor.js.map +1 -1
- package/dist/src/BaseEvent/Process.d.ts +20 -43
- package/dist/src/BaseEvent/Process.js +16 -27
- package/dist/src/BaseEvent/Process.js.map +1 -1
- package/dist/src/BaseEvent/Transaction.d.ts +94 -2
- package/dist/src/BaseEvent/Transaction.js +49 -4
- package/dist/src/BaseEvent/Transaction.js.map +1 -1
- package/dist/src/Config/Configuration.d.ts +48 -66
- package/dist/src/Config/Configuration.js +25 -42
- package/dist/src/Config/Configuration.js.map +1 -1
- package/dist/src/Config/EnvironmentVar.d.ts +30 -57
- package/dist/src/Config/EnvironmentVar.js +28 -41
- package/dist/src/Config/EnvironmentVar.js.map +1 -1
- package/dist/src/Crypto/Crypto.d.ts +17 -35
- package/dist/src/Crypto/Crypto.js +12 -21
- package/dist/src/Crypto/Crypto.js.map +1 -1
- package/dist/src/Crypto/JWT.d.ts +21 -32
- package/dist/src/Crypto/JWT.js +14 -22
- package/dist/src/Crypto/JWT.js.map +1 -1
- package/dist/src/Globals.d.ts +26 -94
- package/dist/src/Globals.js +26 -95
- package/dist/src/Globals.js.map +1 -1
- package/dist/src/Logger/Logger.d.ts +82 -105
- package/dist/src/Logger/Logger.js +111 -136
- package/dist/src/Logger/Logger.js.map +1 -1
- package/dist/src/Mailer/Mailer.d.ts +39 -75
- package/dist/src/Mailer/Mailer.js +36 -65
- package/dist/src/Mailer/Mailer.js.map +1 -1
- package/dist/src/Publisher/Publisher.d.ts +17 -25
- package/dist/src/Publisher/Publisher.js +21 -32
- package/dist/src/Publisher/Publisher.js.map +1 -1
- package/dist/src/Server/RouteResolver.d.ts +14 -22
- package/dist/src/Server/RouteResolver.js +21 -34
- package/dist/src/Server/RouteResolver.js.map +1 -1
- package/dist/src/Server/Router.d.ts +72 -51
- package/dist/src/Server/Router.js +8 -17
- package/dist/src/Server/Router.js.map +1 -1
- package/dist/src/Server/lib/ContainerServer.d.ts +15 -31
- package/dist/src/Server/lib/ContainerServer.js +13 -28
- package/dist/src/Server/lib/ContainerServer.js.map +1 -1
- package/dist/src/Server/lib/Server.d.ts +17 -32
- package/dist/src/Server/lib/Server.js +18 -28
- package/dist/src/Server/lib/Server.js.map +1 -1
- package/dist/src/Server/lib/container/GenericHandler.d.ts +5 -0
- package/dist/src/Server/lib/container/GenericHandler.js +16 -3
- package/dist/src/Server/lib/container/GenericHandler.js.map +1 -1
- package/dist/src/Server/lib/container/GenericHandlerEvent.d.ts +22 -37
- package/dist/src/Server/lib/container/GenericHandlerEvent.js +29 -41
- package/dist/src/Server/lib/container/GenericHandlerEvent.js.map +1 -1
- package/dist/src/Server/lib/container/HealthHandler.d.ts +6 -0
- package/dist/src/Server/lib/container/HealthHandler.js +6 -0
- package/dist/src/Server/lib/container/HealthHandler.js.map +1 -1
- package/dist/src/Server/lib/container/Proxy.d.ts +24 -52
- package/dist/src/Server/lib/container/Proxy.js +49 -50
- package/dist/src/Server/lib/container/Proxy.js.map +1 -1
- package/dist/src/Server/lib/container/Utils.d.ts +6 -10
- package/dist/src/Server/lib/container/Utils.js +6 -10
- package/dist/src/Server/lib/container/Utils.js.map +1 -1
- package/dist/src/Validation/Validator.d.ts +9 -13
- package/dist/src/Validation/Validator.js +8 -12
- package/dist/src/Validation/Validator.js.map +1 -1
- package/index.ts +15 -0
- package/package.json +6 -1
- package/src/API/Request.ts +66 -84
- package/src/API/Response.ts +144 -203
- package/src/API/Utils.ts +28 -44
- package/src/BaseEvent/EventProcessor.ts +52 -77
- package/src/BaseEvent/Process.ts +27 -52
- package/src/BaseEvent/Transaction.ts +100 -10
- package/src/Config/Configuration.ts +59 -76
- package/src/Config/EnvironmentVar.ts +39 -62
- package/src/Crypto/Crypto.ts +20 -36
- package/src/Crypto/JWT.ts +31 -35
- package/src/Globals.ts +28 -96
- package/src/Logger/Logger.ts +141 -160
- package/src/Mailer/Mailer.ts +43 -76
- package/src/Publisher/Publisher.ts +31 -40
- package/src/Server/RouteResolver.ts +31 -52
- package/src/Server/Router.ts +75 -54
- package/src/Server/lib/ContainerServer.ts +20 -32
- package/src/Server/lib/Server.ts +19 -34
- package/src/Server/lib/container/GenericHandler.ts +17 -3
- package/src/Server/lib/container/GenericHandlerEvent.ts +44 -54
- package/src/Server/lib/container/HealthHandler.ts +6 -0
- package/src/Server/lib/container/Proxy.ts +37 -57
- package/src/Server/lib/container/Utils.ts +7 -10
- package/src/Validation/Validator.ts +11 -13
- package/tests/API/Response.test.ts +55 -56
- package/tests/BaseEvent/EventProcessor.test.ts +49 -50
- package/tests/BaseEvent/Process.test.ts +2 -2
- package/tests/BaseEvent/Transaction.test.ts +48 -49
- package/tests/Config/Config.test.ts +27 -27
- package/tests/Config/EnvironmentVar.test.ts +54 -18
- package/tests/Publisher/Publisher.test.ts +3 -3
- package/tests/Server/lib/ContainerServer.test.ts +21 -22
- package/tests/Server/lib/container/GenericHandler.test.ts +31 -32
- package/tests/Server/lib/container/GenericHandlerEvent.test.ts +2 -2
- package/tests/Server/lib/container/HealthHandler.test.ts +6 -7
- package/tests/Server/lib/container/Proxy.test.ts +37 -35
- package/tsconfig.json +1 -1
|
@@ -6,17 +6,19 @@ import EnvironmentVar, { EnvironmentType } from './EnvironmentVar'
|
|
|
6
6
|
// Hold cached values at nodeVM level
|
|
7
7
|
// eslint-disable-next-line no-var
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* @type {*}
|
|
9
|
+
* Creates a new instance of a memory cache store.
|
|
10
|
+
* @returns {MemCache} A new instance of a memory cache store.
|
|
12
11
|
*/
|
|
13
12
|
let cacheStore = new MemCache()
|
|
14
13
|
|
|
15
14
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* @
|
|
19
|
-
* @
|
|
15
|
+
* Represents the schema for the configuration object.
|
|
16
|
+
* @typedef {Object} ConfigurationSchema
|
|
17
|
+
* @property {Object} [name] - The name of the configuration property.
|
|
18
|
+
* @property {boolean} [name.isLocal] - Indicates if the property is for local use.
|
|
19
|
+
* @property {boolean} [name.isRemote] - Indicates if the property is for remote use.
|
|
20
|
+
* @property {boolean} [name.required] - Indicates if the property is required.
|
|
21
|
+
* @property {string} [name.cachingPolicy] - The caching policy for the property.
|
|
20
22
|
*/
|
|
21
23
|
export type ConfigurationSchema = {
|
|
22
24
|
[name: string]: {
|
|
@@ -27,13 +29,12 @@ export type ConfigurationSchema = {
|
|
|
27
29
|
}
|
|
28
30
|
}
|
|
29
31
|
|
|
30
|
-
// runtime type infer to ConfigurationSchema keys, which type is remote
|
|
31
32
|
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
* @
|
|
35
|
-
* @template Type
|
|
33
|
+
* Extracts the properties from a given type that have the `isRemote` property set to `true`.
|
|
34
|
+
* @param {Type} - The type to extract properties from.
|
|
35
|
+
* @returns An object type with the extracted properties.
|
|
36
36
|
*/
|
|
37
|
+
// runtime type infer to ConfigurationSchema keys, which type is remote
|
|
37
38
|
type ExtractRemote<Type> = {
|
|
38
39
|
[Property in keyof Type]-?: Type[Property] extends {
|
|
39
40
|
isRemote: true
|
|
@@ -41,13 +42,12 @@ type ExtractRemote<Type> = {
|
|
|
41
42
|
? Property
|
|
42
43
|
: null
|
|
43
44
|
}
|
|
44
|
-
// runtime type infer to ConfigurationSchema keys, which type is local
|
|
45
45
|
/**
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
* @
|
|
49
|
-
* @template Type
|
|
46
|
+
* Extracts the properties from a given type that have the `isLocal` property set to `true`.
|
|
47
|
+
* @typeparam Type - The type to extract properties from.
|
|
48
|
+
* @returns An object type with the extracted properties as keys and their corresponding types.
|
|
50
49
|
*/
|
|
50
|
+
// runtime type infer to ConfigurationSchema keys, which type is local
|
|
51
51
|
type ExtractLocal<Type> = {
|
|
52
52
|
[Property in keyof Type]-?: Type[Property] extends {
|
|
53
53
|
isLocal: true
|
|
@@ -56,24 +56,24 @@ type ExtractLocal<Type> = {
|
|
|
56
56
|
: null
|
|
57
57
|
}
|
|
58
58
|
|
|
59
|
-
// Helpers to filter null keys
|
|
60
59
|
/**
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
* @
|
|
64
|
-
* @
|
|
65
|
-
* @template U
|
|
60
|
+
* Creates a new type by omitting keys from type T whose values are of type U.
|
|
61
|
+
* @param {T} - The original type
|
|
62
|
+
* @param {U} - The type to omit from T
|
|
63
|
+
* @returns A new type with the omitted keys
|
|
66
64
|
*/
|
|
65
|
+
// Helpers to filter null keys
|
|
67
66
|
type OmitKeysByValueType<T, U> = {
|
|
68
67
|
[P in keyof T]: T[P] extends U ? never : P
|
|
69
68
|
}[keyof T]
|
|
70
|
-
|
|
71
69
|
/**
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
* @
|
|
75
|
-
* @
|
|
76
|
-
* @
|
|
70
|
+
* Omit properties from an object type by their value type.
|
|
71
|
+
* @template T - The object type to omit properties from.
|
|
72
|
+
* @template V - The value type of properties to omit.
|
|
73
|
+
* @typedef OmitByValueType
|
|
74
|
+
* @property {T} T - The object type to omit properties from.
|
|
75
|
+
* @property {V} V - The value type of properties to omit.
|
|
76
|
+
* @returns {OmitKeysByValueType<T, V>} - The resulting object type after omitting properties.
|
|
77
77
|
*/
|
|
78
78
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars, prettier/prettier
|
|
79
79
|
type OmitByValueType<T, V> = T extends infer _
|
|
@@ -83,36 +83,26 @@ type OmitByValueType<T, V> = T extends infer _
|
|
|
83
83
|
: never
|
|
84
84
|
|
|
85
85
|
/**
|
|
86
|
-
*
|
|
87
|
-
*
|
|
88
|
-
* @export
|
|
89
|
-
* @class Configuration
|
|
90
|
-
* @typedef {Configuration}
|
|
91
|
-
* @template {ConfigurationSchema} T
|
|
86
|
+
* Represents a configuration object with a specified schema and remote prefix.
|
|
87
|
+
* @template T - The type of the configuration schema.
|
|
92
88
|
*/
|
|
93
89
|
export default class Configuration<T extends ConfigurationSchema> {
|
|
94
90
|
/**
|
|
95
|
-
*
|
|
96
|
-
*
|
|
97
|
-
* @private
|
|
98
|
-
* @readonly
|
|
91
|
+
* The schema for the private readonly property.
|
|
99
92
|
* @type {T}
|
|
100
93
|
*/
|
|
101
94
|
private readonly schema: T
|
|
102
95
|
/**
|
|
103
|
-
*
|
|
104
|
-
*
|
|
105
|
-
* @private
|
|
106
|
-
* @readonly
|
|
96
|
+
* The prefix used for remote resources.
|
|
107
97
|
* @type {string}
|
|
108
98
|
*/
|
|
109
99
|
private readonly remotePrefix: string
|
|
100
|
+
|
|
110
101
|
/**
|
|
111
|
-
*
|
|
112
|
-
*
|
|
113
|
-
* @
|
|
114
|
-
* @
|
|
115
|
-
* @param {string} remotePrefix
|
|
102
|
+
* Constructs a new instance of the class.
|
|
103
|
+
* @param {T} schema - The schema object.
|
|
104
|
+
* @param {string} remotePrefix - The remote prefix string.
|
|
105
|
+
* @returns None
|
|
116
106
|
*/
|
|
117
107
|
constructor(schema: T, remotePrefix: string) {
|
|
118
108
|
this.schema = schema
|
|
@@ -120,11 +110,9 @@ export default class Configuration<T extends ConfigurationSchema> {
|
|
|
120
110
|
}
|
|
121
111
|
|
|
122
112
|
/**
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
* @
|
|
126
|
-
* @param {keyof OmitByValueType<ExtractLocal<T>, null>} propName
|
|
127
|
-
* @returns {*}
|
|
113
|
+
* Retrieves the value of a property from the environment variables or cache.
|
|
114
|
+
* @param {keyof OmitByValueType<ExtractLocal<T>, null>} propName - The name of the property to retrieve.
|
|
115
|
+
* @returns The value of the property.
|
|
128
116
|
*/
|
|
129
117
|
public get(propName: keyof OmitByValueType<ExtractLocal<T>, null>): any {
|
|
130
118
|
const propString = propName as string
|
|
@@ -141,13 +129,11 @@ export default class Configuration<T extends ConfigurationSchema> {
|
|
|
141
129
|
this.cacheValue(propString, v, propSchema.cachingPolicy)
|
|
142
130
|
return v
|
|
143
131
|
}
|
|
132
|
+
|
|
144
133
|
/**
|
|
145
|
-
*
|
|
146
|
-
*
|
|
147
|
-
* @
|
|
148
|
-
* @async
|
|
149
|
-
* @param {keyof OmitByValueType<ExtractRemote<T>, null>} propName
|
|
150
|
-
* @returns {Promise<any>}
|
|
134
|
+
* Asynchronously retrieves the value of a property from the remote environment.
|
|
135
|
+
* @param {keyof OmitByValueType<ExtractRemote<T>, null>} propName - The name of the property to retrieve.
|
|
136
|
+
* @returns {Promise<any>} - A promise that resolves to the value of the property.
|
|
151
137
|
*/
|
|
152
138
|
public async asyncGet(propName: keyof OmitByValueType<ExtractRemote<T>, null>): Promise<any> {
|
|
153
139
|
const propString = propName as string
|
|
@@ -164,33 +150,30 @@ export default class Configuration<T extends ConfigurationSchema> {
|
|
|
164
150
|
this.cacheValue(propString, v, propSchema.cachingPolicy)
|
|
165
151
|
return v
|
|
166
152
|
}
|
|
167
|
-
|
|
153
|
+
|
|
168
154
|
/**
|
|
169
|
-
*
|
|
170
|
-
*
|
|
171
|
-
* @
|
|
172
|
-
* @param {string} valueKey
|
|
173
|
-
* @returns {*}
|
|
155
|
+
* Retrieves the cached value associated with the given key from the cache store.
|
|
156
|
+
* @param {string} valueKey - The key of the value to retrieve from the cache.
|
|
157
|
+
* @returns The cached value associated with the given key, or undefined if the key does not exist in the cache.
|
|
174
158
|
*/
|
|
175
159
|
private getCachedValue(valueKey: string): any {
|
|
176
160
|
return cacheStore.get(valueKey)
|
|
177
161
|
}
|
|
162
|
+
|
|
178
163
|
/**
|
|
179
|
-
*
|
|
180
|
-
*
|
|
181
|
-
* @
|
|
182
|
-
* @param {string}
|
|
183
|
-
* @
|
|
184
|
-
* @param {string} [policy="1d"]
|
|
164
|
+
* Caches a value with the specified key and expiration policy.
|
|
165
|
+
* @param {string} valueKey - The key to associate with the cached value.
|
|
166
|
+
* @param {any} value - The value to be cached.
|
|
167
|
+
* @param {string} [policy='1d'] - The expiration policy for the cached value. Defaults to '1d' (1 day).
|
|
168
|
+
* @returns None
|
|
185
169
|
*/
|
|
186
170
|
private cacheValue(valueKey: string, value: any, policy: string = '1d'): void {
|
|
187
|
-
cacheStore.set(valueKey, value, DurationParser(policy, 's'))
|
|
171
|
+
cacheStore.set(valueKey, value, DurationParser(policy, 's') || '')
|
|
188
172
|
}
|
|
189
|
-
|
|
173
|
+
|
|
190
174
|
/**
|
|
191
|
-
*
|
|
192
|
-
*
|
|
193
|
-
* @private
|
|
175
|
+
* Resets the cache by creating a new instance of the MemCache class and assigning it to the cacheStore variable.
|
|
176
|
+
* @returns None
|
|
194
177
|
*/
|
|
195
178
|
private resetCache() {
|
|
196
179
|
cacheStore = new MemCache()
|
|
@@ -9,14 +9,16 @@ import { SSMClient, GetParameterCommand } from '@aws-sdk/client-ssm'
|
|
|
9
9
|
*
|
|
10
10
|
* @type {*}
|
|
11
11
|
*/
|
|
12
|
-
let ssmClient = null,
|
|
13
|
-
secretsClient = null
|
|
12
|
+
let ssmClient: SSMClient | null = null,
|
|
13
|
+
secretsClient: SecretsManagerClient | null = null
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* @export
|
|
16
|
+
* An enumeration representing different types of environments.
|
|
19
17
|
* @enum {number}
|
|
18
|
+
* @readonly
|
|
19
|
+
* @property {number} PlainRemote - Represents a plain remote environment.
|
|
20
|
+
* @property {number} SecureRemote - Represents a secure remote environment.
|
|
21
|
+
* @property {number} Local - Represents a local environment.
|
|
20
22
|
*/
|
|
21
23
|
export enum EnvironmentType {
|
|
22
24
|
PlainRemote,
|
|
@@ -24,54 +26,35 @@ export enum EnvironmentType {
|
|
|
24
26
|
Local,
|
|
25
27
|
}
|
|
26
28
|
/**
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* @export
|
|
30
|
-
* @class EnvironmentVar
|
|
31
|
-
* @typedef {EnvironmentVar}
|
|
32
|
-
* @template T
|
|
29
|
+
* Represents an environment variable with methods to resolve its value based on the environment type.
|
|
30
|
+
* @template T - The type of the environment variable value.
|
|
33
31
|
*/
|
|
34
32
|
export default class EnvironmentVar<T> {
|
|
35
33
|
/**
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
* @private
|
|
39
|
-
* @readonly
|
|
40
|
-
* @type {string}
|
|
34
|
+
* The name of the parameter.
|
|
41
35
|
*/
|
|
42
36
|
private readonly paramName: string
|
|
43
37
|
/**
|
|
44
|
-
*
|
|
45
|
-
*
|
|
46
|
-
* @private
|
|
38
|
+
* The type of environment.
|
|
47
39
|
* @readonly
|
|
48
|
-
* @type {EnvironmentType}
|
|
49
40
|
*/
|
|
50
41
|
private readonly type: EnvironmentType
|
|
51
42
|
/**
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
* @private
|
|
55
|
-
* @readonly
|
|
56
|
-
* @type {boolean}
|
|
43
|
+
* A boolean flag indicating whether the field is optional or not.
|
|
57
44
|
*/
|
|
58
45
|
private readonly optional: boolean
|
|
59
46
|
/**
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
* @private
|
|
63
|
-
* @readonly
|
|
47
|
+
* The prefix used for parameters in the class.
|
|
64
48
|
* @type {string}
|
|
65
49
|
*/
|
|
66
50
|
private readonly paramPrefix: string
|
|
51
|
+
|
|
67
52
|
/**
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
* @
|
|
71
|
-
* @param {
|
|
72
|
-
* @param {
|
|
73
|
-
* @param {?boolean} [optional]
|
|
74
|
-
* @param {string} [paramPrefix=`/creatorco-api-\${process.env.STAGE\}/env/`]
|
|
53
|
+
* Constructs a new instance of the EnvironmentVar class.
|
|
54
|
+
* @param {string} paramName - The name of the environment variable.
|
|
55
|
+
* @param {EnvironmentType} type - The type of the environment.
|
|
56
|
+
* @param {boolean} [optional] - Indicates whether the environment variable is optional.
|
|
57
|
+
* @param {string} [paramPrefix] - The prefix to be added to the environment variable name.
|
|
75
58
|
*/
|
|
76
59
|
constructor(
|
|
77
60
|
paramName: string,
|
|
@@ -86,10 +69,9 @@ export default class EnvironmentVar<T> {
|
|
|
86
69
|
}
|
|
87
70
|
|
|
88
71
|
/**
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
* @
|
|
92
|
-
* @returns {T}
|
|
72
|
+
* Resolves the value of the environment variable synchronously.
|
|
73
|
+
* @returns {T} - The resolved value of the environment variable.
|
|
74
|
+
* @throws {Error} - If the environment type is not 'Local'.
|
|
93
75
|
*/
|
|
94
76
|
public syncResolve(): T {
|
|
95
77
|
if (this.type == EnvironmentType.Local) return this.getLocalValue()
|
|
@@ -99,12 +81,10 @@ export default class EnvironmentVar<T> {
|
|
|
99
81
|
)
|
|
100
82
|
}
|
|
101
83
|
}
|
|
84
|
+
|
|
102
85
|
/**
|
|
103
|
-
*
|
|
104
|
-
*
|
|
105
|
-
* @public
|
|
106
|
-
* @async
|
|
107
|
-
* @returns {unknown}
|
|
86
|
+
* Resolves the value based on the environment type.
|
|
87
|
+
* @returns {Promise<any>} - The resolved value.
|
|
108
88
|
*/
|
|
109
89
|
public async resolve() {
|
|
110
90
|
if (this.type == EnvironmentType.Local) return this.getLocalValue()
|
|
@@ -113,10 +93,9 @@ export default class EnvironmentVar<T> {
|
|
|
113
93
|
}
|
|
114
94
|
|
|
115
95
|
/**
|
|
116
|
-
*
|
|
117
|
-
*
|
|
118
|
-
* @
|
|
119
|
-
* @returns {T}
|
|
96
|
+
* Retrieves the value of a local environment variable.
|
|
97
|
+
* @returns {T} - The value of the local environment variable.
|
|
98
|
+
* @throws {Error} - If the local environment variable is not defined and is not optional.
|
|
120
99
|
*/
|
|
121
100
|
private getLocalValue(): T {
|
|
122
101
|
const value = process.env[this.paramName]
|
|
@@ -127,14 +106,13 @@ export default class EnvironmentVar<T> {
|
|
|
127
106
|
}
|
|
128
107
|
return value as T
|
|
129
108
|
}
|
|
109
|
+
|
|
130
110
|
/**
|
|
131
|
-
*
|
|
132
|
-
*
|
|
133
|
-
* @
|
|
134
|
-
* @async
|
|
135
|
-
* @returns {Promise<T>}
|
|
111
|
+
* Retrieves the plain value of a remote environment parameter.
|
|
112
|
+
* @returns {Promise<T>} - A promise that resolves to the plain value of the parameter.
|
|
113
|
+
* @throws {Error} - If the parameter cannot be retrieved and is not optional.
|
|
136
114
|
*/
|
|
137
|
-
private async getPlainValue(): Promise<T> {
|
|
115
|
+
private async getPlainValue(): Promise<T | undefined> {
|
|
138
116
|
if (!ssmClient) ssmClient = new SSMClient()
|
|
139
117
|
const pName = `${this.paramPrefix}${this.paramName}`
|
|
140
118
|
try {
|
|
@@ -147,19 +125,18 @@ export default class EnvironmentVar<T> {
|
|
|
147
125
|
throw new Error(`Unable to retrieve plain remote env value: ${pName}`)
|
|
148
126
|
}
|
|
149
127
|
}
|
|
128
|
+
|
|
150
129
|
/**
|
|
151
|
-
*
|
|
152
|
-
*
|
|
153
|
-
* @
|
|
154
|
-
* @async
|
|
155
|
-
* @returns {Promise<T>}
|
|
130
|
+
* Retrieves a secure value from the secrets manager.
|
|
131
|
+
* @returns {Promise<T>} - A promise that resolves to the secure value.
|
|
132
|
+
* @throws {Error} - Throws an error if the secure value cannot be retrieved and is not optional.
|
|
156
133
|
*/
|
|
157
|
-
private async getSecureValue(): Promise<T> {
|
|
134
|
+
private async getSecureValue(): Promise<T | undefined> {
|
|
158
135
|
if (!secretsClient) secretsClient = new SecretsManagerClient()
|
|
159
136
|
const pName = `${this.paramPrefix}${this.paramName}`
|
|
160
137
|
try {
|
|
161
138
|
const data = await secretsClient.send(new GetSecretValueCommand({ SecretId: pName }))
|
|
162
|
-
if (data.
|
|
139
|
+
if (data.SecretString) return data.SecretString as T
|
|
163
140
|
} catch (error) {
|
|
164
141
|
console.error(error)
|
|
165
142
|
}
|
package/src/Crypto/Crypto.ts
CHANGED
|
@@ -1,58 +1,42 @@
|
|
|
1
1
|
import { KMSClient, EncryptCommand, DecryptCommand } from '@aws-sdk/client-kms'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* @export
|
|
7
|
-
* @class Crypto
|
|
8
|
-
* @typedef {Crypto}
|
|
4
|
+
* Represents a Crypto class that provides encryption and decryption functionality using AWS KMS.
|
|
9
5
|
*/
|
|
10
6
|
export default class Crypto {
|
|
11
7
|
/**
|
|
12
|
-
*
|
|
13
|
-
*
|
|
14
|
-
* @private
|
|
15
|
-
* @readonly
|
|
16
|
-
* @type {KMSClient}
|
|
8
|
+
* The KMSClient instance used for making requests to the Key Management Service (KMS).
|
|
17
9
|
*/
|
|
18
10
|
private readonly client: KMSClient
|
|
19
11
|
/**
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* @private
|
|
23
|
-
* @readonly
|
|
24
|
-
* @type {string}
|
|
12
|
+
* The AWS region of the object. This property is readonly and cannot be modified once set.
|
|
25
13
|
*/
|
|
26
14
|
private readonly region: string
|
|
27
15
|
/**
|
|
28
|
-
*
|
|
29
|
-
*
|
|
30
|
-
* @private
|
|
16
|
+
* The unique identifier for the KMS.
|
|
31
17
|
* @readonly
|
|
32
18
|
* @type {string}
|
|
33
19
|
*/
|
|
34
20
|
private readonly keyId: string
|
|
21
|
+
|
|
35
22
|
/**
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
* @
|
|
39
|
-
* @
|
|
40
|
-
* @param {string} keyId
|
|
23
|
+
* Constructs a new instance of the KMSClient class.
|
|
24
|
+
* @param {string} region - The AWS region to use for the client.
|
|
25
|
+
* @param {string} keyId - The AWS KMS key ID to use for encryption and decryption operations.
|
|
26
|
+
* @returns None
|
|
41
27
|
*/
|
|
42
28
|
constructor(region: string, keyId: string) {
|
|
43
29
|
this.region = region
|
|
44
30
|
this.keyId = keyId
|
|
45
31
|
this.client = new KMSClient({ region: this.region })
|
|
46
32
|
}
|
|
47
|
-
|
|
33
|
+
|
|
48
34
|
/**
|
|
49
|
-
*
|
|
50
|
-
*
|
|
51
|
-
* @
|
|
52
|
-
* @param {(string | any)} data
|
|
53
|
-
* @returns {Promise<string>}
|
|
35
|
+
* Encrypts the given data using RSAES_OAEP_SHA_256 encryption algorithm.
|
|
36
|
+
* @param {string | any} data - The data to be encrypted.
|
|
37
|
+
* @returns {Promise<string | null>} - A promise that resolves to the encrypted data as a hexadecimal string.
|
|
54
38
|
*/
|
|
55
|
-
async encryptData(data: string | any): Promise<string> {
|
|
39
|
+
public async encryptData(data: string | any): Promise<string | null> {
|
|
56
40
|
try {
|
|
57
41
|
const resp = await this.client.send(
|
|
58
42
|
new EncryptCommand({
|
|
@@ -69,14 +53,14 @@ export default class Crypto {
|
|
|
69
53
|
return null
|
|
70
54
|
}
|
|
71
55
|
}
|
|
56
|
+
|
|
72
57
|
/**
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
* @
|
|
76
|
-
*
|
|
77
|
-
* @returns {Promise<string>}
|
|
58
|
+
* Decrypts the given data using the RSAES_OAEP_SHA_256 encryption algorithm.
|
|
59
|
+
* @param {string} data - The encrypted data to decrypt.
|
|
60
|
+
* @returns {Promise<string | null>} - A promise that resolves to the decrypted plaintext string.
|
|
61
|
+
* If decryption fails, null is returned.
|
|
78
62
|
*/
|
|
79
|
-
async decryptData(data: string): Promise<string> {
|
|
63
|
+
public async decryptData(data: string): Promise<string | null> {
|
|
80
64
|
try {
|
|
81
65
|
const resp = await this.client.send(
|
|
82
66
|
new DecryptCommand({
|
package/src/Crypto/JWT.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import * as jwt from 'jsonwebtoken'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
* @
|
|
4
|
+
* Represents the response of a JWT validation.
|
|
5
|
+
* @typedef {Object} JWTValidationResponse
|
|
6
|
+
* @property {boolean} isValid - Indicates whether the JWT is valid or not.
|
|
7
|
+
* @property {jwt.JwtPayload} [decodedToken] - The decoded JWT payload if the JWT is valid.
|
|
8
|
+
* @property {boolean} [isExpired] - Indicates whether the JWT is expired or not, only present if isValid is false.
|
|
7
9
|
*/
|
|
8
10
|
type JWTValidationResponse =
|
|
9
11
|
| { isValid: true; decodedToken: jwt.JwtPayload }
|
|
@@ -13,51 +15,45 @@ type JWTValidationResponse =
|
|
|
13
15
|
}
|
|
14
16
|
|
|
15
17
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* @export
|
|
19
|
-
* @class JWT
|
|
20
|
-
* @typedef {JWT}
|
|
18
|
+
* Represents a JSON Web Token (JWT) utility class.
|
|
21
19
|
*/
|
|
22
20
|
export default class JWT {
|
|
23
21
|
/**
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* @private
|
|
27
|
-
* @readonly
|
|
22
|
+
* The secret key used for generating and verifying tokens.
|
|
28
23
|
* @type {string}
|
|
29
24
|
*/
|
|
30
25
|
private readonly tokenSecret: string
|
|
31
26
|
/**
|
|
32
|
-
*
|
|
33
|
-
*
|
|
34
|
-
* @private
|
|
35
|
-
* @readonly
|
|
27
|
+
* The default expiration time for a cache entry.
|
|
36
28
|
* @type {string}
|
|
37
29
|
*/
|
|
38
30
|
private readonly defaultExpiration: string
|
|
31
|
+
|
|
39
32
|
/**
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
* @
|
|
43
|
-
* @
|
|
44
|
-
* @param {?string} [defaultExpiration]
|
|
33
|
+
* Constructs a new instance of the class.
|
|
34
|
+
* @param {string} tokenSecret - The secret used to sign the tokens.
|
|
35
|
+
* @param {string} [defaultExpiration] - The default expiration time for the tokens.
|
|
36
|
+
* @returns None
|
|
45
37
|
*/
|
|
46
38
|
constructor(tokenSecret: string, defaultExpiration?: string) {
|
|
47
39
|
this.tokenSecret = tokenSecret
|
|
48
40
|
this.defaultExpiration = defaultExpiration
|
|
49
41
|
}
|
|
50
|
-
|
|
42
|
+
|
|
51
43
|
/**
|
|
52
|
-
*
|
|
53
|
-
*
|
|
54
|
-
* @param {
|
|
55
|
-
* @param {
|
|
56
|
-
* @param {
|
|
57
|
-
* @
|
|
58
|
-
* @returns {string}
|
|
44
|
+
* Creates a JSON Web Token (JWT) using the provided data and options.
|
|
45
|
+
* @param {object} data - The data to be included in the token payload.
|
|
46
|
+
* @param {string} [expiration] - The expiration time for the token. If not provided, the default expiration time will be used.
|
|
47
|
+
* @param {string} [overrideToken] - An optional token secret to override the default token secret.
|
|
48
|
+
* @param {any} [opts] - Additional options to be passed to the jwt.sign() function.
|
|
49
|
+
* @returns {string} - The generated JWT.
|
|
59
50
|
*/
|
|
60
|
-
createToken(
|
|
51
|
+
public createToken(
|
|
52
|
+
data: object,
|
|
53
|
+
expiration?: string,
|
|
54
|
+
overrideToken?: string,
|
|
55
|
+
opts?: any
|
|
56
|
+
): string {
|
|
61
57
|
const exp = expiration || this.defaultExpiration
|
|
62
58
|
return jwt.sign(
|
|
63
59
|
data,
|
|
@@ -65,13 +61,13 @@ export default class JWT {
|
|
|
65
61
|
exp ? { expiresIn: exp, ...(opts || {}) } : opts || {}
|
|
66
62
|
)
|
|
67
63
|
}
|
|
64
|
+
|
|
68
65
|
/**
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
* @
|
|
72
|
-
* @returns {JWTValidationResponse}
|
|
66
|
+
* Validates a JSON Web Token (JWT) and returns the validation response.
|
|
67
|
+
* @param {string} token - The JWT to validate.
|
|
68
|
+
* @returns {JWTValidationResponse} - The validation response object.
|
|
73
69
|
*/
|
|
74
|
-
validateToken(token: string): JWTValidationResponse {
|
|
70
|
+
public validateToken(token: string): JWTValidationResponse {
|
|
75
71
|
try {
|
|
76
72
|
// Check if is valid
|
|
77
73
|
const isValid = jwt.verify(token, this.tokenSecret)
|