@middy/util 3.0.0-alpha.5 → 3.0.0-alpha.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/index.js +103 -55
- package/package.json +3 -3
- package/codes.js +0 -66
package/index.js
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
import { Agent } from 'https';
|
|
2
|
-
import statuses from './codes.js';
|
|
3
|
-
import { inherits } from 'util';
|
|
4
2
|
export const awsClientDefaultOptions = {
|
|
5
3
|
httpOptions: {
|
|
6
4
|
agent: new Agent({
|
|
@@ -26,7 +24,10 @@ export const createClient = async (options, request) => {
|
|
|
26
24
|
let awsClientCredentials = {};
|
|
27
25
|
|
|
28
26
|
if (options.awsClientAssumeRole) {
|
|
29
|
-
if (!request)
|
|
27
|
+
if (!request) {
|
|
28
|
+
throw new Error('Request required when assuming role');
|
|
29
|
+
}
|
|
30
|
+
|
|
30
31
|
awsClientCredentials = await getInternal({
|
|
31
32
|
credentials: options.awsClientAssumeRole
|
|
32
33
|
}, request);
|
|
@@ -65,7 +66,7 @@ export const getInternal = async (variables, request) => {
|
|
|
65
66
|
const rootOptionKey = pathOptionKey.shift();
|
|
66
67
|
let valuePromise = request.internal[rootOptionKey];
|
|
67
68
|
|
|
68
|
-
if (
|
|
69
|
+
if (!isPromise(valuePromise)) {
|
|
69
70
|
valuePromise = Promise.resolve(valuePromise);
|
|
70
71
|
}
|
|
71
72
|
|
|
@@ -85,6 +86,9 @@ export const getInternal = async (variables, request) => {
|
|
|
85
86
|
[sanitizeKey(key)]: values[index].value
|
|
86
87
|
}), {});
|
|
87
88
|
};
|
|
89
|
+
|
|
90
|
+
const isPromise = promise => typeof (promise === null || promise === void 0 ? void 0 : promise.then) === 'function';
|
|
91
|
+
|
|
88
92
|
const sanitizeKeyPrefixLeadingNumber = /^([0-9])/;
|
|
89
93
|
const sanitizeKeyRemoveDisallowedChar = /[^a-zA-Z0-9]+/g;
|
|
90
94
|
export const sanitizeKey = key => {
|
|
@@ -153,16 +157,23 @@ export const clearCache = (keys = null) => {
|
|
|
153
157
|
cache[cacheKey] = undefined;
|
|
154
158
|
}
|
|
155
159
|
};
|
|
156
|
-
export const jsonSafeParse = (
|
|
157
|
-
if (typeof
|
|
158
|
-
const firstChar =
|
|
159
|
-
if (firstChar !== '{' && firstChar !== '[' && firstChar !== '"') return
|
|
160
|
+
export const jsonSafeParse = (text, reviver) => {
|
|
161
|
+
if (typeof text !== 'string') return text;
|
|
162
|
+
const firstChar = text[0];
|
|
163
|
+
if (firstChar !== '{' && firstChar !== '[' && firstChar !== '"') return text;
|
|
164
|
+
|
|
165
|
+
try {
|
|
166
|
+
return JSON.parse(text, reviver);
|
|
167
|
+
} catch (e) {}
|
|
160
168
|
|
|
169
|
+
return text;
|
|
170
|
+
};
|
|
171
|
+
export const jsonSafeStringify = (value, replacer, space) => {
|
|
161
172
|
try {
|
|
162
|
-
return JSON.
|
|
173
|
+
return JSON.stringify(value, replacer, space);
|
|
163
174
|
} catch (e) {}
|
|
164
175
|
|
|
165
|
-
return
|
|
176
|
+
return value;
|
|
166
177
|
};
|
|
167
178
|
export const normalizeHttpResponse = request => {
|
|
168
179
|
var _response, _response2, _response3, _response4;
|
|
@@ -184,52 +195,89 @@ export const normalizeHttpResponse = request => {
|
|
|
184
195
|
return response;
|
|
185
196
|
};
|
|
186
197
|
const createErrorRegexp = /[^a-zA-Z]/g;
|
|
187
|
-
export
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
});
|
|
202
|
-
Object.defineProperty(err, 'name', {
|
|
203
|
-
enumerable: false,
|
|
204
|
-
configurable: true,
|
|
205
|
-
value: className,
|
|
206
|
-
writable: true
|
|
207
|
-
});
|
|
208
|
-
return err;
|
|
198
|
+
export class HttpError extends Error {
|
|
199
|
+
constructor(code, message, options = {}) {
|
|
200
|
+
if (message && typeof message !== 'string') {
|
|
201
|
+
options = message;
|
|
202
|
+
message = undefined;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
message ?? (message = httpErrorCodes[code]);
|
|
206
|
+
super(message);
|
|
207
|
+
this.cause = options.cause;
|
|
208
|
+
const name = httpErrorCodes[code].replace(createErrorRegexp, '');
|
|
209
|
+
this.name = name.substr(-5) !== 'Error' ? name + 'Error' : name;
|
|
210
|
+
this.status = this.statusCode = code;
|
|
211
|
+
this.expose = options.expose ?? code < 500;
|
|
209
212
|
}
|
|
210
213
|
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
Object.defineProperty(HttpError, 'name', desc);
|
|
215
|
-
Object.assign(HttpError.prototype, {
|
|
216
|
-
status: code,
|
|
217
|
-
statusCode: code,
|
|
218
|
-
expose: code < 500
|
|
219
|
-
}, properties);
|
|
220
|
-
return new HttpError(message);
|
|
214
|
+
}
|
|
215
|
+
export const createError = (code, message, properties = {}) => {
|
|
216
|
+
return new HttpError(code, message, properties);
|
|
221
217
|
};
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
218
|
+
const httpErrorCodes = {
|
|
219
|
+
100: 'Continue',
|
|
220
|
+
101: 'Switching Protocols',
|
|
221
|
+
102: 'Processing',
|
|
222
|
+
103: 'Early Hints',
|
|
223
|
+
200: 'OK',
|
|
224
|
+
201: 'Created',
|
|
225
|
+
202: 'Accepted',
|
|
226
|
+
203: 'Non-Authoritative Information',
|
|
227
|
+
204: 'No Content',
|
|
228
|
+
205: 'Reset Content',
|
|
229
|
+
206: 'Partial Content',
|
|
230
|
+
207: 'Multi-Status',
|
|
231
|
+
208: 'Already Reported',
|
|
232
|
+
226: 'IM Used',
|
|
233
|
+
300: 'Multiple Choices',
|
|
234
|
+
301: 'Moved Permanently',
|
|
235
|
+
302: 'Found',
|
|
236
|
+
303: 'See Other',
|
|
237
|
+
304: 'Not Modified',
|
|
238
|
+
305: 'Use Proxy',
|
|
239
|
+
306: '(Unused)',
|
|
240
|
+
307: 'Temporary Redirect',
|
|
241
|
+
308: 'Permanent Redirect',
|
|
242
|
+
400: 'Bad Request',
|
|
243
|
+
401: 'Unauthorized',
|
|
244
|
+
402: 'Payment Required',
|
|
245
|
+
403: 'Forbidden',
|
|
246
|
+
404: 'Not Found',
|
|
247
|
+
405: 'Method Not Allowed',
|
|
248
|
+
406: 'Not Acceptable',
|
|
249
|
+
407: 'Proxy Authentication Required',
|
|
250
|
+
408: 'Request Timeout',
|
|
251
|
+
409: 'Conflict',
|
|
252
|
+
410: 'Gone',
|
|
253
|
+
411: 'Length Required',
|
|
254
|
+
412: 'Precondition Failed',
|
|
255
|
+
413: 'Payload Too Large',
|
|
256
|
+
414: 'URI Too Long',
|
|
257
|
+
415: 'Unsupported Media Type',
|
|
258
|
+
416: 'Range Not Satisfiable',
|
|
259
|
+
417: 'Expectation Failed',
|
|
260
|
+
418: "I'm a teapot",
|
|
261
|
+
421: 'Misdirected Request',
|
|
262
|
+
422: 'Unprocessable Entity',
|
|
263
|
+
423: 'Locked',
|
|
264
|
+
424: 'Failed Dependency',
|
|
265
|
+
425: 'Unordered Collection',
|
|
266
|
+
426: 'Upgrade Required',
|
|
267
|
+
428: 'Precondition Required',
|
|
268
|
+
429: 'Too Many Requests',
|
|
269
|
+
431: 'Request Header Fields Too Large',
|
|
270
|
+
451: 'Unavailable For Legal Reasons',
|
|
271
|
+
500: 'Internal Server Error',
|
|
272
|
+
501: 'Not Implemented',
|
|
273
|
+
502: 'Bad Gateway',
|
|
274
|
+
503: 'Service Unavailable',
|
|
275
|
+
504: 'Gateway Timeout',
|
|
276
|
+
505: 'HTTP Version Not Supported',
|
|
277
|
+
506: 'Variant Also Negotiates',
|
|
278
|
+
507: 'Insufficient Storage',
|
|
279
|
+
508: 'Loop Detected',
|
|
280
|
+
509: 'Bandwidth Limit Exceeded',
|
|
281
|
+
510: 'Not Extended',
|
|
282
|
+
511: 'Network Authentication Required'
|
|
235
283
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@middy/util",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.6",
|
|
4
4
|
"description": "🛵 The stylish Node.js middleware engine for AWS Lambda (util package)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -46,12 +46,12 @@
|
|
|
46
46
|
"url": "https://github.com/middyjs/middy/issues"
|
|
47
47
|
},
|
|
48
48
|
"devDependencies": {
|
|
49
|
-
"@middy/core": "^3.0.0-alpha.
|
|
49
|
+
"@middy/core": "^3.0.0-alpha.6",
|
|
50
50
|
"@types/aws-lambda": "^8.10.76",
|
|
51
51
|
"@types/node": "^17.0.0",
|
|
52
52
|
"aws-sdk": "^2.939.0",
|
|
53
53
|
"aws-xray-sdk": "^3.3.3"
|
|
54
54
|
},
|
|
55
55
|
"homepage": "https://github.com/middyjs/middy#readme",
|
|
56
|
-
"gitHead": "
|
|
56
|
+
"gitHead": "176660ed3e0716d6bfb635c77251b301e0e24720"
|
|
57
57
|
}
|
package/codes.js
DELETED
|
@@ -1,66 +0,0 @@
|
|
|
1
|
-
export default {
|
|
2
|
-
100: 'Continue',
|
|
3
|
-
101: 'Switching Protocols',
|
|
4
|
-
102: 'Processing',
|
|
5
|
-
103: 'Early Hints',
|
|
6
|
-
200: 'OK',
|
|
7
|
-
201: 'Created',
|
|
8
|
-
202: 'Accepted',
|
|
9
|
-
203: 'Non-Authoritative Information',
|
|
10
|
-
204: 'No Content',
|
|
11
|
-
205: 'Reset Content',
|
|
12
|
-
206: 'Partial Content',
|
|
13
|
-
207: 'Multi-Status',
|
|
14
|
-
208: 'Already Reported',
|
|
15
|
-
226: 'IM Used',
|
|
16
|
-
300: 'Multiple Choices',
|
|
17
|
-
301: 'Moved Permanently',
|
|
18
|
-
302: 'Found',
|
|
19
|
-
303: 'See Other',
|
|
20
|
-
304: 'Not Modified',
|
|
21
|
-
305: 'Use Proxy',
|
|
22
|
-
306: '(Unused)',
|
|
23
|
-
307: 'Temporary Redirect',
|
|
24
|
-
308: 'Permanent Redirect',
|
|
25
|
-
400: 'Bad Request',
|
|
26
|
-
401: 'Unauthorized',
|
|
27
|
-
402: 'Payment Required',
|
|
28
|
-
403: 'Forbidden',
|
|
29
|
-
404: 'Not Found',
|
|
30
|
-
405: 'Method Not Allowed',
|
|
31
|
-
406: 'Not Acceptable',
|
|
32
|
-
407: 'Proxy Authentication Required',
|
|
33
|
-
408: 'Request Timeout',
|
|
34
|
-
409: 'Conflict',
|
|
35
|
-
410: 'Gone',
|
|
36
|
-
411: 'Length Required',
|
|
37
|
-
412: 'Precondition Failed',
|
|
38
|
-
413: 'Payload Too Large',
|
|
39
|
-
414: 'URI Too Long',
|
|
40
|
-
415: 'Unsupported Media Type',
|
|
41
|
-
416: 'Range Not Satisfiable',
|
|
42
|
-
417: 'Expectation Failed',
|
|
43
|
-
418: "I'm a teapot",
|
|
44
|
-
421: 'Misdirected Request',
|
|
45
|
-
422: 'Unprocessable Entity',
|
|
46
|
-
423: 'Locked',
|
|
47
|
-
424: 'Failed Dependency',
|
|
48
|
-
425: 'Unordered Collection',
|
|
49
|
-
426: 'Upgrade Required',
|
|
50
|
-
428: 'Precondition Required',
|
|
51
|
-
429: 'Too Many Requests',
|
|
52
|
-
431: 'Request Header Fields Too Large',
|
|
53
|
-
451: 'Unavailable For Legal Reasons',
|
|
54
|
-
500: 'Internal Server Error',
|
|
55
|
-
501: 'Not Implemented',
|
|
56
|
-
502: 'Bad Gateway',
|
|
57
|
-
503: 'Service Unavailable',
|
|
58
|
-
504: 'Gateway Timeout',
|
|
59
|
-
505: 'HTTP Version Not Supported',
|
|
60
|
-
506: 'Variant Also Negotiates',
|
|
61
|
-
507: 'Insufficient Storage',
|
|
62
|
-
508: 'Loop Detected',
|
|
63
|
-
509: 'Bandwidth Limit Exceeded',
|
|
64
|
-
510: 'Not Extended',
|
|
65
|
-
511: 'Network Authentication Required'
|
|
66
|
-
}
|