npm.io
0.2.2 • Published 1 week ago

@spacefast/routing

Licence
MIT
Version
0.2.2
Deps
1
Size
2.7 MB
Vulns
0
Weekly
0

@spacefast/routing

Routing parser and matcher for Spacefast _redirects and _headers files.

The CLI and Vite plugin use this package to validate routing rules before a version is finalized. Install it directly when you need to inspect, compile, or test Spacefast routing behavior in your own tooling.

Install

npm install @spacefast/routing

Compile Routing Files

import { compileRoutingFiles } from "@spacefast/routing/compile";

const routing = compileRoutingFiles({
  redirects: "/old /new 301\n/api/* https://api.example.com/:splat 200",
  headers: "/*\n  x-frame-options: DENY",
});

if (routing.diagnostics.length > 0) {
  console.warn(routing.diagnostics);
}

Match Requests

import { headersForRequest, matchRedirect } from "@spacefast/routing/match";

const request = {
  url: new URL("https://example.com/old"),
  headers: new Headers(),
};

const redirect = matchRedirect({ compilation: routing, request });
const headers = headersForRequest(routing, request);

Documentation