Skip to content
Open
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
6 changes: 6 additions & 0 deletions .changeset/clean-types-travel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@bomb.sh/router": patch
---

Export router extension and command model types from the package root for
portable type declarations.
67 changes: 61 additions & 6 deletions lib/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<N extends string = string> = Route<
type CommandZero<N extends string = string> = Route<
N,
"help" | "execute",
[Done<{}, []>]
[DoneInternal<{}, []>]
>;

/** A complete command route */
export interface Command<
N extends string,
M extends Method,
P extends AnyPhases,
> extends Route<N, M, P> {}

// 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<Model, Routes>;

/** A suspended phase */
export type Next<
Model extends object,
Routes extends readonly AnyRoute[],
Requirement,
> = NextInternal<Model, Routes, Requirement>;

/** The completed command type produced by {@link command}. */
export type Result<
N extends string,
E extends readonly Element[],
> = FromRoute<Materialize<Fold<CommandZero<N>, Normalize<E>>>>;
}

type CommandPhase<P> = P extends
NextInternal<infer Model, infer Routes, infer Requirement>
? Command.Next<Model, Routes, Requirement>
: P extends DoneInternal<infer Model, infer Routes>
? Command.Done<Model, Routes>
: P;

type CommandPhases<P extends AnyPhases> = {
[K in keyof P]: CommandPhase<P[K]>;
};

type FromRoute<R> = AnyRoute extends R ? R
: R extends Route<infer N, infer M, infer P> ? Command<N, M, CommandPhases<P>>
: R;

export function command<
const N extends string,
const E extends readonly Input[],
const E extends readonly Command.Element[],
>(
start: Definition<N>,
...elements: E & Validate<CommandZero<N>, E>
): Materialize<Fold<CommandZero<N>, Normalize<E>>> {
): Command.Result<N, E> {
let zero: CommandZero<N> = {
...start,
methods: ["help", "execute"],
Expand All @@ -38,5 +93,5 @@ export function command<
return normalize<E>(elements).reduce<unknown>(
(value, element) => element(value as never),
zero,
) as Materialize<Fold<CommandZero<N>, Normalize<E>>>;
) as Command.Result<N, E>;
}
11 changes: 8 additions & 3 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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";

Expand Down
14 changes: 11 additions & 3 deletions test/command.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down Expand Up @@ -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", () => {
Expand Down
Loading