import { Array, Effect, Record, Schema } from 'effect'; import { Url } from '../url/index.js'; declare const ParseError_base: new = {}>(args: import("effect/Types").VoidIfEmpty<{ readonly [P in keyof A as P extends "_tag" ? never : P]: A[P]; }>) => import("effect/Cause").YieldableError & { readonly _tag: "ParseError"; } & Readonly; /** * Error type for route parsing failures. * * Includes optional `expected`, `actual`, and `position` fields for * diagnostic context when a URL segment does not match. */ export declare class ParseError extends ParseError_base<{ readonly message: string; readonly expected?: string; readonly actual?: string; readonly position?: number; }> { } /** * The result of parsing: a tuple of the parsed value and remaining URL segments. */ export type ParseResult = [A, ReadonlyArray]; type PrintState = { segments: ReadonlyArray; queryParams: URLSearchParams; }; /** * A bidirectional parser that can both parse URL segments into a value * and print a value back to URL segments. */ export type Biparser = { parse: (segments: ReadonlyArray, search?: string) => Effect.Effect, ParseError>; print: (value: A, state: PrintState) => Effect.Effect; }; type BuildFn = A extends { _tag: string; } ? keyof Omit extends never ? (value?: Omit) => string : (value: Omit) => string : never; /** * A parser with a `build` method that can reconstruct URLs from parsed values. * * Created by applying `mapTo` to a `Biparser`, binding it to a tagged * type constructor so parsed values carry a discriminant tag and URLs can be * built from tag payloads. * * Routers are callable — `homeRouter()` or `personRouter({ id: 42 })` — * and also expose `.build` as an alias and `.parse` for URL matching. */ export type Router = BuildFn & { parse: (segments: ReadonlyArray, search?: string) => Effect.Effect, ParseError>; build: BuildFn; }; /** * A `Biparser` that has been terminated (e.g. by `query` or `rest`) * and cannot be extended with `slash`. */ export type TerminalParser = Biparser & { readonly __terminal: true; }; /** * A `Biparser` that can still be extended with `slash`. Terminal parsers * (`query`, `rest`) do not qualify, since nothing can follow them in * the path. */ export type ExtendableBiparser = Biparser & { readonly __terminal?: never; readonly 'Cannot use slash after a terminal parser - nothing can follow query or rest'?: never; }; /** * Creates a parser that matches an exact URL path segment. * * @example * ```ts * literal('users') // matches /users * ``` */ export declare const literal: (segment: string) => Biparser<{}>; /** * Creates a parser for a dynamic URL segment with custom parse and print functions. * * @param label - A descriptive name used in error messages. * @param parse - Converts a raw URL segment string into the parsed value. * @param print - Converts the parsed value back into a URL segment string. */ export declare const param: (label: string, parse: (segment: string) => Effect.Effect, print: (value: A) => string) => Biparser; /** * Creates a parser that captures a URL segment as a named string field. * * @example * ```ts * string('slug') // parses /hello into { slug: "hello" } * ``` */ export declare const string: (name: K) => Biparser>; /** * Creates a parser that captures a URL segment as a named integer field. * * Fails if the segment is not a valid integer. * * @example * ```ts * int('id') // parses /42 into { id: 42 } * ``` */ export declare const int: (name: K) => Biparser>; export declare const __isSingleSegment: (segment: string) => boolean; /** * Creates a parser that captures a URL segment and decodes it through an * Effect `Schema`, producing a named field of the schema's decoded type. * * Decodes the raw segment when parsing and encodes the value back when * printing, so branded ids, refined strings, and string-literal unions * round-trip through the route. The decoded type flows straight into the * route value, so a model carries `UserId` rather than a bare `string`. * * The schema's encoded form must be a single URL segment string. For shapes * that span multiple segments or live in the query string, use `rest` or * `query`. * * @example * ```ts * const UserId = S.String.pipe(S.brand('UserId')) * pipe(literal('users'), slash(schemaSegment('userId', UserId)), mapTo(UserRoute)) * // parses /users/abc into { _tag: 'UserRoute', userId: UserId.make('abc') } * ``` * * @param name - The field name the decoded value is captured under. * @param schema - A codec whose encoded form is a single URL segment string. */ export declare const schemaSegment: (name: K, schema: Schema.Codec) => Biparser>; /** * A parser that matches the root path with no remaining segments. * * Succeeds only when the URL path is exactly `/`. */ export declare const root: Biparser<{}>; /** * Creates a parser that captures all remaining URL segments as a named * non-empty array field. * * Requires at least one remaining segment. A bare prefix like `/files` * does not match; give it its own route alongside the rest route. * * A rest route also matches every URL that a more specific route under * the same prefix accepts, so in `oneOf` the specific route must come * first. * * Nothing can follow `rest` in the path, so the result is a * `TerminalParser`. It can still be extended with `query`. * * @example * ```ts * pipe(literal('files'), slash(rest('path'))) * // parses /files/documents/taxes/2024.pdf * // into { path: ['documents', 'taxes', '2024.pdf'] } * ``` */ export declare const rest: (name: K) => TerminalParser>>; /** * A parse-only parser with no print/build capabilities. * * Returned by `oneOf`, which combines multiple parsers whose print * types may differ and therefore cannot be unified into a single `Biparser`. */ export type Parser = { parse: (segments: ReadonlyArray, search?: string) => Effect.Effect, ParseError>; }; type ParserInput = Biparser | Parser; type InferParsed

= P extends Biparser ? A : P extends Parser ? A : never; /** * Combines multiple parsers, trying each in order until one matches the * entire path. * * A parser only matches when it consumes every segment, so a route never * shadows a longer route that shares its prefix. When several parsers * fully match the same URL, the first one wins. * * Returns a `Parser` (parse-only) since the union of different route * shapes cannot provide a single unified print function. */ export declare const oneOf: >(...parsers: Parsers) => Parser>; /** * Converts a `Biparser` into a `Router` by mapping parsed values to a * tagged type constructor. * * The resulting `Router` can both parse URLs into tagged route values and * build URLs from route payloads. * * @example * ```ts * pipe(literal('users'), slash(int('id')), mapTo(UserRoute)) * ``` */ export declare const mapTo: { (appRouteConstructor: { make: () => T; }): (parser: Biparser<{}>) => Router; (appRouteConstructor: { make: (data: A) => T; }): (parser: Biparser) => Router; }; /** * Composes two `Biparser`s sequentially, combining their parsed values. * * Cannot be used after a terminal parser (`query` or `rest`). * Composing with a terminal second parser yields a `TerminalParser`, * so terminality survives the composition. * * @example * ```ts * pipe(literal('users'), slash(int('id'))) // matches /users/42 * ``` */ export declare const slash: { , B extends Record>(parserB: TerminalParser): (parserA: ExtendableBiparser) => TerminalParser; , B extends Record>(parserB: Biparser): (parserA: ExtendableBiparser) => Biparser; }; /** * Adds query parameter parsing to a `Biparser` using an Effect `Schema`. * * Produces a `TerminalParser` that cannot be extended with `slash`, * since query parameters must appear at the end of a route definition. * * @example * ```ts * pipe( * literal('search'), * query(S.Struct({ q: S.String })), * mapTo(SearchRoute), * ) * ``` * * @param schema - An Effect Schema describing the expected query parameters. */ export declare const query: >(schema: Schema.Codec) => >(parser: Biparser) => TerminalParser; /** * Parses a URL against a parser, falling back to a not-found route if no * parser matches. * * @param parser - The parser (typically from `oneOf`) to attempt. * @param notFoundRouteConstructor - Constructor called with `{ path }` when * no route matches. */ export declare const parseUrlWithFallback: (parser: Parser, notFoundRouteConstructor: { make: (data: { path: string; }) => B; }) => (url: Url) => A | B; export {}; //# sourceMappingURL=parser.d.ts.map