@lidofinance/next-api-wrapper 0.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/README.md ADDED
@@ -0,0 +1,100 @@
1
+ # @lidofinance/next-api-wrapper
2
+
3
+ Wrapper for next server api requests.
4
+
5
+ ## Installation
6
+
7
+ `yarn add @lidofinance/next-api-wrapper`.
8
+
9
+ ## Getting started
10
+
11
+ ### wrapRequest
12
+
13
+ ```ts
14
+ import { wrapRequest, API } from '@lidofinance/next-api-wrapper'
15
+ import { NextApiRequest, NextApiResponse } from 'next'
16
+
17
+ const someRequest: API = async (req, res) => await fetch()
18
+
19
+ export default wrapRequest([SomeWrapper])(someRequest)
20
+ ```
21
+
22
+ ## Examples
23
+
24
+ ### Default error handler
25
+
26
+ ```ts
27
+ import { wrapRequest, defaultErrorHandler } from '@lidofinance/next-api-wrapper'
28
+ import { NextApiRequest, NextApiResponse } from 'next'
29
+
30
+ export type API = (req: NextApiRequest, res: NextApiResponse) => Promise<void>
31
+
32
+ const CUSTOM_API_ERROR_MESSAGE = 'Default message'
33
+ const someRequest: API = async (req, res) => await fetch()
34
+
35
+ export default wrapRequest([defaultErrorHandler()])(someRequest)
36
+ // or
37
+ export default wrapRequest([defaultErrorHandler({ errorMessage: CUSTOM_API_ERROR_MESSAGE, serverLogger: SomeLogger })])(
38
+ someRequest,
39
+ )
40
+ ```
41
+
42
+ ### Cache control
43
+
44
+ ```ts
45
+ import { wrapRequest, cacheControl } from '@lidofinance/next-api-wrapper'
46
+ import { NextApiRequest, NextApiResponse } from 'next'
47
+
48
+ export type API = (req: NextApiRequest, res: NextApiResponse) => Promise<void>
49
+
50
+ const CUSTOM_CACHE_HEADERS = 'public, max-age=180'
51
+ const CUSTOM_ERROR_CACHE_HEADERS = 'no-store'
52
+ const someRequest: API = async (req, res) => await fetch()
53
+
54
+ // use default headers
55
+ export default wrapRequest([cacheControl()])(someRequest)
56
+ // or
57
+ export default wrapRequest([cacheControl({ headers: CUSTOM_CACHE_HEADERS, errorHeaders: CUSTOM_ERROR_CACHE_HEADERS })])(
58
+ someRequest,
59
+ )
60
+ ```
61
+
62
+ ### Mixing wrappers
63
+
64
+ ```ts
65
+ import { wrapRequest, cacheControl, defaultErrorHandler } from '@lidofinance/next-api-wrapper'
66
+ import { NextApiRequest, NextApiResponse } from 'next'
67
+
68
+ export type API = (req: NextApiRequest, res: NextApiResponse) => Promise<void>
69
+
70
+ const CUSTOM_API_ERROR_MESSAGE = 'Default message'
71
+ const CUSTOM_CACHE_HEADERS = 'public, max-age=180'
72
+ const CUSTOM_ERROR_CACHE_HEADERS = 'no-store'
73
+ const someRequest: API = async (req, res) => await fetch()
74
+
75
+ // defaultErrorHandler must be last in the wrapper stack
76
+ export default wrapRequest([cacheControl(), defaultErrorHandler()])(someRequest)
77
+ // or
78
+ export default wrapRequest([
79
+ cacheControl({ headers: CUSTOM_CACHE_HEADERS, errorHeaders: CUSTOM_ERROR_CACHE_HEADERS }),
80
+ defaultErrorHandler({ errorMessage: CUSTOM_API_ERROR_MESSAGE }),
81
+ ])(someRequest)
82
+ ```
83
+
84
+ ### Creation of ready mixed wrappers
85
+
86
+ ```ts
87
+ import { wrapRequest, cacheControl, defaultErrorHandler } from '@lidofinance/next-api-wrapper'
88
+ import { NextApiRequest, NextApiResponse } from 'next'
89
+
90
+ export type API = (req: NextApiRequest, res: NextApiResponse) => Promise<void>
91
+
92
+ const DEFAULT_API_ERROR_MESSAGE = 'Default message'
93
+ const CACHE_HEADERS = 'public, max-age=180'
94
+ // defaultErrorHandler must be last in the wrapper stack
95
+ export const defaultErrorAndCacheWrapper = wrapRequest([cacheControl(), defaultErrorHandler()])
96
+
97
+ const someRequest: API = async (req, res) => await fetch()
98
+
99
+ export default defaultErrorAndCacheWrapper(someRequest)
100
+ ```
package/dist/index.cjs ADDED
@@ -0,0 +1,82 @@
1
+ function $parcel$exportWildcard(dest, source) {
2
+ Object.keys(source).forEach(function(key) {
3
+ if (key === 'default' || key === '__esModule' || dest.hasOwnProperty(key)) {
4
+ return;
5
+ }
6
+
7
+ Object.defineProperty(dest, key, {
8
+ enumerable: true,
9
+ get: function get() {
10
+ return source[key];
11
+ }
12
+ });
13
+ });
14
+
15
+ return dest;
16
+ }
17
+ function $parcel$export(e, n, v, s) {
18
+ Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
19
+ }
20
+ var $b418837fe38f6af4$exports = {};
21
+
22
+ $parcel$export($b418837fe38f6af4$exports, "wrapRequest", function () { return $b418837fe38f6af4$export$b77e87d9092b0632; });
23
+ const $b418837fe38f6af4$export$b77e87d9092b0632 = (wrappers)=>(requestHandler)=>wrappers.reduce((acc, cur)=>(req, res)=>cur(req, res, ()=>acc(req, res)), requestHandler);
24
+
25
+
26
+ var $d48d8f2703fa966d$exports = {};
27
+
28
+ $parcel$export($d48d8f2703fa966d$exports, "defaultErrorHandler", function () { return $d48d8f2703fa966d$export$74226d5f34b9550e; });
29
+ $parcel$export($d48d8f2703fa966d$exports, "cacheControl", function () { return $d48d8f2703fa966d$export$935bfe081ea97cf4; });
30
+ var $b042f7e98544cfc1$exports = {};
31
+
32
+ $parcel$export($b042f7e98544cfc1$exports, "DEFAULT_API_ERROR_MESSAGE", function () { return $b042f7e98544cfc1$export$c982278a0ddc4dbe; });
33
+ $parcel$export($b042f7e98544cfc1$exports, "CACHE_DEFAULT_HEADERS", function () { return $b042f7e98544cfc1$export$6bec5fc4117189a2; });
34
+ $parcel$export($b042f7e98544cfc1$exports, "CACHE_DEFAULT_ERROR_HEADERS", function () { return $b042f7e98544cfc1$export$a3368692186462a4; });
35
+ const $b042f7e98544cfc1$export$c982278a0ddc4dbe = "Something went wrong. Sorry, try again later :(";
36
+ const $b042f7e98544cfc1$export$6bec5fc4117189a2 = "public, max-age=180, stale-if-error=1200, stale-while-revalidate=60";
37
+ const $b042f7e98544cfc1$export$a3368692186462a4 = "no-store, must-revalidate";
38
+
39
+
40
+ const $d48d8f2703fa966d$export$74226d5f34b9550e = (args)=>{
41
+ return async (req, res, next)=>{
42
+ const { errorMessage: errorMessage = (0, $b042f7e98544cfc1$export$c982278a0ddc4dbe) , serverLogger: serverLogger } = args || {};
43
+ try {
44
+ await (next === null || next === void 0 ? void 0 : next(req, res, next));
45
+ } catch (error) {
46
+ if (error instanceof Error) {
47
+ var _message;
48
+ serverLogger === null || serverLogger === void 0 ? void 0 : serverLogger.error((_message = error.message) !== null && _message !== void 0 ? _message : errorMessage);
49
+ var _message1;
50
+ res.status(500).json((_message1 = error.message) !== null && _message1 !== void 0 ? _message1 : errorMessage);
51
+ } else res.status(500).json(errorMessage);
52
+ }
53
+ };
54
+ };
55
+ const $d48d8f2703fa966d$export$935bfe081ea97cf4 = (args)=>{
56
+ return async (req, res, next)=>{
57
+ const { headers: headers = (0, $b042f7e98544cfc1$export$6bec5fc4117189a2) , errorHeaders: errorHeaders = (0, $b042f7e98544cfc1$export$a3368692186462a4) } = args || {};
58
+ try {
59
+ res.setHeader("Cache-Control", headers);
60
+ await (next === null || next === void 0 ? void 0 : next(req, res, next));
61
+ } catch (error) {
62
+ // for requests with cache-control headers
63
+ // need set new headers otherwise error will be cached
64
+ res.setHeader("Cache-Control", errorHeaders);
65
+ // throw error up the stack
66
+ throw error;
67
+ }
68
+ };
69
+ };
70
+
71
+
72
+
73
+ var $6025037db7f777fa$exports = {};
74
+
75
+
76
+ $parcel$exportWildcard(module.exports, $b418837fe38f6af4$exports);
77
+ $parcel$exportWildcard(module.exports, $d48d8f2703fa966d$exports);
78
+ $parcel$exportWildcard(module.exports, $b042f7e98544cfc1$exports);
79
+ $parcel$exportWildcard(module.exports, $6025037db7f777fa$exports);
80
+
81
+
82
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"mappings":";;;;;;;;;;;;;;;;;;;;;;ACEO,MAAM,yCAAW,GACtB,CAAW,QAA6B,GACxC,CAAC,cAAsB,GACrB,QAAQ,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,G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GAAG,CAAA,GAAA,yCAAqB,CAAA,iBAAE,YAAY,GAAG,CAAA,GAAA,yCAA2B,CAAA,GAAE,GAAG,IAAI,IAAI,EAAE;QAClG,IAAI;YACF,GAAG,CAAC,SAAS,CAAC,eAAe,EAAE,OAAO,CAAC;YAEvC,OAAM,IAAI,aAAJ,IAAI,WAAkB,GAAtB,KAAA,CAAsB,GAAtB,IAAI,CAAG,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,CAAA;SAC7B,CAAC,OAAO,KAAK,EAAE;YACd,0CAA0C;YAC1C,sDAAsD;YACtD,GAAG,CAAC,SAAS,CAAC,eAAe,EAAE,YAAY,CAAC;YAE5C,2BAA2B;YAC3B,MAAM,KAAK,CAAA;SACZ;KACF,CAAA;CAAA;;;;;;","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/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 { 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 if (error instanceof Error) {\n serverLogger?.error(error.message ?? errorMessage)\n res.status(500).json(error.message ?? errorMessage)\n } else {\n res.status(500).json(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. Sorry, try again later :('\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 { 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"}
@@ -0,0 +1,21 @@
1
+ import { NextApiResponse, NextApiRequest } from "next";
2
+ import { ServerLogger } from "@lidofinance/api-logger";
3
+ export type API<T = void> = (req: NextApiRequest, res: NextApiResponse) => Promise<T> | T;
4
+ export type RequestWrapper<T = void> = (req: NextApiRequest, res: NextApiResponse, next?: API<T> | RequestWrapper<T>) => Promise<T> | T;
5
+ export type MixedWrapper = <T = void>(api: API<T>) => RequestWrapper<T>;
6
+ export type DefaultErrorHandlerArgs = {
7
+ errorMessage?: string;
8
+ serverLogger?: ServerLogger;
9
+ };
10
+ export type CacheControlArgs = {
11
+ headers?: string;
12
+ errorHeaders?: string;
13
+ };
14
+ export const wrapRequest: <T = void>(wrappers: RequestWrapper<T>[]) => (requestHandler: API<T>) => RequestWrapper<T>;
15
+ export const DEFAULT_API_ERROR_MESSAGE = "Something went wrong. Sorry, try again later :(";
16
+ export const CACHE_DEFAULT_HEADERS = "public, max-age=180, stale-if-error=1200, stale-while-revalidate=60";
17
+ export const CACHE_DEFAULT_ERROR_HEADERS = "no-store, must-revalidate";
18
+ export const defaultErrorHandler: (args?: DefaultErrorHandlerArgs) => RequestWrapper;
19
+ export const cacheControl: (args?: CacheControlArgs) => RequestWrapper;
20
+
21
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"mappings":";;AAGA,gBAAgB,CAAC,GAAG,IAAI,IAAI,CAAC,GAAG,EAAE,cAAc,EAAE,GAAG,EAAE,eAAe,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;AAEzF,2BAA2B,CAAC,GAAG,IAAI,IAAI,CACrC,GAAG,EAAE,cAAc,EACnB,GAAG,EAAE,eAAe,EACpB,IAAI,CAAC,EAAE,IAAI,CAAC,CAAC,GAAG,eAAe,CAAC,CAAC,KAC9B,OAAO,CAAC,CAAC,CAAC,GAAG,CAAC,CAAA;AAEnB,2BAA2B,CAAC,CAAC,GAAG,IAAI,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC,KAAK,eAAe,CAAC,CAAC,CAAA;AAEvE,sCAAsC;IACpC,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,YAAY,CAAC,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,6EAA6E,CAAA;AAE1F,OAAO,MAAM,6FAA6F,CAAA;AAC1G,OAAO,MAAM,yDAAyD,CAAA;ACCtE,OAAO,MAAM,6BACH,uBAAuB,KAAG,cAajC,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/wrappers.ts","packages/next/api-wrapper/src/src/index.ts","packages/next/api-wrapper/src/index.ts"],"sourcesContent":[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 ADDED
@@ -0,0 +1,63 @@
1
+ function $parcel$export(e, n, v, s) {
2
+ Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
3
+ }
4
+ var $bdaf63d697185839$exports = {};
5
+
6
+ $parcel$export($bdaf63d697185839$exports, "wrapRequest", function () { return $bdaf63d697185839$export$b77e87d9092b0632; });
7
+ const $bdaf63d697185839$export$b77e87d9092b0632 = (wrappers)=>(requestHandler)=>wrappers.reduce((acc, cur)=>(req, res)=>cur(req, res, ()=>acc(req, res)), requestHandler);
8
+
9
+
10
+ var $6ca930aaa4992525$exports = {};
11
+
12
+ $parcel$export($6ca930aaa4992525$exports, "defaultErrorHandler", function () { return $6ca930aaa4992525$export$74226d5f34b9550e; });
13
+ $parcel$export($6ca930aaa4992525$exports, "cacheControl", function () { return $6ca930aaa4992525$export$935bfe081ea97cf4; });
14
+ var $b2dbcaa42773f73a$exports = {};
15
+
16
+ $parcel$export($b2dbcaa42773f73a$exports, "DEFAULT_API_ERROR_MESSAGE", function () { return $b2dbcaa42773f73a$export$c982278a0ddc4dbe; });
17
+ $parcel$export($b2dbcaa42773f73a$exports, "CACHE_DEFAULT_HEADERS", function () { return $b2dbcaa42773f73a$export$6bec5fc4117189a2; });
18
+ $parcel$export($b2dbcaa42773f73a$exports, "CACHE_DEFAULT_ERROR_HEADERS", function () { return $b2dbcaa42773f73a$export$a3368692186462a4; });
19
+ const $b2dbcaa42773f73a$export$c982278a0ddc4dbe = "Something went wrong. Sorry, try again later :(";
20
+ const $b2dbcaa42773f73a$export$6bec5fc4117189a2 = "public, max-age=180, stale-if-error=1200, stale-while-revalidate=60";
21
+ const $b2dbcaa42773f73a$export$a3368692186462a4 = "no-store, must-revalidate";
22
+
23
+
24
+ const $6ca930aaa4992525$export$74226d5f34b9550e = (args)=>{
25
+ return async (req, res, next)=>{
26
+ const { errorMessage: errorMessage = (0, $b2dbcaa42773f73a$export$c982278a0ddc4dbe) , serverLogger: serverLogger } = args || {};
27
+ try {
28
+ await (next === null || next === void 0 ? void 0 : next(req, res, next));
29
+ } catch (error) {
30
+ if (error instanceof Error) {
31
+ var _message;
32
+ serverLogger === null || serverLogger === void 0 ? void 0 : serverLogger.error((_message = error.message) !== null && _message !== void 0 ? _message : errorMessage);
33
+ var _message1;
34
+ res.status(500).json((_message1 = error.message) !== null && _message1 !== void 0 ? _message1 : errorMessage);
35
+ } else res.status(500).json(errorMessage);
36
+ }
37
+ };
38
+ };
39
+ const $6ca930aaa4992525$export$935bfe081ea97cf4 = (args)=>{
40
+ return async (req, res, next)=>{
41
+ const { headers: headers = (0, $b2dbcaa42773f73a$export$6bec5fc4117189a2) , errorHeaders: errorHeaders = (0, $b2dbcaa42773f73a$export$a3368692186462a4) } = args || {};
42
+ try {
43
+ res.setHeader("Cache-Control", headers);
44
+ await (next === null || next === void 0 ? void 0 : next(req, res, next));
45
+ } catch (error) {
46
+ // for requests with cache-control headers
47
+ // need set new headers otherwise error will be cached
48
+ res.setHeader("Cache-Control", errorHeaders);
49
+ // throw error up the stack
50
+ throw error;
51
+ }
52
+ };
53
+ };
54
+
55
+
56
+
57
+ var $f88c1e0fd4c47034$exports = {};
58
+
59
+
60
+
61
+
62
+ export {$bdaf63d697185839$export$b77e87d9092b0632 as wrapRequest, $6ca930aaa4992525$export$74226d5f34b9550e as defaultErrorHandler, $6ca930aaa4992525$export$935bfe081ea97cf4 as cacheControl, $b2d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,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,CAAA,SAAa,GAAb,KAAK,CAAC,OAAO,cAAb,SAAa,cAAb,SAAa,GAAI,YAAY,CAAC;aACpD,MACC,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,YAAY,CAAC;SAErC;KACF,CAAA;CAAA;AAEI,MAAM,yCAAY,GACvB,CAAC,IAAuB;IACxB,OAAA,OAAO,GAAG,EAAE,GAAG,EAAE,IAAI,GAAK;QACxB,MAAM,WAAE,OAAO,GAAG,CAAA,GAAA,yCAAqB,CAAA,iBAAE,YAAY,GAAG,CAAA,GAAA,yCAA2B,CAAA,GAAE,GAAG,IAAI,IAAI,EAAE;QAClG,IAAI;YACF,GAAG,CAAC,SAAS,CAAC,eAAe,EAAE,OAAO,CAAC;YAEvC,OAAM,IAAI,aAAJ,IAAI,WAAkB,GAAtB,KAAA,CAAsB,GAAtB,IAAI,CAAG,GAAG,EAAE,GAAG,EAAE,IAAI,CAAC,CAAA;SAC7B,CAAC,OAAO,KAAK,EAAE;YACd,0CAA0C;YAC1C,sDAAsD;YACtD,GAAG,CAAC,SAAS,CAAC,eAAe,EAAE,YAAY,CAAC;YAE5C,2BAA2B;YAC3B,MAAM,KAAK,CAAA;SACZ;KACF,CAAA;CAAA;;;;;;","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/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 { 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 if (error instanceof Error) {\n serverLogger?.error(error.message ?? errorMessage)\n res.status(500).json(error.message ?? errorMessage)\n } else {\n res.status(500).json(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. Sorry, try again later :('\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 { 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 ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@lidofinance/next-api-wrapper",
3
+ "description": "Wrapper for next server api requests",
4
+ "repository": "git@github.com:lidofinance/warehouse.git",
5
+ "license": "MIT",
6
+ "version": "0.2.0",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "engines": {
11
+ "node": ">= 16",
12
+ "browsers": "> 0.5%, last 2 versions, not dead"
13
+ },
14
+ "source": "./src/index.ts",
15
+ "main": "dist/index.cjs",
16
+ "module": "dist/index.mjs",
17
+ "types": "dist/index.d.ts",
18
+ "scripts": {
19
+ "build": "parcel build",
20
+ "format": "prettier --check src",
21
+ "format:fix": "yarn format --write",
22
+ "types": "tsc --noEmit",
23
+ "test": "jest"
24
+ },
25
+ "peerDependencies": {
26
+ "@lidofinance/api-logger": "~0.2.0",
27
+ "next": "^12.2.0"
28
+ },
29
+ "devDependencies": {
30
+ "@lidofinance/api-logger": "~0.2.0",
31
+ "@lidofinance/config-prettier": "~0.2.0",
32
+ "next": "^12.2.0",
33
+ "@types/jest": "^28.1.6",
34
+ "@types/node": "^18.0.6",
35
+ "jest": "^28.1.3",
36
+ "ts-jest": "^28.0.7"
37
+ }
38
+ }