npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@lidofinance/next-api-wrapper

v0.48.0

Published

Wrapper for next server api requests

Downloads

1,844

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)