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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@foxen/config

v1.4.0

Published

Configuration loading and parsing for Foxen - next.config.ts support, redirects, rewrites, headers

Downloads

492

Readme

@foxen/config

Configuration loading and Next.js config support for Foxen. Handles loading and processing foxen.config.ts files with full support for next.config.ts features like redirects, rewrites, and headers.

Installation

bun add @foxen/config

Usage

Loading Configuration

import { loadFoxenConfig, loadNextConfig } from "@foxen/config";

// Load foxen.config.ts
const foxenConfig = await loadFoxenConfig({
    configPath: "./foxen.config.ts",
});

// Load next.config.ts
const nextConfig = await loadNextConfig({
    configPath: "./next.config.ts",
});

Processing Redirects

import { processRedirects, createRedirectResponse } from "@foxen/config";

// In your middleware or handler
const redirect = processRedirects(request.url, nextConfig.redirects);

if (redirect.matched) {
    return createRedirectResponse(redirect.destination, redirect.permanent);
}

Processing Rewrites

import { processRewrites, createRewrittenRequest } from "@foxen/config";

const rewrite = processRewrites(request.url, nextConfig.rewrites);

if (rewrite.matched) {
    // Create new request with rewritten URL
    const newRequest = createRewrittenRequest(request, rewrite.destination);
    // Process with new URL...
}

Processing Headers

import { processHeaders, applyHeadersToResponse } from "@foxen/config";

// Get headers that should be applied
const headerRules = processHeaders(request.url, nextConfig.headers);

// Apply to response
const response = new Response("OK");
applyHeadersToResponse(response, headerRules);

Path Matching

import { matchPath, applyParams } from "@foxen/config";

// Match a path pattern
const result = matchPath("/users/:id", "/users/123");
// { matched: true, params: { id: "123" } }

// Apply params to a destination
const dest = applyParams("/api/users/:id", { id: "123" });
// "/api/users/123"

Configuration Types

Foxen Config

interface FoxenConfig {
    routesDir: string;
    outputDir: string;
    basePath?: string;
    format?: "ts" | "js";
    generateBarrel?: boolean;
    useGroups?: boolean;
    routesAlias?: string;
    tsConfigPath?: string;
    ignorePatterns?: string[];
    elysiaInstanceName?: string;
}

Next.js Config Features

// Redirects
interface NextRedirect {
    source: string;
    destination: string;
    permanent: boolean;
    has?: RouteCondition[];
    missing?: RouteCondition[];
}

// Rewrites
interface NextRewrite {
    source: string;
    destination: string;
    has?: RouteCondition[];
    missing?: RouteCondition[];
}

// Headers
interface NextHeader {
    source: string;
    headers: Array<{ key: string; value: string }>;
    has?: RouteCondition[];
    missing?: RouteCondition[];
}

API Reference

Config Loading

| Function | Description | |----------|-------------| | loadFoxenConfig(options?) | Load foxen.config.ts | | loadNextConfig(options?) | Load next.config.ts | | defineConfig(config) | Type-safe config helper | | findConfigFile(dir) | Find config file in directory |

Path Matching

| Function | Description | |----------|-------------| | matchPath(pattern, path) | Match path against pattern | | matchConditions(request, conditions) | Check route conditions | | applyParams(template, params) | Apply params to template | | parsePath(pattern) | Parse path into segments |

Redirects

| Function | Description | |----------|-------------| | processRedirects(url, redirects) | Find matching redirect | | processRedirect(url, redirect) | Process single redirect | | createRedirectResponse(url, permanent) | Create redirect response |

Rewrites

| Function | Description | |----------|-------------| | processRewrites(url, rewrites) | Find matching rewrite | | processRewrite(url, rewrite) | Process single rewrite | | createRewrittenRequest(request, url) | Create rewritten request |

Headers

| Function | Description | |----------|-------------| | processHeaders(url, headers) | Find matching headers | | applyHeadersToResponse(response, headers) | Apply headers to response | | createCorsHeaders(options) | Generate CORS headers | | SECURITY_HEADERS | Common security headers |

Examples

Full next.config.ts Support

import {
    loadNextConfig,
    processRedirects,
    processRewrites,
    processHeaders,
} from "@foxen/config";

const config = await loadNextConfig();

// In request handler
function handleRequest(request: Request) {
    const url = new URL(request.url);

    // Check redirects
    const redirect = processRedirects(url.pathname, config.redirects);
    if (redirect.matched) {
        return Response.redirect(redirect.destination, redirect.statusCode);
    }

    // Check rewrites
    const rewrite = processRewrites(url.pathname, config.rewrites);
    if (rewrite.matched) {
        url.pathname = rewrite.destination;
        // Continue with new URL
    }

    // Get response headers
    const headerRules = processHeaders(url.pathname, config.headers);

    // Create response and apply headers
    const response = await handleRoute(request);
    for (const rule of headerRules) {
        for (const h of rule.headers) {
            response.headers.set(h.key, h.value);
        }
    }

    return response;
}

License

MIT