@middy/util 6.1.5 → 6.2.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/index.d.ts +117 -117
- package/index.js +256 -249
- package/package.json +67 -71
- package/type-utils.d.ts +73 -56
package/index.d.ts
CHANGED
|
@@ -1,147 +1,147 @@
|
|
|
1
|
-
import middy from
|
|
2
|
-
import { Context as LambdaContext } from
|
|
1
|
+
import type middy from "@middy/core";
|
|
2
|
+
import type { Context as LambdaContext } from "aws-lambda";
|
|
3
3
|
import type {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
} from
|
|
4
|
+
ArrayValues,
|
|
5
|
+
Choose,
|
|
6
|
+
DeepAwaited,
|
|
7
|
+
IsUnknown,
|
|
8
|
+
SanitizeKey,
|
|
9
|
+
SanitizeKeys,
|
|
10
|
+
} from "./type-utils.d.ts";
|
|
11
11
|
|
|
12
12
|
interface Options<Client, ClientOptions> {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
13
|
+
AwsClient?: new (...[config]: [any] | any) => Client;
|
|
14
|
+
awsClientOptions?: Partial<ClientOptions>;
|
|
15
|
+
awsClientAssumeRole?: string;
|
|
16
|
+
awsClientCapture?: (service: Client) => Client;
|
|
17
|
+
fetchData?: { [key: string]: string };
|
|
18
|
+
disablePrefetch?: boolean;
|
|
19
|
+
cacheKey?: string;
|
|
20
|
+
cacheExpiry?: number;
|
|
21
|
+
setToContext?: boolean;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
24
|
declare class HttpError extends Error {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
25
|
+
status: number;
|
|
26
|
+
statusCode: number;
|
|
27
|
+
expose: boolean;
|
|
28
|
+
[key: string]: any;
|
|
29
|
+
[key: number]: any;
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
-
declare function createPrefetchClient<Client, ClientOptions>
|
|
33
|
-
|
|
34
|
-
): Client
|
|
32
|
+
declare function createPrefetchClient<Client, ClientOptions>(
|
|
33
|
+
options: Options<Client, ClientOptions>,
|
|
34
|
+
): Client;
|
|
35
35
|
|
|
36
|
-
declare function createClient<Client, ClientOptions>
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
): Promise<Client
|
|
36
|
+
declare function createClient<Client, ClientOptions>(
|
|
37
|
+
options: Options<Client, ClientOptions>,
|
|
38
|
+
request: middy.Request,
|
|
39
|
+
): Promise<Client>;
|
|
40
40
|
|
|
41
|
-
declare function canPrefetch<Client, ClientOptions>
|
|
42
|
-
|
|
43
|
-
): boolean
|
|
41
|
+
declare function canPrefetch<Client, ClientOptions>(
|
|
42
|
+
options: Options<Client, ClientOptions>,
|
|
43
|
+
): boolean;
|
|
44
44
|
|
|
45
45
|
type InternalOutput<TVariables> = TVariables extends string[]
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
? { [key in TVariables[number]]: unknown }
|
|
47
|
+
: never;
|
|
48
48
|
|
|
49
49
|
// get an empty object if false is passed
|
|
50
50
|
declare function getInternal<
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
>
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
): Promise<{}
|
|
51
|
+
TContext extends LambdaContext,
|
|
52
|
+
TInternal extends Record<string, unknown>,
|
|
53
|
+
>(
|
|
54
|
+
variables: false,
|
|
55
|
+
request: middy.Request<unknown, unknown, unknown, TContext, TInternal>,
|
|
56
|
+
): Promise<{}>;
|
|
57
57
|
|
|
58
58
|
// get all internal values if true is passed (with promises resolved)
|
|
59
59
|
declare function getInternal<
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
>
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
): Promise<DeepAwaited<TInternal
|
|
60
|
+
TContext extends LambdaContext,
|
|
61
|
+
TInternal extends Record<string, unknown>,
|
|
62
|
+
>(
|
|
63
|
+
variables: true,
|
|
64
|
+
request: middy.Request<unknown, unknown, unknown, TContext, TInternal>,
|
|
65
|
+
): Promise<DeepAwaited<TInternal>>;
|
|
66
66
|
|
|
67
67
|
// get a single value
|
|
68
68
|
declare function getInternal<
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
>
|
|
73
|
-
|
|
74
|
-
|
|
69
|
+
TContext extends LambdaContext,
|
|
70
|
+
TInternal extends Record<string, unknown>,
|
|
71
|
+
TVars extends keyof TInternal | string,
|
|
72
|
+
>(
|
|
73
|
+
variables: TVars,
|
|
74
|
+
request: middy.Request<unknown, unknown, unknown, TContext, TInternal>,
|
|
75
75
|
): TVars extends keyof TInternal
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
76
|
+
? Promise<DeepAwaited<{ [_ in SanitizeKey<TVars>]: TInternal[TVars] }>>
|
|
77
|
+
: TVars extends string
|
|
78
|
+
? IsUnknown<Choose<DeepAwaited<TInternal>, TVars>> extends true
|
|
79
|
+
? unknown // could not find the path
|
|
80
|
+
: Promise<{
|
|
81
|
+
[_ in SanitizeKey<TVars>]: Choose<DeepAwaited<TInternal>, TVars>;
|
|
82
|
+
}>
|
|
83
|
+
: unknown; // path is not a string or a keyof TInternal
|
|
84
84
|
|
|
85
85
|
// get multiple values
|
|
86
86
|
declare function getInternal<
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
>
|
|
91
|
-
|
|
92
|
-
|
|
87
|
+
TContext extends LambdaContext,
|
|
88
|
+
TInternal extends Record<string, unknown>,
|
|
89
|
+
TVars extends Array<keyof TInternal | string>,
|
|
90
|
+
>(
|
|
91
|
+
variables: TVars,
|
|
92
|
+
request: middy.Request<unknown, unknown, unknown, TContext, TInternal>,
|
|
93
93
|
): Promise<
|
|
94
|
-
SanitizeKeys<{
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
}>
|
|
101
|
-
|
|
94
|
+
SanitizeKeys<{
|
|
95
|
+
[TVar in ArrayValues<TVars>]: TVar extends keyof TInternal
|
|
96
|
+
? DeepAwaited<TInternal[TVar]>
|
|
97
|
+
: TVar extends string
|
|
98
|
+
? Choose<DeepAwaited<TInternal>, TVar>
|
|
99
|
+
: unknown; // path is not a string or a keyof TInternal
|
|
100
|
+
}>
|
|
101
|
+
>;
|
|
102
102
|
|
|
103
103
|
// remap object
|
|
104
104
|
declare function getInternal<
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
>
|
|
109
|
-
|
|
110
|
-
|
|
105
|
+
TContext extends LambdaContext,
|
|
106
|
+
TInternal extends Record<string, unknown>,
|
|
107
|
+
TMap extends Record<string, keyof TInternal | string>,
|
|
108
|
+
>(
|
|
109
|
+
variables: TMap,
|
|
110
|
+
request: middy.Request<unknown, unknown, unknown, TContext, TInternal>,
|
|
111
111
|
): Promise<{
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
declare function sanitizeKey<T extends string>
|
|
120
|
-
|
|
121
|
-
declare function processCache<Client, ClientOptions>
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
): { value: any
|
|
126
|
-
|
|
127
|
-
declare function getCache
|
|
128
|
-
|
|
129
|
-
declare function clearCache
|
|
130
|
-
|
|
131
|
-
declare function jsonSafeParse
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
): any
|
|
135
|
-
|
|
136
|
-
declare function normalizeHttpResponse
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
): any
|
|
140
|
-
|
|
141
|
-
declare function createError
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
): HttpError
|
|
146
|
-
|
|
147
|
-
declare function modifyCache
|
|
112
|
+
[P in keyof TMap]: TMap[P] extends keyof TInternal
|
|
113
|
+
? DeepAwaited<TInternal[TMap[P]]>
|
|
114
|
+
: TMap[P] extends string
|
|
115
|
+
? Choose<DeepAwaited<TInternal>, TMap[P]>
|
|
116
|
+
: unknown; // path is not a string or a keyof TInternal
|
|
117
|
+
}>;
|
|
118
|
+
|
|
119
|
+
declare function sanitizeKey<T extends string>(key: T): SanitizeKey<T>;
|
|
120
|
+
|
|
121
|
+
declare function processCache<Client, ClientOptions>(
|
|
122
|
+
options: Options<Client, ClientOptions>,
|
|
123
|
+
fetch: (request: middy.Request, cachedValues: any) => any,
|
|
124
|
+
request?: middy.Request,
|
|
125
|
+
): { value: any; expiry: number };
|
|
126
|
+
|
|
127
|
+
declare function getCache(keys: string): any;
|
|
128
|
+
|
|
129
|
+
declare function clearCache(keys?: string | string[] | null): void;
|
|
130
|
+
|
|
131
|
+
declare function jsonSafeParse(
|
|
132
|
+
string: string,
|
|
133
|
+
reviver?: (key: string, value: any) => any,
|
|
134
|
+
): any;
|
|
135
|
+
|
|
136
|
+
declare function normalizeHttpResponse(
|
|
137
|
+
request: any,
|
|
138
|
+
fallbackResponse?: any,
|
|
139
|
+
): any;
|
|
140
|
+
|
|
141
|
+
declare function createError(
|
|
142
|
+
code: number,
|
|
143
|
+
message: string,
|
|
144
|
+
properties?: Record<string, any>,
|
|
145
|
+
): HttpError;
|
|
146
|
+
|
|
147
|
+
declare function modifyCache(cacheKey: string, value: any): void;
|
package/index.js
CHANGED
|
@@ -1,295 +1,302 @@
|
|
|
1
1
|
export const createPrefetchClient = (options) => {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
const { awsClientOptions } = options;
|
|
3
|
+
const client = new options.AwsClient(awsClientOptions);
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
// AWS XRay
|
|
6
|
+
if (options.awsClientCapture) {
|
|
7
|
+
if (options.disablePrefetch) {
|
|
8
|
+
return options.awsClientCapture(client);
|
|
9
|
+
}
|
|
10
|
+
console.warn("Unable to apply X-Ray outside of handler invocation scope.");
|
|
11
|
+
}
|
|
11
12
|
|
|
12
|
-
|
|
13
|
-
}
|
|
13
|
+
return client;
|
|
14
|
+
};
|
|
14
15
|
|
|
15
16
|
export const createClient = async (options, request) => {
|
|
16
|
-
|
|
17
|
+
let awsClientCredentials = {};
|
|
17
18
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
19
|
+
// Role Credentials
|
|
20
|
+
if (options.awsClientAssumeRole) {
|
|
21
|
+
if (!request) {
|
|
22
|
+
throw new Error("Request required when assuming role", {
|
|
23
|
+
cause: { package: "@middy/util" },
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
awsClientCredentials = await getInternal(
|
|
27
|
+
{ credentials: options.awsClientAssumeRole },
|
|
28
|
+
request,
|
|
29
|
+
);
|
|
30
|
+
}
|
|
30
31
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
32
|
+
awsClientCredentials = {
|
|
33
|
+
...awsClientCredentials,
|
|
34
|
+
...options.awsClientOptions,
|
|
35
|
+
};
|
|
35
36
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
}
|
|
37
|
+
return createPrefetchClient({
|
|
38
|
+
...options,
|
|
39
|
+
awsClientOptions: awsClientCredentials,
|
|
40
|
+
});
|
|
41
|
+
};
|
|
41
42
|
|
|
42
43
|
export const canPrefetch = (options = {}) => {
|
|
43
|
-
|
|
44
|
-
}
|
|
44
|
+
return !options.awsClientAssumeRole && !options.disablePrefetch;
|
|
45
|
+
};
|
|
45
46
|
|
|
46
47
|
// Internal Context
|
|
47
48
|
export const getInternal = async (variables, request) => {
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
49
|
+
if (!variables || !request) return {};
|
|
50
|
+
let keys = [];
|
|
51
|
+
let values = [];
|
|
52
|
+
if (variables === true) {
|
|
53
|
+
keys = values = Object.keys(request.internal);
|
|
54
|
+
} else if (typeof variables === "string") {
|
|
55
|
+
keys = values = [variables];
|
|
56
|
+
} else if (Array.isArray(variables)) {
|
|
57
|
+
keys = values = variables;
|
|
58
|
+
} else if (typeof variables === "object") {
|
|
59
|
+
keys = Object.keys(variables);
|
|
60
|
+
values = Object.values(variables);
|
|
61
|
+
}
|
|
62
|
+
const promises = [];
|
|
63
|
+
for (const internalKey of values) {
|
|
64
|
+
// 'internal.key.sub_value' -> { [key]: internal.key.sub_value }
|
|
65
|
+
const pathOptionKey = internalKey.split(".");
|
|
66
|
+
const rootOptionKey = pathOptionKey.shift();
|
|
67
|
+
let valuePromise = request.internal[rootOptionKey];
|
|
68
|
+
if (!isPromise(valuePromise)) {
|
|
69
|
+
valuePromise = Promise.resolve(valuePromise);
|
|
70
|
+
}
|
|
71
|
+
promises.push(
|
|
72
|
+
valuePromise.then((value) =>
|
|
73
|
+
pathOptionKey.reduce((p, c) => p?.[c], value),
|
|
74
|
+
),
|
|
75
|
+
);
|
|
76
|
+
}
|
|
77
|
+
// ensure promise has resolved by the time it's needed
|
|
78
|
+
// If one of the promises throws it will bubble up to @middy/core
|
|
79
|
+
values = await Promise.allSettled(promises);
|
|
80
|
+
const errors = values
|
|
81
|
+
.filter((res) => res.status === "rejected")
|
|
82
|
+
.map((res) => res.reason);
|
|
83
|
+
if (errors.length) {
|
|
84
|
+
throw new Error("Failed to resolve internal values", {
|
|
85
|
+
cause: { package: "@middy/util", data: errors },
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
return keys.reduce(
|
|
89
|
+
(obj, key, index) =>
|
|
90
|
+
Object.assign(obj, { [sanitizeKey(key)]: values[index].value }),
|
|
91
|
+
{},
|
|
92
|
+
);
|
|
93
|
+
};
|
|
92
94
|
|
|
93
|
-
const isPromise = (promise) => typeof promise?.then ===
|
|
95
|
+
const isPromise = (promise) => typeof promise?.then === "function";
|
|
94
96
|
|
|
95
|
-
const sanitizeKeyPrefixLeadingNumber = /^([0-9])
|
|
96
|
-
const sanitizeKeyRemoveDisallowedChar = /[^a-zA-Z0-9]+/g
|
|
97
|
+
const sanitizeKeyPrefixLeadingNumber = /^([0-9])/;
|
|
98
|
+
const sanitizeKeyRemoveDisallowedChar = /[^a-zA-Z0-9]+/g;
|
|
97
99
|
export const sanitizeKey = (key) => {
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
}
|
|
100
|
+
return key
|
|
101
|
+
.replace(sanitizeKeyPrefixLeadingNumber, "_$1")
|
|
102
|
+
.replace(sanitizeKeyRemoveDisallowedChar, "_");
|
|
103
|
+
};
|
|
102
104
|
|
|
103
105
|
// fetch Cache
|
|
104
|
-
const cache = {} // key: { value:{fetchKey:Promise}, expiry }
|
|
106
|
+
const cache = {}; // key: { value:{fetchKey:Promise}, expiry }
|
|
105
107
|
export const processCache = (
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
108
|
+
options,
|
|
109
|
+
middlewareFetch = () => undefined,
|
|
110
|
+
request = {},
|
|
109
111
|
) => {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
112
|
+
let { cacheKey, cacheKeyExpiry, cacheExpiry } = options;
|
|
113
|
+
cacheExpiry = cacheKeyExpiry?.[cacheKey] ?? cacheExpiry;
|
|
114
|
+
const now = Date.now();
|
|
115
|
+
if (cacheExpiry) {
|
|
116
|
+
const cached = getCache(cacheKey);
|
|
117
|
+
const unexpired = cached.expiry && (cacheExpiry < 0 || cached.expiry > now);
|
|
116
118
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
}
|
|
119
|
+
if (unexpired) {
|
|
120
|
+
if (cached.modified) {
|
|
121
|
+
const value = middlewareFetch(request, cached.value);
|
|
122
|
+
Object.assign(cached.value, value);
|
|
123
|
+
cache[cacheKey] = { value: cached.value, expiry: cached.expiry };
|
|
124
|
+
return cache[cacheKey];
|
|
125
|
+
}
|
|
126
|
+
return { ...cached, cache: true };
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
const value = middlewareFetch(request);
|
|
130
|
+
// secrets-manager can override to unix timestamp
|
|
131
|
+
const expiry = cacheExpiry > 86400000 ? cacheExpiry : now + cacheExpiry;
|
|
132
|
+
const duration = cacheExpiry > 86400000 ? cacheExpiry - now : cacheExpiry;
|
|
133
|
+
if (cacheExpiry) {
|
|
134
|
+
const refresh =
|
|
135
|
+
duration > 0
|
|
136
|
+
? setTimeout(
|
|
137
|
+
() => processCache(options, middlewareFetch, request),
|
|
138
|
+
duration,
|
|
139
|
+
)
|
|
140
|
+
: undefined;
|
|
141
|
+
cache[cacheKey] = { value, expiry, refresh };
|
|
142
|
+
}
|
|
143
|
+
return { value, expiry };
|
|
144
|
+
};
|
|
143
145
|
|
|
144
146
|
export const catchInvalidSignatureException = (e, client, command) => {
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
}
|
|
147
|
+
if (e.__type === "InvalidSignatureException") {
|
|
148
|
+
return client.send(command);
|
|
149
|
+
}
|
|
150
|
+
throw e;
|
|
151
|
+
};
|
|
150
152
|
|
|
151
153
|
export const getCache = (key) => {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
}
|
|
154
|
+
if (!cache[key]) return {};
|
|
155
|
+
return cache[key];
|
|
156
|
+
};
|
|
155
157
|
|
|
156
158
|
// Used to remove parts of a cache
|
|
157
159
|
export const modifyCache = (cacheKey, value) => {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
}
|
|
160
|
+
if (!cache[cacheKey]) return;
|
|
161
|
+
clearTimeout(cache[cacheKey]?.refresh);
|
|
162
|
+
cache[cacheKey] = { ...cache[cacheKey], value, modified: true };
|
|
163
|
+
};
|
|
162
164
|
|
|
163
|
-
export const clearCache = (
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
165
|
+
export const clearCache = (inputKeys = null) => {
|
|
166
|
+
let keys = inputKeys;
|
|
167
|
+
keys ??= Object.keys(cache);
|
|
168
|
+
if (!Array.isArray(keys)) {
|
|
169
|
+
keys = [keys];
|
|
170
|
+
}
|
|
171
|
+
for (const cacheKey of keys) {
|
|
172
|
+
clearTimeout(cache[cacheKey]?.refresh);
|
|
173
|
+
cache[cacheKey] = undefined;
|
|
174
|
+
}
|
|
175
|
+
};
|
|
171
176
|
|
|
172
177
|
export const jsonSafeParse = (text, reviver) => {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
178
|
+
if (typeof text !== "string") return text;
|
|
179
|
+
const firstChar = text[0];
|
|
180
|
+
if (firstChar !== "{" && firstChar !== "[" && firstChar !== '"') return text;
|
|
181
|
+
try {
|
|
182
|
+
return JSON.parse(text, reviver);
|
|
183
|
+
} catch (e) {}
|
|
179
184
|
|
|
180
|
-
|
|
181
|
-
}
|
|
185
|
+
return text;
|
|
186
|
+
};
|
|
182
187
|
|
|
183
188
|
export const jsonSafeStringify = (value, replacer, space) => {
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
189
|
+
try {
|
|
190
|
+
return JSON.stringify(value, replacer, space);
|
|
191
|
+
} catch (e) {}
|
|
187
192
|
|
|
188
|
-
|
|
189
|
-
}
|
|
193
|
+
return value;
|
|
194
|
+
};
|
|
190
195
|
|
|
191
196
|
export const normalizeHttpResponse = (request) => {
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
}
|
|
197
|
+
let { response } = request;
|
|
198
|
+
if (typeof response === "undefined") {
|
|
199
|
+
response = {};
|
|
200
|
+
} else if (
|
|
201
|
+
typeof response?.statusCode === "undefined" &&
|
|
202
|
+
typeof response?.body === "undefined" &&
|
|
203
|
+
typeof response?.headers === "undefined"
|
|
204
|
+
) {
|
|
205
|
+
response = { statusCode: 200, body: response };
|
|
206
|
+
}
|
|
207
|
+
response.statusCode ??= 500;
|
|
208
|
+
response.headers ??= {};
|
|
209
|
+
request.response = response;
|
|
210
|
+
return response;
|
|
211
|
+
};
|
|
207
212
|
|
|
208
|
-
const createErrorRegexp = /[^a-zA-Z]/g
|
|
213
|
+
const createErrorRegexp = /[^a-zA-Z]/g;
|
|
209
214
|
export class HttpError extends Error {
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
215
|
+
constructor(code, optionalMessage, optionalOptions = {}) {
|
|
216
|
+
let message = optionalMessage;
|
|
217
|
+
let options = optionalOptions;
|
|
218
|
+
if (message && typeof message !== "string") {
|
|
219
|
+
options = message;
|
|
220
|
+
message = undefined;
|
|
221
|
+
}
|
|
222
|
+
message ??= httpErrorCodes[code];
|
|
223
|
+
super(message, options);
|
|
217
224
|
|
|
218
|
-
|
|
219
|
-
|
|
225
|
+
const name = httpErrorCodes[code].replace(createErrorRegexp, "");
|
|
226
|
+
this.name = name.substr(-5) !== "Error" ? `${name}Error` : name;
|
|
220
227
|
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
228
|
+
this.status = this.statusCode = code; // setting `status` for backwards compatibility w/ `http-errors`
|
|
229
|
+
this.expose = options.expose ?? code < 500;
|
|
230
|
+
}
|
|
224
231
|
}
|
|
225
232
|
|
|
226
233
|
export const createError = (code, message, properties = {}) => {
|
|
227
|
-
|
|
228
|
-
}
|
|
234
|
+
return new HttpError(code, message, properties);
|
|
235
|
+
};
|
|
229
236
|
|
|
230
237
|
const httpErrorCodes = {
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
}
|
|
238
|
+
100: "Continue",
|
|
239
|
+
101: "Switching Protocols",
|
|
240
|
+
102: "Processing",
|
|
241
|
+
103: "Early Hints",
|
|
242
|
+
200: "OK",
|
|
243
|
+
201: "Created",
|
|
244
|
+
202: "Accepted",
|
|
245
|
+
203: "Non-Authoritative Information",
|
|
246
|
+
204: "No Content",
|
|
247
|
+
205: "Reset Content",
|
|
248
|
+
206: "Partial Content",
|
|
249
|
+
207: "Multi-Status",
|
|
250
|
+
208: "Already Reported",
|
|
251
|
+
226: "IM Used",
|
|
252
|
+
300: "Multiple Choices",
|
|
253
|
+
301: "Moved Permanently",
|
|
254
|
+
302: "Found",
|
|
255
|
+
303: "See Other",
|
|
256
|
+
304: "Not Modified",
|
|
257
|
+
305: "Use Proxy",
|
|
258
|
+
306: "(Unused)",
|
|
259
|
+
307: "Temporary Redirect",
|
|
260
|
+
308: "Permanent Redirect",
|
|
261
|
+
400: "Bad Request",
|
|
262
|
+
401: "Unauthorized",
|
|
263
|
+
402: "Payment Required",
|
|
264
|
+
403: "Forbidden",
|
|
265
|
+
404: "Not Found",
|
|
266
|
+
405: "Method Not Allowed",
|
|
267
|
+
406: "Not Acceptable",
|
|
268
|
+
407: "Proxy Authentication Required",
|
|
269
|
+
408: "Request Timeout",
|
|
270
|
+
409: "Conflict",
|
|
271
|
+
410: "Gone",
|
|
272
|
+
411: "Length Required",
|
|
273
|
+
412: "Precondition Failed",
|
|
274
|
+
413: "Payload Too Large",
|
|
275
|
+
414: "URI Too Long",
|
|
276
|
+
415: "Unsupported Media Type",
|
|
277
|
+
416: "Range Not Satisfiable",
|
|
278
|
+
417: "Expectation Failed",
|
|
279
|
+
418: "I'm a teapot",
|
|
280
|
+
421: "Misdirected Request",
|
|
281
|
+
422: "Unprocessable Entity",
|
|
282
|
+
423: "Locked",
|
|
283
|
+
424: "Failed Dependency",
|
|
284
|
+
425: "Unordered Collection",
|
|
285
|
+
426: "Upgrade Required",
|
|
286
|
+
428: "Precondition Required",
|
|
287
|
+
429: "Too Many Requests",
|
|
288
|
+
431: "Request Header Fields Too Large",
|
|
289
|
+
451: "Unavailable For Legal Reasons",
|
|
290
|
+
500: "Internal Server Error",
|
|
291
|
+
501: "Not Implemented",
|
|
292
|
+
502: "Bad Gateway",
|
|
293
|
+
503: "Service Unavailable",
|
|
294
|
+
504: "Gateway Timeout",
|
|
295
|
+
505: "HTTP Version Not Supported",
|
|
296
|
+
506: "Variant Also Negotiates",
|
|
297
|
+
507: "Insufficient Storage",
|
|
298
|
+
508: "Loop Detected",
|
|
299
|
+
509: "Bandwidth Limit Exceeded",
|
|
300
|
+
510: "Not Extended",
|
|
301
|
+
511: "Network Authentication Required",
|
|
302
|
+
};
|
package/package.json
CHANGED
|
@@ -1,73 +1,69 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
"type": "github",
|
|
70
|
-
"url": "https://github.com/sponsors/willfarrell"
|
|
71
|
-
},
|
|
72
|
-
"gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
|
|
2
|
+
"name": "@middy/util",
|
|
3
|
+
"version": "6.2.0",
|
|
4
|
+
"description": "🛵 The stylish Node.js middleware engine for AWS Lambda (util package)",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"engines": {
|
|
7
|
+
"node": ">=20"
|
|
8
|
+
},
|
|
9
|
+
"engineStrict": true,
|
|
10
|
+
"publishConfig": {
|
|
11
|
+
"access": "public"
|
|
12
|
+
},
|
|
13
|
+
"module": "./index.js",
|
|
14
|
+
"exports": {
|
|
15
|
+
".": {
|
|
16
|
+
"import": {
|
|
17
|
+
"types": "./index.d.ts",
|
|
18
|
+
"default": "./index.js"
|
|
19
|
+
},
|
|
20
|
+
"require": {
|
|
21
|
+
"default": "./index.js"
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
"types": "index.d.ts",
|
|
26
|
+
"files": ["index.js", "index.d.ts", "type-utils.d.ts"],
|
|
27
|
+
"scripts": {
|
|
28
|
+
"test": "npm run test:unit && npm run test:fuzz",
|
|
29
|
+
"test:unit": "node --test",
|
|
30
|
+
"test:fuzz": "node --test index.fuzz.js",
|
|
31
|
+
"test:perf": "node --test index.perf.js"
|
|
32
|
+
},
|
|
33
|
+
"license": "MIT",
|
|
34
|
+
"keywords": [
|
|
35
|
+
"Lambda",
|
|
36
|
+
"Middleware",
|
|
37
|
+
"Serverless",
|
|
38
|
+
"Framework",
|
|
39
|
+
"AWS",
|
|
40
|
+
"AWS Lambda",
|
|
41
|
+
"helper",
|
|
42
|
+
"util"
|
|
43
|
+
],
|
|
44
|
+
"author": {
|
|
45
|
+
"name": "Middy contributors",
|
|
46
|
+
"url": "https://github.com/middyjs/middy/graphs/contributors"
|
|
47
|
+
},
|
|
48
|
+
"repository": {
|
|
49
|
+
"type": "git",
|
|
50
|
+
"url": "git+https://github.com/middyjs/middy.git",
|
|
51
|
+
"directory": "packages/util"
|
|
52
|
+
},
|
|
53
|
+
"bugs": {
|
|
54
|
+
"url": "https://github.com/middyjs/middy/issues"
|
|
55
|
+
},
|
|
56
|
+
"devDependencies": {
|
|
57
|
+
"@aws-sdk/client-ssm": "^3.0.0",
|
|
58
|
+
"@middy/core": "6.2.0",
|
|
59
|
+
"@types/aws-lambda": "^8.10.76",
|
|
60
|
+
"@types/node": "^20.0.0",
|
|
61
|
+
"aws-xray-sdk": "^3.3.3"
|
|
62
|
+
},
|
|
63
|
+
"homepage": "https://middy.js.org",
|
|
64
|
+
"funding": {
|
|
65
|
+
"type": "github",
|
|
66
|
+
"url": "https://github.com/sponsors/willfarrell"
|
|
67
|
+
},
|
|
68
|
+
"gitHead": "7a6c0fbb8ab71d6a2171e678697de9f237568431"
|
|
73
69
|
}
|
package/type-utils.d.ts
CHANGED
|
@@ -1,71 +1,88 @@
|
|
|
1
|
-
type Digit =
|
|
2
|
-
type LetterLower =
|
|
3
|
-
|
|
|
4
|
-
|
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
type Digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
|
|
2
|
+
type LetterLower =
|
|
3
|
+
| "a"
|
|
4
|
+
| "b"
|
|
5
|
+
| "c"
|
|
6
|
+
| "d"
|
|
7
|
+
| "e"
|
|
8
|
+
| "f"
|
|
9
|
+
| "g"
|
|
10
|
+
| "h"
|
|
11
|
+
| "i"
|
|
12
|
+
| "j"
|
|
13
|
+
| "k"
|
|
14
|
+
| "l"
|
|
15
|
+
| "m"
|
|
16
|
+
| "n"
|
|
17
|
+
| "o"
|
|
18
|
+
| "p"
|
|
19
|
+
| "q"
|
|
20
|
+
| "r"
|
|
21
|
+
| "s"
|
|
22
|
+
| "t"
|
|
23
|
+
| "u"
|
|
24
|
+
| "v"
|
|
25
|
+
| "w"
|
|
26
|
+
| "x"
|
|
27
|
+
| "y"
|
|
28
|
+
| "z";
|
|
29
|
+
type LetterUpper = Uppercase<LetterLower>;
|
|
30
|
+
type AlphaNumeric = Digit | LetterLower | LetterUpper;
|
|
7
31
|
|
|
8
32
|
type SanitizeKeyPrefixLeadingNumber<T> =
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
? `_${T}`
|
|
12
|
-
: T
|
|
33
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
34
|
+
T extends `${infer _ extends Digit}${any}` ? `_${T}` : T;
|
|
13
35
|
|
|
14
36
|
type SanitizeKeyRemoveDisallowedChar<T> =
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
37
|
+
T extends `${infer First}${infer Rest}`
|
|
38
|
+
? First extends AlphaNumeric
|
|
39
|
+
? `${First}${SanitizeKeyRemoveDisallowedChar<Rest>}`
|
|
40
|
+
: `_${SanitizeKeyRemoveDisallowedChar<Rest>}`
|
|
41
|
+
: T extends ""
|
|
42
|
+
? T
|
|
43
|
+
: never;
|
|
22
44
|
|
|
23
|
-
type RemoveAllLeadingUnderscore<T> =
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
: never
|
|
45
|
+
type RemoveAllLeadingUnderscore<T> = T extends `_${infer Rest}`
|
|
46
|
+
? RemoveAllLeadingUnderscore<Rest>
|
|
47
|
+
: T extends string
|
|
48
|
+
? T
|
|
49
|
+
: never;
|
|
29
50
|
|
|
30
|
-
type RemoveRepeatedUnderscore<T> =
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
: never
|
|
51
|
+
type RemoveRepeatedUnderscore<T> = T extends `${infer First}_${infer Rest}`
|
|
52
|
+
? `${First}_${RemoveRepeatedUnderscore<RemoveAllLeadingUnderscore<Rest>>}`
|
|
53
|
+
: T extends string
|
|
54
|
+
? T
|
|
55
|
+
: never;
|
|
36
56
|
|
|
37
|
-
export type SanitizeKey<T> =
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
SanitizeKeyPrefixLeadingNumber<T>
|
|
41
|
-
>
|
|
42
|
-
>
|
|
57
|
+
export type SanitizeKey<T> = RemoveRepeatedUnderscore<
|
|
58
|
+
SanitizeKeyRemoveDisallowedChar<SanitizeKeyPrefixLeadingNumber<T>>
|
|
59
|
+
>;
|
|
43
60
|
|
|
44
61
|
export type SanitizeKeys<T extends Record<string, unknown>> = {
|
|
45
|
-
|
|
46
|
-
}
|
|
62
|
+
[P in keyof T as SanitizeKey<P>]: T[P];
|
|
63
|
+
};
|
|
47
64
|
|
|
48
65
|
// recursion limit hack from https://www.angularfix.com/2022/01/why-am-i-getting-instantiation-is.html
|
|
49
|
-
type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
|
|
50
|
-
export type DeepAwaited<T, RecursionDepth extends Prev[number] = 4> =
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
66
|
+
type Prev = [never, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
|
67
|
+
export type DeepAwaited<T, RecursionDepth extends Prev[number] = 4> = [
|
|
68
|
+
RecursionDepth,
|
|
69
|
+
] extends [never]
|
|
70
|
+
? unknown // stop evaluating recursively rather than failing with never
|
|
71
|
+
: T extends Promise<infer R>
|
|
72
|
+
? Awaited<R>
|
|
73
|
+
: T extends Record<any, any>
|
|
74
|
+
? {
|
|
75
|
+
[P in keyof T]: T[P] extends Promise<infer R>
|
|
76
|
+
? Awaited<R> // if it's a Promise resolve
|
|
77
|
+
: DeepAwaited<T[P], Prev[RecursionDepth]>;
|
|
78
|
+
}
|
|
79
|
+
: T;
|
|
63
80
|
|
|
64
81
|
export type Choose<
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
> = K extends `${infer U}.${infer Rest}` ? Choose<T[U], Rest> : T[K]
|
|
82
|
+
T extends Record<string | number, any>,
|
|
83
|
+
K extends string,
|
|
84
|
+
> = K extends `${infer U}.${infer Rest}` ? Choose<T[U], Rest> : T[K];
|
|
68
85
|
|
|
69
|
-
export type IsUnknown<T> = unknown extends T ? true : false
|
|
86
|
+
export type IsUnknown<T> = unknown extends T ? true : false;
|
|
70
87
|
|
|
71
|
-
export type ArrayValues<T> = T extends Array<infer U> ? U : never
|
|
88
|
+
export type ArrayValues<T> = T extends Array<infer U> ? U : never;
|