Configuration
This page will cover some general topics for configuring this library.
Client options
name
To track spam and more, Patreon has made it a requirement to send a User-Agent
header. This library will send a user agent in the form of: {name} (library, version, {rest.userAgentAppendix})
oauth
See the oauth guide for configuration of the oauth client.
store
See the oauth guide for configuration of the store for the oauth client.
rest.emitter
Connect an event emitter to listen to rest events (such as request
, response
or ratelimit
)
import { EventEmitter } from 'node:stream'
import { PatreonCreatorClient, type RestEventMap } from 'patreon-api.ts'
const emitter = new EventEmitter<RestEventMap>()
emitter.on('request', data => {
console.log('Sent a new request to', data.url)
})
emitter.on('response', data => {
console.log('Received a response with status', data.status)
})
const client = new PatreonCreatorClient({
oauth: {
clientId: 'id',
clientSecret: 'secret',
},
rest: {
emitter,
},
})
rest.fetch
Overwrite the global fetch function. This can also be overwritten per request. This will default to the global fetch
variable on the runtime.
rest.includeAllQueries
Set the default query to return all relationships and attributes, instead of empty attributes.
import { PatreonCreatorClient } from 'patreon-api.ts'
const client = new PatreonCreatorClient({
oauth: {
clientId: 'id',
clientSecret: 'secret',
},
rest: {
includeAllQueries: true,
}
})
import { PatreonCreatorClient } from 'patreon-api.ts'
declare const enabledClient: PatreonCreatorClient<true>
declare const disabledClient: PatreonCreatorClient
const completeCampaign = await enabledClient.fetchCampaign('id')
console.log(completeCampaign.data.attributes)
const emptyCampaign = await disabledClient.fetchCampaign('id')
console.log(emptyCampaign.data.attributes)
The client has a generic boolean to indicate if this option is enabled and correctly changes the response type for all requests.
rest.retries
Retry a failed request (network lost or 5XX response) a certain amount (defaults to 3
). You can also change:
- the range of status codes that can be retried
- the backoff strategy for delaying repeated retries
rest.{timeout}
The following timeout options can be configured:
rest.timeout
: the default timeout for a request. Can be overwritten per request.rest.ratelimitTimeout
: the timeout to wait after a request is rate limited.rest.globalRequestPerSecond
: the maximum amount of request / second for this client.
Module augmentation
Patreon resources
Since the Patreon API documentation is somewhat vague for defining resources and issues are open for a few years, you can patch some resources yourself. This library will always follow the documentation, undocumented features will always be opt-in since they are not supported by Patreon.
The patching is supported by using TypeScript module augmentation for overriding fields. Please open an issue if you are using this feature for something that is not covered on this page!
// ./@types/patreon-api.d.ts
import 'patreon-api.ts'
declare module 'patreon-api.ts' {
interface CustomTypeOptions {
social_connections: Record<string, { url: string, user_id: string } | null>
}
}
The following keys can be used in CustomTypeOptions
:
Key | Resource | Default | Recommended type |
---|---|---|---|
social_connections | User | object | Record<string, { url: string, user_id: string } | null> |
An example for the configuration can be found in the CJS example.
Customization
You can also use module augmentation in this library:
- for creating custom (typed) response parsers.