diff --git a/README.md b/README.md index 46774b1..015be24 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,9 @@ SPONSORKIT_AFDIAN_TOKEN= ; Polar provider. ; Get your token at https://polar.sh/settings SPONSORKIT_POLAR_TOKEN= +; The name of the organization to fetch sponsorships from. +; If not set, it will fetch the sponsorships of the user. +SPONSORKIT_POLAR_ORGANIZATION= ``` > Only one provider is required to be configured. diff --git a/src/configs/env.ts b/src/configs/env.ts index e679270..cd5332d 100644 --- a/src/configs/env.ts +++ b/src/configs/env.ts @@ -35,6 +35,7 @@ export function loadEnv(): Partial { }, polar: { token: process.env.SPONSORKIT_POLAR_TOKEN || process.env.POLAR_TOKEN, + organization: process.env.SPONSORKIT_POLAR_ORGANIZATION || process.env.POLAR_ORGANIZATION, }, outputDir: process.env.SPONSORKIT_DIR, } diff --git a/src/providers/polar.ts b/src/providers/polar.ts index d6430f0..7dc2e09 100644 --- a/src/providers/polar.ts +++ b/src/providers/polar.ts @@ -4,11 +4,11 @@ import type { Provider, Sponsorship } from '../types' export const PolarProvider: Provider = { name: 'polar', fetchSponsors(config) { - return fetchPolarSponsors(config.polar?.token || config.token!) + return fetchPolarSponsors(config.polar?.token || config.token!, config.polar?.organization) }, } -export async function fetchPolarSponsors(token: string): Promise { +export async function fetchPolarSponsors(token: string, organization?: string): Promise { if (!token) throw new Error('Polar token is required') @@ -25,7 +25,13 @@ export async function fetchPolarSponsors(token: string): Promise let pages = 1 const subscriptions = [] do { - const subs = await apiFetch('/subscriptions/subscriptions/search', { params: { page } }) + const params: Record = { page } + if (organization) { + params.organization_name = organization + // Polar only supports GitHub for now + params.platform = 'github' + } + const subs = await apiFetch('/subscriptions/subscriptions/search', { params }) subscriptions.push(...subs.items) pages = subs.pagination.max_page diff --git a/src/types.ts b/src/types.ts index d8bd775..1513490 100644 --- a/src/types.ts +++ b/src/types.ts @@ -180,6 +180,12 @@ export interface ProvidersConfig { * @deprecated It's not recommended set this value directly, pass from env or use `.env` file. */ token?: string + /** + * The name of the organization to fetch sponsorships from. If not set, it will fetch the sponsorships of the user. + * + * Will read from `SPONSORKIT_POLAR_ORGANIZATION` environment variable if not set. + */ + organization?: string } }