blob: c84b0054d9a9a1ecfc79f4ba4e786664b0b7ec93 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
import { ref } from 'vue'
import { defineStore } from 'pinia'
export interface Article {
source: { id: string | null; name: string }
author: string | null
title: string
description: string | null
url: string
urlToImage: string | null
publishedAt: string
content: string | null
}
export const useNewsStore = defineStore('news', () => {
const articles = ref<Article[]>([])
const loading = ref(false)
const error = ref<string | null>(null)
async function fetchTopHeadlines(params: Record<string, string> = {}) {
loading.value = true
error.value = null
const query = new URLSearchParams(params)
const response = await fetch(`/api/top-headlines?${query}`)
const data = await response.json()
if (!response.ok) {
error.value = data.message ?? 'Failed to fetch top headlines'
} else {
articles.value = data.articles
}
loading.value = false
}
async function fetchEverything(params: Record<string, string> = {}) {
loading.value = true
error.value = null
const query = new URLSearchParams(params)
const response = await fetch(`/api/everything?${query}`)
const data = await response.json()
if (!response.ok) {
error.value = data.message ?? 'Failed to fetch articles'
} else {
articles.value = data.articles
}
loading.value = false
}
return { articles, loading, error, fetchTopHeadlines, fetchEverything }
})
|