1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-15 09:04:04 +02:00
joplin/packages/tools/utils/loadSponsors.ts

49 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-03-21 19:29:22 +02:00
import { readFile } from 'fs-extra';
import { getRootDir } from '@joplin/utils';
2023-03-21 19:29:22 +02:00
import { fetchWithRetry } from '@joplin/utils/net';
2023-04-03 20:38:40 +02:00
import { githubOauthToken } from '../tool-utils';
2023-03-21 19:29:22 +02:00
export interface GithubSponsor {
name: string;
id: string;
}
export interface Sponsors {
github: GithubSponsor[];
orgs: OrgSponsor[];
}
export interface OrgSponsor {
url: string;
urlWebsite?: string;
title: string;
imageName: string;
}
export const loadSponsors = async (): Promise<Sponsors> => {
const sponsorsPath = `${await getRootDir()}/packages/tools/sponsors.json`;
2023-03-21 19:29:22 +02:00
const output: Sponsors = JSON.parse(await readFile(sponsorsPath, 'utf8'));
output.orgs = output.orgs.map(o => {
if (o.urlWebsite) o.url = o.urlWebsite;
return o;
});
2023-04-03 20:38:40 +02:00
const oauthToken = await githubOauthToken();
2023-03-21 19:29:22 +02:00
for (const ghSponsor of output.github) {
2023-04-03 20:38:40 +02:00
const userResponse = await fetchWithRetry(`https://api.github.com/users/${ghSponsor.name}`, {
headers: {
'Content-Type': 'application/json',
'Authorization': `token ${oauthToken}`,
},
});
if (!userResponse.ok) throw new Error(await userResponse.text());
2023-03-21 19:29:22 +02:00
const user = await userResponse.json();
ghSponsor.id = user.id;
}
return output;
};