@simon_he/sponsorkit
Version:
Toolkit for generating sponsors images
365 lines (356 loc) • 11.6 kB
text/typescript
import { Buffer } from 'node:buffer';
import * as consola_dist_core from 'consola/dist/core';
declare function genSvgImage(x: number, y: number, size: number, url: string): string;
declare function generateBadge(x: number, y: number, sponsor: Sponsor, preset: BadgePreset): string;
declare class SvgComposer {
readonly config: Required<SponsorkitConfig>;
height: number;
body: string;
constructor(config: Required<SponsorkitConfig>);
addSpan(height?: number): this;
addTitle(text: string, classes?: string): this;
addText(text: string, classes?: string): this;
addRaw(svg: string): this;
addSponsorLine(sponsors: Sponsorship[], preset: BadgePreset): void;
addSponsorGrid(sponsors: Sponsorship[], preset: BadgePreset): this;
generateSvg(): string;
}
interface BadgePreset {
boxWidth: number;
boxHeight: number;
avatar: {
size: number;
classes?: string;
};
name?: false | {
color?: string;
classes?: string;
maxLength?: number;
};
container?: {
sidePadding?: number;
};
classes?: string;
}
interface Provider {
name: string;
fetchSponsors: (config: SponsorkitConfig) => Promise<Sponsorship[]>;
}
interface Sponsor {
type: 'User' | 'Organization';
login: string;
name: string;
avatarUrl: string;
avatarUrlHighRes?: string;
avatarUrlMediumRes?: string;
avatarUrlLowRes?: string;
websiteUrl?: string;
linkUrl?: string;
avatarBuffer?: string;
}
interface Sponsorship {
sponsor: Sponsor;
monthlyDollars: number;
privacyLevel?: 'PUBLIC' | 'PRIVATE';
tierName?: string;
createdAt?: string;
expireAt?: string;
isOneTime?: boolean;
provider?: ProviderName | string;
/**
* Raw data from provider
*/
raw?: any;
}
type OutputFormat = 'svg' | 'png' | 'json';
type ProviderName = 'github' | 'patreon' | 'opencollective' | 'afdian';
interface ProvidersConfig {
github?: {
/**
* User id of your GitHub account.
*
* Will read from `SPONSORKIT_GITHUB_LOGIN` environment variable if not set.
*/
login?: string;
/**
* GitHub Token that have access to your sponsorships.
*
* Will read from `SPONSORKIT_GITHUB_TOKEN` environment variable if not set.
*
* @deprecated It's not recommended set this value directly, pass from env or use `.env` file.
*/
token?: string;
/**
* The account type for sponsorships.
*
* Possible values are `user`(default) and `organization`.
* Will read from `SPONSORKIT_GITHUB_TYPE` environment variable if not set.
*/
type?: string;
};
patreon?: {
/**
* Patreon Token that have access to your sponsorships.
*
* Will read from `SPONSORKIT_PATREON_TOKEN` environment variable if not set.
*
* @deprecated It's not recommended set this value directly, pass from env or use `.env` file.
*/
token?: string;
};
opencollective?: {
/**
* Api key of your OpenCollective account.
*
* Will read from `SPONSORKIT_OPENCOLLECTIVE_KEY` environment variable if not set.
*
* @deprecated It's not recommended set this value directly, pass from env or use `.env` file.
*/
key?: string;
/**
* The id of your account.
*
* Will read from `SPONSORKIT_OPENCOLLECTIVE_ID` environment variable if not set.
*/
id?: string;
/**
* The slug of your account.
*
* Will read from `SPONSORKIT_OPENCOLLECTIVE_SLUG` environment variable if not set.
*/
slug?: string;
/**
* The GitHub handle of your account.
*
* Will read from `SPONSORKIT_OPENCOLLECTIVE_GH_HANDLE` environment variable if not set.
*/
githubHandle?: string;
type?: string;
};
afdian?: {
/**
* The userId of your Afdian.
*
* Will read from `SPONSORKIT_AFDIAN_USER_ID` environment variable if not set.
*
* @see https://afdian.net/dashboard/dev
*/
userId?: string;
/**
* Afdian Token that have access to your sponsorships.
*
* Will read from `SPONSORKIT_AFDIAN_TOKEN` environment variable if not set.
*
* @see https://afdian.net/dashboard/dev
* @deprecated It's not recommended set this value directly, pass from env or use `.env` file.
*/
token?: string;
/**
* Exchange rate of USD to CNY
*
* @default 6.5
*/
exechangeRate?: number;
};
}
interface SponsorkitConfig extends ProvidersConfig {
/**
* @deprecated use `github.login` instead
*/
login?: string;
/**
* @deprecated use `github.token` instead
*/
token?: string;
/**
* @default auto detect based on the config provided
*/
providers?: ProviderName[];
/**
* Whether to display the private sponsors
*
* @default false
*/
includePrivate?: boolean;
/**
* Whether to display the past sponsors
* Currently only works with GitHub provider
*
* @default auto detect based on tiers
*/
includePastSponsors?: boolean;
/**
* By pass cache
*/
force?: boolean;
/**
* Directory of output files.
*
* @default './sponsorkit'
*/
outputDir?: string;
/**
* Name of exported files
*
* @default 'sponsors'
*/
name?: string;
/**
* Output formats
*
* @default ['json', 'svg', 'png']
*/
formats?: OutputFormat[];
/**
* Output formats
*
* @default ['json', 'svg', 'png']
*/
customGithubUser?: {
user: string;
monthlyDollars: number;
}[];
/**
* Hook to modify sponsors data before fetching the avatars.
*/
onSponsorsFetched?: (sponsors: Sponsorship[], provider: ProviderName | string) => PromiseLike<void | Sponsorship[]> | void | Sponsorship[];
/**
* Hook to modify sponsors data before rendering.
*/
onSponsorsReady?: (sponsors: Sponsorship[]) => PromiseLike<void | Sponsorship[]> | void | Sponsorship[];
/**
* Hook to get or modify the SVG before writing.
*/
onSvgGenerated?: (svg: string) => PromiseLike<string | void | undefined | null> | string | void | undefined | null;
/**
* Compose the SVG
*/
customComposer?: (composer: SvgComposer, sponsors: Sponsorship[], config: SponsorkitConfig) => PromiseLike<void> | void;
/**
* Filter of sponsorships to render in the final image.
*/
filter?: (sponsor: Sponsorship, all: Sponsorship[]) => boolean | void;
/**
* Tiers
*/
tiers?: Tier[];
/**
* Width of the image.
*
* @default 700
*/
width?: number;
/**
* Url to fallback avatar.
* Setting false to disable fallback avatar.
*/
fallbackAvatar?: string | false | Buffer | Promise<Buffer>;
/**
* Path to cache file
*
* @default './sponsorkit/.cache.json'
*/
cacheFile?: string;
/**
* Padding of image container
*/
padding?: {
top?: number;
bottom?: number;
};
/**
* Inline CSS of generated SVG
*/
svgInlineCSS?: string;
type?: 'tiers' | 'circle' | 'all';
circles?: CircleRenderOptions;
}
interface CircleRenderOptions {
/**
* Min radius for sponsors
*
* @default 10
*/
radiusMin?: number;
/**
* Max radius for sponsors
*
* @default 300
*/
radiusMax?: number;
/**
* Radius for past sponsors
*
* @default 5
*/
radiusPast?: number;
/**
* Custom function to calculate the weight of the sponsor.
*
* When provided, `radiusMin`, `radiusMax` and `radiusPast` will be ignored.
*/
weightInterop?: (sponsor: Sponsorship, maxAmount: number) => number;
}
interface Tier {
/**
* The lower bound of the tier (inclusive)
*/
monthlyDollars?: number;
title?: string;
preset?: BadgePreset;
padding?: {
top?: number;
bottom?: number;
};
/**
* Replace the default composer with your own.
*/
compose?: (composer: SvgComposer, sponsors: Sponsorship[], config: SponsorkitConfig) => void;
/**
* Compose the SVG before the main composer.
*/
composeBefore?: (composer: SvgComposer, tierSponsors: Sponsorship[], config: SponsorkitConfig) => void;
/**
* Compose the SVG after the main composer.
*/
composeAfter?: (composer: SvgComposer, tierSponsors: Sponsorship[], config: SponsorkitConfig) => void;
}
declare function resolveAvatars(ships: Sponsorship[], fallbackAvatar: SponsorkitConfig['fallbackAvatar'], t?: consola_dist_core.ConsolaInstance): Promise<void[]>;
declare function round(image: string | ArrayBuffer, radius?: number, size?: number): Promise<Buffer>;
declare function svgToPng(svg: string): Promise<Buffer>;
declare function base64ToArrayBuffer(base64: string): ArrayBufferLike;
declare function arrayBufferToBase64(buffer: ArrayBuffer): string;
declare function pngToDataUri(png: Buffer): string;
declare const defaultTiers: Tier[];
declare const defaultInlineCSS = "\ntext {\n font-weight: 300;\n font-size: 14px;\n fill: #777777;\n font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;\n}\n.sponsorkit-link {\n cursor: pointer;\n}\n.sponsorkit-tier-title {\n font-weight: 500;\n font-size: 20px;\n}\n";
declare const defaultConfig: SponsorkitConfig;
declare function defineConfig(config: SponsorkitConfig): SponsorkitConfig;
declare function loadConfig(inlineConfig?: SponsorkitConfig): Promise<Required<SponsorkitConfig>>;
interface TierPartition {
monthlyDollars: number;
tier: Tier;
sponsors: Sponsorship[];
}
declare function partitionTiers(sponsors: Sponsorship[], tiers: Tier[]): TierPartition[];
declare const presets: {
none: BadgePreset;
xs: BadgePreset;
small: BadgePreset;
base: BadgePreset;
medium: BadgePreset;
large: BadgePreset;
xl: BadgePreset;
};
declare const GitHubProvider: Provider;
declare function fetchGitHubSponsors(token: string, login: string, type: string, config: SponsorkitConfig): Promise<Sponsorship[]>;
declare function makeQuery(login: string, type: string, cursor?: string): string;
declare const ProvidersMap: {
github: Provider;
patreon: Provider;
opencollective: Provider;
afdian: Provider;
};
declare function guessProviders(config: SponsorkitConfig): ProviderName[];
declare function resolveProviders(names: (ProviderName | Provider)[]): Provider[];
declare function fetchSponsors(config: SponsorkitConfig): Promise<Sponsorship[]>;
export { type BadgePreset, type CircleRenderOptions, GitHubProvider, type OutputFormat, type Provider, type ProviderName, type ProvidersConfig, ProvidersMap, type Sponsor, type SponsorkitConfig, type Sponsorship, SvgComposer, type Tier, type TierPartition, arrayBufferToBase64, base64ToArrayBuffer, defaultConfig, defaultInlineCSS, defaultTiers, defineConfig, fetchGitHubSponsors, fetchSponsors, genSvgImage, generateBadge, guessProviders, loadConfig, makeQuery, partitionTiers, pngToDataUri, presets, resolveAvatars, resolveProviders, round, svgToPng };