Skip to content

Commit

Permalink
Simplify fetch-code
Browse files Browse the repository at this point in the history
Signed-off-by: Knut Ahlers <knut@ahlers.me>
  • Loading branch information
Luzifer committed Jul 13, 2024
1 parent 6642dbd commit 35eb383
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 16 deletions.
17 changes: 7 additions & 10 deletions src/components/botauth.vue
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,22 @@ export default defineComponent({
})
},
fetchAuthURLs(): Promise<void> {
return fetch('config-editor/auth-urls', this.$root?.fetchOpts)
.then((resp: Response) => this.$root?.parseResponseFromJSON(resp))
fetchAuthURLs(): Promise<void> | undefined {
return this.$root?.fetchJSON('config-editor/auth-urls')
.then((data: any) => {
this.authURLs = data
})
},
fetchBotProfile(user: string): Promise<void> {
return fetch(`config-editor/user?user=${user}`, this.$root?.fetchOpts)
.then((resp: Response) => this.$root?.parseResponseFromJSON(resp))
fetchBotProfile(user: string): Promise<void> | undefined {
return this.$root?.fetchJSON(`config-editor/user?user=${user}`)
.then((data: any) => {
this.botProfile = data
})
},
fetchGeneralConfig(): Promise<void> {
return fetch('config-editor/general', this.$root?.fetchOpts)
.then((resp: Response) => this.$root?.parseResponseFromJSON(resp))
fetchGeneralConfig(): Promise<void> | undefined {
return this.$root?.fetchJSON('config-editor/general')
.then((data: any) => {
this.generalConfig = data
})
Expand All @@ -109,7 +106,7 @@ export default defineComponent({
mounted() {
this.fetchAuthURLs()
this.fetchGeneralConfig()
.then(() => this.fetchBotProfile(this.generalConfig.bot_name))
?.then(() => this.fetchBotProfile(this.generalConfig.bot_name))
},
name: 'TwitchBotEditorBotAuth',
Expand Down
3 changes: 1 addition & 2 deletions src/components/dashboard/activeRaffles.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ export default defineComponent({
methods: {
fetchRaffleCount(): void {
fetch('raffle/', this.$root?.fetchOpts)
.then((resp: Response) => this.$root?.parseResponseFromJSON(resp))
this.$root?.fetchJSON('raffle/')
.then((data: any) => {
this.activeRaffles = data.filter((raffle: any) => raffle.status === 'active').length
this.loading = false
Expand Down
3 changes: 1 addition & 2 deletions src/components/dashboard/healthcheck.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ export default defineComponent({
methods: {
fetchStatus(): void {
fetch('status/status.json?fail-status=200')
.then((resp: Response) => resp.json())
this.$root?.fetchJSON('status/status.json?fail-status=200')
.then((data: any) => {
this.status = data
})
Expand Down
3 changes: 1 addition & 2 deletions src/components/dashboard/scopes.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ export default defineComponent({
methods: {
fetchGeneralConfig(): void {
fetch('config-editor/general', this.$root?.fetchOpts)
.then((resp: Response) => this.$root?.parseResponseFromJSON(resp))
this.$root?.fetchJSON('config-editor/general')
.then((data: any) => {
this.botScopes = data.channel_scopes[data.bot_name] || []
this.loading = false
Expand Down
2 changes: 2 additions & 0 deletions src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ type EditorVars = {
Version: string
}

type FetchJSONFunction = (path: string, opts: Object = {}) => Promise<any>
type ParseResponseFunction = (resp: Response) => Promise<any>
type TickerRegisterFunction = (id: string, func: TimerHandler, intervalMs: number) => void
type TickerUnregisterFunction = (id: string) => void
Expand All @@ -31,6 +32,7 @@ declare module '@vue/runtime-core' {

// On the $root
check403: CheckAccessFunction
fetchJSON: FetchJSONFunction
fetchOpts: RequestInit
parseResponseFromJSON: ParseResponseFunction
registerTicker: TickerRegisterFunction
Expand Down
8 changes: 8 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ const app = createApp({
return resp
},

fetchJSON(path: string, opts: Object = {}): Promise<any> {
return fetch(path, {
...this.fetchOpts,
...opts,
})
.then((resp: Response) => this.parseResponseFromJSON(resp))
},

loadVars(): Promise<void | Response> {
return fetch('editor/vars.json')
.then((resp: Response) => resp.json())
Expand Down

0 comments on commit 35eb383

Please sign in to comment.