Skip to content

Instantly share code, notes, and snippets.

@cowboyd
Last active September 25, 2026 14:08
Show Gist options
  • Select an option

  • Save cowboyd/63e2e09f18ede4af3626b55277dace19 to your computer and use it in GitHub Desktop.

Select an option

Save cowboyd/63e2e09f18ede4af3626b55277dace19 to your computer and use it in GitHub Desktop.
Configliere transform schema
// checkpoint-options.ts
import { checkpoint, cli, command, description, name, option, schema, transform, version } from "configliere";
import type { ModelOf } from "configliere";
import { z } from "zod";
import { domain, endpoint, port, protocol } from "./schemas.ts";
export const app = command(
name("auth0"),
description("Provision and inspect Auth0 tenants."),
version("1.0.0"),
option(
name("config"),
description("JSON config path"),
cli(["--config", "-c"]),
schema(z.string()),
),
checkpoint(),
option(
name("port"),
description("server port"),
cli(["--port", "-p"]),
schema(port.optional()),
),
option(
name("domain"),
description("server domain"),
cli(["--domain"]),
schema(domain.optional()),
),
option(
name("protocol"),
description("server protocol"),
cli(["--protocol"]),
schema(protocol.optional()),
),
transform(endpoint),
option(
name("audience"),
description("Auth0 audience"),
cli(["--audience"]),
schema(z.string()),
),
);
export type Configuration = ModelOf<typeof app>;
// schemas.ts
import { z } from "zod";
export const port = z.number().int().min(1).max(65535);
export const domain = z.string();
export const protocol = z.enum(["http", "https"]);
export const endpoint = z.object({
port: port.optional(),
domain: domain.optional(),
protocol: protocol.optional(),
}).transform(({ domain, port, protocol }, context) => {
let parsed = domain === undefined ? undefined : parseDomain(domain);
if (domain !== undefined && parsed === undefined) {
context.addIssue({
code: "custom",
input: domain,
path: ["domain"],
message: `domain must be an HTTP or HTTPS host, received ${domain}`,
});
}
if (
parsed?.protocol !== undefined &&
protocol !== undefined &&
parsed.protocol !== protocol
) {
context.addIssue({
code: "custom",
input: protocol,
path: ["protocol"],
message:
`domain protocol ${parsed.protocol} conflicts with protocol ${protocol}`,
});
}
if (
parsed?.port !== undefined &&
port !== undefined &&
parsed.port !== port
) {
context.addIssue({
code: "custom",
input: port,
path: ["port"],
message: `domain port ${parsed.port} conflicts with port ${port}`,
});
}
let resolvedProtocol = protocol ?? parsed?.protocol ??
(port === 80 || parsed?.port === 80 ? "http" : "https");
let resolvedPort = port ?? parsed?.port ??
(resolvedProtocol === "https" ? 443 : 80);
let host = parsed?.host ?? "localhost";
return {
port: resolvedPort,
domain: `${resolvedProtocol}://${host}:${resolvedPort}`,
protocol: resolvedProtocol,
};
});
type Protocol = "http" | "https";
type ParsedDomain = {
host: string;
port?: number;
protocol?: Protocol;
};
function parseDomain(value: string): ParsedDomain | undefined {
let hasProtocol = /^[a-z][a-z\d+.-]*:\/\//i.test(value);
let url: URL;
try {
url = new URL(hasProtocol ? value : `http://${value}`);
} catch {
return undefined;
}
if (
!["http:", "https:"].includes(url.protocol) ||
url.pathname !== "/" ||
url.search !== "" ||
url.hash !== "" ||
url.username !== "" ||
url.password !== ""
) {
return undefined;
}
return {
host: url.hostname,
port: url.port === "" ? undefined : Number(url.port),
protocol: hasProtocol
? url.protocol.slice(0, -1) as Protocol
: undefined,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment