Skip to content

Commit

Permalink
Improve internal route and parameter parsing (#330)
Browse files Browse the repository at this point in the history
* Tokenize all route template parameter segments (e.g. `{post?}`) into descriptive objects:

- immediately extract the name of every parameter present in the route template
- check what the type of the passed parameters *is* (instead of what it *isn't*) so that we can wrap strings and integers in an array and completely ignore objects
- pre-fill default parameters immediately
- remove the numericParamIndices flag and conditionals that check for it
- remove conditionals that depend on missing parameter key names, since there now shouldn't ever be any

* Fix default parameter assignment and merging, formatting

* Refactor core parameter matching logic:

- use route model binding keys more frequently now that they're always available
- use type checks to return early for non-object parameter values
- don't modify the parameters object with `delete` during matching
- normalize single object parameters properly

* Replace UrlBuilder class with simplified getters on main Router class

- remove unreachable error thrown if the provided `name` is undefined (called from UrlBuilder which was only instantiated at all if `name` was set)
- return early from hydrateUrl if there are no template segments to replace
- error as early as possible on non-existent routes
- remove check for pre-existing hydrated URL (this never happened)
- formatting

* Formatting

* Add support for parsing query parameters out of current URL, formatting, update tests

* Re-implement current route checking with full support for partially matching params

- Remove matchUrl method (undocumented, only used internally, and pretty much useless on its own)
- Refactor some internal methods to accept a route so they can be used in any context
- Match passed params partially against current route
- Add option to match passed params exactly when necessary
- Remove normalizeParams method and replace with internal _parseParameters()
- Formatting and renaming things

* Cleanup

- extract new substituteBindings helper and replace switch statements
- extract new dehydrate helper
- add comments and jsdocs
- improve error messages
- rename and deprecate things, mostly maintaining backwards compatibility
- add class properties
- rename internal configuration object from 'ziggy' to 'config'

* Enable microbundle property mangling to reduce size and restrict internal APIs

See /~https://github.com/developit/microbundle#mangling-properties

* Formatting, errors, organization, and comments

* Remove `exact` flag from `current()`

* Add tests for errors, strings, and bindings

* :)

* Update description and tightenco → tighten

* Remove comment

* Cleanup

* Collapse everything into a simple new Route class

* Clean up formatting and comments, rename _locationURL to _dehydrate

* Check for Ziggy config on `globalThis` as a fallback

* Remove tests for removed/deprecated methods

* Parse the current URL once instead of for each route

* Make config and queryParams private

* Revert "Update description and tightenco → tighten"

This reverts commit c22342c.

* Update Changelog

* Add and improve comments, rename/reorder methods

* Add implicit test coverage for default parameters

* Allow fallback route model 'binding' of 'id' to increase backwards compatibility

* Formatting
  • Loading branch information
bakerkretzmar authored Oct 9, 2020
1 parent d178479 commit aeb1137
Show file tree
Hide file tree
Showing 12 changed files with 516 additions and 339 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ Breaking changes are marked with ⚠️.
- Document the `check()` method ([#294](/~https://github.com/tighten/ziggy/pull/294)) and how to install and use Ziggy via `npm` and over a CDN ([#299](/~https://github.com/tighten/ziggy/pull/299))
- Add support for [custom scoped route model binding](https://laravel.com/docs/7.x/routing#implicit-binding), e.g. `/users/{user}/posts/{post:slug}` ([#307](/~https://github.com/tighten/ziggy/pull/307))
- Add support for [implicit route model binding](https://laravel.com/docs/7.x/routing#implicit-binding) ([#315](/~https://github.com/tighten/ziggy/pull/315))
- Add support for passing parameters to `current()` to check against the current URL in addition to the route name ([#330](/~https://github.com/tighten/ziggy/pull/330))

**Changed**

Expand All @@ -26,10 +27,19 @@ Breaking changes are marked with ⚠️.
- Use Jest instead of Mocha for JS tests ([#309](/~https://github.com/tighten/ziggy/pull/309))
- Use [microbundle](/~https://github.com/developit/microbundle) instead of Webpack to build and distribute Ziggy ([#312](/~https://github.com/tighten/ziggy/pull/312))
- ⚠️ Default Ziggy's `baseUrl` to the value of the `APP_URL` environment variable instead of `url('/')` ([#334](/~https://github.com/tighten/ziggy/pull/334))
- ⚠️ Allow getting the route name with `current()` when the current URL has a query string ([#330](/~https://github.com/tighten/ziggy/pull/330))

**Deprecated**

- Deprecate the `with()` and `check()` methods ([#330](/~https://github.com/tighten/ziggy/pull/330))

**Removed**

- ⚠️ Remove `Route` Facade macros `Route::only()` and `Route::except()` (previously `Route::whitelist()` and `Route::blacklist()`) ([#306](/~https://github.com/tighten/ziggy/pull/306))
- ⚠️ Remove the following undocumented public properties and methods from the `Router` class returned by the `route()` function ([#330](/~https://github.com/tighten/ziggy/pull/330)):
- `name`, `absolute`, `ziggy`, `urlBuilder`, `template`, `urlParams`, `queryParams`, and `hydrated`
- `normalizeParams()`, `hydrateUrl()`, `matchUrl()`, `constructQuery()`, `extractParams()`, `parse()`, and `trimParam()`
- ⚠️ Remove the `UrlBuilder` class ([#330](/~https://github.com/tighten/ziggy/pull/330)):

**Fixed**

Expand Down
11 changes: 9 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "ziggy-js",
"version": "0.9.1",
"description": "Generates a Blade directive exporting all of your named Laravel routes. Also provides a nice route() helper function in JavaScript.",
"description": "Use your Laravel named routes in JavaScript.",
"keywords": [
"laravel",
"routes",
"ziggy"
],
"homepage": "/~https://github.com/tighten/ziggy#readme",
"homepage": "/~https://github.com/tighten/ziggy",
"bugs": "/~https://github.com/tighten/ziggy/issues",
"license": "MIT",
"authors": [
Expand All @@ -18,6 +18,10 @@
{
"name": "Jake Bathman",
"email": "jake@tighten.co"
},
{
"name": "Jacob Baker-Kretzmar",
"email": "jacob@tighten.co"
}
],
"files": [
Expand All @@ -40,6 +44,9 @@
"test": "jest",
"prepublishOnly": "npm run build"
},
"mangle": {
"regex": "^_"
},
"dependencies": {
"qs": "^6.8.0"
},
Expand Down
2 changes: 1 addition & 1 deletion src/Ziggy.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function __construct(string $group = null, string $url = null)
{
$this->group = $group;

$this->baseUrl = Str::finish($url ?? config('app.url', url('/')), '/');
$this->baseUrl = rtrim($url ?? config('app.url', url('/')), '/');

tap(parse_url($this->baseUrl), function ($url) {
$this->baseProtocol = $url['scheme'] ?? 'http';
Expand Down
40 changes: 0 additions & 40 deletions src/js/UrlBuilder.js

This file was deleted.

Loading

0 comments on commit aeb1137

Please sign in to comment.