-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetchNews.ts
68 lines (62 loc) · 1.5 KB
/
fetchNews.ts
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { gql } from "graphql-request";
import sortNewsByImg from "./sortNewsByImg";
const fetchNews = async (keywords: string) => {
const query = gql`
query MyQuery($access_key: String!, $keywords: String) {
myQuery(
access_key: $access_key
categories: ""
countries: ""
keywords: $keywords
languages: "en"
limit: "50"
offset: ""
sort: "published_desc"
) {
data {
author
category
country
description
language
image
published_at
source
title
url
}
pagination {
count
limit
offset
total
}
}
}
`;
// fetch function with nextjs13 caching
const response = await fetch(
"https://hauppauge.stepzen.net/api/rousing-gibbon/__graphql",
{
method: "POST",
headers: {
"Content-Type": "application/json",
Authorization: `Apikey ${process.env.STEPZEN_API_KEY}`,
},
body: JSON.stringify({
query: query,
variables: {
access_key: process.env.MEDIASTACK_API_KEY,
keywords: keywords,
},
}),
}
);
// console.log("Loading new data from API", keywords);
const newsResponse = await response.json();
// Sort by images vs no images present
console.log(newsResponse.data);
const news = sortNewsByImg(newsResponse.data.myQuery);
return news;
};
export default fetchNews;