Skip to content

Commit a8a5d03

Browse files
committed
add route_pattern request field
1 parent 9b9c8ea commit a8a5d03

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

docs/actions.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,14 @@ $options_table{
450450
assert @route_name == "my_page"
451451
]]}
452452
},
453+
{
454+
name = $self_ref{"route_pattern"},
455+
description = "The route pattern string that was matched during routing, if available",
456+
example = $dual_code{[[
457+
app\match "my_page", "/my-page", =>
458+
assert @route_pattern == "/my-page"
459+
]]}
460+
},
453461
{
454462
name = $self_ref{"params"},
455463
description = "A table containing all request parameters merged together, including query parameters and form-encoded parameters from the body of the request. See [Request Parameters](#request_parameters) for more details."

lapis/application.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ do
166166
do
167167
local _with_0 = r
168168
_with_0.route_name = name
169+
_with_0.route_pattern = path
169170
support.add_params(r, r.req.params_get, "GET")
170171
support.add_params(r, r.req.params_post, "POST")
171172
support.add_params(r, params, "url_params")

lapis/application.moon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ class Application
220220

221221
with r
222222
.route_name = name
223+
.route_pattern = path
223224

224225
support.add_params r, r.req.params_get, "GET"
225226
support.add_params r, r.req.params_post, "POST"
@@ -491,4 +492,3 @@ json_params = (fn) ->
491492
:capture_errors, :capture_errors_json
492493
:json_params, :assert_error, :yield_error
493494
}
494-

spec/application_spec.moon

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,6 +637,27 @@ describe "lapis.application", ->
637637
assert.has_error ->
638638
assert_request SomeApp, "/cool", method: "DELETE"
639639

640+
it "exposes matched route pattern on request", ->
641+
local named_pattern, unnamed_pattern
642+
local named_route_name, unnamed_route_name
643+
644+
class SomeApp extends lapis.Application
645+
@match "named_user", "/user/:id", =>
646+
named_pattern = @route_pattern
647+
named_route_name = @route_name
648+
649+
@match "/post/:slug", =>
650+
unnamed_pattern = @route_pattern
651+
unnamed_route_name = @route_name
652+
653+
assert_request SomeApp, "/user/123"
654+
assert.same "/user/:id", named_pattern
655+
assert.same "named_user", named_route_name
656+
657+
assert_request SomeApp, "/post/hello-world"
658+
assert.same "/post/:slug", unnamed_pattern
659+
assert.same nil, unnamed_route_name
660+
640661
describe "instancing", ->
641662
it "matchs a route", ->
642663
local res
@@ -887,4 +908,3 @@ describe "lapis.application", ->
887908
mock_app "/hello", { host: "leaf", port: 2000 }, =>
888909
@build_url "http://leafo.net"
889910

890-

0 commit comments

Comments
 (0)