@lidofinance/next-api-wrapper
v0.46.0
Published
Wrapper for next server api requests
Downloads
1,251
Readme
@lidofinance/next-api-wrapper
Wrapper for next server api requests.
Installation
yarn add @lidofinance/next-api-wrapper
.
Getting started
wrapRequest
import { wrapRequest, API } from '@lidofinance/next-api-wrapper'
import { NextApiRequest, NextApiResponse } from 'next'
const someRequest: API = async (req, res) => await fetch()
export default wrapRequest([SomeWrapper])(someRequest)
Examples
Default error handler
import { wrapRequest, defaultErrorHandler } from '@lidofinance/next-api-wrapper'
import { NextApiRequest, NextApiResponse } from 'next'
export type API = (req: NextApiRequest, res: NextApiResponse) => Promise<void>
const CUSTOM_API_ERROR_MESSAGE = 'Default message'
const someRequest: API = async (req, res) => await fetch()
export default wrapRequest([defaultErrorHandler()])(someRequest)
// or
export default wrapRequest([defaultErrorHandler({ errorMessage: CUSTOM_API_ERROR_MESSAGE, serverLogger: SomeLogger })])(
someRequest,
)
Cache control
import { wrapRequest, cacheControl } from '@lidofinance/next-api-wrapper'
import { NextApiRequest, NextApiResponse } from 'next'
export type API = (req: NextApiRequest, res: NextApiResponse) => Promise<void>
const CUSTOM_CACHE_HEADERS = 'public, max-age=180'
const CUSTOM_ERROR_CACHE_HEADERS = 'no-store'
const someRequest: API = async (req, res) => await fetch()
// use default headers
export default wrapRequest([cacheControl()])(someRequest)
// or
export default wrapRequest([cacheControl({ headers: CUSTOM_CACHE_HEADERS, errorHeaders: CUSTOM_ERROR_CACHE_HEADERS })])(
someRequest,
)
Mixing wrappers
import { wrapRequest, cacheControl, defaultErrorHandler } from '@lidofinance/next-api-wrapper'
import { NextApiRequest, NextApiResponse } from 'next'
export type API = (req: NextApiRequest, res: NextApiResponse) => Promise<void>
const CUSTOM_API_ERROR_MESSAGE = 'Default message'
const CUSTOM_CACHE_HEADERS = 'public, max-age=180'
const CUSTOM_ERROR_CACHE_HEADERS = 'no-store'
const someRequest: API = async (req, res) => await fetch()
// defaultErrorHandler must be last in the wrapper stack
export default wrapRequest([cacheControl(), defaultErrorHandler()])(someRequest)
// or
export default wrapRequest([
cacheControl({ headers: CUSTOM_CACHE_HEADERS, errorHeaders: CUSTOM_ERROR_CACHE_HEADERS }),
defaultErrorHandler({ errorMessage: CUSTOM_API_ERROR_MESSAGE }),
])(someRequest)
Creation of ready mixed wrappers
import { wrapRequest, cacheControl, defaultErrorHandler } from '@lidofinance/next-api-wrapper'
import { NextApiRequest, NextApiResponse } from 'next'
export type API = (req: NextApiRequest, res: NextApiResponse) => Promise<void>
const DEFAULT_API_ERROR_MESSAGE = 'Default message'
const CACHE_HEADERS = 'public, max-age=180'
// defaultErrorHandler must be last in the wrapper stack
export const defaultErrorAndCacheWrapper = wrapRequest([cacheControl(), defaultErrorHandler()])
const someRequest: API = async (req, res) => await fetch()
export default defaultErrorAndCacheWrapper(someRequest)
Response time metric
import { wrapRequest, responseTimeMetric } from '@lidofinance/next-api-wrapper'
import { NextApiRequest, NextApiResponse } from 'next'
import { Registry, Histogram } from 'prom-client'
export type API = (req: NextApiRequest, res: NextApiResponse) => Promise<void>
const apiTimings = new Histogram({
name: 'frontend_template_api_response_internal',
help: 'API response time',
labelNames: ['hostname', 'route', 'entity', 'status'],
buckets: [0.1, 0.2, 0.3, 0.6, 1, 1.5, 2, 5],
registers: new Registry(),
});
const someRequest: API = async (req, res) => await fetch()
export default wrapRequest([
responseTimeMetric(apiTimings, 'api/some-request'),
])(someRequest)