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

@p-j/eapi-middleware-headers

v1.2.0

Published

A middleware to manage headers or requests within an EAPI app. Check worker-eapi-template for context.

Downloads

16

Readme

@p-j/eapi-middleware-headers

A middleware to configure headers on both Request (to an upstream server) and Response on a per route or request basis

Installation

  • From the NPM registry
npm install @p-j/eapi-middleware-headers
# or
yarn add @p-j/eapi-middleware-headers

API

  • withHeaders is a Middleware Factory; it takes in the following options:

    export interface WithHeadersOptions {
      addRequestHeaders?: HeadersInit
      removeRequestHeaders?: string[]
      addResponseHeaders?: HeadersInit
      removeResponseHeaders?: string[]
      existing?: 'combine' | 'override' | 'skip'
    }

    As noted above, none of the options are required.

    • addRequestHeaders headers to add to the Request before passing it down to the requestHandler
    • removeRequestHeaders headers to remove from the Request before passing it down to the requestHandler
    • addResponseHeaders headers to add to the Response before returning it
    • removeResponseHeaders headers to remove from the Response before returning it
    • existing define how add<Request|Response>Headers will handle existing headers, it can be set to either 'combine', 'override' or 'skip'. Defaults to 'combine'.
  • managerHeaders is a utility function that adds CORS headers to a Request or a Response; it takes in the following options:

    export interface ManageHeadersOptions {
      subject: Request | Response
      addHeaders?: HeadersInit
      removeHeaders?: string[]
      existing?: 'combine' | 'override' | 'skip'
    }
    • subject the Request or Response to work on
    • addHeaders headers to add
    • removeHeaders headers to remove
    • existing define how addHeaders will handle existing headers, it can be set to either 'combine', 'override' or 'skip'. Defaults to 'combine'.

Usage

Adding a header to a Request aimed at an upstream server

Another way of implementing the Proxy example from the eapi-middleware-redirect

import { withHeaders } from '@p-j/eapi-middleware-headers'

const callUpstream: RequestHandler = ({ request }) => {
  // combine the url with the request details provided, including the Authorization Header added by the middleware
  const searchParams = new URLSearchParams({ x: request.query.param1, y: request.query.param2 })
  const upstreamRequest = new Request(`https://some.api.com/endpoint?${searchParams.toString()}`, request)
  return fetch(upstreamRequest)
}

const addAuthorizationHeader = withHeaders({ addRequestHeader: { Authorization: `Bearer ${API_KEY}` } })

const requestHandler = addAuthorizationHeader(callUpstream)

addEventListener('fetch', (event) => event.respondWith(requestHandler({ event, request: event.request })))

Removing headers before forwarding a request to a 3rd party server

As a follow up to the example above, we may want to remove sensitive informations from the original request before sending it to a 3rd party server:

import { withHeaders } from '@p-j/eapi-middleware-headers'

const callUpstream: RequestHandler = ({ request }) => {
  // combine the url with the request details provided, including the Authorization Header added by the middleware
  const searchParams = new URLSearchParams({ x: request.query.param1, y: request.query.param2 })
  const upstreamRequest = new Request(`https://some.api.com/endpoint?${searchParams.toString()}`, request)
  return fetch(upstreamRequest)
}

const addAuthorizationHeader = withHeaders({
  addRequestHeader: { Authorization: `Bearer ${API_KEY}` },
  removeRequestHeaders: [
    // these headers will not be sent to the upstream server
    'user-agent',
    'referer',
    'cookie',
    'cf-connecting-ip',
  ],
})

const requestHandler = addAuthorizationHeader(callUpstream)

addEventListener('fetch', (event) => event.respondWith(requestHandler({ event, request: event.request })))

Adding headers to a Response

Overriding CORS headers from an upstream API (you should likely use `eapi-middleware-cors for this instead)

import { withHeaders } from '@p-j/eapi-middleware-headers'

const callUpstream: RequestHandler = ({ request }) => {
  // combine the url with the request details provided, including the Authorization Header added by the middleware
  const searchParams = new URLSearchParams({ x: request.query.param1, y: request.query.param2 })
  const upstreamRequest = new Request(`https://some.api.com/endpoint?${searchParams.toString()}`, request)
  return fetch(upstreamRequest)
}

const addAuthorizationHeader = withHeaders({
  addRequestHeader: { Authorization: `Bearer ${API_KEY}` },
  removeRequestHeaders: [
    // these headers will not be sent to the upstream server
    'user-agent',
    'referer',
    'cookie',
    'cf-connecting-ip',
  ],
  addResponseHeader: {
    'Access-Control-Allow-Origin': 'https://my-awesome-origin.com',
    'Access-Control-Allow-Headers': ['Origin', 'Content-Type', 'Accept', 'Authorization'].join(','),
    'Access-Control-Allow-Methods': ['GET', 'OPTIONS', 'HEAD'].join(','),
    'Access-Control-Max-Age': '3600',
  },
  existing: 'override', // Make sure we override any exsting headers
})

const requestHandler = addAuthorizationHeader(callUpstream)

addEventListener('fetch', (event) => event.respondWith(requestHandler({ event, request: event.request })))