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 🙏

© 2026 – Pkg Stats / Ryan Hefner

abc-middleware

v0.0.5

Published

middleware

Readme

abc-middleware

Middleware utilities package for handling Next.js middleware operations in the ABC system. This package provides functions for managing routing, cookies, redirects, and device-specific handling.

📦 Installation

pnpm add abc-middleware

🚀 Key Features

1. Cookie Management

  • Extract and parse test IDs from cookies
  • Handle app-specific cookie data
  • Manage pro app status cookies

2. Routing & Redirects

  • Handle practice test rewrites
  • Manage mobile and desktop routing
  • Support for different test types (practice, diagnostic, custom, final)

3. Device-Specific Handling

  • Separate logic for mobile and desktop devices
  • Optimized routing for different screen sizes
  • Device-specific URL rewrites

4. App Data Management

  • Extract app information from slug data
  • Handle state-specific app configurations
  • Manage app-specific test IDs

📚 API Reference

Cookie Management

getTestIds(request)

Extract test IDs from cookies.

import { getTestIds } from "abc-middleware";

const testIds = getTestIds(request);
// Returns: number[] | null

getTestIdsByApp(options)

Get test IDs for a specific app.

import { getTestIdsByApp } from "abc-middleware";

const testIds = getTestIdsByApp({
  request,
  appShortName: "asvab",
  state: "california",
});
// Returns: number[] | null

getProAppByCookie(options)

Check if app is pro version via cookie.

import { getProAppByCookie } from "abc-middleware";

const isPro = getProAppByCookie({
  request,
  appShortName: "asvab",
});
// Returns: boolean

Routing & Redirects

redirectTo(url, request)

Redirect to a specific URL.

import { redirectTo } from "abc-middleware";

const response = redirectTo("/new-path", request);
// Returns: NextResponse

rewriteTo(url, request)

Rewrite URL without changing the browser address.

import { rewriteTo } from "abc-middleware";

const response = rewriteTo("/internal-path", request);
// Returns: NextResponse

handlePracticeTestRewrite(options)

Handle practice test URL rewrites based on available test IDs.

import { handlePracticeTestRewrite } from "abc-middleware";

const redirectPath = handlePracticeTestRewrite({
  appShortName: "asvab",
  practiceTestIds: [1, 2, 3, 4, 5],
  ids: [1, 2, 3],
});
// Returns: string - redirect path

App Data Management

handleGetDataByApp(options)

Extract app-specific data from slug data.

import { handleGetDataByApp } from "abc-middleware";

const appData = handleGetDataByApp({
  app: "asvab",
  slugData: { asvab: { p: [1, 2, 3], f: [4, 5] } },
  state: "california",
});
// Returns: { p: number[], f: number[] }

handleGetInfoByApp(options)

Get app information from apps array.

import { handleGetInfoByApp } from "abc-middleware";

const appInfo = handleGetInfoByApp({
  app: "asvab",
  apps: appInfoArray,
});
// Returns: IAppInfoCore | { appShortName: "", hasState: false }

Device-Specific Handling

handleMobile(options)

Handle mobile-specific routing logic.

import { handleMobile } from "abc-middleware";

const response = handleMobile({
  slug: "practice-test",
  testIds: [1, 2, 3],
  prefixPath: "/asvab",
  request,
  finalTestId: [4, 5],
  response: NextResponse.next(),
});
// Returns: NextResponse

handleDesktop(options)

Handle desktop-specific routing logic.

import { handleDesktop } from "abc-middleware";

const response = handleDesktop({
  appShortName: "asvab",
  slug: "practice-test",
  testIds: [1, 2, 3],
  prefixPath: "/asvab",
  request,
  finalTestId: [4, 5],
  practiceTestIds: [1, 2, 3, 4, 5],
  response: NextResponse.next(),
});
// Returns: NextResponse

🔧 Usage Examples

Complete Middleware Example

import { NextRequest, NextResponse } from "next/server";
import {
  getTestIdsByApp,
  getProAppByCookie,
  handleMobile,
  handleDesktop,
  handleGetDataByApp,
} from "abc-middleware";

export function middleware(request: NextRequest) {
  const { pathname } = request.nextUrl;
  const appShortName = "asvab";

  // Get test IDs for the app
  const testIds = getTestIdsByApp({
    request,
    appShortName,
    state: "california",
  });

  // Check if app is pro
  const isPro = getProAppByCookie({
    request,
    appShortName,
  });

  // Get app data
  const appData = handleGetDataByApp({
    app: appShortName,
    slugData: slugDataObject,
    state: "california",
  });

  // Detect device type
  const userAgent = request.headers.get("user-agent") || "";
  const isMobile = /mobile|android|iphone|ipad|phone/i.test(userAgent);

  const prefixPath = `/asvab/california`;
  const response = NextResponse.next();

  if (isMobile) {
    return handleMobile({
      slug: pathname.split("/").pop() || "",
      testIds,
      prefixPath,
      request,
      finalTestId: appData.f,
      response,
    });
  } else {
    return handleDesktop({
      appShortName,
      slug: pathname.split("/").pop() || "",
      testIds,
      prefixPath,
      request,
      finalTestId: appData.f,
      practiceTestIds: appData.p,
      response,
    });
  }
}

Cookie Management Example

import { getTestIds, getProAppByCookie } from "abc-middleware";

// Get all test IDs
const allTestIds = getTestIds(request);

// Get app-specific test IDs
const appTestIds = getTestIdsByApp({
  request,
  appShortName: "asvab",
  state: "california",
});

// Check pro status
const isProUser = getProAppByCookie({
  request,
  appShortName: "asvab",
});

📋 Type Definitions

interface IItemSlugData {
  p: number[];
  f: number[];
}

interface ISlugData {
  [key: string]: IItemSlugData;
}

interface IProps {
  appShortName: string;
  slug: string;
  testIds: number[];
  prefixPath: string;
  request: NextRequest;
  finalTestId: number[] | number;
  practiceTestIds: number[];
  response: NextResponse;
}

🔧 Development

# Build package
pnpm build

# Development mode with watch
pnpm dev

# Clean dist
pnpm clean

📦 Dependencies

Production Dependencies

  • abc-constants - Constants and router configurations
  • abc-model - Data models and types

Development Dependencies

  • @repo/eslint-config - ESLint configuration
  • @types/node - Node.js types
  • eslint - Linting
  • tsup - TypeScript bundler
  • typescript - TypeScript compiler

🔄 Routing Patterns

Mobile Routing

  • /mobile/practice-test - Practice test page
  • /mobile/diagnostic-test - Diagnostic test page
  • /mobile/custom-test - Custom test page
  • /mobile/final-test - Final test page
  • /mobile/result-test - Test results page

Desktop Routing

  • /practice-test-1 - Practice test with ID
  • /diagnostic-test - Diagnostic test
  • /custom-test - Custom test
  • /final-test - Final test
  • /result-test - Test results

📄 License

MIT