Simplify fetch-code

Signed-off-by: Knut Ahlers <knut@ahlers.me>
This commit is contained in:
Knut Ahlers 2024-07-13 13:08:37 +02:00
parent 6ff0e0008a
commit 6df45ce9fa
Signed by: luzifer
SSH key fingerprint: SHA256:/xtE5lCgiRDQr8SLxHMS92ZBlACmATUmF1crK16Ks4E
6 changed files with 20 additions and 16 deletions

View file

@ -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
})
@ -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',

View file

@ -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

View file

@ -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
})

View file

@ -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

2
src/global.d.ts vendored
View file

@ -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
@ -31,6 +32,7 @@ declare module '@vue/runtime-core' {
// On the $root
check403: CheckAccessFunction
fetchJSON: FetchJSONFunction
fetchOpts: RequestInit
parseResponseFromJSON: ParseResponseFunction
registerTicker: TickerRegisterFunction

View file

@ -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())