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 d2dbab4155
commit 12a7416ebf
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> { fetchAuthURLs(): Promise<void> | undefined {
return fetch('config-editor/auth-urls', this.$root?.fetchOpts) return this.$root?.fetchJSON('config-editor/auth-urls')
.then((resp: Response) => this.$root?.parseResponseFromJSON(resp))
.then((data: any) => { .then((data: any) => {
this.authURLs = data this.authURLs = data
}) })
}, },
fetchBotProfile(user: string): Promise<void> { fetchBotProfile(user: string): Promise<void> | undefined {
return fetch(`config-editor/user?user=${user}`, this.$root?.fetchOpts) return this.$root?.fetchJSON(`config-editor/user?user=${user}`)
.then((resp: Response) => this.$root?.parseResponseFromJSON(resp))
.then((data: any) => { .then((data: any) => {
this.botProfile = data this.botProfile = data
}) })
}, },
fetchGeneralConfig(): Promise<void> { fetchGeneralConfig(): Promise<void> | undefined {
return fetch('config-editor/general', this.$root?.fetchOpts) return this.$root?.fetchJSON('config-editor/general')
.then((resp: Response) => this.$root?.parseResponseFromJSON(resp))
.then((data: any) => { .then((data: any) => {
this.generalConfig = data this.generalConfig = data
}) })
@ -109,7 +106,7 @@ export default defineComponent({
mounted() { mounted() {
this.fetchAuthURLs() this.fetchAuthURLs()
this.fetchGeneralConfig() this.fetchGeneralConfig()
.then(() => this.fetchBotProfile(this.generalConfig.bot_name)) ?.then(() => this.fetchBotProfile(this.generalConfig.bot_name))
}, },
name: 'TwitchBotEditorBotAuth', name: 'TwitchBotEditorBotAuth',

View File

@ -31,8 +31,7 @@ export default defineComponent({
methods: { methods: {
fetchRaffleCount(): void { fetchRaffleCount(): void {
fetch('raffle/', this.$root?.fetchOpts) this.$root?.fetchJSON('raffle/')
.then((resp: Response) => this.$root?.parseResponseFromJSON(resp))
.then((data: any) => { .then((data: any) => {
this.activeRaffles = data.filter((raffle: any) => raffle.status === 'active').length this.activeRaffles = data.filter((raffle: any) => raffle.status === 'active').length
this.loading = false this.loading = false

View File

@ -41,8 +41,7 @@ export default defineComponent({
methods: { methods: {
fetchStatus(): void { fetchStatus(): void {
fetch('status/status.json?fail-status=200') this.$root?.fetchJSON('status/status.json?fail-status=200')
.then((resp: Response) => resp.json())
.then((data: any) => { .then((data: any) => {
this.status = data this.status = data
}) })

View File

@ -42,8 +42,7 @@ export default defineComponent({
methods: { methods: {
fetchGeneralConfig(): void { fetchGeneralConfig(): void {
fetch('config-editor/general', this.$root?.fetchOpts) this.$root?.fetchJSON('config-editor/general')
.then((resp: Response) => this.$root?.parseResponseFromJSON(resp))
.then((data: any) => { .then((data: any) => {
this.botScopes = data.channel_scopes[data.bot_name] || [] this.botScopes = data.channel_scopes[data.bot_name] || []
this.loading = false this.loading = false

2
src/global.d.ts vendored
View File

@ -14,6 +14,7 @@ type EditorVars = {
Version: string Version: string
} }
type FetchJSONFunction = (path: string, opts: Object = {}) => Promise<any>
type ParseResponseFunction = (resp: Response) => Promise<any> type ParseResponseFunction = (resp: Response) => Promise<any>
type TickerRegisterFunction = (id: string, func: TimerHandler, intervalMs: number) => void type TickerRegisterFunction = (id: string, func: TimerHandler, intervalMs: number) => void
type TickerUnregisterFunction = (id: string) => void type TickerUnregisterFunction = (id: string) => void
@ -31,6 +32,7 @@ declare module '@vue/runtime-core' {
// On the $root // On the $root
check403: CheckAccessFunction check403: CheckAccessFunction
fetchJSON: FetchJSONFunction
fetchOpts: RequestInit fetchOpts: RequestInit
parseResponseFromJSON: ParseResponseFunction parseResponseFromJSON: ParseResponseFunction
registerTicker: TickerRegisterFunction registerTicker: TickerRegisterFunction

View File

@ -74,6 +74,14 @@ const app = createApp({
return resp 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> { loadVars(): Promise<void | Response> {
return fetch('editor/vars.json') return fetch('editor/vars.json')
.then((resp: Response) => resp.json()) .then((resp: Response) => resp.json())