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-redirect

v1.1.0

Published

A redirect middleware to work within an EAPI app. Check worker-eapi-template for context.

Downloads

8

Readme

@p-j/eapi-middleware-redirect

A middleware to configure redirect behavior on a per route or request basis

Installation

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

API

withRedirect is a Middleware Factory; it takes the following options:

export interface WithRedirectOptions {
  urlOrPath?: string
  transform?: Transform
  transparent?: boolean
  permanent?: boolean
  redirect?: 'follow' | 'error' | 'manual' | undefined
}

As noted above, none of the options are required.

  • urlOrPath the url, absolute or relative to redirect to. Defaults to ''.
  • transform an optional Transform function to do avdanced transformation on the original Request
  • transparent whether the redirection is transparent or not. Defaults to true. Transparent redirection will make the request handler behave like a proxy.
  • permanent if not transparent, the redirection is either permanent (301) or not (302). Defaults to false.
  • redirect one of Request.redirect valid values (follow, manual, error) determining the behavior of the request handler if it encounters a redirect while applying the RequestHandler. Defaults to 'follow'. If set to follow and the upstream has a redirect, the worker will act as a proxy to the final response.

This middlware allow you to define redirections as well a to transparently proxy request.

Usage

Proxy example

Let's say you want to proxy calls to an external API without disclosing sensitive informations like an API Key to your end users, or you want to guarantee anonimity for your users with regards to that 3rd party library for compliance reason or else.

import { withRedirect } from '@p-j/eapi-middleware-redirect'

const passThrough: RequestHandler = ({ request }) => fetch(request)
const proxySomeApi = withRedirect({
  transparent: true, // forward the transformed request to the requestHandler
  redirect: 'follow', // ensure we follow any redirect that would appear while executing the transformed request
  transform: ({ request }) => {
    const url = new URL('https://some.api.com/endpoint')
    url.searchParams = { x: request.query.param1, y: request.query.param2 }
    // Making a clean new request, free of cookies, without disclosing your users IP address etc...
    // While also keeping the API KEY private to your worker
    return new Request(url, { headers: { Authorization: `Bearer ${API_KEY}` } })
  },
})

const requestHandler = proxySomeApi(passThrough)

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

Of course this example is trivial, but the passThrough request handler could contain logic of its own to validate query params and/or use KV Store for cache etc...

Also, the transform could be more generic if you were to use multiple endpoints of the 3rd party API and combined this middleware with a Router like showcased in p-j/worker-eapi-template.

Redirect example

Replacing Cloudflare Page Rules for redirections 😅

import { withRedirec } from '@p-j/eapi-middleware-redirect'

const passThrough: RequestHandler = ({ request }) => fetch(request)
const redirectToNewPath = withRedirec({
  transparent: false, // force a redirect to the new URL
  urlOrPath: '/newPath', // you can make use of transform for advanced url replacement
})

const requestHandler = redirectToNewPath(passThrough)

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

A note on redirect & transparent

transparent is applied at the middleware level, if within the withRedirect a change of url happen and:

  • transparent is false, then the new URL will be sent to the client as a redirect.
  • transparent is true, then the new Request will passed down to the RequestHandler.

redirect is applied at the RequestHandler level, it's the standard Request.redirect.

  • If set to 'manual' and the RequestHandler encounter a redirection, this redirection will be sent back to the client.
  • If set to 'follow' (the default) and the RequestHandler encounter a redirection, it will follow it, get back the final response and send it to the client, effectively masking the redirection from the client perspective.