Skip to content

Commit

Permalink
feat(polar): support organizations (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
robingenz authored May 11, 2024
1 parent e24077d commit ebaaafa
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 3 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
1 change: 1 addition & 0 deletions src/configs/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export function loadEnv(): Partial<SponsorkitConfig> {
},
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,
}
Expand Down
12 changes: 9 additions & 3 deletions src/providers/polar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Sponsorship[]> {
export async function fetchPolarSponsors(token: string, organization?: string): Promise<Sponsorship[]> {
if (!token)
throw new Error('Polar token is required')

Expand All @@ -25,7 +25,13 @@ export async function fetchPolarSponsors(token: string): Promise<Sponsorship[]>
let pages = 1
const subscriptions = []
do {
const subs = await apiFetch('/subscriptions/subscriptions/search', { params: { page } })
const params: Record<string, any> = { 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
Expand Down
6 changes: 6 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down

0 comments on commit ebaaafa

Please sign in to comment.