Rocon Website

SearchRouteBuilder

type SearchRouteBuilder<
  ActionResult,
  WildcardFlag extends WildcardFlagType,
  Match
>

The Search route builder is a route builder that defines one search query param (?key=value pair).

A Search route builder always holds one route record that represents a route with the specified query parameter.

Initiaization

Rocon.Search<Key extends string, IsOptional extends boolean>(
  matchKey: Key,
  options?: SearchRouteBuilderOptions<IsOptional>
): SearchRouteBuilder<...>

type SearchRouteBuilderOptions<IsOptional extends boolean> = {
  searchKey?: string;
  optional?: IsOptional;
};

Creates a new instance of Search route builder. It has one route defined from first which has no action at first. To define a route with action, use the action method.

Example

// Creates a route record which represents /?foo=value
const builder = Rocon.Search("foo");
// Creates a route record which represents /?foo=value
// where foo is optional // and `value` is stored
// in match object with key "fooValue"
const builder2 = Rocon.Search("fooValue", {
  searchKey: "foo",
  optional: true
});

Example 2

// Attach a Search route builder to a Path route builder
// to create a route for /foo?key=value
const builder = Rocon.Path()
  .route("foo")
  ._.foo.attach(Rocon.Search("key"));

builder.action(func)

action(
  action: ActionType<ActionResult, Match>
): SearchRouteBuilder<...>

Returns a new Search route builder whose single route has an action associated to it.

Example

// Create a Search route buillder for /?foo=value.
// By accessing /?foo=abcde you see "The value of foo is abcde"
const fooSearchRoute = Rocon.Search("foo")
  .action(({ foo }) => <p>The value of foo is {foo}</p>);

builder.attach(otherBuilder)

attach: AttachFunction<ActionResult, Match>

Alias of builder.route.attach(otherBuilder). Attaches otherBuilder to this builder's route and returns otherBuilder.

Example

// By attaching one Search route builder to another, you can
// create a builder for /?foo=123&bar=456
const foobarRoute = Rocon.Search("foo")
  .attach(Rocon.Search("bar"))
  .action(({ foo, bar }) => <p>Foo is {foo} and bar is {bar}</p>);

builder.route

The route record defined by this Search route builder.