-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathapi.js
More file actions
69 lines (61 loc) · 1.43 KB
/
api.js
File metadata and controls
69 lines (61 loc) · 1.43 KB
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
69
import { runGraphQL, webAppConnectHandlersUse } from "meteor/vulcan:core";
import express from "express";
import get from "lodash/get";
import { postStatus } from "../../modules/data";
const query = `
query apiPostsQuery($input: MultiPostInput) {
posts(input: $input) {
results {
# posts
_id
title
url
credit
domain
postedAt
htmlBody
isSponsored
# categories
categories {
...CategoryFragment
}
}
}
}
`;
export const getJSON = async (input) => {
const results = await runGraphQL(query, { input }, {}, { useCache: true, key: 'api' });
const posts = get(results, "data.posts.results", []);
const apiItems = posts.map((post) => {
const { title, url, credit, postedAt, _id, htmlBody, domain } = post;
const item = {
title,
author: credit,
twitterName: credit,
date: postedAt,
url,
guid: _id,
body: htmlBody,
domain,
};
return item;
});
return JSON.stringify(apiItems);
};
const app = express();
app.get("/api/:limit?", async function (req, res) {
let { limit = 20 } = req.params;
if (limit > 200) {
limit = 200;
}
const input = {
filter: { status: { _eq: postStatus.published } },
sort: { postedAt: "desc" },
limit,
};
res.status(200).send(await getJSON(input));
});
webAppConnectHandlersUse(Meteor.bindEnvironment(app), {
name: "api_endpoint",
order: 200,
});