diff --git a/.changeset/clean-types-travel.md b/.changeset/clean-types-travel.md new file mode 100644 index 0000000..8a852d2 --- /dev/null +++ b/.changeset/clean-types-travel.md @@ -0,0 +1,6 @@ +--- +"@bomb.sh/router": patch +--- + +Export router extension and command model types from the package root for +portable type declarations. diff --git a/lib/command.ts b/lib/command.ts index a73d6a3..2c7ad9f 100644 --- a/lib/command.ts +++ b/lib/command.ts @@ -6,21 +6,76 @@ import { normalize, type Validate, } from "./route.ts"; -import type { Definition, Done, Route } from "./types.ts"; +import type { + AnyPhases, + AnyRoute, + Definition, + Done as DoneInternal, + Method, + Next as NextInternal, + Route, +} from "./types.ts"; -export type CommandZero = Route< +type CommandZero = Route< N, "help" | "execute", - [Done<{}, []>] + [DoneInternal<{}, []>] >; +/** A complete command route */ +export interface Command< + N extends string, + M extends Method, + P extends AnyPhases, +> extends Route {} + +// deno-lint-ignore no-namespace +export namespace Command { + /** A pipeline element accepted by {@link command}. */ + export type Element = Input; + + /** A completed phase */ + export type Done< + Model extends object, + Routes extends readonly AnyRoute[], + > = DoneInternal; + + /** A suspended phase */ + export type Next< + Model extends object, + Routes extends readonly AnyRoute[], + Requirement, + > = NextInternal; + + /** The completed command type produced by {@link command}. */ + export type Result< + N extends string, + E extends readonly Element[], + > = FromRoute, Normalize>>>; +} + +type CommandPhase

= P extends + NextInternal + ? Command.Next + : P extends DoneInternal + ? Command.Done + : P; + +type CommandPhases

= { + [K in keyof P]: CommandPhase; +}; + +type FromRoute = AnyRoute extends R ? R + : R extends Route ? Command> + : R; + export function command< const N extends string, - const E extends readonly Input[], + const E extends readonly Command.Element[], >( start: Definition, ...elements: E & Validate, E> -): Materialize, Normalize>> { +): Command.Result { let zero: CommandZero = { ...start, methods: ["help", "execute"], @@ -38,5 +93,5 @@ export function command< return normalize(elements).reduce( (value, element) => element(value as never), zero, - ) as Materialize, Normalize>>; + ) as Command.Result; } diff --git a/mod.ts b/mod.ts index 215f43e..6176ffa 100644 --- a/mod.ts +++ b/mod.ts @@ -2,7 +2,7 @@ export { argument } from "./lib/argument.ts"; export { extend } from "./lib/extend.ts"; export { command } from "./lib/command.ts"; -export type { CommandZero } from "./lib/command.ts"; +export type { Command } from "./lib/command.ts"; export { checkpoint } from "./lib/checkpoint.ts"; export { dynamic } from "./lib/dynamic.ts"; @@ -16,12 +16,17 @@ export { option } from "./lib/option.ts"; export { multiple } from "./lib/multiple.ts"; export { param, schema } from "./lib/param.ts"; -export type { Param } from "./lib/param.ts"; +export type { Param, ParamModel } from "./lib/param.ts"; export { parse } from "./lib/parse.ts"; export { mark } from "./lib/pipeline.ts"; -export type { Transform, TransformElement } from "./lib/pipeline.ts"; +export type { + Extension, + ModelElement, + Transform, + TransformElement, +} from "./lib/pipeline.ts"; export { printErrors, printHelp, printVersion } from "./lib/print.ts"; diff --git a/test/command.test.ts b/test/command.test.ts index 4b78cc7..15ef626 100644 --- a/test/command.test.ts +++ b/test/command.test.ts @@ -1,12 +1,12 @@ import { expect } from "@std/expect"; import { describe, it } from "@std/testing/bdd"; import { type } from "arktype"; -import { command } from "../lib/command.ts"; +import { type Command, command } from "../lib/command.ts"; import { name } from "../lib/definition.ts"; import { option } from "../lib/option.ts"; import { schema } from "../lib/param.ts"; import { version } from "../lib/route.ts"; -import type { Done, ModelOf } from "../lib/types.ts"; +import type { ModelOf } from "../lib/types.ts"; describe("command()", () => { it("creates an executable route without an explicit executable element", () => { @@ -34,9 +34,17 @@ describe("command()", () => { expectType< Equal< typeof result.phases, - readonly [Done<{ port: number; domain: string }, []>] + readonly [Command.Done<{ port: number; domain: string }, []>] > >(true); + expectType< + typeof result extends Command< + typeof result.name, + typeof result.methods[number], + typeof result.phases + > ? true + : false + >(true); }); it("infers the exact model across thirty route elements", () => {