1
0
mirror of https://github.com/laurent22/joplin.git synced 2024-12-30 10:36:35 +02:00
joplin/packages/tools/utils/loadSponsors.ts

53 lines
1.3 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}`,
},
});
2024-03-17 13:59:46 +02:00
if (!userResponse.ok) {
const apiErrorMessage = await userResponse.text();
throw new Error(`Could not get user: "${ghSponsor.name}": ${apiErrorMessage}`);
}
2023-03-21 19:29:22 +02:00
const user = await userResponse.json();
ghSponsor.id = user.id;
}
return output;
};