Skip to content

Commit 13e7019

Browse files
committed
Rework API a bit and do some minor cleanup
1 parent 160f300 commit 13e7019

File tree

2 files changed

+40
-38
lines changed

2 files changed

+40
-38
lines changed

lib/Cro/HTTP/Router.pm6

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1354,17 +1354,31 @@ module Cro::HTTP::Router {
13541354
}
13551355
}
13561356

1357-
sub make-link($route-name, *@args, *%args) is export {
1357+
sub rel-link($route-name, *@params, *%params) is export {
1358+
with get-link($route-name, 'rel-link') {
1359+
return $_.relative(|@params, |%params);
1360+
}
1361+
"";
1362+
}
1363+
1364+
sub abs-link($route-name, *@params, *%params) is export {
1365+
with get-link($route-name, 'abs-link') {
1366+
return $_.absolute(|@params, |%params);
1367+
}
1368+
"";
1369+
}
1370+
1371+
my sub get-link($route-name, $sub-name) {
13581372
my $maker = router-plugin-get-configs($link-plugin);
13591373
my @options;
13601374
for @$maker -> $links {
13611375
with $links.link-generators{$route-name} {
1362-
return $_(|@args, |%args);
1376+
return $_;
13631377
}
13641378
@options.push: |$links.link-generators.keys;
13651379
}
1366-
warn "Called the make-link subroutine with $route-name but no such route defined, options are: @options.join('/')";
1367-
"";
1380+
warn "Called the $sub-name subroutine with $route-name but no such route defined, options are: @options.join('/')";
1381+
Nil;
13681382
}
13691383

13701384
#| Register a router plugin. The provided ID is for debugging purposes.

t/http-router-named-urls.t

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,9 @@ test-route-urls route :name<main->, {
2424
};
2525
}
2626

27-
{
28-
my $*CRO-ROOT-URL = 'https://foobar.com';
29-
test-route-urls route {
30-
get :name<home>, -> {
31-
is make-link('home'), '/', 'Basic call of a generator is correct';
32-
is make-link('home').relative, '', 'Relative URL is correct';
33-
is make-link('home').absolute, '/', 'Absolute URL is correct';
34-
is make-link('home').url, 'https://foobar.com/', 'CRO-ROOT-URL was used';
35-
};
36-
}
37-
}
38-
3927
test-route-urls route :name<main>, {
4028
get :name<home>, -> {
41-
is make-link('main.home'), '/', 'Basic call of a generator by a qualified name is correct';
29+
is abs-link('main.home'), '/', 'Basic call of a generator by a qualified name is correct';
4230
};
4331
}
4432

@@ -58,53 +46,53 @@ throws-like {
5846

5947
test-route-urls route {
6048
get -> {
61-
is make-link('hello', 'world'), '/hello/world', 'URL is generated correctly';
62-
throws-like { make-link('hello') }, Exception, message => "Not enough arguments";
63-
throws-like { make-link('hello', 'a', 'b') }, Exception, message => "Extraneous arguments";
49+
is abs-link('hello', 'world'), '/hello/world', 'URL is generated correctly';
50+
throws-like { abs-link('hello') }, Exception, message => "Not enough arguments";
51+
throws-like { abs-link('hello', 'a', 'b') }, Exception, message => "Extraneous arguments";
6452
}
6553

6654
get :name<hello>, -> 'hello', $name {};
6755
}
6856

6957
test-route-urls route {
7058
get :name<hello>, -> :$a, :$b {
71-
is make-link('hello', :a(1), :b(2)), '/?a=1&b=2';
72-
is make-link('hello', :a(1)), '/?a=1';
73-
is make-link('hello', :b(2)), '/?b=2';
74-
throws-like { make-link('hello', 1) }, Exception, message => "Extraneous arguments";
75-
throws-like { make-link('hello', :c(3)) }, Exception, message => "Extraneous named arguments: c.";
76-
throws-like { make-link('hello', :a(1), :c(3)) }, Exception, message => "Extraneous named arguments: c.";
59+
is abs-link('hello', :a(1), :b(2)), '/?a=1&b=2';
60+
is abs-link('hello', :a(1)), '/?a=1';
61+
is abs-link('hello', :b(2)), '/?b=2';
62+
throws-like { abs-link('hello', 1) }, Exception, message => "Extraneous arguments";
63+
throws-like { abs-link('hello', :c(3)) }, Exception, message => "Extraneous named arguments: c.";
64+
throws-like { abs-link('hello', :a(1), :c(3)) }, Exception, message => "Extraneous named arguments: c.";
7765
};
7866
}
7967

8068
test-route-urls route {
8169
get -> {
82-
is make-link('hello', :a(1), :b(2)), '/?a=1&b=2';
83-
throws-like { make-link('hello', :a(1)) }, Exception, message => "Missing named arguments: b.";
84-
throws-like { make-link('hello', :b(2)) }, Exception, message => "Missing named arguments: a.";
85-
throws-like { make-link('hello', 1) }, Exception, message => "Extraneous arguments";
86-
throws-like { make-link('hello', :c(3)) }, Exception, message => "Missing named arguments: a, b. Extraneous named arguments: c.";
87-
throws-like { make-link('hello', :a(1), :c(3)) }, Exception, message => "Missing named arguments: b. Extraneous named arguments: c.";
70+
is abs-link('hello', :a(1), :b(2)), '/?a=1&b=2';
71+
throws-like { abs-link('hello', :a(1)) }, Exception, message => "Missing named arguments: b.";
72+
throws-like { abs-link('hello', :b(2)) }, Exception, message => "Missing named arguments: a.";
73+
throws-like { abs-link('hello', 1) }, Exception, message => "Extraneous arguments";
74+
throws-like { abs-link('hello', :c(3)) }, Exception, message => "Missing named arguments: a, b. Extraneous named arguments: c.";
75+
throws-like { abs-link('hello', :a(1), :c(3)) }, Exception, message => "Missing named arguments: b. Extraneous named arguments: c.";
8876
}
8977

9078
get :name<hello>, -> :$a!, :$b! {};
9179
}
9280

9381
test-route-urls route {
9482
get -> {
95-
is make-link('css'), '/css';
96-
is make-link('css', 'x', 'y', 'z'), '/css/x/y/z';
83+
is abs-link('css'), '/css';
84+
is abs-link('css', 'x', 'y', 'z'), '/css/x/y/z';
9785
}
9886

9987
get :name<css>, -> 'css', +a { };
10088
}
10189

10290
test-route-urls route {
10391
get -> {
104-
is make-link('css'), '/', 'Splat with no args at all';
105-
is make-link('css', 'x', 'y', 'z'), '/x/y/z', 'Splat with no named args';
106-
is make-link('css', :a(1), :b(2), :c(3)), '/?a=1&b=2&c=3', 'Splat with no pos args';
107-
is make-link('css', 'x', 'y', 'z', :a(1), :b(2), :où('Ÿ')), '/x/y/z?a=1&b=2&o%C3%B9=%C5%B8', 'Splat with both types of args';
92+
is abs-link('css'), '/', 'Splat with no args at all';
93+
is abs-link('css', 'x', 'y', 'z'), '/x/y/z', 'Splat with no named args';
94+
is abs-link('css', :a(1), :b(2), :c(3)), '/?a=1&b=2&c=3', 'Splat with no pos args';
95+
is abs-link('css', 'x', 'y', 'z', :a(1), :b(2), :où('Ÿ')), '/x/y/z?a=1&b=2&o%C3%B9=%C5%B8', 'Splat with both types of args';
10896
}
10997

11098
get :name<css>, -> *@a, *%b { };

0 commit comments

Comments
 (0)