Skip to content

Commit

Permalink
Merge branch 'develop' into jbk/language
Browse files Browse the repository at this point in the history
  • Loading branch information
bakerkretzmar committed Jun 26, 2020
2 parents 6c9ffee + d4d8735 commit 7350264
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 58 deletions.
54 changes: 54 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
name: Bug report
about: Report an issue to help us improve Ziggy
title: ''
labels: needs triage
assignees: ''

---

### Description

<!-- Provide a clear and concise description of the current behaviour and what the bug is. -->

### Expected behavior

<!-- Provide a clear and concise description of what you expected to happen. -->

### Environment

<!-- Include ALL of the information below: -->

- **Laravel version**:
- **Ziggy version**:

**Related routes**:

<!-- The route with the issue, from your Laravel routes file. E.g.: -->

```php
Route::get('/', 'HomeController')->name('home');
```

**`Ziggy.namedRoutes`**:

<!-- In your browser console/devtools, run `Ziggy.namedRoutes` and paste the result here. E.g.: -->

```js
{
home: {
uri: '/',
methods: ['GET', 'HEAD'],
},
// ...
}
```

**Ziggy call**:

<!-- Show where and how you're using Ziggy when this bug occurs. E.g.: -->

```js
// ApiClient is a wrapper around axios
ApiClient.get(route('home', { lang: 'en' }));
```
20 changes: 20 additions & 0 deletions .github/ISSUE_TEMPLATE/feature_request.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
name: Feature request
about: Suggest a new feature for Ziggy
title: ''
labels: enhancement, needs triage
assignees: ''

---

### Description

<!-- Is your request related to a problem? If so, please describe the problem. E.g.: I'm always frustrated when [...] -->

### Suggestion

<!-- Tell us about the solution you'd like, with a clear and concise description of what you want to happen. -->

### Alternatives

<!-- Describe alternative solutions and approaches you considered, if any. -->
36 changes: 0 additions & 36 deletions .github/issue_template.md

This file was deleted.

55 changes: 55 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Changelog

All notable changes to this project will be documented in this file.

This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html), and the format of this changelog is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [Unreleased]

Breaking changes are marked with ⚠️.

**Added**

- Document the `check()` method ([#294](https://github.com/tightenco/ziggy/pull/294)) and how to install and use Ziggy via `npm` and over a CDN ([#299](https://github.com/tightenco/ziggy/pull/299))

**Changed**

- ⚠️ Update `ziggy:generate` output path for Laravel 5.7+ `resources` directory structure, thanks [@Somethingideally](https://github.com/Somethingideally)! ([#269](https://github.com/tightenco/ziggy/pull/269))
- ⚠️ Update automatic `id` parameter detection to check for higher priority named route parameters and allow passing `id` as a query parameter ([#301](https://github.com/tightenco/ziggy/pull/301))

**Fixed**

- Fix automatic `id` parameter detection by also excluding routes with an _optional_ `id` parameter (`{id?}`), thanks [@Livijn](https://github.com/Livijn)! ([#263](https://github.com/tightenco/ziggy/pull/263))
- Fix port not being added to URL for routes with subdomains ([#293](https://github.com/tightenco/ziggy/pull/293))
- Fix getting parameters of routes in apps installed in subfolders ([#302](https://github.com/tightenco/ziggy/pull/302))

## [0.9.4] - 2020-06-05

**Fixed**

- Fix escaping of `.` characters in the `current()` method, thanks [@davejamesmiller](https://github.com/davejamesmiller)! ([#296](https://github.com/tightenco/ziggy/pull/296))

## [0.9.3] - 2020-05-08

**Added**

- Add support for passing a CSP `nonce` attribute to the `@routes` Blade directive to be set on the script tag, thanks [@tminich](https://github.com/tminich)! (#287)

**Changed**

- Improve support for using Ziggy with server-side rendering, thanks [@emielmolenaar](https://github.com/emielmolenaar)! ([#260](https://github.com/tightenco/ziggy/pull/260))
- Avoid resolving the Blade compiler unless necessary, thanks [@axlon](https://github.com/axlon)! ([#267](https://github.com/tightenco/ziggy/pull/267))
- Use `dist/js/route.js` as the npm package's main target, thanks [@ankurk91](https://github.com/ankurk91) and [@benallfree](https://github.com/benallfree)! ([#276](https://github.com/tightenco/ziggy/pull/276))
- Readme and quality-of-life improvements ([#289](https://github.com/tightenco/ziggy/pull/289))

**Fixed**

- Ensure Ziggy's assets are always generated in the correct location ([#290](https://github.com/tightenco/ziggy/pull/290))

---

For previous changes see the [Releases](https://github.com/tightenco/ziggy/releases) page.

[Unreleased]: https://github.com/tightenco/ziggy/compare/0.9.4...HEAD
[0.9.4]: https://github.com/tightenco/ziggy/compare/0.9.3...0.9.4
[0.9.3]: https://github.com/tightenco/ziggy/compare/v0.9.2...0.9.3
3 changes: 2 additions & 1 deletion dist/js/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -1250,7 +1250,8 @@ var route_Router = /*#__PURE__*/function (_String) {
key: "params",
get: function get() {
var namedRoute = this.ziggy.namedRoutes[this.current()];
return _extends(this.extractParams(window.location.hostname, namedRoute.domain || '', '.'), this.extractParams(window.location.pathname.slice(1), namedRoute.uri, '/'));
var pathname = window.location.pathname.replace(this.ziggy.baseUrl.split('://')[1].split('/')[1], '').replace(/^\/+/, '');
return _extends(this.extractParams(window.location.hostname, namedRoute.domain || '', '.'), this.extractParams(pathname, namedRoute.uri, '/'));
}
}]);

Expand Down
2 changes: 1 addition & 1 deletion dist/js/route.min.js

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
"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.",
"keywords": ["laravel", "routes", "ziggy"],
"keywords": [
"laravel",
"routes",
"ziggy"
],
"module": "src/js/route.js",
"main": "dist/js/route.js",
"browser": "dist/js/route.min.js",
Expand Down
32 changes: 17 additions & 15 deletions src/js/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,6 @@ class Router extends String {
// If you passed in a string or integer, wrap it in an array
params = typeof params !== 'object' ? [params] : params;

// If the tags object contains an ID and there isn't an ID param in the
// url template, they probably passed in a single model object and we should
// wrap this in an array. This could be slightly dangerous and I want to find
// a better solution for this rare case.

if (
params.hasOwnProperty('id') &&
this.template.indexOf('{id}') === -1 &&
this.template.indexOf('{id?}') === -1
) {
params = [params.id];
}

this.numericParamIndices = Array.isArray(params);
return Object.assign({}, params);
}
Expand Down Expand Up @@ -80,7 +67,18 @@ class Router extends String {
delete this.urlParams[keyName];
}

// The value is null or defined; is this param
// If a value wasn't provided for this named parameter explicitly,
// but the object that was passed contains an ID, that object
// was probably a model, so we use the ID.
// Note that we are not explicitly ensuring here that the template
// doesn't have an ID param (`this.template.indexOf('{id}') == -1`)
// because we don't need to - if it does, we won't get this far.
if (!tagValue && !this.urlParams[keyName] && this.urlParams['id']) {
tagValue = this.urlParams['id']
delete this.urlParams['id'];
}

// The value is null or undefined; is this param
// optional or not
if (tagValue == null) {
if (tag.indexOf('?') === -1) {
Expand Down Expand Up @@ -213,14 +211,18 @@ class Router extends String {
get params() {
const namedRoute = this.ziggy.namedRoutes[this.current()];

let pathname = window.location.pathname
.replace(this.ziggy.baseUrl.split('://')[1].split('/')[1], '')
.replace(/^\/+/, '');

return Object.assign(
this.extractParams(
window.location.hostname,
namedRoute.domain || '',
'.'
),
this.extractParams(
window.location.pathname.slice(1),
pathname,
namedRoute.uri,
'/'
)
Expand Down
51 changes: 47 additions & 4 deletions tests/js/test.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ global.Ziggy = {
uri: 'optionalId/{type}/{id?}',
methods: ['GET', 'HEAD'],
domain: null
}
},
'products.show': {
uri: '{country?}/{language?}/products/{id}',
methods: ['GET', 'HEAD'],
domain: null
},
},
baseUrl: 'http://myapp.dev/',
baseProtocol: 'http',
Expand All @@ -107,6 +112,29 @@ describe('route()', function() {
assert.equal('http://myapp.dev/posts', route('posts.index'));
});

it('Can handle routing for apps in a subfolder', function() {
let orgBaseUrl = Ziggy.baseUrl;
let orgBaseDomain = Ziggy.baseDomain;
let orgWindow = global.window;

global.Ziggy.baseUrl = 'http://domain.com/subfolder/';
global.Ziggy.baseDomain = 'domain.com';

global.window = {
location: {
href: 'http://domain.com/subfolder/ph/en/products/4',
hostname: 'domain.com',
pathname: '/subfolder/ph/en/products/4',
},
};

assert.deepStrictEqual(route().params, { country: 'ph', language: 'en', id: '4' });

global.Ziggy.baseUrl = orgBaseUrl;
global.Ziggy.baseDomain = orgBaseDomain;
global.window = orgWindow;
});

it('Should return URL when run without params on a route without params , with default params', function() {
assert.equal(
'http://myapp.dev/en/posts',
Expand Down Expand Up @@ -474,16 +502,28 @@ describe('route()', function() {
page: 2
})
);

assert.equal(
route('events.venues.show', {
id: 2,
event: 1,
venue: 2,
search: 'rogers',
}),
'http://myapp.dev/events/1/venues/2?id=2&search=rogers'
);
});

it('Should accept queryString params as keyed values in withQuery object', function() {
let router = route('events.venues.show', [1, 2]).withQuery({
search: 'rogers',
page: 2
page: 2,
id: 20,
});

assert.equal(
router,
'http://myapp.dev/events/1/venues/2?search=rogers&page=2'
'http://myapp.dev/events/1/venues/2?search=rogers&page=2&id=20',
router
);
});

Expand Down Expand Up @@ -882,16 +922,19 @@ describe('route()', function() {
});

it('Should combine dynamic params from the domain and the URI', function() {
global.window.location.href = 'http://tighten.myapp.dev/users/1';
global.window.location.hostname = 'tighten.myapp.dev';
global.window.location.pathname = '/users/1';

assert.deepStrictEqual(route().params, { team: 'tighten', id: '1' });

global.window.location.href = 'http://' + global.Ziggy.baseDomain + '/posts/1';
global.window.location.hostname = global.Ziggy.baseDomain;
global.window.location.pathname = '/posts/1';

assert.deepStrictEqual(route().params, { post: '1' });

global.window.location.href = 'http://myapp.dev/events/1/venues/2';
global.window.location.pathname = '/events/1/venues/2';

assert.deepStrictEqual(route().params, { event: '1', venue: '2' });
Expand Down

0 comments on commit 7350264

Please sign in to comment.