@middy/util 3.0.0-alpha.5 → 3.0.0-alpha.8
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 +23 -13
- package/index.cjs +3 -0
- package/index.js +2 -234
- package/package.json +12 -5
- package/codes.js +0 -66
package/README.md
CHANGED
|
@@ -1,26 +1,36 @@
|
|
|
1
|
-
# Middy Util
|
|
2
|
-
|
|
3
|
-
<div align="center">
|
|
4
|
-
<img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.png"/>
|
|
5
|
-
</div>
|
|
6
|
-
|
|
7
1
|
<div align="center">
|
|
2
|
+
<h1>Middy Utilities</h1>
|
|
3
|
+
<img alt="Middy logo" src="https://raw.githubusercontent.com/middyjs/middy/main/docs/img/middy-logo.svg"/>
|
|
8
4
|
<p><strong>Util component of the middy middleware, the stylish Node.js middleware engine for AWS Lambda</strong></p>
|
|
9
|
-
</div>
|
|
10
|
-
|
|
11
|
-
<div align="center">
|
|
12
5
|
<p>
|
|
13
|
-
<a href="
|
|
6
|
+
<a href="https://www.npmjs.com/package/@middy/util?activeTab=versions">
|
|
14
7
|
<img src="https://badge.fury.io/js/%40middy%2Futil.svg" alt="npm version" style="max-width:100%;">
|
|
15
8
|
</a>
|
|
9
|
+
<a href="https://packagephobia.com/result?p=@middy/util">
|
|
10
|
+
<img src="https://packagephobia.com/badge?p=@middy/util" alt="npm install size" style="max-width:100%;">
|
|
11
|
+
</a>
|
|
12
|
+
<a href="https://github.com/middyjs/middy/actions">
|
|
13
|
+
<img src="https://github.com/middyjs/middy/workflows/Tests/badge.svg" alt="GitHub Actions test status badge" style="max-width:100%;">
|
|
14
|
+
</a>
|
|
15
|
+
<br/>
|
|
16
|
+
<a href="https://standardjs.com/">
|
|
17
|
+
<img src="https://img.shields.io/badge/code_style-standard-brightgreen.svg" alt="Standard Code Style" style="max-width:100%;">
|
|
18
|
+
</a>
|
|
16
19
|
<a href="https://snyk.io/test/github/middyjs/middy">
|
|
17
20
|
<img src="https://snyk.io/test/github/middyjs/middy/badge.svg" alt="Known Vulnerabilities" data-canonical-src="https://snyk.io/test/github/middyjs/middy" style="max-width:100%;">
|
|
18
21
|
</a>
|
|
19
|
-
<a href="https://
|
|
20
|
-
<img src="https://img.shields.io/
|
|
22
|
+
<a href="https://lgtm.com/projects/g/middyjs/middy/context:javascript">
|
|
23
|
+
<img src="https://img.shields.io/lgtm/grade/javascript/g/middyjs/middy.svg?logo=lgtm&logoWidth=18" alt="Language grade: JavaScript" style="max-width:100%;">
|
|
24
|
+
</a>
|
|
25
|
+
<a href="https://bestpractices.coreinfrastructure.org/projects/5280">
|
|
26
|
+
<img src="https://bestpractices.coreinfrastructure.org/projects/5280/badge" alt="Core Infrastructure Initiative (CII) Best Practices" style="max-width:100%;">
|
|
21
27
|
</a>
|
|
28
|
+
<br/>
|
|
22
29
|
<a href="https://gitter.im/middyjs/Lobby">
|
|
23
|
-
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter"
|
|
30
|
+
<img src="https://badges.gitter.im/gitterHQ/gitter.svg" alt="Chat on Gitter" style="max-width:100%;">
|
|
31
|
+
</a>
|
|
32
|
+
<a href="https://stackoverflow.com/questions/tagged/middy?sort=Newest&uqlId=35052">
|
|
33
|
+
<img src="https://img.shields.io/badge/StackOverflow-[middy]-yellow" alt="Ask questions on StackOverflow" style="max-width:100%;">
|
|
24
34
|
</a>
|
|
25
35
|
</p>
|
|
26
36
|
</div>
|
package/index.cjs
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.createError=exports.normalizeHttpResponse=exports.jsonSafeStringify=exports.jsonSafeParse=exports.clearCache=exports.modifyCache=exports.getCache=exports.processCache=exports.sanitizeKey=exports.getInternal=exports.canPrefetch=exports.createClient=exports.createPrefetchClient=exports.awsClientDefaultOptions=void 0;var _https=require("https");var _response;const awsClientDefaultOptions={httpOptions:{agent:new _https.Agent({secureProtocol:"TLSv1_2_method"})}};exports.awsClientDefaultOptions=awsClientDefaultOptions;const createPrefetchClient=options=>{const awsClientOptions={...awsClientDefaultOptions,...options.awsClientOptions};const client=new options.AwsClient(awsClientOptions);if(options.awsClientCapture&&options.disablePrefetch){return options.awsClientCapture(client)}else if(options.awsClientCapture){console.warn("Unable to apply X-Ray outside of handler invocation scope.")}return client};exports.createPrefetchClient=createPrefetchClient;const createClient=async(options,request)=>{let awsClientCredentials={};if(options.awsClientAssumeRole){if(!request){throw new Error("Request required when assuming role")}awsClientCredentials=await getInternal({credentials:options.awsClientAssumeRole},request)}awsClientCredentials={...awsClientCredentials,...options.awsClientOptions};return createPrefetchClient({...options,awsClientOptions:awsClientCredentials})};exports.createClient=createClient;const canPrefetch=(options={})=>{return!options.awsClientAssumeRole&&!options.disablePrefetch};exports.canPrefetch=canPrefetch;const getInternal=async(variables,request)=>{if(!variables||!request)return{};let keys=[];let values=[];if(variables===true){keys=values=Object.keys(request.internal)}else if(typeof variables==="string"){keys=values=[variables]}else if(Array.isArray(variables)){keys=values=variables}else if(typeof variables==="object"){keys=Object.keys(variables);values=Object.values(variables)}const promises=[];for(const internalKey of values){const pathOptionKey=internalKey.split(".");const rootOptionKey=pathOptionKey.shift();let valuePromise=request.internal[rootOptionKey];if(!isPromise(valuePromise)){valuePromise=Promise.resolve(valuePromise)}promises.push(valuePromise.then(value=>pathOptionKey.reduce((p,c)=>p?.[c],value)))}values=await Promise.allSettled(promises);const errors=values.filter(res=>res.status==="rejected").map(res=>res.reason);if(errors.length){const error=new Error("Failed to resolve internal values");error.cause=errors;throw error}return keys.reduce((obj,key,index)=>({...obj,[sanitizeKey(key)]:values[index].value}),{})};exports.getInternal=getInternal;const isPromise=promise=>typeof promise?.then==="function";const sanitizeKeyPrefixLeadingNumber=/^([0-9])/;const sanitizeKeyRemoveDisallowedChar=/[^a-zA-Z0-9]+/g;const sanitizeKey=key=>{return key.replace(sanitizeKeyPrefixLeadingNumber,"_$1").replace(sanitizeKeyRemoveDisallowedChar,"_")};exports.sanitizeKey=sanitizeKey;const cache={};const processCache=(options,fetch=()=>undefined,request)=>{const{cacheExpiry,cacheKey}=options;if(cacheExpiry){const cached=getCache(cacheKey);const unexpired=cached.expiry&&(cacheExpiry<0||cached.expiry>Date.now());if(unexpired&&cached.modified){const value=fetch(request,cached.value);cache[cacheKey]={value:{...cached.value,...value},expiry:cached.expiry};return cache[cacheKey]}if(unexpired){return{...cached,cache:true}}}const value=fetch(request);const expiry=Date.now()+cacheExpiry;if(cacheExpiry){cache[cacheKey]={value,expiry}}return{value,expiry}};exports.processCache=processCache;const getCache=key=>{if(!cache[key])return{};return cache[key]};exports.getCache=getCache;const modifyCache=(cacheKey,value)=>{if(!cache[cacheKey])return;cache[cacheKey]={...cache[cacheKey],value,modified:true}};exports.modifyCache=modifyCache;const clearCache=(keys=null)=>{keys=keys??Object.keys(cache);if(!Array.isArray(keys))keys=[keys];for(const cacheKey of keys){cache[cacheKey]=undefined}};exports.clearCache=clearCache;const jsonSafeParse=(text,reviver)=>{if(typeof text!=="string")return text;const firstChar=text[0];if(firstChar!=="{"&&firstChar!=="["&&firstChar!=='"')return text;try{return JSON.parse(text,reviver)}catch(e){}return text};exports.jsonSafeParse=jsonSafeParse;const jsonSafeStringify=(value,replacer,space)=>{try{return JSON.stringify(value,replacer,space)}catch(e){}return value};exports.jsonSafeStringify=jsonSafeStringify;const normalizeHttpResponse=request=>{let{response}=request;if(response===undefined){response={}}else if(response?.statusCode===undefined&&response?.body===undefined&&response?.headers===undefined){response={body:response}}(_response=response).headers??(_response.headers={});request.response=response;return response};exports.normalizeHttpResponse=normalizeHttpResponse;const createErrorRegexp=/[^a-zA-Z]/g;class HttpError extends Error{constructor(code,message,options={}){if(message&& typeof message!=="string"){options=message;message=undefined}message??(message=httpErrorCodes[code]);super(message);this.cause=options.cause;const name=httpErrorCodes[code].replace(createErrorRegexp,"");this.name=name.substr(-5)!=="Error"?name+"Error":name;this.status=this.statusCode=code;this.expose=options.expose??code<500}}exports.HttpError=HttpError;const createError=(code,message,properties={})=>{return new HttpError(code,message,properties)};exports.createError=createError;const httpErrorCodes={100:"Continue",101:"Switching Protocols",102:"Processing",103:"Early Hints",200:"OK",201:"Created",202:"Accepted",203:"Non-Authoritative Information",204:"No Content",205:"Reset Content",206:"Partial Content",207:"Multi-Status",208:"Already Reported",226:"IM Used",300:"Multiple Choices",301:"Moved Permanently",302:"Found",303:"See Other",304:"Not Modified",305:"Use Proxy",306:"(Unused)",307:"Temporary Redirect",308:"Permanent Redirect",400:"Bad Request",401:"Unauthorized",402:"Payment Required",403:"Forbidden",404:"Not Found",405:"Method Not Allowed",406:"Not Acceptable",407:"Proxy Authentication Required",408:"Request Timeout",409:"Conflict",410:"Gone",411:"Length Required",412:"Precondition Failed",413:"Payload Too Large",414:"URI Too Long",415:"Unsupported Media Type",416:"Range Not Satisfiable",417:"Expectation Failed",418:"I'm a teapot",421:"Misdirected Request",422:"Unprocessable Entity",423:"Locked",424:"Failed Dependency",425:"Unordered Collection",426:"Upgrade Required",428:"Precondition Required",429:"Too Many Requests",431:"Request Header Fields Too Large",451:"Unavailable For Legal Reasons",500:"Internal Server Error",501:"Not Implemented",502:"Bad Gateway",503:"Service Unavailable",504:"Gateway Timeout",505:"HTTP Version Not Supported",506:"Variant Also Negotiates",507:"Insufficient Storage",508:"Loop Detected",509:"Bandwidth Limit Exceeded",510:"Not Extended",511:"Network Authentication Required"}
|
|
2
|
+
|
|
3
|
+
//# sourceMappingURL=index.cjs.map
|
package/index.js
CHANGED
|
@@ -1,235 +1,3 @@
|
|
|
1
|
-
import { Agent }
|
|
2
|
-
import statuses from './codes.js';
|
|
3
|
-
import { inherits } from 'util';
|
|
4
|
-
export const awsClientDefaultOptions = {
|
|
5
|
-
httpOptions: {
|
|
6
|
-
agent: new Agent({
|
|
7
|
-
secureProtocol: 'TLSv1_2_method'
|
|
8
|
-
})
|
|
9
|
-
}
|
|
10
|
-
};
|
|
11
|
-
export const createPrefetchClient = options => {
|
|
12
|
-
const awsClientOptions = { ...awsClientDefaultOptions,
|
|
13
|
-
...options.awsClientOptions
|
|
14
|
-
};
|
|
15
|
-
const client = new options.AwsClient(awsClientOptions);
|
|
1
|
+
var _response;import{Agent}from"https";export const awsClientDefaultOptions={httpOptions:{agent:new Agent({secureProtocol:"TLSv1_2_method"})}};export const createPrefetchClient=options=>{const awsClientOptions={...awsClientDefaultOptions,...options.awsClientOptions};const client=new options.AwsClient(awsClientOptions);if(options.awsClientCapture&&options.disablePrefetch){return options.awsClientCapture(client)}else if(options.awsClientCapture){console.warn("Unable to apply X-Ray outside of handler invocation scope.")}return client};export const createClient=async(options,request)=>{let awsClientCredentials={};if(options.awsClientAssumeRole){if(!request){throw new Error("Request required when assuming role")}awsClientCredentials=await getInternal({credentials:options.awsClientAssumeRole},request)}awsClientCredentials={...awsClientCredentials,...options.awsClientOptions};return createPrefetchClient({...options,awsClientOptions:awsClientCredentials})};export const canPrefetch=(options={})=>{return!options.awsClientAssumeRole&&!options.disablePrefetch};export const getInternal=async(variables,request)=>{if(!variables||!request)return{};let keys=[];let values=[];if(variables===true){keys=values=Object.keys(request.internal)}else if(typeof variables==="string"){keys=values=[variables]}else if(Array.isArray(variables)){keys=values=variables}else if(typeof variables==="object"){keys=Object.keys(variables);values=Object.values(variables)}const promises=[];for(const internalKey of values){const pathOptionKey=internalKey.split(".");const rootOptionKey=pathOptionKey.shift();let valuePromise=request.internal[rootOptionKey];if(!isPromise(valuePromise)){valuePromise=Promise.resolve(valuePromise)}promises.push(valuePromise.then(value=>pathOptionKey.reduce((p,c)=>p?.[c],value)))}values=await Promise.allSettled(promises);const errors=values.filter(res=>res.status==="rejected").map(res=>res.reason);if(errors.length){const error=new Error("Failed to resolve internal values");error.cause=errors;throw error}return keys.reduce((obj,key,index)=>({...obj,[sanitizeKey(key)]:values[index].value}),{})};const isPromise=promise=>typeof promise?.then==="function";const sanitizeKeyPrefixLeadingNumber=/^([0-9])/;const sanitizeKeyRemoveDisallowedChar=/[^a-zA-Z0-9]+/g;export const sanitizeKey=key=>{return key.replace(sanitizeKeyPrefixLeadingNumber,"_$1").replace(sanitizeKeyRemoveDisallowedChar,"_")};const cache={};export const processCache=(options,fetch=()=>undefined,request)=>{const{cacheExpiry,cacheKey}=options;if(cacheExpiry){const cached=getCache(cacheKey);const unexpired=cached.expiry&&(cacheExpiry<0||cached.expiry>Date.now());if(unexpired&&cached.modified){const value=fetch(request,cached.value);cache[cacheKey]={value:{...cached.value,...value},expiry:cached.expiry};return cache[cacheKey]}if(unexpired){return{...cached,cache:true}}}const value=fetch(request);const expiry=Date.now()+cacheExpiry;if(cacheExpiry){cache[cacheKey]={value,expiry}}return{value,expiry}};export const getCache=key=>{if(!cache[key])return{};return cache[key]};export const modifyCache=(cacheKey,value)=>{if(!cache[cacheKey])return;cache[cacheKey]={...cache[cacheKey],value,modified:true}};export const clearCache=(keys=null)=>{keys=keys??Object.keys(cache);if(!Array.isArray(keys))keys=[keys];for(const cacheKey of keys){cache[cacheKey]=undefined}};export const jsonSafeParse=(text,reviver)=>{if(typeof text!=="string")return text;const firstChar=text[0];if(firstChar!=="{"&&firstChar!=="["&&firstChar!=='"')return text;try{return JSON.parse(text,reviver)}catch(e){}return text};export const jsonSafeStringify=(value,replacer,space)=>{try{return JSON.stringify(value,replacer,space)}catch(e){}return value};export const normalizeHttpResponse=request=>{let{response}=request;if(response===undefined){response={}}else if(response?.statusCode===undefined&&response?.body===undefined&&response?.headers===undefined){response={body:response}}(_response=response).headers??(_response.headers={});request.response=response;return response};const createErrorRegexp=/[^a-zA-Z]/g;export class HttpError extends Error{constructor(code,message,options={}){if(message&& typeof message!=="string"){options=message;message=undefined}message??(message=httpErrorCodes[code]);super(message);this.cause=options.cause;const name=httpErrorCodes[code].replace(createErrorRegexp,"");this.name=name.substr(-5)!=="Error"?name+"Error":name;this.status=this.statusCode=code;this.expose=options.expose??code<500}}export const createError=(code,message,properties={})=>{return new HttpError(code,message,properties)};const httpErrorCodes={100:"Continue",101:"Switching Protocols",102:"Processing",103:"Early Hints",200:"OK",201:"Created",202:"Accepted",203:"Non-Authoritative Information",204:"No Content",205:"Reset Content",206:"Partial Content",207:"Multi-Status",208:"Already Reported",226:"IM Used",300:"Multiple Choices",301:"Moved Permanently",302:"Found",303:"See Other",304:"Not Modified",305:"Use Proxy",306:"(Unused)",307:"Temporary Redirect",308:"Permanent Redirect",400:"Bad Request",401:"Unauthorized",402:"Payment Required",403:"Forbidden",404:"Not Found",405:"Method Not Allowed",406:"Not Acceptable",407:"Proxy Authentication Required",408:"Request Timeout",409:"Conflict",410:"Gone",411:"Length Required",412:"Precondition Failed",413:"Payload Too Large",414:"URI Too Long",415:"Unsupported Media Type",416:"Range Not Satisfiable",417:"Expectation Failed",418:"I'm a teapot",421:"Misdirected Request",422:"Unprocessable Entity",423:"Locked",424:"Failed Dependency",425:"Unordered Collection",426:"Upgrade Required",428:"Precondition Required",429:"Too Many Requests",431:"Request Header Fields Too Large",451:"Unavailable For Legal Reasons",500:"Internal Server Error",501:"Not Implemented",502:"Bad Gateway",503:"Service Unavailable",504:"Gateway Timeout",505:"HTTP Version Not Supported",506:"Variant Also Negotiates",507:"Insufficient Storage",508:"Loop Detected",509:"Bandwidth Limit Exceeded",510:"Not Extended",511:"Network Authentication Required"}
|
|
16
2
|
|
|
17
|
-
|
|
18
|
-
return options.awsClientCapture(client);
|
|
19
|
-
} else if (options.awsClientCapture) {
|
|
20
|
-
console.warn('Unable to apply X-Ray outside of handler invocation scope.');
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
return client;
|
|
24
|
-
};
|
|
25
|
-
export const createClient = async (options, request) => {
|
|
26
|
-
let awsClientCredentials = {};
|
|
27
|
-
|
|
28
|
-
if (options.awsClientAssumeRole) {
|
|
29
|
-
if (!request) throw new Error('Request required when assuming role');
|
|
30
|
-
awsClientCredentials = await getInternal({
|
|
31
|
-
credentials: options.awsClientAssumeRole
|
|
32
|
-
}, request);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
awsClientCredentials = { ...awsClientCredentials,
|
|
36
|
-
...options.awsClientOptions
|
|
37
|
-
};
|
|
38
|
-
return createPrefetchClient({ ...options,
|
|
39
|
-
awsClientOptions: awsClientCredentials
|
|
40
|
-
});
|
|
41
|
-
};
|
|
42
|
-
export const canPrefetch = (options = {}) => {
|
|
43
|
-
return !options.awsClientAssumeRole && !options.disablePrefetch;
|
|
44
|
-
};
|
|
45
|
-
export const getInternal = async (variables, request) => {
|
|
46
|
-
if (!variables || !request) return {};
|
|
47
|
-
let keys = [];
|
|
48
|
-
let values = [];
|
|
49
|
-
|
|
50
|
-
if (variables === true) {
|
|
51
|
-
keys = values = Object.keys(request.internal);
|
|
52
|
-
} else if (typeof variables === 'string') {
|
|
53
|
-
keys = values = [variables];
|
|
54
|
-
} else if (Array.isArray(variables)) {
|
|
55
|
-
keys = values = variables;
|
|
56
|
-
} else if (typeof variables === 'object') {
|
|
57
|
-
keys = Object.keys(variables);
|
|
58
|
-
values = Object.values(variables);
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
const promises = [];
|
|
62
|
-
|
|
63
|
-
for (const internalKey of values) {
|
|
64
|
-
const pathOptionKey = internalKey.split('.');
|
|
65
|
-
const rootOptionKey = pathOptionKey.shift();
|
|
66
|
-
let valuePromise = request.internal[rootOptionKey];
|
|
67
|
-
|
|
68
|
-
if (typeof valuePromise.then !== 'function') {
|
|
69
|
-
valuePromise = Promise.resolve(valuePromise);
|
|
70
|
-
}
|
|
71
|
-
|
|
72
|
-
promises.push(valuePromise.then(value => pathOptionKey.reduce((p, c) => p === null || p === void 0 ? void 0 : p[c], value)));
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
values = await Promise.allSettled(promises);
|
|
76
|
-
const errors = values.filter(res => res.status === 'rejected').map(res => res.reason);
|
|
77
|
-
|
|
78
|
-
if (errors.length) {
|
|
79
|
-
const error = new Error('Failed to resolve internal values');
|
|
80
|
-
error.cause = errors;
|
|
81
|
-
throw error;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
return keys.reduce((obj, key, index) => ({ ...obj,
|
|
85
|
-
[sanitizeKey(key)]: values[index].value
|
|
86
|
-
}), {});
|
|
87
|
-
};
|
|
88
|
-
const sanitizeKeyPrefixLeadingNumber = /^([0-9])/;
|
|
89
|
-
const sanitizeKeyRemoveDisallowedChar = /[^a-zA-Z0-9]+/g;
|
|
90
|
-
export const sanitizeKey = key => {
|
|
91
|
-
return key.replace(sanitizeKeyPrefixLeadingNumber, '_$1').replace(sanitizeKeyRemoveDisallowedChar, '_');
|
|
92
|
-
};
|
|
93
|
-
const cache = {};
|
|
94
|
-
export const processCache = (options, fetch = () => undefined, request) => {
|
|
95
|
-
const {
|
|
96
|
-
cacheExpiry,
|
|
97
|
-
cacheKey
|
|
98
|
-
} = options;
|
|
99
|
-
|
|
100
|
-
if (cacheExpiry) {
|
|
101
|
-
const cached = getCache(cacheKey);
|
|
102
|
-
const unexpired = cached.expiry && (cacheExpiry < 0 || cached.expiry > Date.now());
|
|
103
|
-
|
|
104
|
-
if (unexpired && cached.modified) {
|
|
105
|
-
const value = fetch(request, cached.value);
|
|
106
|
-
cache[cacheKey] = {
|
|
107
|
-
value: { ...cached.value,
|
|
108
|
-
...value
|
|
109
|
-
},
|
|
110
|
-
expiry: cached.expiry
|
|
111
|
-
};
|
|
112
|
-
return cache[cacheKey];
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
if (unexpired) {
|
|
116
|
-
return { ...cached,
|
|
117
|
-
cache: true
|
|
118
|
-
};
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
const value = fetch(request);
|
|
123
|
-
const expiry = Date.now() + cacheExpiry;
|
|
124
|
-
|
|
125
|
-
if (cacheExpiry) {
|
|
126
|
-
cache[cacheKey] = {
|
|
127
|
-
value,
|
|
128
|
-
expiry
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
return {
|
|
133
|
-
value,
|
|
134
|
-
expiry
|
|
135
|
-
};
|
|
136
|
-
};
|
|
137
|
-
export const getCache = key => {
|
|
138
|
-
if (!cache[key]) return {};
|
|
139
|
-
return cache[key];
|
|
140
|
-
};
|
|
141
|
-
export const modifyCache = (cacheKey, value) => {
|
|
142
|
-
if (!cache[cacheKey]) return;
|
|
143
|
-
cache[cacheKey] = { ...cache[cacheKey],
|
|
144
|
-
value,
|
|
145
|
-
modified: true
|
|
146
|
-
};
|
|
147
|
-
};
|
|
148
|
-
export const clearCache = (keys = null) => {
|
|
149
|
-
keys = keys ?? Object.keys(cache);
|
|
150
|
-
if (!Array.isArray(keys)) keys = [keys];
|
|
151
|
-
|
|
152
|
-
for (const cacheKey of keys) {
|
|
153
|
-
cache[cacheKey] = undefined;
|
|
154
|
-
}
|
|
155
|
-
};
|
|
156
|
-
export const jsonSafeParse = (string, reviver) => {
|
|
157
|
-
if (typeof string !== 'string') return string;
|
|
158
|
-
const firstChar = string[0];
|
|
159
|
-
if (firstChar !== '{' && firstChar !== '[' && firstChar !== '"') return string;
|
|
160
|
-
|
|
161
|
-
try {
|
|
162
|
-
return JSON.parse(string, reviver);
|
|
163
|
-
} catch (e) {}
|
|
164
|
-
|
|
165
|
-
return string;
|
|
166
|
-
};
|
|
167
|
-
export const normalizeHttpResponse = request => {
|
|
168
|
-
var _response, _response2, _response3, _response4;
|
|
169
|
-
|
|
170
|
-
let {
|
|
171
|
-
response
|
|
172
|
-
} = request;
|
|
173
|
-
|
|
174
|
-
if (response === undefined) {
|
|
175
|
-
response = {};
|
|
176
|
-
} else if (((_response = response) === null || _response === void 0 ? void 0 : _response.statusCode) === undefined && ((_response2 = response) === null || _response2 === void 0 ? void 0 : _response2.body) === undefined && ((_response3 = response) === null || _response3 === void 0 ? void 0 : _response3.headers) === undefined) {
|
|
177
|
-
response = {
|
|
178
|
-
body: response
|
|
179
|
-
};
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
(_response4 = response).headers ?? (_response4.headers = {});
|
|
183
|
-
request.response = response;
|
|
184
|
-
return response;
|
|
185
|
-
};
|
|
186
|
-
const createErrorRegexp = /[^a-zA-Z]/g;
|
|
187
|
-
export const createError = (code, message, properties = {}) => {
|
|
188
|
-
const name = statuses[code].replace(createErrorRegexp, '');
|
|
189
|
-
const className = name.substr(-5) !== 'Error' ? name + 'Error' : name;
|
|
190
|
-
|
|
191
|
-
function HttpError(message) {
|
|
192
|
-
const msg = message ?? statuses[code];
|
|
193
|
-
const err = new Error(msg);
|
|
194
|
-
Error.captureStackTrace(err, HttpError);
|
|
195
|
-
Object.setPrototypeOf(err, HttpError.prototype);
|
|
196
|
-
Object.defineProperty(err, 'message', {
|
|
197
|
-
enumerable: true,
|
|
198
|
-
configurable: true,
|
|
199
|
-
value: msg,
|
|
200
|
-
writable: true
|
|
201
|
-
});
|
|
202
|
-
Object.defineProperty(err, 'name', {
|
|
203
|
-
enumerable: false,
|
|
204
|
-
configurable: true,
|
|
205
|
-
value: className,
|
|
206
|
-
writable: true
|
|
207
|
-
});
|
|
208
|
-
return err;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
inherits(HttpError, Error);
|
|
212
|
-
const desc = Object.getOwnPropertyDescriptor(HttpError, 'name');
|
|
213
|
-
desc.value = className;
|
|
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);
|
|
221
|
-
};
|
|
222
|
-
export default {
|
|
223
|
-
createPrefetchClient,
|
|
224
|
-
createClient,
|
|
225
|
-
canPrefetch,
|
|
226
|
-
getInternal,
|
|
227
|
-
sanitizeKey,
|
|
228
|
-
processCache,
|
|
229
|
-
getCache,
|
|
230
|
-
modifyCache,
|
|
231
|
-
clearCache,
|
|
232
|
-
jsonSafeParse,
|
|
233
|
-
normalizeHttpResponse,
|
|
234
|
-
createError
|
|
235
|
-
};
|
|
3
|
+
//# sourceMappingURL=index.js.map
|
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.8",
|
|
4
4
|
"description": "🛵 The stylish Node.js middleware engine for AWS Lambda (util package)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"engines": {
|
|
@@ -10,10 +10,17 @@
|
|
|
10
10
|
"publishConfig": {
|
|
11
11
|
"access": "public"
|
|
12
12
|
},
|
|
13
|
-
"exports":
|
|
13
|
+
"exports": {
|
|
14
|
+
".": {
|
|
15
|
+
"import": "./index.js",
|
|
16
|
+
"require": "./index.cjs",
|
|
17
|
+
"types": "./index.d.ts"
|
|
18
|
+
}
|
|
19
|
+
},
|
|
14
20
|
"types": "index.d.ts",
|
|
15
21
|
"files": [
|
|
16
22
|
"index.js",
|
|
23
|
+
"index.cjs",
|
|
17
24
|
"index.d.ts",
|
|
18
25
|
"codes.js"
|
|
19
26
|
],
|
|
@@ -46,12 +53,12 @@
|
|
|
46
53
|
"url": "https://github.com/middyjs/middy/issues"
|
|
47
54
|
},
|
|
48
55
|
"devDependencies": {
|
|
49
|
-
"@middy/core": "^3.0.0-alpha.
|
|
56
|
+
"@middy/core": "^3.0.0-alpha.8",
|
|
50
57
|
"@types/aws-lambda": "^8.10.76",
|
|
51
58
|
"@types/node": "^17.0.0",
|
|
52
59
|
"aws-sdk": "^2.939.0",
|
|
53
60
|
"aws-xray-sdk": "^3.3.3"
|
|
54
61
|
},
|
|
55
|
-
"homepage": "https://
|
|
56
|
-
"gitHead": "
|
|
62
|
+
"homepage": "https://middy.js.org",
|
|
63
|
+
"gitHead": "c04b3a0c2f326906b34973878d6f823778a5ea99"
|
|
57
64
|
}
|
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
|
-
}
|