@sponsorkit/sdk
Browser client (popup or redirect sign-in, localStorage), plus every shared type and helper.
On this page
- Example
- Exports
- Classes
- Interfaces
- Type aliases
- Functions
- Constants
- Functions
- canonicalTargets()
- codeChallenge()
- createCodeVerifier()
- isSponsorkitError()
- localStorageAdapter()
- memoryStorage()
- normalizeTarget()
- parseTarget()
- resetProcessState()
- targetsFrom()
- targetsHash()
- targetsParam()
- targetToString()
- Constants
- DEFAULT_API_BASE_URL
- DEFAULT_CACHE_TTL
- DEFAULT_CHECKOUT_POLL_INTERVAL
- DEFAULT_CHECKOUT_TIMEOUT
- DEFAULT_SIGN_IN_TIMEOUT
- DEFAULT_SITE_BASE_URL
- MOCK_PRESETS
- PROVIDERS
- SPONSORKIT_ERROR_CODES
@sponsorkit/sdk: the isomorphic Sponsorkit client for browsers (popup or
redirect sign-in, localStorage). Native apps use @sponsorkit/sdk/node,
backends use @sponsorkit/sdk/server.
import { … } from "@sponsorkit/sdk";Example#
import { Sponsorkit } from "@sponsorkit/sdk";
const sk = new Sponsorkit({ github: "octocat", openCollective: "my-collective" });
const fc = await sk.getFinancialContribution();
button.onclick = () => sk.openCheckout({ frequency: "monthly", amount: 500 });Exports#
Classes#
| Name | Summary |
|---|---|
FinancialContribution | A ContributionResult with filter helpers. |
Sponsorkit | Sponsorkit client for browsers. |
SponsorkitCore | Base class with the shared client behaviour. |
SponsorkitError | The only error type thrown by the SDK. |
Interfaces#
| Name | Summary |
|---|---|
CheckoutOptions | Options for openCheckout() and checkoutUrl(). |
Contribution | A sponsorship (GitHub) or order (Open Collective), normalized. |
ContributionResult | The plain JSON result of getFinancialContribution(). |
Identity | A supporter's provider account (without its token, which never leaves Sponsorkit). |
Money | An amount of money in minor units (cents) with its ISO 4217 currency. |
Nag | Nag helper returned by Sponsorkit.nag(). |
NagDueOptions | Options for Nag.due(). |
NagOptions | Options for nag(). |
NagState | Nag state as persisted in the StorageFile, keyed by nag id. |
OpenCheckoutOptions | Options for openCheckout(). |
SignInOptions | Options for signIn(). |
SponsorEntity | Who actually pays for a contribution: the user, or an organization the user belongs to. |
SponsorkitCoreOptions | Options shared by every Sponsorkit flavour. |
SponsorkitOptions | Options for the browser client. |
SponsorkitStorage | Where the SDK keeps its per-target-set document. |
StorageFile | The JSON document stored per target set (session token, cached result and nag state). |
Supporter | The signed-in supporter. |
Target | A normalized target. |
TargetInfo | Public metadata about a target (GET /api/v1/targets). |
TargetOptions | Targets as accepted by verifySession(). |
TargetTier | A tier of a target's sponsorship listing. |
WebStorageLike | The subset of the Web Storage API used by localStorageAdapter. |
Type aliases#
| Name | Summary |
|---|---|
CheckoutResult | The outcome of openCheckout(). |
ContributionKind | Whether a contribution recurs. |
ContributionStatus | Normalized contribution status. |
Env | An environment map (process.env-shaped). |
FetchLike | A fetch-compatible function. |
Frequency | How often a contribution is charged. |
MockOption | Mock mode: a preset name, or custom contributions to report as the signed-in supporter's. |
MockPreset | The names of the built-in mock presets. |
NagPredicate | A predicate deciding whether the current supporter should see the nag. |
Provider | A contribution platform supported by Sponsorkit. |
SponsorkitErrorCode | Error codes carried by SponsorkitError. |
Functions#
| Name | Summary |
|---|---|
canonicalTargets | Deduplicates and sorts (byte-wise) a target set; at least one target, at most one per provider. |
codeChallenge | PKCE S256: base64url(sha256(ascii(verifier))). |
createCodeVerifier | A new PKCE code_verifier: 32 random bytes, base64url (43 characters). |
isSponsorkitError | True if error is a SponsorkitError (optionally with the given code). |
localStorageAdapter | Wraps a Web Storage object (default: localStorage) using the key sponsorkit:<targets_hash>. |
memoryStorage | In-memory storage. |
normalizeTarget | Normalizes and validates one target. |
parseTarget | Parses provider:account. |
resetProcessState | Forgets per-process state: nag runs already counted, the dev-check memo and the Retry-After memo. |
targetsFrom | Builds the canonical target set from { github?, openCollective? }, a target param string, or a list of target strings. |
targetsHash | targets_hash: lowercase hex SHA-256 of the UTF-8 target param (storage key). |
targetsParam | The canonical target param: github:x,open-collective:y. |
targetToString | github:<login> / open-collective:<slug>. |
Constants#
| Name | Summary |
|---|---|
DEFAULT_API_BASE_URL | The default API origin: where every /api/v1/… call goes. |
DEFAULT_CACHE_TTL | Default cacheTtl in seconds. |
DEFAULT_CHECKOUT_POLL_INTERVAL | Default checkout poll interval in seconds. |
DEFAULT_CHECKOUT_TIMEOUT | Default checkout timeout in seconds (polling phase only). |
DEFAULT_SIGN_IN_TIMEOUT | Default signInTimeout in seconds. |
DEFAULT_SITE_BASE_URL | The default site base URL: origin plus base path of the pages (/connect/, /checkout/). |
MOCK_PRESETS | Names of the built-in presets. |
PROVIDERS | All providers, in canonical order. |
SPONSORKIT_ERROR_CODES | All error codes, in the order of the wire protocol's error contract. |
Functions#
canonicalTargets()#
function canonicalTargets(targets: readonly Target[]): Target[]Deduplicates and sorts (byte-wise) a target set; at least one target, at most one per provider.
Also exported from @sponsorkit/sdk/node.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
targets | readonly Target[] | required | See Target. |
Returns Target[] (Target)
codeChallenge()#
async function codeChallenge(verifier: string): Promise<string>PKCE S256: base64url(sha256(ascii(verifier))).
Also exported from @sponsorkit/sdk/node.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
verifier | string | required |
Returns Promise<string>
createCodeVerifier()#
function createCodeVerifier(): stringA new PKCE code_verifier: 32 random bytes, base64url (43 characters).
Also exported from @sponsorkit/sdk/node.
Returns string
isSponsorkitError()#
function isSponsorkitError(error: unknown, code?: SponsorkitErrorCode): error is SponsorkitErrorTrue if error is a SponsorkitError (optionally with the given code).
Also exported from @sponsorkit/sdk/node, @sponsorkit/sdk/server.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
error | unknown | required | |
code | SponsorkitErrorCode | optional | See SponsorkitErrorCode. |
Returns error is SponsorkitError (SponsorkitError)
localStorageAdapter()#
function localStorageAdapter(storage?: WebStorageLike): SponsorkitStorageWraps a Web Storage object (default: localStorage) using the key sponsorkit:<targets_hash>.
Also exported from @sponsorkit/sdk/node.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
storage | WebStorageLike | optional | See WebStorageLike. |
Returns SponsorkitStorage (SponsorkitStorage)
memoryStorage()#
function memoryStorage(map: Map<string, string> = sharedMemory): SponsorkitStorageIn-memory storage. Without an argument, all instances in this process share one map.
Also exported from @sponsorkit/sdk/node.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
map | Map<string, string> | sharedMemory |
Returns SponsorkitStorage (SponsorkitStorage)
normalizeTarget()#
function normalizeTarget(provider: string, account: string): TargetNormalizes and validates one target. Throws invalid_config when malformed.
Also exported from @sponsorkit/sdk/node.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
provider | string | required | |
account | string | required |
Returns Target (Target)
parseTarget()#
function parseTarget(s: string): TargetParses provider:account. Throws invalid_config when malformed.
Also exported from @sponsorkit/sdk/node.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
s | string | required |
Returns Target (Target)
resetProcessState()#
function resetProcessState(): voidForgets per-process state: nag runs already counted, the dev-check memo and
the Retry-After memo. Simulates a new process (tests).
Also exported from @sponsorkit/sdk/node.
Returns void
targetsFrom()#
function targetsFrom(input: TargetOptions | string | readonly string[]): Target[]Builds the canonical target set from { github?, openCollective? }, a target param string, or a list of target strings.
Also exported from @sponsorkit/sdk/node.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
input | TargetOptions | string | readonly string[] | required | See TargetOptions. |
Returns Target[] (Target)
targetsHash()#
async function targetsHash(targetsOrParam: readonly Target[] | string): Promise<string>targets_hash: lowercase hex SHA-256 of the UTF-8 target param (storage key).
Also exported from @sponsorkit/sdk/node.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
targetsOrParam | readonly Target[] | string | required | See Target. |
Returns Promise<string>
targetsParam()#
function targetsParam(targets: readonly Target[]): stringThe canonical target param: github:x,open-collective:y.
Also exported from @sponsorkit/sdk/node.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
targets | readonly Target[] | required | See Target. |
Returns string
targetToString()#
function targetToString(t: Target): stringgithub:<login> / open-collective:<slug>.
Also exported from @sponsorkit/sdk/node.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
t | Target | required | See Target. |
Returns string
Constants#
DEFAULT_API_BASE_URL#
const DEFAULT_API_BASE_URL: "https://api.sponsorkit.io" = "https://api.sponsorkit.io"The default API origin: where every /api/v1/… call goes.
Also exported from @sponsorkit/sdk/node.
DEFAULT_CACHE_TTL#
const DEFAULT_CACHE_TTL: 3600 = 3600Default cacheTtl in seconds.
Also exported from @sponsorkit/sdk/node.
DEFAULT_CHECKOUT_POLL_INTERVAL#
const DEFAULT_CHECKOUT_POLL_INTERVAL: 60 = 60Default checkout poll interval in seconds.
Also exported from @sponsorkit/sdk/node.
DEFAULT_CHECKOUT_TIMEOUT#
const DEFAULT_CHECKOUT_TIMEOUT: 600 = 600Default checkout timeout in seconds (polling phase only).
Also exported from @sponsorkit/sdk/node.
DEFAULT_SIGN_IN_TIMEOUT#
const DEFAULT_SIGN_IN_TIMEOUT: 300 = 300Default signInTimeout in seconds.
Also exported from @sponsorkit/sdk/node.
DEFAULT_SITE_BASE_URL#
const DEFAULT_SITE_BASE_URL: "https://ffmathy.github.io/sponsorkit" = "https://ffmathy.github.io/sponsorkit"The default site base URL: origin plus base path of the pages (/connect/, /checkout/).
Also exported from @sponsorkit/sdk/node.
MOCK_PRESETS#
const MOCK_PRESETS: readonly MockPreset[] = ["signed-out", "none", "active-monthly", "one-time", "cancelled", "org"]Names of the built-in presets.
Also exported from @sponsorkit/sdk/node.
Type: MockPreset
PROVIDERS#
const PROVIDERS: readonly Provider[] = ["github", "open-collective"]All providers, in canonical order.
Also exported from @sponsorkit/sdk/node, @sponsorkit/sdk/server.
Type: Provider
SPONSORKIT_ERROR_CODES#
const SPONSORKIT_ERROR_CODES: readonly SponsorkitErrorCode[]All error codes, in the order of the wire protocol's error contract.
Also exported from @sponsorkit/sdk/node, @sponsorkit/sdk/server.
Type: SponsorkitErrorCode