@sponsorkit/sdk

Browser client (popup or redirect sign-in, localStorage), plus every shared type and helper.

On this page

@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#

NameSummary
FinancialContributionA ContributionResult with filter helpers.
SponsorkitSponsorkit client for browsers.
SponsorkitCoreBase class with the shared client behaviour.
SponsorkitErrorThe only error type thrown by the SDK.

Interfaces#

NameSummary
CheckoutOptionsOptions for openCheckout() and checkoutUrl().
ContributionA sponsorship (GitHub) or order (Open Collective), normalized.
ContributionResultThe plain JSON result of getFinancialContribution().
IdentityA supporter's provider account (without its token, which never leaves Sponsorkit).
MoneyAn amount of money in minor units (cents) with its ISO 4217 currency.
NagNag helper returned by Sponsorkit.nag().
NagDueOptionsOptions for Nag.due().
NagOptionsOptions for nag().
NagStateNag state as persisted in the StorageFile, keyed by nag id.
OpenCheckoutOptionsOptions for openCheckout().
SignInOptionsOptions for signIn().
SponsorEntityWho actually pays for a contribution: the user, or an organization the user belongs to.
SponsorkitCoreOptionsOptions shared by every Sponsorkit flavour.
SponsorkitOptionsOptions for the browser client.
SponsorkitStorageWhere the SDK keeps its per-target-set document.
StorageFileThe JSON document stored per target set (session token, cached result and nag state).
SupporterThe signed-in supporter.
TargetA normalized target.
TargetInfoPublic metadata about a target (GET /api/v1/targets).
TargetOptionsTargets as accepted by verifySession().
TargetTierA tier of a target's sponsorship listing.
WebStorageLikeThe subset of the Web Storage API used by localStorageAdapter.

Type aliases#

NameSummary
CheckoutResultThe outcome of openCheckout().
ContributionKindWhether a contribution recurs.
ContributionStatusNormalized contribution status.
EnvAn environment map (process.env-shaped).
FetchLikeA fetch-compatible function.
FrequencyHow often a contribution is charged.
MockOptionMock mode: a preset name, or custom contributions to report as the signed-in supporter's.
MockPresetThe names of the built-in mock presets.
NagPredicateA predicate deciding whether the current supporter should see the nag.
ProviderA contribution platform supported by Sponsorkit.
SponsorkitErrorCodeError codes carried by SponsorkitError.

Functions#

NameSummary
canonicalTargetsDeduplicates and sorts (byte-wise) a target set; at least one target, at most one per provider.
codeChallengePKCE S256: base64url(sha256(ascii(verifier))).
createCodeVerifierA new PKCE code_verifier: 32 random bytes, base64url (43 characters).
isSponsorkitErrorTrue if error is a SponsorkitError (optionally with the given code).
localStorageAdapterWraps a Web Storage object (default: localStorage) using the key sponsorkit:<targets_hash>.
memoryStorageIn-memory storage.
normalizeTargetNormalizes and validates one target.
parseTargetParses provider:account.
resetProcessStateForgets per-process state: nag runs already counted, the dev-check memo and the Retry-After memo.
targetsFromBuilds the canonical target set from { github?, openCollective? }, a target param string, or a list of target strings.
targetsHashtargets_hash: lowercase hex SHA-256 of the UTF-8 target param (storage key).
targetsParamThe canonical target param: github:x,open-collective:y.
targetToStringgithub:<login> / open-collective:<slug>.

Constants#

NameSummary
DEFAULT_API_BASE_URLThe default API origin: where every /api/v1/… call goes.
DEFAULT_CACHE_TTLDefault cacheTtl in seconds.
DEFAULT_CHECKOUT_POLL_INTERVALDefault checkout poll interval in seconds.
DEFAULT_CHECKOUT_TIMEOUTDefault checkout timeout in seconds (polling phase only).
DEFAULT_SIGN_IN_TIMEOUTDefault signInTimeout in seconds.
DEFAULT_SITE_BASE_URLThe default site base URL: origin plus base path of the pages (/connect/, /checkout/).
MOCK_PRESETSNames of the built-in presets.
PROVIDERSAll providers, in canonical order.
SPONSORKIT_ERROR_CODESAll 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

ParameterTypeDefaultDescription
targetsreadonly Target[]requiredSee Target.

Returns Target[] (Target)

View source

codeChallenge()#

async function codeChallenge(verifier: string): Promise<string>

PKCE S256: base64url(sha256(ascii(verifier))).

Also exported from @sponsorkit/sdk/node.

Parameters

ParameterTypeDefaultDescription
verifierstringrequired

Returns Promise<string>

View source

createCodeVerifier()#

function createCodeVerifier(): string

A new PKCE code_verifier: 32 random bytes, base64url (43 characters).

Also exported from @sponsorkit/sdk/node.

Returns string

View source

isSponsorkitError()#

function isSponsorkitError(error: unknown, code?: SponsorkitErrorCode): error is SponsorkitError

True if error is a SponsorkitError (optionally with the given code).

Also exported from @sponsorkit/sdk/node, @sponsorkit/sdk/server.

Parameters

ParameterTypeDefaultDescription
errorunknownrequired
codeSponsorkitErrorCodeoptionalSee SponsorkitErrorCode.

Returns error is SponsorkitError (SponsorkitError)

View source

localStorageAdapter()#

function localStorageAdapter(storage?: WebStorageLike): SponsorkitStorage

Wraps a Web Storage object (default: localStorage) using the key sponsorkit:<targets_hash>.

Also exported from @sponsorkit/sdk/node.

Parameters

ParameterTypeDefaultDescription
storageWebStorageLikeoptionalSee WebStorageLike.

Returns SponsorkitStorage (SponsorkitStorage)

View source

memoryStorage()#

function memoryStorage(map: Map<string, string> = sharedMemory): SponsorkitStorage

In-memory storage. Without an argument, all instances in this process share one map.

Also exported from @sponsorkit/sdk/node.

Parameters

ParameterTypeDefaultDescription
mapMap<string, string>sharedMemory

Returns SponsorkitStorage (SponsorkitStorage)

View source

normalizeTarget()#

function normalizeTarget(provider: string, account: string): Target

Normalizes and validates one target. Throws invalid_config when malformed.

Also exported from @sponsorkit/sdk/node.

Parameters

ParameterTypeDefaultDescription
providerstringrequired
accountstringrequired

Returns Target (Target)

View source

parseTarget()#

function parseTarget(s: string): Target

Parses provider:account. Throws invalid_config when malformed.

Also exported from @sponsorkit/sdk/node.

Parameters

ParameterTypeDefaultDescription
sstringrequired

Returns Target (Target)

View source

resetProcessState()#

function resetProcessState(): void

Forgets 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

View source

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

ParameterTypeDefaultDescription
inputTargetOptions | string | readonly string[]requiredSee TargetOptions.

Returns Target[] (Target)

View source

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

ParameterTypeDefaultDescription
targetsOrParamreadonly Target[] | stringrequiredSee Target.

Returns Promise<string>

View source

targetsParam()#

function targetsParam(targets: readonly Target[]): string

The canonical target param: github:x,open-collective:y.

Also exported from @sponsorkit/sdk/node.

Parameters

ParameterTypeDefaultDescription
targetsreadonly Target[]requiredSee Target.

Returns string

View source

targetToString()#

function targetToString(t: Target): string

github:<login> / open-collective:<slug>.

Also exported from @sponsorkit/sdk/node.

Parameters

ParameterTypeDefaultDescription
tTargetrequiredSee Target.

Returns string

View source

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.

View source

DEFAULT_CACHE_TTL#

const DEFAULT_CACHE_TTL: 3600 = 3600

Default cacheTtl in seconds.

Also exported from @sponsorkit/sdk/node.

View source

DEFAULT_CHECKOUT_POLL_INTERVAL#

const DEFAULT_CHECKOUT_POLL_INTERVAL: 60 = 60

Default checkout poll interval in seconds.

Also exported from @sponsorkit/sdk/node.

View source

DEFAULT_CHECKOUT_TIMEOUT#

const DEFAULT_CHECKOUT_TIMEOUT: 600 = 600

Default checkout timeout in seconds (polling phase only).

Also exported from @sponsorkit/sdk/node.

View source

DEFAULT_SIGN_IN_TIMEOUT#

const DEFAULT_SIGN_IN_TIMEOUT: 300 = 300

Default signInTimeout in seconds.

Also exported from @sponsorkit/sdk/node.

View source

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.

View source

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

View source

PROVIDERS#

const PROVIDERS: readonly Provider[] = ["github", "open-collective"]

All providers, in canonical order.

Also exported from @sponsorkit/sdk/node, @sponsorkit/sdk/server.

Type: Provider

View source

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

View source