Modules@robinpath/router
router

@robinpath/router

0.1.1Node.jsPublic

URL routing and pattern matching with support for path parameters (:param), wildcards (*), route groups, and middleware. No external dependencies.

Router

URL routing and pattern matching with support for path parameters (:param), wildcards (*), route groups, and middleware. No external dependencies.

Package: @robinpath/router | Category: Web | Type: Utility

Authentication

No authentication required. All functions are available immediately.

Use Cases

Use the router module when you need to:

  • Create a new router instance -- Use router.create to perform this operation
  • Add a route with method, path pattern, and handler -- Use router.add to perform this operation
  • Match a URL against registered routes and return the matching route -- Use router.match to perform this operation
  • Extract path parameters from a URL using a pattern -- Use router.params to perform this operation
  • Parse a URL into pathname, segments, and query parameters -- Use router.parse to perform this operation

Quick Reference

FunctionDescriptionReturns
createCreate a new router instanceobject
addAdd a route with method, path pattern, and handlerobject
matchMatch a URL against registered routes and return the matching routeobject
paramsExtract path parameters from a URL using a patternobject
parseParse a URL into pathname, segments, and query parametersobject
buildBuild a URL from a pattern and parameter valuesobject
normalizeNormalize a URL path (collapse slashes, ensure leading slash, strip trailing slash)object
isMatchTest if a URL path matches a route patternobject
groupCreate a route group with a shared prefixobject
listList all routes registered in a routerobject
removeRemove routes by method and/or pathobject
middlewareAdd a middleware function to the routerobject

Functions

create

Create a new router instance

Module: router | Returns: object -- API response.

router.create
ParameterTypeRequiredDescription
idstringNoUnique router identifier
prefixstringNoBase prefix for all routes

add

Add a route with method, path pattern, and handler

Module: router | Returns: object -- API response.

router.add
ParameterTypeRequiredDescription
idstringNoRouter identifier
methodstringNoHTTP method (default GET)
pathstringNoRoute path pattern (supports :param and *)
handleranyNoRoute handler

match

Match a URL against registered routes and return the matching route

Module: router | Returns: object -- API response.

router.match
ParameterTypeRequiredDescription
idstringNoRouter identifier
methodstringNoHTTP method
urlstringNoURL to match

params

Extract path parameters from a URL using a pattern

Module: router | Returns: object -- API response.

router.params
ParameterTypeRequiredDescription
patternstringNoRoute pattern with :param placeholders
urlstringNoURL to extract params from

parse

Parse a URL into pathname, segments, and query parameters

Module: router | Returns: object -- API response.

router.parse
ParameterTypeRequiredDescription
urlstringNoURL to parse

build

Build a URL from a pattern and parameter values

Module: router | Returns: object -- API response.

router.build
ParameterTypeRequiredDescription
patternstringNoRoute pattern with :param placeholders
paramsobjectNoParameter values to substitute
queryobjectNoQuery parameters to append

normalize

Normalize a URL path (collapse slashes, ensure leading slash, strip trailing slash)

Module: router | Returns: object -- API response.

router.normalize
ParameterTypeRequiredDescription
pathstringNoPath to normalize

isMatch

Test if a URL path matches a route pattern

Module: router | Returns: object -- API response.

router.isMatch
ParameterTypeRequiredDescription
patternstringNoRoute pattern
urlstringNoURL to test

group

Create a route group with a shared prefix

Module: router | Returns: object -- API response.

router.group
ParameterTypeRequiredDescription
idstringNoRouter identifier
prefixstringNoPrefix for all routes in the group
routesarrayNoArray of route definitions { method, path, handler }

list

List all routes registered in a router

Module: router | Returns: object -- API response.

router.list
ParameterTypeRequiredDescription
idstringNoRouter identifier

remove

Remove routes by method and/or path

Module: router | Returns: object -- API response.

router.remove
ParameterTypeRequiredDescription
idstringNoRouter identifier
methodstringNoHTTP method to filter
pathstringNoRoute path to filter

middleware

Add a middleware function to the router

Module: router | Returns: object -- API response.

router.middleware
ParameterTypeRequiredDescription
idstringNoRouter identifier
handlerstringNoMiddleware function (ctx: any) => ctx

Error Handling

All functions throw on failure. Common errors:

ErrorCause
Router id is requiredCheck the error message for details
path is requiredCheck the error message for details
url is requiredCheck the error message for details
pattern is requiredCheck the error message for details
prefix is requiredCheck the error message for details
Each route in the group must have a pathCheck the error message for details
handler must be a functionCheck the error message for details
Router "..." not foundCheck the error message for details
@desc "Create and validate result"
do
  set $result as router.create
  if $result != null
    print "Success"
  else
    print "No result"
  end
enddo

Recipes

1. List and iterate

Retrieve all items and loop through them.

@desc "List and iterate results"
do
  set $result as router.list
  each $item in $result
    print $item
  end
enddo

2. Create a new item with create

Create a new resource and capture the result.

set $result as router.create
print "Created: " + $result

3. Check before creating

List existing items and only create if needed.

@desc "List and create"
do
  set $existing as router.list
  if $existing == null
    router.create
    print "Item created"
  else
    print "Item already exists"
  end
enddo

4. Multi-step Router workflow

Chain multiple router operations together.

@desc "Create, add, and more"
do
  set $r_create as router.create
  set $r_add as router.add
  set $r_match as router.match
  print "All operations complete"
enddo

5. Safe create with validation

Check results before proceeding.

@desc "Create and validate result"
do
  set $result as router.create
  if $result != null
    print "Success: " + $result
  else
    print "Operation returned no data"
  end
enddo

Related Modules

  • json -- JSON module for complementary functionality

Versions (1)

VersionTagPublished
0.1.1latest1 months ago
Install
$ robinpath add @robinpath/router

Collaborators

Dumitru Balaban
Dumitru Balaban
@dumitru
View all @robinpath modules
Version0.1.1
LicenseMIT
Unpacked Size6.2 KB
Versions1
Weekly Downloads21
Total Downloads21
Stars0
Last Publish1 months ago
Created1 months ago

Keywords

Category

web