Fixed JavaScript error for GitHub organization URLs

This commit is contained in:
squidfunk
2017-05-24 16:01:21 +02:00
committed by Martin Donath
parent 056e91e9f4
commit 0e30256604
4 changed files with 28 additions and 17 deletions

File diff suppressed because one or more lines are too long

View File

@@ -149,7 +149,7 @@
{% endblock %} {% endblock %}
</div> </div>
{% block scripts %} {% block scripts %}
<script src="{{ base_url }}/assets/javascripts/application-6b4ff16fa7.js"></script> <script src="{{ base_url }}/assets/javascripts/application-fb835c1558.js"></script>
<script>app.initialize({url:{base:"{{ base_url }}"}})</script> <script>app.initialize({url:{base:"{{ base_url }}"}})</script>
{% for path in extra_javascript %} {% for path in extra_javascript %}
<script src="{{ path }}"></script> <script src="{{ path }}"></script>

View File

@@ -43,7 +43,6 @@ export default class Abstract {
const ref = (typeof el === "string") const ref = (typeof el === "string")
? document.querySelector(el) ? document.querySelector(el)
: el : el
if (!(ref instanceof HTMLAnchorElement)) if (!(ref instanceof HTMLAnchorElement))
throw new ReferenceError throw new ReferenceError
this.el_ = ref this.el_ = ref

View File

@@ -40,14 +40,17 @@ export default class GitHub extends Abstract {
constructor(el) { constructor(el) {
super(el) super(el)
/* Extract user and repository name from URL, as we have to query for all /* Extract user (and repository name) from URL, as we have to query for all
repositories, to omit 404 errors for private repositories */ repositories, to omit 404 errors for private repositories */
const [, user, name] = /^.+github\.com\/([^\/]+)\/([^\/]+).*$/ const matches = /^.+github\.com\/([^\/]+)\/?([^\/]+)?.*$/
.exec(this.base_) .exec(this.base_)
if (matches && matches.length === 3) {
const [, user, name] = matches
/* Initialize base URL and repository name */ /* Initialize base URL and repository name */
this.base_ = `https://api.github.com/users/${user}/repos` this.base_ = `https://api.github.com/users/${user}/repos`
this.name_ = name this.name_ = name
}
} }
/** /**
@@ -57,15 +60,27 @@ export default class GitHub extends Abstract {
*/ */
fetch_() { fetch_() {
return fetch(this.base_) return fetch(this.base_)
.then(response => response.json()) .then(response => response.json(), () => {})
.then(data => { .then(data => {
const repo = data.find(item => item.name === this.name_) if (!(data instanceof Array))
return repo throw new TypeError
? [
`${this.format_(repo.stargazers_count)} Stars`, /* Display number of stars and forks, if repository is given */
`${this.format_(repo.forks_count)} Forks` if (this.name_) {
const repo = data.find(item => item.name === this.name_)
return repo
? [
`${this.format_(repo.stargazers_count)} Stars`,
`${this.format_(repo.forks_count)} Forks`
]
: []
/* Display number of repositories, otherwise */
} else {
return [
`${data.length} Repositories`
] ]
: [] }
}) })
} }
} }