forked from antfu-collective/sponsorkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Polar.sh provider (antfu-collective#66)
- Loading branch information
1 parent
e729cb5
commit 6fb2203
Showing
5 changed files
with
94 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { ofetch } from 'ofetch' | ||
import type { Provider, Sponsorship } from '../types' | ||
|
||
export const PolarProvider: Provider = { | ||
name: 'polar', | ||
fetchSponsors(config) { | ||
return fetchPolarSponsors(config.polar?.token || config.token!) | ||
}, | ||
} | ||
|
||
export async function fetchPolarSponsors(token: string): Promise<Sponsorship[]> { | ||
if (!token) | ||
throw new Error('Polar token is required') | ||
|
||
const apiFetch = ofetch.create({ | ||
baseURL: 'https://api.polar.sh/api/v1', | ||
headers: { Authorization: `Bearer ${token}` }, | ||
}) | ||
|
||
/** | ||
* First fetch the list of all subscriptions. This is a paginated | ||
* endpoint so loop through all the pages | ||
*/ | ||
let page = 1 | ||
let pages = 1 | ||
const subscriptions = [] | ||
do { | ||
const subs = await apiFetch('/subscriptions/subscriptions/search', { params: { page } }) | ||
subscriptions.push(...subs.items) | ||
|
||
pages = subs.pagination.max_page | ||
page += 1 | ||
} while (page <= pages) | ||
|
||
return subscriptions | ||
/** | ||
* - People can subscribe for free on Polar : the price is null in this case | ||
*/ | ||
.filter(sub => !!sub.price) | ||
.map((sub) => { | ||
const isActive = sub.status === 'active' | ||
|
||
return { | ||
sponsor: { | ||
name: sub.user.public_name, | ||
avatarUrl: sub.user.avatar_url, | ||
login: sub.user.github_username, | ||
type: sub.subscription_tier.type === 'individual' ? 'User' : 'Organization', | ||
}, | ||
isOneTime: false, | ||
provider: 'polar', | ||
privacyLevel: 'PUBLIC', | ||
createdAt: new Date(sub.created_at).toISOString(), | ||
tierName: isActive ? sub.subscription_tier.name : undefined, | ||
monthlyDollars: isActive ? sub.sub.price.price_amount / 100 : -1, | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters