@lidofinance/next-api-wrapper 0.13.0 → 0.15.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 +18 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +18 -6
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/index.cjs
CHANGED
|
@@ -32,23 +32,35 @@ var $b042f7e98544cfc1$exports = {};
|
|
|
32
32
|
$parcel$export($b042f7e98544cfc1$exports, "DEFAULT_API_ERROR_MESSAGE", function () { return $b042f7e98544cfc1$export$c982278a0ddc4dbe; });
|
|
33
33
|
$parcel$export($b042f7e98544cfc1$exports, "CACHE_DEFAULT_HEADERS", function () { return $b042f7e98544cfc1$export$6bec5fc4117189a2; });
|
|
34
34
|
$parcel$export($b042f7e98544cfc1$exports, "CACHE_DEFAULT_ERROR_HEADERS", function () { return $b042f7e98544cfc1$export$a3368692186462a4; });
|
|
35
|
-
const $b042f7e98544cfc1$export$c982278a0ddc4dbe = "Something went wrong.
|
|
35
|
+
const $b042f7e98544cfc1$export$c982278a0ddc4dbe = "Something went wrong.";
|
|
36
36
|
const $b042f7e98544cfc1$export$6bec5fc4117189a2 = "public, max-age=180, stale-if-error=1200, stale-while-revalidate=60";
|
|
37
37
|
const $b042f7e98544cfc1$export$a3368692186462a4 = "no-store, must-revalidate";
|
|
38
38
|
|
|
39
39
|
|
|
40
|
+
|
|
41
|
+
const $ff2b7f149731334e$export$8cc545829c7461b0 = (error, defaultMessage)=>{
|
|
42
|
+
if (typeof error === "string") return error;
|
|
43
|
+
if (error instanceof Error) return error.message;
|
|
44
|
+
return defaultMessage !== null && defaultMessage !== void 0 ? defaultMessage : (0, $b042f7e98544cfc1$export$c982278a0ddc4dbe);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
|
|
40
48
|
const $d48d8f2703fa966d$export$74226d5f34b9550e = (args)=>{
|
|
41
49
|
return async (req, res, next)=>{
|
|
42
50
|
const { errorMessage: errorMessage = (0, $b042f7e98544cfc1$export$c982278a0ddc4dbe) , serverLogger: serverLogger } = args || {};
|
|
43
51
|
try {
|
|
44
52
|
await (next === null || next === void 0 ? void 0 : next(req, res, next));
|
|
45
53
|
} catch (error) {
|
|
54
|
+
const isInnerError = res.statusCode === 200;
|
|
55
|
+
const status = isInnerError ? 500 : res.statusCode || 500;
|
|
46
56
|
if (error instanceof Error) {
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
} else res.status(
|
|
57
|
+
serverLogger === null || serverLogger === void 0 ? void 0 : serverLogger.error((0, $ff2b7f149731334e$export$8cc545829c7461b0)(error, errorMessage));
|
|
58
|
+
res.status(status).json({
|
|
59
|
+
message: (0, $ff2b7f149731334e$export$8cc545829c7461b0)(error, errorMessage)
|
|
60
|
+
});
|
|
61
|
+
} else res.status(status).json({
|
|
62
|
+
message: errorMessage
|
|
63
|
+
});
|
|
52
64
|
}
|
|
53
65
|
};
|
|
54
66
|
};
|
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;;;;;;;;;;AGAO,MAAM,4CAA4B;AAElC,MAAM,4CAAwB;AAC9B,MAAM,4CAA8B;;ADH3C;
|
|
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;;;;;;;;;;AGAO,MAAM,4CAA4B;AAElC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BAA2B;YAC3B,MAAM,MAAK;QACb;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 { DEFAULT_API_ERROR_MESSAGE, CACHE_DEFAULT_ERROR_HEADERS, CACHE_DEFAULT_HEADERS } from './constants'\nimport { extractErrorMessage } from './utils'\nimport { RequestWrapper, DefaultErrorHandlerArgs, CacheControlArgs } from './types'\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 (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","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.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ export type CacheControlArgs = {
|
|
|
12
12
|
errorHeaders?: string;
|
|
13
13
|
};
|
|
14
14
|
export const wrapRequest: <T = void>(wrappers: RequestWrapper<T>[]) => (requestHandler: API<T>) => RequestWrapper<T>;
|
|
15
|
-
export const DEFAULT_API_ERROR_MESSAGE = "Something went wrong.
|
|
15
|
+
export const DEFAULT_API_ERROR_MESSAGE = "Something went wrong.";
|
|
16
16
|
export const CACHE_DEFAULT_HEADERS = "public, max-age=180, stale-if-error=1200, stale-while-revalidate=60";
|
|
17
17
|
export const CACHE_DEFAULT_ERROR_HEADERS = "no-store, must-revalidate";
|
|
18
18
|
export const defaultErrorHandler: (args?: DefaultErrorHandlerArgs) => RequestWrapper;
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":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AC,EAAE,YAAY,CAAA;CAC5B,CAAA;AAED,+BAA+B;IAC7B,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,YAAY,CAAC,EAAE,MAAM,CAAA;CACtB,CAAA;ACnBD,OAAO,MAAM,uGAGsF,CAAA;ACLnG,OAAO,MAAM,mDAAmD,CAAA;AAEhE,OAAO,MAAM,6FAA6F,CAAA;AAC1G,OAAO,MAAM,yDAAyD,CAAA;AEEtE,OAAO,MAAM,6BACH,uBAAuB,KAAG,cAgBjC,CAAA;AAEH,OAAO,MAAM,sBACH,gBAAgB,KAAG,cAe1B,CAAA","sources":["packages/next/api-wrapper/src/src/types.ts","packages/next/api-wrapper/src/src/wrapRequest.ts","packages/next/api-wrapper/src/src/constants.ts","packages/next/api-wrapper/src/src/utils.ts","packages/next/api-wrapper/src/src/wrappers.ts","packages/next/api-wrapper/src/src/index.ts","packages/next/api-wrapper/src/index.ts"],"sourcesContent":[null,null,null,null,null,null,"export * from './wrapRequest'\nexport * from './wrappers'\nexport * from './constants'\nexport * from './types'\n"],"names":[],"version":3,"file":"index.d.ts.map"}
|
package/dist/index.mjs
CHANGED
|
@@ -16,23 +16,35 @@ var $b2dbcaa42773f73a$exports = {};
|
|
|
16
16
|
$parcel$export($b2dbcaa42773f73a$exports, "DEFAULT_API_ERROR_MESSAGE", function () { return $b2dbcaa42773f73a$export$c982278a0ddc4dbe; });
|
|
17
17
|
$parcel$export($b2dbcaa42773f73a$exports, "CACHE_DEFAULT_HEADERS", function () { return $b2dbcaa42773f73a$export$6bec5fc4117189a2; });
|
|
18
18
|
$parcel$export($b2dbcaa42773f73a$exports, "CACHE_DEFAULT_ERROR_HEADERS", function () { return $b2dbcaa42773f73a$export$a3368692186462a4; });
|
|
19
|
-
const $b2dbcaa42773f73a$export$c982278a0ddc4dbe = "Something went wrong.
|
|
19
|
+
const $b2dbcaa42773f73a$export$c982278a0ddc4dbe = "Something went wrong.";
|
|
20
20
|
const $b2dbcaa42773f73a$export$6bec5fc4117189a2 = "public, max-age=180, stale-if-error=1200, stale-while-revalidate=60";
|
|
21
21
|
const $b2dbcaa42773f73a$export$a3368692186462a4 = "no-store, must-revalidate";
|
|
22
22
|
|
|
23
23
|
|
|
24
|
+
|
|
25
|
+
const $e74a3752b07b80f6$export$8cc545829c7461b0 = (error, defaultMessage)=>{
|
|
26
|
+
if (typeof error === "string") return error;
|
|
27
|
+
if (error instanceof Error) return error.message;
|
|
28
|
+
return defaultMessage !== null && defaultMessage !== void 0 ? defaultMessage : (0, $b2dbcaa42773f73a$export$c982278a0ddc4dbe);
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
|
|
24
32
|
const $6ca930aaa4992525$export$74226d5f34b9550e = (args)=>{
|
|
25
33
|
return async (req, res, next)=>{
|
|
26
34
|
const { errorMessage: errorMessage = (0, $b2dbcaa42773f73a$export$c982278a0ddc4dbe) , serverLogger: serverLogger } = args || {};
|
|
27
35
|
try {
|
|
28
36
|
await (next === null || next === void 0 ? void 0 : next(req, res, next));
|
|
29
37
|
} catch (error) {
|
|
38
|
+
const isInnerError = res.statusCode === 200;
|
|
39
|
+
const status = isInnerError ? 500 : res.statusCode || 500;
|
|
30
40
|
if (error instanceof Error) {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
} else res.status(
|
|
41
|
+
serverLogger === null || serverLogger === void 0 ? void 0 : serverLogger.error((0, $e74a3752b07b80f6$export$8cc545829c7461b0)(error, errorMessage));
|
|
42
|
+
res.status(status).json({
|
|
43
|
+
message: (0, $e74a3752b07b80f6$export$8cc545829c7461b0)(error, errorMessage)
|
|
44
|
+
});
|
|
45
|
+
} else res.status(status).json({
|
|
46
|
+
message: errorMessage
|
|
47
|
+
});
|
|
36
48
|
}
|
|
37
49
|
};
|
|
38
50
|
};
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"mappings":";;;;;;ACEO,MAAM,4CACX,CAAW,WACX,CAAC,iBACC,SAAS,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BAAE,SAAS;YAAa;QAEpD;IACF;;AAEK,MAAM,4CACX,CAAC;IACD,OAAA,OAAO,KAAK,KAAK,OAAS;QACxB,MAAM,WAAE,UAAU,CAAA,GAAA,yCAAoB,kBAAG,eAAe,CAAA,GAAA,yCAA2B,AAAD,IAAG,GAAG,QAAQ,CAAC;QACjG,IAAI;YACF,IAAI,SAAS,CAAC,iBAAiB;YAE/B,OAAM,iBAAA,kBAAA,KAAA,IAAA,KAAO,KAAK,KAAK;QACzB,EAAE,OAAO,OAAO;YACd,0CAA0C;YAC1C,sDAAsD;YACtD,IAAI,SAAS,CAAC,iBAAiB;YAE/B,2BAA2B;YAC3B,MAAM,MAAK;QACb;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 { DEFAULT_API_ERROR_MESSAGE, CACHE_DEFAULT_ERROR_HEADERS, CACHE_DEFAULT_HEADERS } from './constants'\nimport { extractErrorMessage } from './utils'\nimport { RequestWrapper, DefaultErrorHandlerArgs, CacheControlArgs } from './types'\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 (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","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.15.0",
|
|
7
7
|
"files": [
|
|
8
8
|
"dist"
|
|
9
9
|
],
|
|
@@ -23,12 +23,12 @@
|
|
|
23
23
|
"test": "jest"
|
|
24
24
|
},
|
|
25
25
|
"peerDependencies": {
|
|
26
|
-
"@lidofinance/api-logger": "~0.
|
|
26
|
+
"@lidofinance/api-logger": "~0.15.0",
|
|
27
27
|
"next": "^12.2.0"
|
|
28
28
|
},
|
|
29
29
|
"devDependencies": {
|
|
30
|
-
"@lidofinance/api-logger": "~0.
|
|
31
|
-
"@lidofinance/config-prettier": "~0.
|
|
30
|
+
"@lidofinance/api-logger": "~0.15.0",
|
|
31
|
+
"@lidofinance/config-prettier": "~0.15.0",
|
|
32
32
|
"@types/jest": "^29.2.4",
|
|
33
33
|
"@types/node": "^18.11.17",
|
|
34
34
|
"jest": "^29.3.1",
|