@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
package/src/API/Response.ts
CHANGED
|
@@ -2,121 +2,90 @@ import type { Context } from 'aws-lambda'
|
|
|
2
2
|
|
|
3
3
|
import Transaction from '../BaseEvent/Transaction'
|
|
4
4
|
import Globals from '../Globals'
|
|
5
|
-
|
|
5
|
+
|
|
6
6
|
/**
|
|
7
|
-
*
|
|
8
|
-
*
|
|
9
|
-
* @
|
|
10
|
-
* @
|
|
11
|
-
* @
|
|
7
|
+
* CustomError class that extends the built-in Error class.
|
|
8
|
+
* @class
|
|
9
|
+
* @extends Error
|
|
10
|
+
* @param {any} body - The body of the error, which can be an object or a string.
|
|
11
|
+
* @constructor
|
|
12
|
+
* @property {string} name - The name of the error.
|
|
12
13
|
*/
|
|
13
14
|
class CustomError extends Error {
|
|
14
|
-
/**
|
|
15
|
-
* Creates an instance of CustomError.
|
|
16
|
-
*
|
|
17
|
-
* @constructor
|
|
18
|
-
* @param {*} body
|
|
19
|
-
*/
|
|
20
15
|
constructor(body: any) {
|
|
21
16
|
super(body.message || 'Unknown error!')
|
|
22
17
|
this.name = body.error || 'UnknownError'
|
|
23
18
|
}
|
|
24
19
|
}
|
|
25
|
-
|
|
20
|
+
|
|
26
21
|
/**
|
|
27
|
-
*
|
|
28
|
-
*
|
|
29
|
-
* @
|
|
30
|
-
* @
|
|
22
|
+
* Represents an error response from an API.
|
|
23
|
+
* @typedef {Object} ResponseErrorType
|
|
24
|
+
* @property {string} err - The error message.
|
|
25
|
+
* @property {string} [errCode] - The error code, if available.
|
|
31
26
|
*/
|
|
32
27
|
export type ResponseErrorType = {
|
|
33
28
|
err: string
|
|
34
29
|
errCode?: string
|
|
35
30
|
}
|
|
36
|
-
|
|
31
|
+
|
|
37
32
|
/**
|
|
38
|
-
*
|
|
39
|
-
*
|
|
40
|
-
* @export
|
|
41
|
-
* @class Response
|
|
42
|
-
* @typedef {Response}
|
|
43
|
-
* @template [BodyType=null]
|
|
33
|
+
* Represents a response object with various methods for building and manipulating the response.
|
|
34
|
+
* @template BodyType - The type of the response body.
|
|
44
35
|
*/
|
|
45
36
|
export default class Response<BodyType = null> {
|
|
46
37
|
/**
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
* @private
|
|
38
|
+
* The private property that stores the status code.
|
|
50
39
|
* @type {number}
|
|
40
|
+
* @private
|
|
51
41
|
*/
|
|
52
|
-
private
|
|
42
|
+
private statusCode: number
|
|
53
43
|
/**
|
|
54
|
-
*
|
|
55
|
-
*
|
|
44
|
+
* Private property representing the body of an object.
|
|
45
|
+
* @type {any}
|
|
56
46
|
* @private
|
|
57
|
-
* @type {*}
|
|
58
47
|
*/
|
|
59
|
-
private
|
|
48
|
+
private body: any
|
|
60
49
|
/**
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
* @private
|
|
50
|
+
* Indicates whether the object is currently piping out.
|
|
64
51
|
* @type {boolean}
|
|
65
52
|
*/
|
|
66
|
-
private
|
|
53
|
+
private isPipingOut: boolean
|
|
67
54
|
/**
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
* @private
|
|
71
|
-
* @type {object}
|
|
55
|
+
* Private property that stores the headers as an object.
|
|
72
56
|
*/
|
|
73
|
-
private
|
|
57
|
+
private headers: object
|
|
74
58
|
/**
|
|
75
|
-
*
|
|
76
|
-
*
|
|
77
|
-
* @public
|
|
78
|
-
* @readonly
|
|
79
|
-
* @type {boolean}
|
|
59
|
+
* Determines whether streaming is enabled or not.
|
|
60
|
+
* @returns {boolean} - True if streaming is enabled, false otherwise.
|
|
80
61
|
*/
|
|
81
62
|
public readonly shouldStream: boolean
|
|
82
63
|
/**
|
|
83
|
-
*
|
|
84
|
-
*
|
|
85
|
-
* @public
|
|
86
|
-
* @readonly
|
|
64
|
+
* Indicates whether the request body should be treated as raw data.
|
|
87
65
|
* @type {boolean}
|
|
88
66
|
*/
|
|
89
67
|
public readonly rawBody: boolean
|
|
90
68
|
/**
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
* @readonly
|
|
95
|
-
* @type {boolean}
|
|
69
|
+
* A boolean flag indicating whether to throw an error when encountering errors.
|
|
70
|
+
* If set to true, any errors encountered will result in an exception being thrown.
|
|
71
|
+
* If set to false, errors will be logged but the program will continue execution.
|
|
96
72
|
*/
|
|
97
73
|
public readonly throwOnErrors: boolean
|
|
98
74
|
/**
|
|
99
|
-
*
|
|
100
|
-
*
|
|
101
|
-
* @public
|
|
102
|
-
* @readonly
|
|
75
|
+
* Indicates whether the transaction ID is disabled.
|
|
103
76
|
* @type {boolean}
|
|
104
77
|
*/
|
|
105
78
|
public readonly disableTransactionID: boolean
|
|
106
|
-
|
|
79
|
+
|
|
107
80
|
/**
|
|
108
|
-
*
|
|
109
|
-
*
|
|
110
|
-
* @
|
|
111
|
-
* @param {
|
|
112
|
-
* @param {
|
|
113
|
-
* @param {
|
|
114
|
-
*
|
|
115
|
-
*
|
|
116
|
-
* throwOnErrors?: boolean
|
|
117
|
-
* disableTransactionID?: boolean
|
|
118
|
-
* }
|
|
119
|
-
* | undefined)\} [optBehaviour]
|
|
81
|
+
* Constructs a new Response object with the given status code, body, and optional behavior.
|
|
82
|
+
* @param {number} statusCode - The HTTP status code of the response.
|
|
83
|
+
* @param {BodyType} body - The body of the response.
|
|
84
|
+
* @param {Object} [optBehaviour] - Optional behavior configuration for the response.
|
|
85
|
+
* @param {boolean} [optBehaviour.shouldStream] - Indicates whether the response should be streamed.
|
|
86
|
+
* @param {boolean} [optBehaviour.rawBody] - Indicates whether the response body should be treated as raw data.
|
|
87
|
+
* @param {boolean} [optBehaviour.throwOnErrors] - Indicates whether errors should be thrown for non-successful status codes.
|
|
88
|
+
* @param {boolean} [optBehaviour
|
|
120
89
|
*/
|
|
121
90
|
constructor(
|
|
122
91
|
statusCode: number,
|
|
@@ -131,140 +100,132 @@ export default class Response<BodyType = null> {
|
|
|
131
100
|
| undefined
|
|
132
101
|
) {
|
|
133
102
|
// response
|
|
134
|
-
this.
|
|
135
|
-
this.
|
|
136
|
-
this.
|
|
103
|
+
this.statusCode = statusCode
|
|
104
|
+
this.body = body
|
|
105
|
+
this.headers = {
|
|
137
106
|
'Access-Control-Allow-Origin': '*',
|
|
138
107
|
'Access-Control-Allow-Credentials': true,
|
|
139
108
|
'Content-Type': 'application/json',
|
|
140
109
|
}
|
|
141
110
|
// behaviour
|
|
142
|
-
this.
|
|
111
|
+
this.isPipingOut = false //internal -- flag to indicate streaming out has started and avoid double stream call
|
|
143
112
|
// options
|
|
144
113
|
this.shouldStream = !!optBehaviour?.shouldStream //
|
|
145
114
|
this.rawBody = !!optBehaviour?.rawBody
|
|
146
115
|
this.throwOnErrors = !!optBehaviour?.throwOnErrors
|
|
147
116
|
this.disableTransactionID = !!optBehaviour?.disableTransactionID
|
|
148
117
|
}
|
|
118
|
+
|
|
149
119
|
/**
|
|
150
|
-
*
|
|
151
|
-
*
|
|
152
|
-
* @public
|
|
153
|
-
* @returns {number}
|
|
120
|
+
* Get the status code of the response.
|
|
121
|
+
* @returns {number} The status code.
|
|
154
122
|
*/
|
|
155
123
|
public getCode(): number {
|
|
156
|
-
return this.
|
|
124
|
+
return this.statusCode
|
|
157
125
|
}
|
|
126
|
+
|
|
158
127
|
/**
|
|
159
|
-
*
|
|
160
|
-
*
|
|
161
|
-
* @public
|
|
162
|
-
* @returns {BodyType}
|
|
128
|
+
* Get the body of the object.
|
|
129
|
+
* @returns {BodyType} The body of the object.
|
|
163
130
|
*/
|
|
164
131
|
public getBody(): BodyType {
|
|
165
|
-
return this.
|
|
132
|
+
return this.body
|
|
166
133
|
}
|
|
134
|
+
|
|
167
135
|
/**
|
|
168
|
-
*
|
|
169
|
-
*
|
|
170
|
-
* @
|
|
171
|
-
* @
|
|
172
|
-
* @param {*} value
|
|
136
|
+
* Appends a key-value pair into the body object.
|
|
137
|
+
* @param {string} key - The key to append.
|
|
138
|
+
* @param {any} value - The value to append.
|
|
139
|
+
* @returns None
|
|
173
140
|
*/
|
|
174
141
|
public appendIntoBody(key: string, value: any): void {
|
|
175
|
-
this.
|
|
142
|
+
this.body[key] = value
|
|
176
143
|
}
|
|
144
|
+
|
|
177
145
|
/**
|
|
178
|
-
*
|
|
179
|
-
*
|
|
180
|
-
* @
|
|
181
|
-
* @
|
|
182
|
-
* @param {*} value
|
|
146
|
+
* Appends a header to the existing headers object.
|
|
147
|
+
* @param {string} key - The key of the header.
|
|
148
|
+
* @param {any} value - The value of the header.
|
|
149
|
+
* @returns None
|
|
183
150
|
*/
|
|
184
151
|
public appendHeader(key: string, value: any): void {
|
|
185
|
-
this.
|
|
152
|
+
this.headers[key] = value
|
|
186
153
|
}
|
|
154
|
+
|
|
187
155
|
/**
|
|
188
|
-
*
|
|
189
|
-
*
|
|
190
|
-
* @
|
|
191
|
-
* @
|
|
192
|
-
* @
|
|
193
|
-
* @param {Transaction<any, any, any>} transaction
|
|
194
|
-
* @param {boolean} _optDoNotCallContext
|
|
195
|
-
* @returns {Promise<void>}
|
|
156
|
+
* Builds the response for the given context and transaction.
|
|
157
|
+
* @param {Context} context - The context object.
|
|
158
|
+
* @param {Transaction<any, any, any>} transaction - The transaction object.
|
|
159
|
+
* @param {boolean} optDoNotCallContext - Optional flag to indicate whether to call the context or not.
|
|
160
|
+
* @returns {Promise<void>} - A promise that resolves when the response is built.
|
|
196
161
|
*/
|
|
197
162
|
public async build(
|
|
198
163
|
context: Context,
|
|
199
164
|
transaction: Transaction<any, any, any>,
|
|
200
|
-
|
|
165
|
+
optDoNotCallContext: boolean
|
|
201
166
|
): Promise<void> {
|
|
202
167
|
//Stream support
|
|
203
|
-
if (this.
|
|
204
|
-
if (this.shouldStream) return this.
|
|
168
|
+
if (this.isPipingOut) return
|
|
169
|
+
if (this.shouldStream) return this.pipe(context)
|
|
205
170
|
|
|
206
171
|
//append default fields
|
|
207
|
-
if (transaction.request.getRequestID() && this.
|
|
172
|
+
if (transaction.request.getRequestID() && this.body && !this.disableTransactionID)
|
|
208
173
|
this.appendIntoBody('transactionID', transaction.request.getRequestID()) //append transaction ID
|
|
209
174
|
//Raw response support
|
|
210
|
-
if (this.rawBody) return this.
|
|
175
|
+
if (this.rawBody) return this.rawContext(context, transaction)
|
|
211
176
|
|
|
212
177
|
//build response
|
|
213
178
|
const b = {
|
|
214
|
-
statusCode: this.
|
|
215
|
-
headers: this.
|
|
216
|
-
...(this.
|
|
179
|
+
statusCode: this.statusCode,
|
|
180
|
+
headers: this.headers,
|
|
181
|
+
...(this.body ? { body: JSON.stringify(this.body) } : {}),
|
|
217
182
|
}
|
|
218
183
|
//log response and respond to context
|
|
219
184
|
transaction.logger.debug(b)
|
|
220
185
|
//Check for transaction response proxy
|
|
221
186
|
if (transaction.responseProxy) await transaction.responseProxy(this)
|
|
222
187
|
//Batch does not succeed directly just on upper transaction (which will should be a batch)
|
|
223
|
-
if (!
|
|
188
|
+
if (!optDoNotCallContext) context.succeed(b)
|
|
224
189
|
}
|
|
190
|
+
|
|
225
191
|
/**
|
|
226
|
-
*
|
|
227
|
-
*
|
|
228
|
-
* @
|
|
229
|
-
* @param {Context} context
|
|
192
|
+
* Private method that pipes the response to the given context.
|
|
193
|
+
* @param {Context} context - The context object provided by AWS Lambda.
|
|
194
|
+
* @returns None
|
|
230
195
|
*/
|
|
231
|
-
private
|
|
196
|
+
private pipe(context: Context): void {
|
|
232
197
|
//Check if not streaming
|
|
233
|
-
this.
|
|
198
|
+
this.isPipingOut = true
|
|
234
199
|
//build response
|
|
235
200
|
const b = {
|
|
236
|
-
statusCode: this.
|
|
237
|
-
body: this.
|
|
238
|
-
headers: this.
|
|
201
|
+
statusCode: this.statusCode,
|
|
202
|
+
body: this.body,
|
|
203
|
+
headers: this.headers,
|
|
239
204
|
}
|
|
240
205
|
//log response and respond to context
|
|
241
206
|
context.succeed(b)
|
|
242
207
|
}
|
|
208
|
+
|
|
243
209
|
/**
|
|
244
|
-
*
|
|
245
|
-
*
|
|
246
|
-
* @
|
|
247
|
-
* @
|
|
248
|
-
* @param {Transaction<null, BodyType>} transaction
|
|
210
|
+
* Private method that handles the raw context of a transaction.
|
|
211
|
+
* @param {Context} context - The context object.
|
|
212
|
+
* @param {Transaction<null, BodyType>} transaction - The transaction object.
|
|
213
|
+
* @returns None
|
|
249
214
|
*/
|
|
250
|
-
private
|
|
215
|
+
private rawContext(context: Context, transaction: Transaction<null, BodyType>): void {
|
|
251
216
|
//log response and respond to context
|
|
252
|
-
transaction.logger.debug(this.
|
|
253
|
-
if (this.getCode() <= 200 && this.getCode() <= 299) context.succeed(this.
|
|
217
|
+
transaction.logger.debug(this.body)
|
|
218
|
+
if (this.getCode() <= 200 && this.getCode() <= 299) context.succeed(this.body)
|
|
254
219
|
else {
|
|
255
|
-
if (!this.throwOnErrors) context.fail(new CustomError(this.
|
|
256
|
-
else throw new CustomError(this.
|
|
220
|
+
if (!this.throwOnErrors) context.fail(new CustomError(this.body))
|
|
221
|
+
else throw new CustomError(this.body)
|
|
257
222
|
}
|
|
258
223
|
}
|
|
259
224
|
|
|
260
|
-
/* Shortcuts */
|
|
261
225
|
/**
|
|
262
|
-
*
|
|
263
|
-
*
|
|
264
|
-
* @
|
|
265
|
-
* @static
|
|
266
|
-
* @param {string} paramName
|
|
267
|
-
* @returns {Response<ResponseErrorType>}
|
|
226
|
+
* Generates a response object for a missing path parameter error.
|
|
227
|
+
* @param {string} paramName - The name of the missing path parameter.
|
|
228
|
+
* @returns {Response<ResponseErrorType>} - The response object with error details.
|
|
268
229
|
*/
|
|
269
230
|
public static MissingParamResponse(paramName: string): Response<ResponseErrorType> {
|
|
270
231
|
console.warn(`Invalid request - Path parameter ${paramName} is missing.`)
|
|
@@ -273,13 +234,11 @@ export default class Response<BodyType = null> {
|
|
|
273
234
|
errCode: Globals.ErrorCode_MissingParam,
|
|
274
235
|
})
|
|
275
236
|
}
|
|
237
|
+
|
|
276
238
|
/**
|
|
277
|
-
*
|
|
278
|
-
*
|
|
279
|
-
* @
|
|
280
|
-
* @static
|
|
281
|
-
* @param {string} paramName
|
|
282
|
-
* @returns {Response<ResponseErrorType>}
|
|
239
|
+
* Creates a response object for a missing query parameter error.
|
|
240
|
+
* @param {string} paramName - The name of the missing query parameter.
|
|
241
|
+
* @returns {Response<ResponseErrorType>} - The response object with error details.
|
|
283
242
|
*/
|
|
284
243
|
public static MissingQueryResponse(paramName: string): Response<ResponseErrorType> {
|
|
285
244
|
console.warn(`Invalid request - Query parameter ${paramName} is missing.`)
|
|
@@ -288,15 +247,13 @@ export default class Response<BodyType = null> {
|
|
|
288
247
|
errCode: Globals.ErrorCode_MissingParam,
|
|
289
248
|
})
|
|
290
249
|
}
|
|
250
|
+
|
|
291
251
|
/**
|
|
292
|
-
*
|
|
293
|
-
*
|
|
294
|
-
* @
|
|
295
|
-
* @
|
|
296
|
-
* @
|
|
297
|
-
* @param {?string} [errCode]
|
|
298
|
-
* @param {?*} [optBody]
|
|
299
|
-
* @returns {Response<ResponseErrorType>}
|
|
252
|
+
* Creates a BadRequestResponse object with the given parameters.
|
|
253
|
+
* @param {string} [msg] - The error message.
|
|
254
|
+
* @param {string} [errCode] - The error code.
|
|
255
|
+
* @param {any} [optBody] - Optional additional body data.
|
|
256
|
+
* @returns {Response<ResponseErrorType>} - The BadRequestResponse object.
|
|
300
257
|
*/
|
|
301
258
|
public static BadRequestResponse(
|
|
302
259
|
msg?: string,
|
|
@@ -310,15 +267,13 @@ export default class Response<BodyType = null> {
|
|
|
310
267
|
...(optBody || {}),
|
|
311
268
|
})
|
|
312
269
|
}
|
|
270
|
+
|
|
313
271
|
/**
|
|
314
|
-
*
|
|
315
|
-
*
|
|
316
|
-
* @
|
|
317
|
-
* @
|
|
318
|
-
* @
|
|
319
|
-
* @param {?string} [errCode]
|
|
320
|
-
* @param {?*} [optBody]
|
|
321
|
-
* @returns {Response<ResponseErrorType>}
|
|
272
|
+
* Creates a BadRequestResponse object with rollback option.
|
|
273
|
+
* @param {string} msg - The error message.
|
|
274
|
+
* @param {string} [errCode] - The error code.
|
|
275
|
+
* @param {any} [optBody] - Optional body to include in the response.
|
|
276
|
+
* @returns {Response<ResponseErrorType>} - The BadRequestResponse object.
|
|
322
277
|
*/
|
|
323
278
|
public static BadRequestResponseWithRollback(
|
|
324
279
|
msg: string,
|
|
@@ -333,14 +288,12 @@ export default class Response<BodyType = null> {
|
|
|
333
288
|
...(optBody || {}),
|
|
334
289
|
})
|
|
335
290
|
}
|
|
291
|
+
|
|
336
292
|
/**
|
|
337
|
-
*
|
|
338
|
-
*
|
|
339
|
-
* @
|
|
340
|
-
* @
|
|
341
|
-
* @param {string} msg
|
|
342
|
-
* @param {?string} [errCode]
|
|
343
|
-
* @returns {Response<ResponseErrorType>}
|
|
293
|
+
* Creates an unauthorized response with the given error message and error code.
|
|
294
|
+
* @param {string} msg - The error message.
|
|
295
|
+
* @param {string} [errCode] - The error code (optional).
|
|
296
|
+
* @returns {Response<ResponseErrorType>} - The unauthorized response.
|
|
344
297
|
*/
|
|
345
298
|
public static UnauthorizedResponse(msg: string, errCode?: string): Response<ResponseErrorType> {
|
|
346
299
|
console.warn(`Denying request - ${msg}`)
|
|
@@ -349,49 +302,40 @@ export default class Response<BodyType = null> {
|
|
|
349
302
|
...(errCode ? { errCode: errCode } : {}),
|
|
350
303
|
})
|
|
351
304
|
}
|
|
305
|
+
|
|
352
306
|
/**
|
|
353
|
-
*
|
|
354
|
-
*
|
|
355
|
-
* @
|
|
356
|
-
* @static
|
|
357
|
-
* @template BodyType
|
|
358
|
-
* @param {BodyType} body
|
|
359
|
-
* @returns {Response<BodyType>}
|
|
307
|
+
* Creates a success response object with the given body.
|
|
308
|
+
* @param {BodyType} body - The body of the response.
|
|
309
|
+
* @returns {Response<BodyType>} - The success response object.
|
|
360
310
|
*/
|
|
361
311
|
public static SuccessResponse<BodyType>(body: BodyType): Response<BodyType> {
|
|
362
312
|
return new Response<BodyType>(200, (body ? body : {}) as BodyType)
|
|
363
313
|
}
|
|
314
|
+
|
|
364
315
|
/**
|
|
365
|
-
*
|
|
366
|
-
*
|
|
367
|
-
* @
|
|
368
|
-
* @static
|
|
369
|
-
* @param {string} url
|
|
370
|
-
* @returns {Response<null>}
|
|
316
|
+
* Creates a redirect response with the specified URL.
|
|
317
|
+
* @param {string} url - The URL to redirect to.
|
|
318
|
+
* @returns {Response<null>} - The redirect response.
|
|
371
319
|
*/
|
|
372
320
|
public static RedirectResponse(url: string): Response<null> {
|
|
373
321
|
const resp = new Response<null>(302, null)
|
|
374
322
|
resp.appendHeader('Location', url)
|
|
375
323
|
return resp
|
|
376
324
|
}
|
|
325
|
+
|
|
377
326
|
/**
|
|
378
|
-
*
|
|
379
|
-
*
|
|
380
|
-
* @public
|
|
381
|
-
* @static
|
|
382
|
-
* @returns {Response<null>}
|
|
327
|
+
* Creates a success response with no content.
|
|
328
|
+
* @returns {Response<null>} A response object with a status code of 204 and no content.
|
|
383
329
|
*/
|
|
384
330
|
public static SuccessNoContentResponse(): Response<null> {
|
|
385
331
|
return new Response<null>(204, null)
|
|
386
332
|
}
|
|
333
|
+
|
|
387
334
|
/**
|
|
388
|
-
*
|
|
389
|
-
*
|
|
390
|
-
* @
|
|
391
|
-
* @
|
|
392
|
-
* @param {*} stream
|
|
393
|
-
* @param {string} contentType
|
|
394
|
-
* @returns {Response}
|
|
335
|
+
* Creates a success response object with a streaming body and specified content type.
|
|
336
|
+
* @param {any} stream - The stream object to be used as the response body.
|
|
337
|
+
* @param {string} contentType - The content type of the response.
|
|
338
|
+
* @returns {Response} - The success response object.
|
|
395
339
|
*/
|
|
396
340
|
public static SuccessStreamResponse(stream: any, contentType: string): Response {
|
|
397
341
|
const resp = new Response(200, stream, {
|
|
@@ -401,15 +345,12 @@ export default class Response<BodyType = null> {
|
|
|
401
345
|
if (contentType) resp.appendHeader('Content-Type', contentType)
|
|
402
346
|
return resp
|
|
403
347
|
}
|
|
348
|
+
|
|
404
349
|
/**
|
|
405
|
-
*
|
|
406
|
-
*
|
|
407
|
-
* @
|
|
408
|
-
* @
|
|
409
|
-
* @template BodyType
|
|
410
|
-
* @param {BodyType} body
|
|
411
|
-
* @param {?number} [optionalCode]
|
|
412
|
-
* @returns {Response<BodyType>}
|
|
350
|
+
* Creates a simple HTTP response with the given body and optional status code.
|
|
351
|
+
* @param {BodyType} body - The body of the response.
|
|
352
|
+
* @param {number} [optionalCode] - The optional status code of the response. Defaults to 200.
|
|
353
|
+
* @returns {Response<BodyType>} - The created response object.
|
|
413
354
|
*/
|
|
414
355
|
public static SimpleResponse<BodyType>(
|
|
415
356
|
body: BodyType,
|
package/src/API/Utils.ts
CHANGED
|
@@ -1,69 +1,55 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* @export
|
|
5
|
-
* @class Utils
|
|
6
|
-
* @typedef {Utils}
|
|
2
|
+
* Utility class containing various static methods for common operations.
|
|
7
3
|
*/
|
|
8
4
|
export default class Utils {
|
|
9
5
|
/**
|
|
10
|
-
*
|
|
11
|
-
*
|
|
12
|
-
* @public
|
|
13
|
-
* @static
|
|
14
|
-
* @returns {boolean}
|
|
6
|
+
* Checks if the application is running in a hybridless container.
|
|
7
|
+
* @returns {boolean} - True if the application is running in a hybridless container, false otherwise.
|
|
15
8
|
*/
|
|
16
9
|
public static isHybridlessContainer(): boolean {
|
|
17
10
|
return process.env.HYBRIDLESS_RUNTIME == 'true'
|
|
18
11
|
}
|
|
12
|
+
|
|
19
13
|
/**
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
* @
|
|
23
|
-
* @static
|
|
24
|
-
* @param {string} string
|
|
25
|
-
* @returns {boolean}
|
|
14
|
+
* Checks if a given string is valid.
|
|
15
|
+
* @param {string} string - The string to check.
|
|
16
|
+
* @returns {boolean} - True if the string is valid, false otherwise.
|
|
26
17
|
*/
|
|
27
18
|
public static isValidString(string: string): boolean {
|
|
28
19
|
return string?.length > 0 && !Array.isArray(string)
|
|
29
20
|
}
|
|
21
|
+
|
|
30
22
|
/**
|
|
31
|
-
*
|
|
32
|
-
*
|
|
33
|
-
* @
|
|
34
|
-
* @static
|
|
35
|
-
* @param {string} string
|
|
36
|
-
* @returns {(number | null)}
|
|
23
|
+
* Parses a string into an integer and returns null if the string is not a valid number.
|
|
24
|
+
* @param {string} string - The string to parse into an integer.
|
|
25
|
+
* @returns {number | null} - The parsed integer or null if the string is not a valid number.
|
|
37
26
|
*/
|
|
38
27
|
public static parseIntNullIfNaN(string: string): number | null {
|
|
39
28
|
const n = parseInt(string)
|
|
40
29
|
return isNaN(n) ? null : n
|
|
41
30
|
}
|
|
31
|
+
|
|
42
32
|
/**
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
* @
|
|
46
|
-
* @
|
|
47
|
-
* @param {string} string
|
|
48
|
-
* @returns {(any | null)}
|
|
33
|
+
* Parses a JSON string and returns the resulting object. If the string is empty or
|
|
34
|
+
* cannot be parsed, null is returned.
|
|
35
|
+
* @param {string} string - The JSON string to parse.
|
|
36
|
+
* @returns {any | null} - The parsed object or null if the string is empty or invalid.
|
|
49
37
|
*/
|
|
50
|
-
public static parseObjectNullIfEmpty(string: string): any | null {
|
|
38
|
+
public static parseObjectNullIfEmpty(string: string | undefined): any | null {
|
|
51
39
|
let o = null
|
|
52
40
|
try {
|
|
53
|
-
o = JSON.parse(string)
|
|
54
|
-
if (Object.keys(o).length <= 0) o = null
|
|
41
|
+
o = string ? JSON.parse(string) : null
|
|
42
|
+
if (o && Object.keys(o).length <= 0) o = null
|
|
55
43
|
} catch (e) {
|
|
56
44
|
/* empty */
|
|
57
45
|
}
|
|
58
46
|
return o
|
|
59
47
|
}
|
|
48
|
+
|
|
60
49
|
/**
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
* @
|
|
64
|
-
* @static
|
|
65
|
-
* @param {string} number
|
|
66
|
-
* @returns {boolean}
|
|
50
|
+
* Checks if a given value is a valid number.
|
|
51
|
+
* @param {string} number - The value to be checked.
|
|
52
|
+
* @returns {boolean} - True if the value is a valid number, false otherwise.
|
|
67
53
|
*/
|
|
68
54
|
public static isValidNumber(number: string): boolean {
|
|
69
55
|
let validNumb = NaN
|
|
@@ -74,14 +60,12 @@ export default class Utils {
|
|
|
74
60
|
}
|
|
75
61
|
return !isNaN(validNumb) && !Array.isArray(number)
|
|
76
62
|
}
|
|
63
|
+
|
|
77
64
|
/**
|
|
78
|
-
*
|
|
79
|
-
*
|
|
80
|
-
* @
|
|
81
|
-
* @
|
|
82
|
-
* @param {*} obj
|
|
83
|
-
* @param {string} key
|
|
84
|
-
* @returns {(any | null)}
|
|
65
|
+
* Retrieves the value from an object using a case-insensitive key lookup.
|
|
66
|
+
* @param {any} obj - The object to search for the key.
|
|
67
|
+
* @param {string} key - The key to search for in the object.
|
|
68
|
+
* @returns {any | null} The value associated with the key, or null if the key is not found.
|
|
85
69
|
*/
|
|
86
70
|
public static caseInsensitiveObjectForKey(obj: any, key: string): any | null {
|
|
87
71
|
if (!obj) return null
|