generateUrls

This commit is contained in:
Tomek Łaziuk
2018-06-12 23:50:05 +02:00
parent d8150f8d2f
commit da4df7b29d
4 changed files with 26 additions and 2 deletions
+14
View File
@@ -0,0 +1,14 @@
import UniversalRouter, { Params } from ".";
export interface generateUrlsOptions {
/**
* Provide a function to stringifyQueryParams option to generate URL with query string from unknown route params.
*/
stringifyQueryParams?: (params: Params) => string;
[_: string]: any;
}
/**
* In most web applications it's much simpler to just use a string for hyperlinks.
*/
export default function generateUrls(router: UniversalRouter, options?: generateUrlsOptions): (routeName: string, params?: Params) => string;
+1 -1
View File
@@ -10,7 +10,7 @@
* Each URL parameter resolves to a string.
*/
export interface Params {
[_: string]: string;
[_: string]: any;
}
/**
+2 -1
View File
@@ -20,6 +20,7 @@
},
"files": [
"index.d.ts",
"generateUrls.d.ts",
"universal-router-tests.ts"
]
}
}
@@ -1,4 +1,5 @@
import UniversalRouter from "universal-router";
import generateUrls from "universal-router/generateUrls";
new UniversalRouter(
{
@@ -63,3 +64,11 @@ new UniversalRouter({
},
],
});
const url = generateUrls(new UniversalRouter([
{ name: 'users', path: '/users' },
{ name: 'user', path: '/user/:username' },
], { baseUrl: '/base' }));
url('users'); // => '/base/users'
url('user', { username: 'john' }); // => '/base/user/john'