Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions guardrails.suppressions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
"rules": {
"anti-slop/no-unknown-parameters": 1
},
"tsExpectError": 1,
"tsExpectError": 0,
"tsIgnore": 0,
"safety": 0,
"safety": 1,
"config": {},
"agentFiles": true
}
6 changes: 5 additions & 1 deletion knip.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { antelopeKnipConfig } from "@antelopejs/tooling-configs/knip";

export default antelopeKnipConfig({
entry: ["src/tests/**/*.test.ts", "src/antelope.test.ts"],
entry: [
"src/tests/**/*.test.ts",
"src/tests/**/*.untyped.spec.js",
"src/antelope.test.ts",
],
});
13 changes: 6 additions & 7 deletions src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,10 @@ export type ParameterModifier<Next = unknown> = (
*/
export interface ComputedParameter {
/**
* Source of the parameter. It reads the context its decorator declared,
* which the route that registers it supplies, so it holds a
* `ParameterProvider` of any `RequestContext` subtype.
* Source of the parameter. A provider that declared a `RequestContext`
* subtype is stored here after `ControllerMeta.setProvider` widens it.
*/
provider?: {
provide(context: RequestContext): ReturnType<ParameterProvider>;
}["provide"];
provider?: ParameterProvider;
modifiers: ParameterModifier[];
}

Expand Down Expand Up @@ -227,7 +224,9 @@ export class ControllerMeta {
param: number | undefined,
provider: ParameterProvider<unknown, Context>,
) {
this.getComputedParameter(key, param).provider = provider;
// SAFETY: Context names the slots that the caller's controller decorator or prefix handler sets on the request before this provider runs, and computeParameter passes that same request object, so the provider receives the Context it declared.
this.getComputedParameter(key, param).provider =
provider as ParameterProvider;
}

/**
Expand Down
19 changes: 0 additions & 19 deletions src/tests/route-lifecycle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,25 +114,6 @@ describe("Route lifecycle", () => {
assert.equal(routesAt("/lifecycle/bystander"), 0);
});

it("refuses a handler that names no controller and registers nothing", () => {
const handler = handlerAt("/lifecycle/no-controller");
// @ts-expect-error off-contract handler without its required controller; RegisterRoute must throw a TypeError at registration
delete handler.controller;
const before = getRegisteredRoutes().length;

assert.throws(
() => RegisterRoute(handler),
(error) =>
error instanceof TypeError &&
error.message.includes("GET") &&
error.message.includes("/lifecycle/no-controller") &&
error.message.includes("controller"),
);

assert.equal(getRegisteredRoutes().length, before);
assert.deepEqual(proxyCalls, []);
});

it("tolerates unregistering the same id twice", () => {
const id = RegisterRoute(handlerAt("/lifecycle/twice"));
UnregisterRoute(id);
Expand Down
44 changes: 44 additions & 0 deletions src/tests/route-lifecycle.untyped.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import assert from "node:assert";

import { getRegisteredRoutes, RegisterRoute, routesProxy } from "../index";

const proxyCalls = [];

describe("Route lifecycle, off-contract handlers", () => {
before(() => {
routesProxy.onRegister((id) => {
proxyCalls.push({ kind: "register", id });
}, true);
routesProxy.onUnregister((id) => {
proxyCalls.push({ kind: "unregister", id });
});
});

beforeEach(() => {
proxyCalls.length = 0;
});

it("refuses a handler that names no controller and registers nothing", () => {
const handler = {
location: "/lifecycle/no-controller",
method: "GET",
mode: "handler",
parameters: [],
properties: {},
callback: async () => new Response(),
};
const before = getRegisteredRoutes().length;

assert.throws(
() => RegisterRoute(handler),
(error) =>
error instanceof TypeError &&
error.message.includes("GET") &&
error.message.includes("/lifecycle/no-controller") &&
error.message.includes("controller"),
);

assert.equal(getRegisteredRoutes().length, before);
assert.deepEqual(proxyCalls, []);
});
});
2 changes: 2 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"target": "esnext",
"module": "commonjs",
"lib": ["ES2022"],
"allowJs": true,
"checkJs": false,
"types": ["node", "mocha"],
"esModuleInterop": true,
"strict": true,
Expand Down
Loading