Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/providers/patreon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export async function fetchPatreonSponsors(token: string | undefined): Promise<S
if (!token)
throw new Error('Patreon token is required')

// Get current authenticated user's campaign ID (Everyone has one default campaign)
// Get the authenticated creator's campaign ID using API v2.
const userData = await $fetch(
'https://www.patreon.com/api/oauth2/api/current_user/campaigns?include=null',
'https://www.patreon.com/api/oauth2/v2/campaigns',
{
method: 'GET',
headers: {
Expand Down
106 changes: 106 additions & 0 deletions tests/fixtures/patreon.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import assert from 'node:assert/strict'

const apiBase = 'https://www.patreon.com/api/oauth2/v2'
const membersPath = '/api/oauth2/v2/campaigns/1234560/members'
const nextPage = `${apiBase}/campaigns/1234560/members?page%5Bcursor%5D=second-page`
const pledgeStart = '2026-01-01T00:00:00Z'
const requests = []

function memberPage(rows, next) {
return {
data: rows.map(({ name, status, cents, gifted }) => ({
id: `member-${name}`,
type: 'member',
attributes: {
currently_entitled_amount_cents: cents,
patron_status: status,
pledge_relationship_start: pledgeStart,
lifetime_support_cents: cents,
},
relationships: {
user: { data: { id: name, type: 'user' } },
currently_entitled_tiers: { data: gifted ? [{ id: 'tier', type: 'tier' }] : [] },
},
})),
included: [
...rows.map(({ name }) => ({
id: name,
type: 'user',
attributes: {
first_name: name,
full_name: `${name} Patron`,
image_url: `https://example.com/${name}.png`,
url: `https://www.patreon.com/${name}`,
},
})),
{ id: 'tier', type: 'tier', attributes: { amount_cents: 1000 } },
],
links: { next },
}
}

globalThis.fetch = async (request, options) => {
const url = new URL(request)
requests.push(url.href)
assert.equal(url.origin, 'https://www.patreon.com')
assert.ok(url.pathname.startsWith('/api/oauth2/v2/'), 'All Patreon requests must use API v2')
assert.equal(options.method, 'GET')
assert.equal(new Headers(options.headers).get('Authorization'), 'bearer test-token')

let response
if (url.pathname === '/api/oauth2/v2/campaigns') {
assert.equal(url.searchParams.get('include'), null, 'Do not send the v1 include=null parameter')
// API v2 returns resource IDs even when no attributes are requested.
response = { data: [{ id: '1234560', type: 'campaign', attributes: {} }] }
}
else {
assert.equal(url.pathname, membersPath)
if (url.searchParams.has('page[cursor]')) {
assert.equal(url.href, nextPage)
response = memberPage([
{ name: 'gifted', status: 'active_patron', cents: 0, gifted: true },
{ name: 'former', status: 'former_patron', cents: 500 },
{ name: 'declined', status: 'declined_patron', cents: 500 },
], null)
}
else {
assert.equal(url.searchParams.get('include'), 'user,currently_entitled_tiers')
assert.ok(url.searchParams.get('fields[member]').includes('patron_status'))
assert.ok(url.searchParams.get('fields[user]').includes('full_name'))
assert.equal(url.searchParams.get('fields[tier]'), 'amount_cents')
response = memberPage([
{ name: 'active', status: 'active_patron', cents: 599 },
{ name: 'free', status: null, cents: 0 },
], nextPage)
}
}
return new Response(JSON.stringify(response), {
headers: { 'Content-Type': 'application/json' },
})
}

const { fetchPatreonSponsors } = await import('../../src/providers/patreon.ts')
const sponsorships = await fetchPatreonSponsors('test-token')
assert.equal(requests.length, 3)
assert.equal(requests[0], `${apiBase}/campaigns`)
assert.equal(requests[2], nextPage)
assert.deepEqual(sponsorships.map(({ sponsor, monthlyDollars }) => [sponsor.login, monthlyDollars]), [
['active', 5],
['gifted', 10],
['former', -1],
['declined', -1],
])
assert.deepEqual(sponsorships[0], {
sponsor: {
avatarUrl: 'https://example.com/active.png',
login: 'active',
name: 'active Patron',
type: 'User',
linkUrl: 'https://www.patreon.com/active',
},
isOneTime: false,
monthlyDollars: 5,
privacyLevel: 'PUBLIC',
tierName: 'Patreon',
createdAt: pledgeStart,
})
11 changes: 11 additions & 0 deletions tests/patreon.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { execFileSync } from 'node:child_process'
import { resolve } from 'node:path'
import { expect, it } from '@jest/globals'

it('fetches Patreon campaigns and paginated members using API v2', () => {
// Intercept fetch before importing ofetch, using the development CLI's loader.
expect(() => execFileSync(process.execPath, [
'--import', 'tsx',
resolve('tests/fixtures/patreon.mjs'),
], { encoding: 'utf8', timeout: 10000 })).not.toThrow()
})
Loading