This repository was archived by the owner on Jan 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetchers.js
More file actions
53 lines (47 loc) · 1.28 KB
/
fetchers.js
File metadata and controls
53 lines (47 loc) · 1.28 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
import dotenv from "dotenv";
import fetch from "node-fetch";
dotenv.config();
const DOMAIN = "https://patreon.com";
const { CLIENT_ID, CLIENT_SECRET, REDIRECT_URL } = process.env;
function tokenRequest(body) {
return fetch(`${DOMAIN}/api/oauth2/token`, {
method: "POST",
headers: { "Content-Type": "application/x-www-form-urlencoded" },
body: new URLSearchParams({
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
...body,
}).toString(),
});
}
export async function getMembership(token) {
const url = `${DOMAIN}/api/oauth2/v2/identity?include=memberships&fields[member]=patron_status`;
const response = await fetch(encodeURI(url), {
headers: { Authorization: `Bearer ${token}` },
});
return {
status: response.status,
...(await response.json()),
};
}
export async function getTokens(code) {
const response = await tokenRequest({
code: code,
grant_type: "authorization_code",
redirect_uri: REDIRECT_URL,
});
return {
status: response.status,
...(await response.json()),
};
}
export async function refreshTokens(refreshToken) {
const response = await tokenRequest({
grant_type: "refresh_token",
refresh_token: refreshToken,
});
return {
status: response.status,
...(await response.json()),
};
}