caselaw-access
v0.5.0
Published
TypeScript client for the Case Law Explorer shared access service.
Downloads
44
Readme
caselaw-access
TypeScript client for the Case Law Explorer access service. One call decides authentication, endpoint scope, and rate limit, so a product implements none of them itself.
Install
npm install caselaw-accessThe package ships raw TypeScript with no build step (same praxis as
caselaw-ui), so it expects a bundler or a TS-aware runtime.
Configuration
Everything comes from the environment:
| Variable | Meaning | Default |
| --- | --- | --- |
| ACCESS_SERVICE_URL | Base URL of the access service. Empty disables all checks. | — |
| ACCESS_PROJECT | Project slug registered in the console. No default. | — |
| ACCESS_SERVICE_TIMEOUT_SECONDS | Per-check timeout. | 5 |
| ACCESS_FAIL_OPEN | Waive quota for already-validated credentials when the service is unreachable. Never waives authentication. | false |
| REQUIRE_AUTH / ACCESS_ENFORCE | Master switch for enforcement. Unset on a connected deployment is an error, not an opt-out. | unset |
| ACCESS_IDENTITY_CACHE_SECONDS | How long a validated credential stays eligible for fail-open. | 300 |
Checks are only sent when the service URL is set and enforcement is on. That combination is what lets a service run locally, and its tests run in CI, without the control plane.
Direct use
import { AccessClient, RateLimited, PermissionDenied } from "caselaw-access";
const client = AccessClient.fromEnv();
try {
const decision = await client.check(token, { endpoint: "echr", method: "POST" });
// decision.plan, decision.scopes, decision.headers()
} catch (error) {
if (error instanceof RateLimited) { /* 429, error.headers() has Retry-After */ }
if (error instanceof PermissionDenied) { /* 403 */ }
}Express
import { AccessClient, createGuard } from "caselaw-access";
const guard = createGuard(AccessClient.fromEnv());
app.post("/api/echr", guard.middleware("echr"), (req, res) => {
// req.accessDecision is set; quota headers are already on the response
});A denial is answered before your handler runs, with the right status,
{ error: code } body, and Retry-After / X-RateLimit-Remaining-* headers.
Failure semantics
| Situation | Result |
| --- | --- |
| No or malformed Authorization header | 401, service never called |
| ACCESS_PROJECT unset while enforcing | 503, service never called, not subject to fail-open |
| Service answers 401 | 401 with the service's code |
| Service answers 403, or allowed: false with a scope error | 403 |
| Service answers 429, or allowed: false with rate_limit_exceeded | 429 with Retry-After |
| Service unreachable, times out, or answers something it cannot decide on | 503, or allowed when ACCESS_FAIL_OPEN=true and this credential was recently validated for this endpoint |
Connection failures are retried because the request provably never arrived. Read timeouts are not retried: the service may already have counted the call against the caller's quota, and a retry would charge them twice.
Tests
npm ci && npm run check && npm testThe suite drives the shared conformance cases in ../contract,
the same file the Python client runs, so the two cannot drift.
Releasing
Released by GitHub Actions via npm trusted publishing — no token anywhere.
Bump version in package.json, commit, push, then:
git tag js-v0.2.0 && git push origin js-v0.2.0The workflow refuses to publish if the tag and package.json disagree. See
../README.md for the full procedure.
Framework adapters
The client is framework-agnostic — check() takes a token and returns a
decision. These wrap it so guarding a route is one import and one call. None of
them adds a dependency: each is typed structurally against the small part of
its framework it touches.
import { protect } from "caselaw-access/express"; // app.get(p, protect("search"), handler)
import { protect } from "caselaw-access/fastify"; // { preHandler: protect("search") }
import { protect } from "caselaw-access/koa"; // app.use(protect("search"))
import { protect } from "caselaw-access/hono"; // app.get(p, protect("search"), handler)
import { protect } from "caselaw-access/fetch"; // export const GET = protect("search", handler)
import { protect } from "caselaw-access/sveltekit"; // export const GET = protect("search", handler)
import { protect } from "caselaw-access/nuxt"; // defineEventHandler(protect("search", handler))caselaw-access/fetch wraps a (Request) => Response handler, which covers
Next.js route handlers, Cloudflare Workers, Deno and Bun. SvelteKit gets its
own entry only because its endpoints take an event rather than a bare request.
caselaw-access/nuxt (also exported as /h3) is for Nuxt server routes and
Nitro. H3 is the odd one out: a handler returns a value H3 serialises rather
than a Response it builds, so a denial is thrown as an H3 error. Quota headers
need a response handle, which the Node preset Nuxt uses by default provides;
an edge preset keeps the status and the body but not those headers.
The endpoints a project guards are declared in the access console, not by the client. Rate limits are configured before any traffic exists, so the list has to exist then too — and the console verifies it by probing each route with no credential, which proves the guard is actually on the handler rather than just that a client started.
On a denial the response is already written when the handler is skipped:
status, JSON body, and the Retry-After and X-RateLimit-* headers. Those
headers are the part hand-written glue reliably drops, because nothing fails
when they are missing — the caller simply never learns when to retry.
What fail-open does, and does not, do
/v1/check answers two questions at once: who is this caller, and do they
have quota left. Waiving both during an outage means any non-empty bearer
string is admitted — an authentication bypass dressed up as an availability
feature.
So ACCESS_FAIL_OPEN=true waives quota only, and only for a credential
this process has already watched the service accept for that same endpoint
within ACCESS_IDENTITY_CACHE_SECONDS. Anything else is denied exactly as if
the policy were fail-closed.
In practice: an outage does not interrupt traffic that was already flowing, and
does not let anything new in. The cost is that a credential revoked during an
outage keeps working until its cache entry expires — set
ACCESS_IDENTITY_CACHE_SECONDS=0 to disable the mechanism entirely, which
makes ACCESS_FAIL_OPEN a no-op.
The pair is keyed on credential and endpoint on purpose. Keying on the credential alone would let an outage promote a caller onto an endpoint they were never entitled to, turning a quota waiver into an authorization bypass.
Enforcement must be stated
Leaving REQUIRE_AUTH / ACCESS_ENFORCE unset while a project and a secret
are configured raises access_enforcement_not_configured rather than running
inert. A project secret is issued once, by hand, for a specific deployment, so
carrying one is unambiguous intent — an absent flag is a forgotten variable,
and guessing wrong means enforcing nothing while looking fully configured.
Set it explicitly to false to run inert on purpose.
