@lidofinance/next-api-wrapper 0.23.0 → 0.25.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/dist/index.cjs +9 -15
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +9 -15
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/index.cjs
CHANGED
|
@@ -45,7 +45,7 @@ const $b042f7e98544cfc1$export$a3368692186462a4 = "no-store, must-revalidate";
|
|
|
45
45
|
const $ff2b7f149731334e$export$8cc545829c7461b0 = (error, defaultMessage)=>{
|
|
46
46
|
if (typeof error === "string") return error;
|
|
47
47
|
if (error instanceof Error) return error.message;
|
|
48
|
-
return defaultMessage
|
|
48
|
+
return defaultMessage ?? (0, $b042f7e98544cfc1$export$c982278a0ddc4dbe);
|
|
49
49
|
};
|
|
50
50
|
|
|
51
51
|
|
|
@@ -53,21 +53,20 @@ const $d48d8f2703fa966d$var$isErrorWithStatus = (error)=>{
|
|
|
53
53
|
if (typeof error !== "object" || error === null) return false;
|
|
54
54
|
return "status" in error;
|
|
55
55
|
};
|
|
56
|
-
const $d48d8f2703fa966d$export$74226d5f34b9550e = (args)=>{
|
|
57
|
-
return async (req, res, next)=>{
|
|
56
|
+
const $d48d8f2703fa966d$export$74226d5f34b9550e = (args)=>async (req, res, next)=>{
|
|
58
57
|
const { errorMessage: errorMessage = (0, $b042f7e98544cfc1$export$c982278a0ddc4dbe) , serverLogger: serverLogger } = args || {};
|
|
59
58
|
try {
|
|
60
|
-
await
|
|
59
|
+
await next?.(req, res, next);
|
|
61
60
|
} catch (error) {
|
|
62
61
|
const isInnerError = res.statusCode === 200;
|
|
63
62
|
const status = isInnerError ? 500 : res.statusCode || 500;
|
|
64
63
|
if ($d48d8f2703fa966d$var$isErrorWithStatus(error)) {
|
|
65
|
-
serverLogger
|
|
64
|
+
serverLogger?.error((0, $ff2b7f149731334e$export$8cc545829c7461b0)(error, errorMessage));
|
|
66
65
|
res.status(error.status).json({
|
|
67
66
|
message: (0, $ff2b7f149731334e$export$8cc545829c7461b0)(error, errorMessage)
|
|
68
67
|
});
|
|
69
68
|
} else if (error instanceof Error) {
|
|
70
|
-
serverLogger
|
|
69
|
+
serverLogger?.error((0, $ff2b7f149731334e$export$8cc545829c7461b0)(error, errorMessage));
|
|
71
70
|
res.status(status).json({
|
|
72
71
|
message: (0, $ff2b7f149731334e$export$8cc545829c7461b0)(error, errorMessage)
|
|
73
72
|
});
|
|
@@ -76,13 +75,11 @@ const $d48d8f2703fa966d$export$74226d5f34b9550e = (args)=>{
|
|
|
76
75
|
});
|
|
77
76
|
}
|
|
78
77
|
};
|
|
79
|
-
|
|
80
|
-
const $d48d8f2703fa966d$export$935bfe081ea97cf4 = (args)=>{
|
|
81
|
-
return async (req, res, next)=>{
|
|
78
|
+
const $d48d8f2703fa966d$export$935bfe081ea97cf4 = (args)=>async (req, res, next)=>{
|
|
82
79
|
const { headers: headers = (0, $b042f7e98544cfc1$export$6bec5fc4117189a2) , errorHeaders: errorHeaders = (0, $b042f7e98544cfc1$export$a3368692186462a4) } = args || {};
|
|
83
80
|
try {
|
|
84
81
|
res.setHeader("Cache-Control", headers);
|
|
85
|
-
await
|
|
82
|
+
await next?.(req, res, next);
|
|
86
83
|
} catch (error) {
|
|
87
84
|
// for requests with cache-control headers
|
|
88
85
|
// need set new headers otherwise error will be cached
|
|
@@ -91,15 +88,13 @@ const $d48d8f2703fa966d$export$935bfe081ea97cf4 = (args)=>{
|
|
|
91
88
|
throw error;
|
|
92
89
|
}
|
|
93
90
|
};
|
|
94
|
-
|
|
95
|
-
const $d48d8f2703fa966d$export$156cbc8f54439240 = (metrics, route)=>{
|
|
96
|
-
return async (req, res, next)=>{
|
|
91
|
+
const $d48d8f2703fa966d$export$156cbc8f54439240 = (metrics, route)=>async (req, res, next)=>{
|
|
97
92
|
let status = "2xx";
|
|
98
93
|
const endMetric = metrics.startTimer({
|
|
99
94
|
route: route
|
|
100
95
|
});
|
|
101
96
|
try {
|
|
102
|
-
await
|
|
97
|
+
await next?.(req, res, next);
|
|
103
98
|
status = (0, $93IVf$lidofinanceapimetrics.getStatusLabel)(res.statusCode);
|
|
104
99
|
} catch (error) {
|
|
105
100
|
status = (0, $93IVf$lidofinanceapimetrics.getStatusLabel)(res.statusCode);
|
|
@@ -111,7 +106,6 @@ const $d48d8f2703fa966d$export$156cbc8f54439240 = (metrics, route)=>{
|
|
|
111
106
|
});
|
|
112
107
|
}
|
|
113
108
|
};
|
|
114
|
-
};
|
|
115
109
|
|
|
116
110
|
|
|
117
111
|
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;;;;;;;;;;;;;;;;;ACEO,MAAM,4CACX,CAAW,WACX,CAAC,iBACC,SAAS,MAAM,CAAC,CAAC,KAAK,MAAQ,CAAC,KAAK,MAAQ,IAAI,KAAK,KAAK,IAAM,IAAI,KAAK,OAAO;;ADLpF;;;;;;AEAA;;;;;;ACAO,MAAM,4CAA4B;AAElC,MAAM,4CAAwB;AAC9B,MAAM,4CAA8B;;;ACH3C;AAEO,MAAM,4CAAsB,CAAC,OAAgB,iBAAoC;IACtF,IAAI,OAAO,UAAU,UAAU,OAAO;IACtC,IAAI,iBAAiB,OAAO,OAAO,MAAM,OAAO;IAChD,OAAO,
|
|
1
|
+
{"mappings":";;;;;;;;;;;;;;;;;;;;;;;;ACEO,MAAM,4CACX,CAAW,WACX,CAAC,iBACC,SAAS,MAAM,CAAC,CAAC,KAAK,MAAQ,CAAC,KAAK,MAAQ,IAAI,KAAK,KAAK,IAAM,IAAI,KAAK,OAAO;;ADLpF;;;;;;AEAA;;;;;;ACAO,MAAM,4CAA4B;AAElC,MAAM,4CAAwB;AAC9B,MAAM,4CAA8B;;;ACH3C;AAEO,MAAM,4CAAsB,CAAC,OAAgB,iBAAoC;IACtF,IAAI,OAAO,UAAU,UAAU,OAAO;IACtC,IAAI,iBAAiB,OAAO,OAAO,MAAM,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tD,IAAI,SAAS,CAAC,iBAAiB;YAE/B,2BAA2B;YAC3B,MAAM,MAAK;QACb;IACF;AAEK,MAAM,4CACX,CAAC,SAA4B,QAC7B,OAAO,KAAK,KAAK,OAAS;QACxB,IAAI,SAAS;QACb,MAAM,YAAY,QAAQ,UAAU,CAAC;mBAAE;QAAM;QAE7C,IAAI;YACF,MAAM,OAAO,KAAK,KAAK;YACvB,SAAS,CAAA,GAAA,2CAAc,AAAD,EAAE,IAAI,UAAU;QACxC,EAAE,OAAO,OAAO;YACd,SAAS,CAAA,GAAA,2CAAc,AAAD,EAAE,IAAI,UAAU;YACtC,2BAA2B;YAC3B,MAAM,MAAK;QACb,SAAU;YACR,UAAU;wBAAE;YAAO;QACrB;IACF;;;;;;","sources":["packages/next/api-wrapper/src/index.ts","packages/next/api-wrapper/src/wrapRequest.ts","packages/next/api-wrapper/src/wrappers.ts","packages/next/api-wrapper/src/constants.ts","packages/next/api-wrapper/src/utils.ts","packages/next/api-wrapper/src/types.ts"],"sourcesContent":["export * from './wrapRequest'\nexport * from './wrappers'\nexport * from './constants'\nexport * from './types'\n","import { RequestWrapper, API } from './types'\n\nexport const wrapRequest =\n <T = void>(wrappers: RequestWrapper<T>[]) =>\n (requestHandler: API<T>) =>\n wrappers.reduce((acc, cur) => (req, res) => cur(req, res, () => acc(req, res)), requestHandler)\n","import { Histogram } from 'prom-client'\nimport { getStatusLabel } from '@lidofinance/api-metrics'\n\nimport { DEFAULT_API_ERROR_MESSAGE, CACHE_DEFAULT_ERROR_HEADERS, CACHE_DEFAULT_HEADERS } from './constants'\nimport { extractErrorMessage } from './utils'\nimport { RequestWrapper, DefaultErrorHandlerArgs, CacheControlArgs } from './types'\n\ntype ErrorWithStatus = Error & { status: number }\n\nconst isErrorWithStatus = (error: unknown): error is ErrorWithStatus => {\n if (typeof error !== 'object' || error === null) return false\n return 'status' in error\n}\n\n// must be last in the wrapper stack\nexport const defaultErrorHandler =\n (args?: DefaultErrorHandlerArgs): RequestWrapper =>\n async (req, res, next) => {\n const { errorMessage = DEFAULT_API_ERROR_MESSAGE, serverLogger } = args || {}\n try {\n await next?.(req, res, next)\n } catch (error) {\n const isInnerError = res.statusCode === 200\n const status = isInnerError ? 500 : res.statusCode || 500\n\n if (isErrorWithStatus(error)) {\n serverLogger?.error(extractErrorMessage(error, errorMessage))\n res.status(error.status).json({ message: extractErrorMessage(error, errorMessage) })\n } else if (error instanceof Error) {\n serverLogger?.error(extractErrorMessage(error, errorMessage))\n res.status(status).json({ message: extractErrorMessage(error, errorMessage) })\n } else {\n res.status(status).json({ message: errorMessage })\n }\n }\n }\n\nexport const cacheControl =\n (args?: CacheControlArgs): RequestWrapper =>\n async (req, res, next) => {\n const { headers = CACHE_DEFAULT_HEADERS, errorHeaders = CACHE_DEFAULT_ERROR_HEADERS } = args || {}\n try {\n res.setHeader('Cache-Control', headers)\n\n await next?.(req, res, next)\n } catch (error) {\n // for requests with cache-control headers\n // need set new headers otherwise error will be cached\n res.setHeader('Cache-Control', errorHeaders)\n\n // throw error up the stack\n throw error\n }\n }\n\nexport const responseTimeMetric =\n (metrics: Histogram<string>, route: string): RequestWrapper =>\n async (req, res, next) => {\n let status = '2xx'\n const endMetric = metrics.startTimer({ route })\n\n try {\n await next?.(req, res, next)\n status = getStatusLabel(res.statusCode)\n } catch (error) {\n status = getStatusLabel(res.statusCode)\n // throw error up the stack\n throw error\n } finally {\n endMetric({ status })\n }\n }\n","export const DEFAULT_API_ERROR_MESSAGE = 'Something went wrong.'\n\nexport const CACHE_DEFAULT_HEADERS = 'public, max-age=180, stale-if-error=1200, stale-while-revalidate=60'\nexport const CACHE_DEFAULT_ERROR_HEADERS = 'no-store, must-revalidate'\n","import { DEFAULT_API_ERROR_MESSAGE } from './constants'\n\nexport const extractErrorMessage = (error: unknown, defaultMessage?: string): string => {\n if (typeof error === 'string') return error\n if (error instanceof Error) return error.message\n return defaultMessage ?? DEFAULT_API_ERROR_MESSAGE\n}\n","import { NextApiResponse, NextApiRequest } from 'next'\nimport { ServerLogger } from '@lidofinance/api-logger'\n\nexport type API<T = void> = (req: NextApiRequest, res: NextApiResponse) => Promise<T> | T\n\nexport type RequestWrapper<T = void> = (\n req: NextApiRequest,\n res: NextApiResponse,\n next?: API<T> | RequestWrapper<T>,\n) => Promise<T> | T\n\nexport type MixedWrapper = <T = void>(api: API<T>) => RequestWrapper<T>\n\nexport type DefaultErrorHandlerArgs = {\n errorMessage?: string\n serverLogger?: ServerLogger\n}\n\nexport type CacheControlArgs = {\n headers?: string\n errorHeaders?: string\n}\n"],"names":[],"version":3,"file":"index.cjs.map"}
|
package/dist/index.mjs
CHANGED
|
@@ -29,7 +29,7 @@ const $b2dbcaa42773f73a$export$a3368692186462a4 = "no-store, must-revalidate";
|
|
|
29
29
|
const $e74a3752b07b80f6$export$8cc545829c7461b0 = (error, defaultMessage)=>{
|
|
30
30
|
if (typeof error === "string") return error;
|
|
31
31
|
if (error instanceof Error) return error.message;
|
|
32
|
-
return defaultMessage
|
|
32
|
+
return defaultMessage ?? (0, $b2dbcaa42773f73a$export$c982278a0ddc4dbe);
|
|
33
33
|
};
|
|
34
34
|
|
|
35
35
|
|
|
@@ -37,21 +37,20 @@ const $6ca930aaa4992525$var$isErrorWithStatus = (error)=>{
|
|
|
37
37
|
if (typeof error !== "object" || error === null) return false;
|
|
38
38
|
return "status" in error;
|
|
39
39
|
};
|
|
40
|
-
const $6ca930aaa4992525$export$74226d5f34b9550e = (args)=>{
|
|
41
|
-
return async (req, res, next)=>{
|
|
40
|
+
const $6ca930aaa4992525$export$74226d5f34b9550e = (args)=>async (req, res, next)=>{
|
|
42
41
|
const { errorMessage: errorMessage = (0, $b2dbcaa42773f73a$export$c982278a0ddc4dbe) , serverLogger: serverLogger } = args || {};
|
|
43
42
|
try {
|
|
44
|
-
await
|
|
43
|
+
await next?.(req, res, next);
|
|
45
44
|
} catch (error) {
|
|
46
45
|
const isInnerError = res.statusCode === 200;
|
|
47
46
|
const status = isInnerError ? 500 : res.statusCode || 500;
|
|
48
47
|
if ($6ca930aaa4992525$var$isErrorWithStatus(error)) {
|
|
49
|
-
serverLogger
|
|
48
|
+
serverLogger?.error((0, $e74a3752b07b80f6$export$8cc545829c7461b0)(error, errorMessage));
|
|
50
49
|
res.status(error.status).json({
|
|
51
50
|
message: (0, $e74a3752b07b80f6$export$8cc545829c7461b0)(error, errorMessage)
|
|
52
51
|
});
|
|
53
52
|
} else if (error instanceof Error) {
|
|
54
|
-
serverLogger
|
|
53
|
+
serverLogger?.error((0, $e74a3752b07b80f6$export$8cc545829c7461b0)(error, errorMessage));
|
|
55
54
|
res.status(status).json({
|
|
56
55
|
message: (0, $e74a3752b07b80f6$export$8cc545829c7461b0)(error, errorMessage)
|
|
57
56
|
});
|
|
@@ -60,13 +59,11 @@ const $6ca930aaa4992525$export$74226d5f34b9550e = (args)=>{
|
|
|
60
59
|
});
|
|
61
60
|
}
|
|
62
61
|
};
|
|
63
|
-
|
|
64
|
-
const $6ca930aaa4992525$export$935bfe081ea97cf4 = (args)=>{
|
|
65
|
-
return async (req, res, next)=>{
|
|
62
|
+
const $6ca930aaa4992525$export$935bfe081ea97cf4 = (args)=>async (req, res, next)=>{
|
|
66
63
|
const { headers: headers = (0, $b2dbcaa42773f73a$export$6bec5fc4117189a2) , errorHeaders: errorHeaders = (0, $b2dbcaa42773f73a$export$a3368692186462a4) } = args || {};
|
|
67
64
|
try {
|
|
68
65
|
res.setHeader("Cache-Control", headers);
|
|
69
|
-
await
|
|
66
|
+
await next?.(req, res, next);
|
|
70
67
|
} catch (error) {
|
|
71
68
|
// for requests with cache-control headers
|
|
72
69
|
// need set new headers otherwise error will be cached
|
|
@@ -75,15 +72,13 @@ const $6ca930aaa4992525$export$935bfe081ea97cf4 = (args)=>{
|
|
|
75
72
|
throw error;
|
|
76
73
|
}
|
|
77
74
|
};
|
|
78
|
-
|
|
79
|
-
const $6ca930aaa4992525$export$156cbc8f54439240 = (metrics, route)=>{
|
|
80
|
-
return async (req, res, next)=>{
|
|
75
|
+
const $6ca930aaa4992525$export$156cbc8f54439240 = (metrics, route)=>async (req, res, next)=>{
|
|
81
76
|
let status = "2xx";
|
|
82
77
|
const endMetric = metrics.startTimer({
|
|
83
78
|
route: route
|
|
84
79
|
});
|
|
85
80
|
try {
|
|
86
|
-
await
|
|
81
|
+
await next?.(req, res, next);
|
|
87
82
|
status = (0, $dU25M$getStatusLabel)(res.statusCode);
|
|
88
83
|
} catch (error) {
|
|
89
84
|
status = (0, $dU25M$getStatusLabel)(res.statusCode);
|
|
@@ -95,7 +90,6 @@ const $6ca930aaa4992525$export$156cbc8f54439240 = (metrics, route)=>{
|
|
|
95
90
|
});
|
|
96
91
|
}
|
|
97
92
|
};
|
|
98
|
-
};
|
|
99
93
|
|
|
100
94
|
|
|
101
95
|
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;;;;ACEO,MAAM,4CACX,CAAW,WACX,CAAC,iBACC,SAAS,MAAM,CAAC,CAAC,KAAK,MAAQ,CAAC,KAAK,MAAQ,IAAI,KAAK,KAAK,IAAM,IAAI,KAAK,OAAO;;ADLpF;;;;;;AEAA;;;;;;ACAO,MAAM,4CAA4B;AAElC,MAAM,4CAAwB;AAC9B,MAAM,4CAA8B;;;ACH3C;AAEO,MAAM,4CAAsB,CAAC,OAAgB,iBAAoC;IACtF,IAAI,OAAO,UAAU,UAAU,OAAO;IACtC,IAAI,iBAAiB,OAAO,OAAO,MAAM,OAAO;IAChD,OAAO,
|
|
1
|
+
{"mappings":";;;;;;;;ACEO,MAAM,4CACX,CAAW,WACX,CAAC,iBACC,SAAS,MAAM,CAAC,CAAC,KAAK,MAAQ,CAAC,KAAK,MAAQ,IAAI,KAAK,KAAK,IAAM,IAAI,KAAK,OAAO;;ADLpF;;;;;;AEAA;;;;;;ACAO,MAAM,4CAA4B;AAElC,MAAM,4CAAwB;AAC9B,MAAM,4CAA8B;;;ACH3C;AAEO,MAAM,4CAAsB,CAAC,OAAgB,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/B,MAAM,OAAO,KAAK,KAAK;QACzB,EAAE,OAAO,OAAO;YACd,0CAA0C;YAC1C,sDAAsD;YACtD,IAAI,SAAS,CAAC,iBAAiB;YAE/B,2BAA2B;YAC3B,MAAM,MAAK;QACb;IACF;AAEK,MAAM,4CACX,CAAC,SAA4B,QAC7B,OAAO,KAAK,KAAK,OAAS;QACxB,IAAI,SAAS;QACb,MAAM,YAAY,QAAQ,UAAU,CAAC;mBAAE;QAAM;QAE7C,IAAI;YACF,MAAM,OAAO,KAAK,KAAK;YACvB,SAAS,CAAA,GAAA,qBAAc,AAAD,EAAE,IAAI,UAAU;QACxC,EAAE,OAAO,OAAO;YACd,SAAS,CAAA,GAAA,qBAAc,AAAD,EAAE,IAAI,UAAU;YACtC,2BAA2B;YAC3B,MAAM,MAAK;QACb,SAAU;YACR,UAAU;wBAAE;YAAO;QACrB;IACF;;;;;;","sources":["packages/next/api-wrapper/src/index.ts","packages/next/api-wrapper/src/wrapRequest.ts","packages/next/api-wrapper/src/wrappers.ts","packages/next/api-wrapper/src/constants.ts","packages/next/api-wrapper/src/utils.ts","packages/next/api-wrapper/src/types.ts"],"sourcesContent":["export * from './wrapRequest'\nexport * from './wrappers'\nexport * from './constants'\nexport * from './types'\n","import { RequestWrapper, API } from './types'\n\nexport const wrapRequest =\n <T = void>(wrappers: RequestWrapper<T>[]) =>\n (requestHandler: API<T>) =>\n wrappers.reduce((acc, cur) => (req, res) => cur(req, res, () => acc(req, res)), requestHandler)\n","import { Histogram } from 'prom-client'\nimport { getStatusLabel } from '@lidofinance/api-metrics'\n\nimport { DEFAULT_API_ERROR_MESSAGE, CACHE_DEFAULT_ERROR_HEADERS, CACHE_DEFAULT_HEADERS } from './constants'\nimport { extractErrorMessage } from './utils'\nimport { RequestWrapper, DefaultErrorHandlerArgs, CacheControlArgs } from './types'\n\ntype ErrorWithStatus = Error & { status: number }\n\nconst isErrorWithStatus = (error: unknown): error is ErrorWithStatus => {\n if (typeof error !== 'object' || error === null) return false\n return 'status' in error\n}\n\n// must be last in the wrapper stack\nexport const defaultErrorHandler =\n (args?: DefaultErrorHandlerArgs): RequestWrapper =>\n async (req, res, next) => {\n const { errorMessage = DEFAULT_API_ERROR_MESSAGE, serverLogger } = args || {}\n try {\n await next?.(req, res, next)\n } catch (error) {\n const isInnerError = res.statusCode === 200\n const status = isInnerError ? 500 : res.statusCode || 500\n\n if (isErrorWithStatus(error)) {\n serverLogger?.error(extractErrorMessage(error, errorMessage))\n res.status(error.status).json({ message: extractErrorMessage(error, errorMessage) })\n } else if (error instanceof Error) {\n serverLogger?.error(extractErrorMessage(error, errorMessage))\n res.status(status).json({ message: extractErrorMessage(error, errorMessage) })\n } else {\n res.status(status).json({ message: errorMessage })\n }\n }\n }\n\nexport const cacheControl =\n (args?: CacheControlArgs): RequestWrapper =>\n async (req, res, next) => {\n const { headers = CACHE_DEFAULT_HEADERS, errorHeaders = CACHE_DEFAULT_ERROR_HEADERS } = args || {}\n try {\n res.setHeader('Cache-Control', headers)\n\n await next?.(req, res, next)\n } catch (error) {\n // for requests with cache-control headers\n // need set new headers otherwise error will be cached\n res.setHeader('Cache-Control', errorHeaders)\n\n // throw error up the stack\n throw error\n }\n }\n\nexport const responseTimeMetric =\n (metrics: Histogram<string>, route: string): RequestWrapper =>\n async (req, res, next) => {\n let status = '2xx'\n const endMetric = metrics.startTimer({ route })\n\n try {\n await next?.(req, res, next)\n status = getStatusLabel(res.statusCode)\n } catch (error) {\n status = getStatusLabel(res.statusCode)\n // throw error up the stack\n throw error\n } finally {\n endMetric({ status })\n }\n }\n","export const DEFAULT_API_ERROR_MESSAGE = 'Something went wrong.'\n\nexport const CACHE_DEFAULT_HEADERS = 'public, max-age=180, stale-if-error=1200, stale-while-revalidate=60'\nexport const CACHE_DEFAULT_ERROR_HEADERS = 'no-store, must-revalidate'\n","import { DEFAULT_API_ERROR_MESSAGE } from './constants'\n\nexport const extractErrorMessage = (error: unknown, defaultMessage?: string): string => {\n if (typeof error === 'string') return error\n if (error instanceof Error) return error.message\n return defaultMessage ?? DEFAULT_API_ERROR_MESSAGE\n}\n","import { NextApiResponse, NextApiRequest } from 'next'\nimport { ServerLogger } from '@lidofinance/api-logger'\n\nexport type API<T = void> = (req: NextApiRequest, res: NextApiResponse) => Promise<T> | T\n\nexport type RequestWrapper<T = void> = (\n req: NextApiRequest,\n res: NextApiResponse,\n next?: API<T> | RequestWrapper<T>,\n) => Promise<T> | T\n\nexport type MixedWrapper = <T = void>(api: API<T>) => RequestWrapper<T>\n\nexport type DefaultErrorHandlerArgs = {\n errorMessage?: string\n serverLogger?: ServerLogger\n}\n\nexport type CacheControlArgs = {\n headers?: string\n errorHeaders?: string\n}\n"],"names":[],"version":3,"file":"index.mjs.map"}
|
package/package.json
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
"description": "Wrapper for next server api requests",
|
|
4
4
|
"repository": "git@github.com:lidofinance/warehouse.git",
|
|
5
5
|
"license": "MIT",
|
|
6
|
-
"version": "0.
|
|
6
|
+
"version": "0.25.0",
|
|
7
7
|
"files": [
|
|
8
8
|
"dist"
|
|
9
9
|
],
|
|
@@ -23,14 +23,14 @@
|
|
|
23
23
|
"test": "jest"
|
|
24
24
|
},
|
|
25
25
|
"peerDependencies": {
|
|
26
|
-
"@lidofinance/api-metrics": "~0.
|
|
26
|
+
"@lidofinance/api-metrics": "~0.25.0",
|
|
27
27
|
"next": "^12.2.0",
|
|
28
28
|
"prom-client": "^14.0.0"
|
|
29
29
|
},
|
|
30
30
|
"devDependencies": {
|
|
31
|
-
"@lidofinance/api-logger": "~0.
|
|
32
|
-
"@lidofinance/api-metrics": "~0.
|
|
33
|
-
"@lidofinance/config-prettier": "~0.
|
|
31
|
+
"@lidofinance/api-logger": "~0.25.0",
|
|
32
|
+
"@lidofinance/api-metrics": "~0.25.0",
|
|
33
|
+
"@lidofinance/config-prettier": "~0.25.0",
|
|
34
34
|
"@types/jest": "^29.2.4",
|
|
35
35
|
"@types/node": "^18.11.17",
|
|
36
36
|
"jest": "^29.3.1",
|