Skip to content
This repository has been archived by the owner on Aug 3, 2024. It is now read-only.

Commit

Permalink
Use base URL for axios (#241)
Browse files Browse the repository at this point in the history
* Switch site to use axios base url

* Fix team invites

* Fix find/replace setting the wrong thing

* Fix analytics being blocking, small issues
  • Loading branch information
Geometrically authored May 28, 2021
1 parent 03cbab5 commit 5017c5a
Show file tree
Hide file tree
Showing 25 changed files with 137 additions and 209 deletions.
3 changes: 3 additions & 0 deletions app/router.scrollBehavior.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function (to, from, savedPosition) {
return savedPosition || { x: 0, y: 0 }
}
2 changes: 1 addition & 1 deletion layouts/default.vue
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ export default {
},
computed: {
authUrl() {
return `https://api.modrinth.com/api/v1/auth/init?url=${process.env.domain}${this.$route.fullPath}`
return `${this.$axios.defaults.baseURL}auth/init?url=${process.env.domain}${this.$route.fullPath}`
},
userUrl() {
return `/user/${this.$auth.user.id}`
Expand Down
3 changes: 0 additions & 3 deletions layouts/error.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ export default {
},
},
layout: 'home',
created() {
// console.log(this.error)
},
}
</script>

Expand Down
26 changes: 16 additions & 10 deletions middleware/analytics.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import axios from 'axios'
export default async function (context) {
export default function (context) {
if (context.$config.analytics.base_url == null) {
return
}
let domain = ''
if (process.server) {
domain = context.req.headers.host
Expand All @@ -8,13 +11,16 @@ export default async function (context) {
}
const url = context.$config.analytics.base_url + '/register/visit'
const path = context.route.path.split('?')[0]
try {
return await axios.post(url, {
path,
domain,
consent: false,
})
} catch (e) {
// Simply silence the issue.
}
setTimeout(() => {
axios
.post(url, {
path,
domain,
consent: false,
})
.then(() => {})
.catch((e) => {
console.error('An error occurred while registering the visit: ', e)
})
})
}
2 changes: 1 addition & 1 deletion middleware/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export default async function (context) {

if (!context.$auth.user) {
return context.redirect(
`https://api.modrinth.com/api/v1/auth/init?url=${process.env.domain}${context.route.fullPath}`
`${context.$axios.defaults.baseURL}auth/init?url=${process.env.domain}${context.route.fullPath}`
)
}
}
Expand Down
1 change: 1 addition & 0 deletions nuxt.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ export default {
** See https://axios.nuxtjs.org/options
*/
axios: {
baseURL: 'https://api.modrinth.com/api/v1/',
headers: {
common: {
Accept: 'application/json',
Expand Down
2 changes: 1 addition & 1 deletion pages/dashboard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default {
},
computed: {
authUrl() {
return `https://api.modrinth.com/api/v1/auth/init?url=${process.env.domain}${this.$route.fullPath}`
return `${this.$axios.defaults.baseURL}auth/init?url=${process.env.domain}${this.$route.fullPath}`
},
},
}
Expand Down
14 changes: 5 additions & 9 deletions pages/dashboard/follows.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,6 @@
</template>

<script>
import axios from 'axios'
import ModCard from '~/components/ui/ProjectCard'
import FollowIcon from '~/assets/images/utils/heart.svg?inline'
import FollowIllustration from '~/assets/images/illustrations/follow_illustration.svg?inline'
Expand All @@ -59,15 +57,13 @@ export default {
FollowIllustration,
},
async asyncData(data) {
const res = await axios.get(
`https://api.modrinth.com/api/v1/user/${data.$auth.user.id}/follows`,
const res = await data.$axios.get(
`user/${data.$auth.user.id}/follows`,
data.$auth.headers
)
const mods = (
await axios.get(
`https://api.modrinth.com/api/v1/mods?ids=${JSON.stringify(res.data)}`
)
await data.$axios.get(`mods?ids=${JSON.stringify(res.data)}`)
).data.sort((a, b) => a.title > b.title)
return {
Expand All @@ -76,8 +72,8 @@ export default {
},
methods: {
async unfavMod(index) {
await axios.delete(
`https://api.modrinth.com/api/v1/mod/${this.mods[index].id}/follow`,
await this.$axios.delete(
`mod/${this.mods[index].id}/follow`,
this.$auth.headers
)
Expand Down
2 changes: 1 addition & 1 deletion pages/dashboard/misc/revoke-token.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export default {
async logout() {
this.$cookies.set('auth-token-reset', true)
await this.$router.replace(
`https://api.modrinth.com/api/v1/auth/init?url=${process.env.domain}${this.$route.fullPath}`
`auth/init?url=${process.env.domain}${this.$route.fullPath}`
)
},
},
Expand Down
25 changes: 7 additions & 18 deletions pages/dashboard/moderation.vue
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@
</template>

<script>
import axios from 'axios'
import ModCard from '~/components/ui/ProjectCard'
import Security from '~/assets/images/illustrations/security.svg?inline'
Expand All @@ -98,19 +96,10 @@ export default {
Security,
},
async asyncData(data) {
const mods = (
await axios.get(
`https://api.modrinth.com/api/v1/moderation/mods`,
data.$auth.headers
)
).data
const mods = (await data.$axios.get(`moderation/mods`, data.$auth.headers))
.data
const reports = (
await axios.get(
`https://api.modrinth.com/api/v1/report`,
data.$auth.headers
)
).data
const reports = (await data.$axios.get(`report`, data.$auth.headers)).data
return {
mods,
Expand All @@ -119,8 +108,8 @@ export default {
},
methods: {
async changeModStatus(id, status, index) {
await axios.patch(
`https://api.modrinth.com/api/v1/mod/${id}`,
await this.$axios.patch(
`mod/${id}`,
{
status,
},
Expand All @@ -130,8 +119,8 @@ export default {
this.mods.splice(index, 1)
},
async deleteReport(index) {
await axios.delete(
`https://api.modrinth.com/api/v1/report/${this.reports[index].id}`,
await this.$axios.delete(
`report/${this.reports[index].id}`,
this.$auth.headers
)
Expand Down
13 changes: 6 additions & 7 deletions pages/dashboard/notifications.vue
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
</template>

<script>
import axios from 'axios'
import UpToDate from '~/assets/images/illustrations/up_to_date.svg?inline'
export default {
Expand All @@ -61,8 +60,8 @@ export default {
},
async asyncData(data) {
const notifications = (
await axios.get(
`https://api.modrinth.com/api/v1/user/${data.$auth.user.id}/notifications`,
await data.$axios.get(
`user/${data.$auth.user.id}/notifications`,
data.$auth.headers
)
).data.sort((a, b) => new Date(b.created) - new Date(a.created))
Expand All @@ -79,17 +78,17 @@ export default {
if (index) {
const config = {
method: notification.actions[index].action_route[0].toLowerCase(),
url: `https://api.modrinth.com/api/v1/${notification.actions[index].action_route[1]}`,
url: `${notification.actions[index].action_route[1]}`,
headers: {
Authorization: this.$auth.token,
},
}
await axios(config)
await this.$axios(config)
}
await axios.delete(
`https://api.modrinth.com/api/v1/notification/${notification.id}`,
await this.$axios.delete(
`notification/${notification.id}`,
this.$auth.headers
)
Expand Down
9 changes: 4 additions & 5 deletions pages/dashboard/projects.vue
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
</template>

<script>
import axios from 'axios'
import ModCard from '~/components/ui/ProjectCard'
import UpToDate from '~/assets/images/illustrations/up_to_date.svg?inline'
Expand All @@ -56,13 +55,13 @@ export default {
UpToDate,
},
async asyncData(data) {
let res = await axios.get(
`https://api.modrinth.com/api/v1/user/${data.$auth.user.id}/mods`,
let res = await data.$axios.get(
`user/${data.$auth.user.id}/mods`,
data.$auth.headers
)
res = await axios.get(
`https://api.modrinth.com/api/v1/mods?ids=${JSON.stringify(res.data)}`,
res = await data.$axios.get(
`mods?ids=${JSON.stringify(res.data)}`,
data.$auth.headers
)
Expand Down
9 changes: 4 additions & 5 deletions pages/dashboard/settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@
</template>

<script>
import axios from 'axios'
import ConfirmPopup from '~/components/ui/ConfirmPopup'
export default {
Expand Down Expand Up @@ -167,8 +166,8 @@ export default {
bio: this.bio,
}
await axios.patch(
`https://api.modrinth.com/api/v1/user/${this.$auth.user.id}`,
await this.$axios.patch(
`user/${this.$auth.user.id}`,
data,
this.$auth.headers
)
Expand All @@ -191,8 +190,8 @@ export default {
this.$nuxt.$loading.start()
try {
await axios.delete(
`https://api.modrinth.com/api/v1/user/${this.$auth.user.id}`,
await this.$axios.delete(
`user/${this.$auth.user.id}`,
this.$auth.headers
)
} catch (err) {
Expand Down
43 changes: 16 additions & 27 deletions pages/mod/_id.vue
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@
:featured-versions="featuredVersions"
:members="members"
:current-member="currentMember"
:all-members="allMembers"
:link-bar.sync="linkBar"
/>
</div>
Expand Down Expand Up @@ -352,7 +353,6 @@
</template>

<script>
import axios from 'axios'
import Categories from '~/components/ui/search/Categories'
import MFooter from '~/components/layout/MFooter'
Expand Down Expand Up @@ -398,33 +398,26 @@ export default {
async asyncData(data) {
try {
const mod = (
await axios.get(
`https://api.modrinth.com/api/v1/mod/${data.params.id}`,
data.$auth.headers
)
await data.$axios.get(`mod/${data.params.id}`, data.$auth.headers)
).data
const [members, versions, featuredVersions, userFollows] = (
await Promise.all([
axios.get(`https://api.modrinth.com/api/v1/team/${mod.team}/members`),
axios.get(`https://api.modrinth.com/api/v1/mod/${mod.id}/version`),
axios.get(
`https://api.modrinth.com/api/v1/mod/${mod.id}/version?featured=true`
),
axios.get(
data.$axios.get(`team/${mod.team}/members`, data.$auth.headers),
data.$axios.get(`mod/${mod.id}/version`),
data.$axios.get(`mod/${mod.id}/version?featured=true`),
data.$axios.get(
data.$auth.user
? `https://api.modrinth.com/api/v1/user/${data.$auth.user.id}/follows`
? `user/${data.$auth.user.id}/follows`
: `https://api.modrinth.com`,
data.$auth.headers
),
])
).map((it) => it.data)
const users = (
await axios.get(
`https://api.modrinth.com/api/v1/users?ids=${JSON.stringify(
members.map((it) => it.user_id)
)}`,
await data.$axios.get(
`users?ids=${JSON.stringify(members.map((it) => it.user_id))}`,
data.$auth.headers
)
).data
Expand All @@ -440,14 +433,15 @@ export default {
: null
if (mod.body_url && !mod.body) {
mod.body = (await axios.get(mod.body_url)).data
mod.body = (await data.$axios.get(mod.body_url)).data
}
return {
mod,
versions,
featuredVersions,
members,
members: members.filter((x) => x.accepted),
allMembers: members,
currentMember,
userFollows: userFollows.name ? null : userFollows,
linkBar: [],
Expand Down Expand Up @@ -477,29 +471,24 @@ export default {
return file
},
async downloadFile(hash, url) {
await axios.get(
`https://api.modrinth.com/api/v1/version_file/${hash}/download`
)
await this.$axios.get(`version_file/${hash}/download`)
const elem = document.createElement('a')
elem.download = hash
elem.href = url
elem.click()
},
async followMod() {
await axios.post(
`https://api.modrinth.com/api/v1/mod/${this.mod.id}/follow`,
await this.$axios.post(
`mod/${this.mod.id}/follow`,
{},
this.$auth.headers
)
this.userFollows.push(this.mod.id)
},
async unfollowMod() {
await axios.delete(
`https://api.modrinth.com/api/v1/mod/${this.mod.id}/follow`,
this.$auth.headers
)
await this.$axios.delete(`mod/${this.mod.id}/follow`, this.$auth.headers)
this.userFollows.splice(this.userFollows.indexOf(this.mod.id), 1)
},
Expand Down
Loading

0 comments on commit 5017c5a

Please sign in to comment.