fp/services/next/app/lib/patreon.ts

63 lines
1.6 KiB
TypeScript
Raw Normal View History

2024-01-20 16:16:14 +00:00
import { strapiUrl, patreonVideoAccessBenefitId, giteaUrl } from './constants'
2024-07-10 22:11:18 +00:00
import { IAuthData } from '@/app/components/auth';
2024-01-20 16:16:14 +00:00
export interface IPatron {
username: string;
vanityLink?: string;
}
export interface ICampaign {
pledgeSum: number;
patronCount: number;
}
export interface IMarshalledCampaign {
data: {
attributes: {
pledge_sum: number,
patron_count: number
}
}
}
export function isEntitledToPatronVideoAccess(authData: IAuthData): boolean {
if (!authData.user?.patreonBenefits) return false;
const patreonBenefits = authData.user.patreonBenefits
return (patreonBenefits.includes(patreonVideoAccessBenefitId))
}
export async function getPatrons(): Promise<IPatron[]> {
2024-08-13 21:18:01 +00:00
try {
const res = await fetch(`${strapiUrl}/api/patreon/patrons`);
const data = await res.json();
if (!data.data) throw new Error(`no patron data was available. ${JSON.stringify(data)}`);
} catch (e) {
console.error('failed to get patrons list')
console.error(e)
return [] as IPatron[]
}
2024-01-20 16:16:14 +00:00
}
export async function getCampaign(): Promise<ICampaign> {
const res = await fetch('https://www.patreon.com/api/campaigns/8012692', {
headers: {
accept: 'application/json'
},
next: {
revalidate: 43200 // 12 hour cache
}
})
const campaignData = await res.json();
const data = {
patronCount: campaignData.data.attributes.patron_count,
pledgeSum: campaignData.data.attributes.campaign_pledge_sum
}
return data
}