* Get the compiled URL string for the current route and parameters. * * @example * // with 'posts.show' route 'posts/{post}' * (new Router('posts.show', 1)).toString(); // 'https://ziggy.dev/posts/1' * * @return {String}
()
| 47 | * @return {String} |
| 48 | */ |
| 49 | toString() { |
| 50 | // Get parameters that don't correspond to any route segments to append them to the query |
| 51 | const unhandled = Object.keys(this._params) |
| 52 | .filter((key) => !this._route.parameterSegments.some(({ name }) => name === key)) |
| 53 | .filter((key) => key !== '_query') |
| 54 | .reduce((result, current) => ({ ...result, [current]: this._params[current] }), {}); |
| 55 | |
| 56 | return ( |
| 57 | this._route.compile(this._params) + |
| 58 | stringify( |
| 59 | { ...unhandled, ...this._params['_query'] }, |
| 60 | { |
| 61 | addQueryPrefix: true, |
| 62 | arrayFormat: 'indices', |
| 63 | encodeValuesOnly: true, |
| 64 | skipNulls: true, |
| 65 | encoder: (value, encoder) => |
| 66 | typeof value === 'boolean' ? Number(value) : encoder(value), |
| 67 | }, |
| 68 | ) |
| 69 | ); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Get the parameters, values, and metadata from the given URL. |