queries.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import { apiLocation } from "../config";
  2. import { Tokenomics } from "../types";
  3. import axios from "axios";
  4. export const queryJstats = (route: string) => {
  5. const url = `${apiLocation}/${route}`;
  6. return axios
  7. .get(url)
  8. .then(({ data }) => {
  9. if (data && !data.error) return data;
  10. return console.error(`Jstats query failed: ${route}`, data);
  11. })
  12. .catch((e: any) => console.warn(`Failed to fetch ${route}: ${e.message}`));
  13. };
  14. export const getTokenomics = async (old?: Tokenomics) => {
  15. if (old?.timestamp + 300000 > new Date()) return;
  16. console.debug(`Updating tokenomics`);
  17. let { data } = await axios.get("https://status.joystream.org/status");
  18. if (!data || data.error) return;
  19. data.timestamp = new Date();
  20. return data;
  21. };
  22. export const getFAQ = async () => {
  23. const { data } = await axios.get(
  24. `https://joystreamstats.live/static/faq.json`
  25. );
  26. if (!data || data.error) return console.error(`failed to fetch from API`);
  27. return data;
  28. };
  29. // Reports
  30. export const getReports = async () => {
  31. const domain = `https://raw.githubusercontent.com/Joystream/community-repo/master/council-reports`;
  32. const apiBase = `https://api.github.com/repos/joystream/community-repo/contents/council-reports`;
  33. const urls: { [key: string]: string } = {
  34. alexandria: `${apiBase}/alexandria-testnet`,
  35. archive: `${apiBase}/archived-reports`,
  36. template: `${domain}/templates/council_report_template_v1.md`,
  37. };
  38. ["alexandria", "archive"].map((folder) => getGithubDir(urls[folder]));
  39. getGithubFile(urls.template);
  40. };
  41. const getGithubFile = async (url: string): Promise<string> => {
  42. const { data } = await axios.get(url);
  43. return data;
  44. };
  45. const getGithubDir = async (url: string) => {
  46. const { data } = await axios.get(url);
  47. data.forEach(
  48. async (o: {
  49. name: string;
  50. type: string;
  51. url: string;
  52. download_url: string;
  53. }) => {
  54. const match = o.name.match(/^(.+)\.md$/);
  55. const name = match ? match[1] : o.name;
  56. if (o.type === "file")
  57. this.saveReport(name, this.fetchGithubFile(o.download_url));
  58. else this.fetchGithubDir(o.url);
  59. }
  60. );
  61. };
  62. export const bootstrap = (save: (key: string, data: any) => {}) => {
  63. [
  64. { tokenomics: () => getTokenomics() },
  65. //{ faq: () => getFAQ() },
  66. //{ reports: () => getReports() },
  67. { members: () => queryJstats(`/v2/members`) },
  68. { proposals: () => queryJstats(`/v2/proposals`) },
  69. { posts: () => queryJstats(`/v2/posts`) },
  70. { threads: () => queryJstats(`/v2/threads`) },
  71. { categories: () => queryJstats(`/v2/categories`) },
  72. //{ providers: () => getStorageProviders() },
  73. //{ assets: () => getAssets() },
  74. ].reduce(async (promise, request) => {
  75. //promise.then(async () => {
  76. const key = Object.keys(request)[0];
  77. //console.debug(`Requesting ${key}`);
  78. const result = await request[key]();
  79. return result ? save(key, result) : [];
  80. //}, new Promise((res) => res))
  81. });
  82. };