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[] | nullgetTestIdsByApp(options)
Get test IDs for a specific app.
import { getTestIdsByApp } from "abc-middleware";
const testIds = getTestIdsByApp({
request,
appShortName: "asvab",
state: "california",
});
// Returns: number[] | nullgetProAppByCookie(options)
Check if app is pro version via cookie.
import { getProAppByCookie } from "abc-middleware";
const isPro = getProAppByCookie({
request,
appShortName: "asvab",
});
// Returns: booleanRouting & Redirects
redirectTo(url, request)
Redirect to a specific URL.
import { redirectTo } from "abc-middleware";
const response = redirectTo("/new-path", request);
// Returns: NextResponserewriteTo(url, request)
Rewrite URL without changing the browser address.
import { rewriteTo } from "abc-middleware";
const response = rewriteTo("/internal-path", request);
// Returns: NextResponsehandlePracticeTestRewrite(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 pathApp 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: NextResponsehandleDesktop(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 configurationsabc-model- Data models and types
Development Dependencies
@repo/eslint-config- ESLint configuration@types/node- Node.js typeseslint- Lintingtsup- TypeScript bundlertypescript- 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
