Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import Octokit from "@octokit/rest";
import inbuiltPatrons from "patrons.json";
import type { Patrons, PatreonUser } from "scripts/fetchPatrons";
const github = new Octokit();
const oneHour = 60 * 60 * 1000;
const cache = {
latest: {
value: inbuiltPatrons,
timestamp: 0,
},
};
const isPatronUser = (input: unknown): input is PatreonUser => {
Iif (typeof input === "object" && input !== null) {
const obj = input as PatreonUser;
return (
obj.type === "user" &&
typeof obj.id === "string" &&
typeof obj.attributes === "object" &&
typeof obj.attributes.full_name === "string"
);
}
return false;
};
const isPatrons = (input: unknown): input is Patrons => {
Iif (input === null || typeof input !== "object") {
return false;
}
const obj = input as Record<string, unknown>;
Iif (!("goldTier" in obj) || !("silverTier" in obj)) {
return false;
}
Iif (!Array.isArray(obj.goldTier) || !Array.isArray(obj.silverTier)) {
return false;
}
Iif (
!obj.goldTier.every(isPatronUser) ||
!obj.silverTier.every(isPatronUser)
) {
return false;
}
return true;
};
export const getPatronsFromGithub = async () => {
if (process.env.NODE_ENV === "test") {
return inbuiltPatrons;
}
try {
const now = new Date().getTime();
Iif (cache.latest.timestamp > now) {
return cache.latest.value;
}
const result = await github.repos.getContents({
owner: "chrismaltby",
repo: "gb-studio",
path: "patrons.json",
ref: "develop",
});
Iif (result) {
const content = Buffer.from(result.data.content, "base64").toString();
const patrons = JSON.parse(content);
Iif (isPatrons(patrons)) {
cache.latest.value = patrons;
cache.latest.timestamp = now + oneHour;
return patrons;
}
}
} catch (e) {
console.error(e);
}
return inbuiltPatrons;
};
getPatronsFromGithub();
|