@@ -33,6 +33,19 @@ export interface MissingParams {
3333 query ?: MissingParam [ ] ;
3434}
3535
36+ /**
37+ * Immutable fluent builder for constructing and sending HTTP requests from the
38+ * Counterfact REPL.
39+ *
40+ * Each builder method returns a **new** `RouteBuilder` instance with the
41+ * updated field — the original is never mutated. When all required parameters
42+ * are set, call {@link send} to execute the request.
43+ *
44+ * ```ts
45+ * // Inside the REPL:
46+ * route("/pets/{petId}").method("get").path({ petId: 1 }).send();
47+ * ```
48+ */
3649export class RouteBuilder {
3750 public readonly routePath : string ;
3851
@@ -116,22 +129,47 @@ export class RouteBuilder {
116129 } ) ;
117130 }
118131
132+ /**
133+ * Returns a new builder with the HTTP method set.
134+ *
135+ * @param method - HTTP method name (case-insensitive, e.g. `"get"`, `"POST"`).
136+ */
119137 public method ( method : string ) : RouteBuilder {
120138 return this . clone ( { method : method . toUpperCase ( ) } ) ;
121139 }
122140
141+ /**
142+ * Returns a new builder with additional path parameters merged in.
143+ *
144+ * @param params - Key/value map of path variable names to values.
145+ */
123146 public path ( params : Params ) : RouteBuilder {
124147 return this . clone ( { pathParams : { ...this . _pathParams , ...params } } ) ;
125148 }
126149
150+ /**
151+ * Returns a new builder with additional query parameters merged in.
152+ *
153+ * @param params - Key/value map of query parameter names to values.
154+ */
127155 public query ( params : Params ) : RouteBuilder {
128156 return this . clone ( { queryParams : { ...this . _queryParams , ...params } } ) ;
129157 }
130158
159+ /**
160+ * Returns a new builder with additional request headers merged in.
161+ *
162+ * @param params - Key/value map of header names to values.
163+ */
131164 public headers ( params : Params ) : RouteBuilder {
132165 return this . clone ( { headerParams : { ...this . _headerParams , ...params } } ) ;
133166 }
134167
168+ /**
169+ * Returns a new builder with the request body set.
170+ *
171+ * @param body - The request body (will be serialised to JSON or sent as-is).
172+ */
135173 public body ( body : unknown ) : RouteBuilder {
136174 return this . clone ( { body } ) ;
137175 }
@@ -140,12 +178,21 @@ export class RouteBuilder {
140178 return this . _operation ;
141179 }
142180
181+ /**
182+ * Returns `true` when a method is set and no required parameters are
183+ * missing.
184+ */
143185 public ready ( ) : boolean {
144186 if ( ! this . _method ) return false ;
145187
146188 return this . missing ( ) === undefined ;
147189 }
148190
191+ /**
192+ * Returns a {@link MissingParams} object describing all required parameters
193+ * that have not yet been set, or `undefined` when nothing is missing (or
194+ * when the operation has no parameters).
195+ */
149196 public missing ( ) : MissingParams | undefined {
150197 const operation = this . getOperation ( ) ;
151198
@@ -177,6 +224,10 @@ export class RouteBuilder {
177224 return missingParams ;
178225 }
179226
227+ /**
228+ * Returns a human-readable help string describing the operation, its
229+ * parameters, and the expected responses.
230+ */
180231 public help ( ) : string {
181232 const method = this . _method ?? "[no method set]" ;
182233 const operation = this . getOperation ( ) ;
@@ -260,6 +311,13 @@ export class RouteBuilder {
260311 return lines . join ( "\n" ) ;
261312 }
262313
314+ /**
315+ * Executes the HTTP request and returns the parsed response body.
316+ *
317+ * @throws When no HTTP method has been set.
318+ * @throws When required parameters are missing.
319+ * @throws When an unsupported HTTP method is used.
320+ */
263321 public async send ( ) : Promise < unknown > {
264322 if ( ! this . _method ) {
265323 throw new Error (
@@ -389,6 +447,16 @@ export class RouteBuilder {
389447 }
390448}
391449
450+ /**
451+ * Creates a factory function that constructs a {@link RouteBuilder} for a
452+ * given route path, pre-configured with the server's host, port, and OpenAPI
453+ * document.
454+ *
455+ * @param port - The port the Counterfact server is listening on.
456+ * @param host - The server hostname (default `"localhost"`).
457+ * @param openApiDocument - Optional OpenAPI document for parameter introspection.
458+ * @returns A function `(routePath: string) => RouteBuilder`.
459+ */
392460export function createRouteFunction (
393461 port : number ,
394462 host ?: string ,
0 commit comments