npm API
वही पाइपलाइन स्क्रिप्ट, बिल्ड टूल, ब्राउज़र, एज फ़ंक्शन या अपने सर्वर से इस्तेमाल करें।
इंपोर्ट
import {
generateStats,
loadConfig,
validateConfig,
applyRuntimeOverrides,
fetchStats,
renderCards,
listThemes
} from "profile-graphics";
कॉन्फ़िग से बनाएँ
generateStats आँकड़े लाता है, charts के हर कार्ड को रेंडर करता है और फ़ाइलें output.dir में लिखता है, बिल्कुल CLI की तरह।
const config = await loadConfig("profile-graphics.yml");
const { outputs } = await generateStats(config);
for (const output of outputs) console.log(output.chart, output.path);
फ़ाइल लिखे बिना रेंडर करें
validateConfig डिफ़ॉल्ट भरता है, और applyRuntimeOverrides विकल्पों को उनके camelCase नामों से, स्ट्रिंग के रूप में लागू करता है, जैसे CLI फ़्लैग करते हैं। fetchStats सभी स्रोतों के मिले हुए आँकड़े लौटाता है, और renderCards उन्हें डिस्क छुए बिना SVG स्ट्रिंग या PNG और WebP बाइट में बदलता है।
const base = validateConfig({ provider: { user: "octocat" }, charts: ["streak"] });
const config = applyRuntimeOverrides(base, { theme: "dracula", calendarMode: "rolling" });
const stats = await fetchStats(config);
const [card] = await renderCards(config, stats);
console.log(card.content);
ब्राउज़र और एज रनटाइम
profile-graphics/core में स्कीमा, थीम, कार्ड के अनुवाद और रेंडरर हैं, बिना किसी Node.js API के, इसलिए यह ब्राउज़र, वर्कर और एज फ़ंक्शन में चलता है। stats न देने पर renderBrowserCards डेमो आँकड़े रेंडर करता है, और यह renderConfigCards, createDemoStats, resolveWindow और listThemes भी एक्सपोर्ट करता है।
import { renderBrowserCards } from "profile-graphics/core";
const { cards } = renderBrowserCards({
config: {
provider: { user: "octocat" },
charts: ["streak", "langs"],
theme: { preset: "dracula" }
}
});
document.body.innerHTML = cards.map((card) => card.svg).join("");
कार्ड सर्वर
profile-graphics/server, createCardHandler एक्सपोर्ट करता है, जो profile-graphics serve के पीछे का Fetch API हैंडलर है, allowedUsers, cacheSeconds, cacheEntries, rateLimit, clientKey और counter विकल्पों के साथ, और RateLimit टाइप भी। डिफ़ॉल्ट रूप से क्लाइंट x-forwarded-for का पहला पता होता है, और जिन अनुरोधों में यह नहीं होता उन पर सीमा नहीं लगती। listen किसी हैंडलर को Node.js HTTP सर्वर पर चलाता है।
createVercelHandler हैंडलर को Vercel फ़ंक्शन के लिए लपेटता है: यह /api प्रीफ़िक्स हटाता है, /api/stats?username=… का जवाब GitHub आँकड़ों के JSON से देता है और हर क्लाइंट को 60 सेकंड में 30 अनुरोधों तक सीमित करता है। सीमा बदलने के लिए PG_RATE_LIMIT (जैसे 120/60) और सिर्फ़ कुछ यूज़र को सेवा देने के लिए PG_ALLOWED_USERS (जैसे mona,octocat) सेट करें; बाकी को HTTP 403 मिलता है।
import { createCardHandler, createVercelHandler } from "profile-graphics/server";
export const handler = createCardHandler({
allowedUsers: ["octocat"],
cacheSeconds: 3600,
rateLimit: { requests: 60, windowSeconds: 60 },
clientKey: (request) => request.headers.get("cf-connecting-ip") ?? ""
});
export const GET = createVercelHandler();
व्यू काउंटर स्टोर
व्यू काउंटर अपनी गिनती CounterStore में रखता है: increment(key) और read(key) वाला एक ऑब्जेक्ट, जो दोनों { total, days } लौटाते हैं, जहाँ days UTC तारीखों को गिनती से जोड़ता है। MemoryCounterStore डिफ़ॉल्ट है, FileCounterStore एक JSON फ़ाइल लिखता है, और UpstashCounterStore, fetch से Upstash Redis REST API से बात करता है और हर यूज़र के लिए एक hash रखता है। कोई दूसरा डेटाबेस जोड़ने के लिए अपना स्टोर दें।
जब UPSTASH_REDIS_REST_URL और UPSTASH_REDIS_REST_TOKEN (या KV_REST_API_URL और KV_REST_API_TOKEN) सेट हों, तो createVercelHandler ख़ुद UpstashCounterStore चुनता है।
import { createCardHandler, UpstashCounterStore } from "profile-graphics/server";
const counter = new UpstashCounterStore({
url: process.env.UPSTASH_REDIS_REST_URL,
token: process.env.UPSTASH_REDIS_REST_TOKEN
});
export const handler = createCardHandler({ allowedUsers: ["octocat"], counter });
वेब कंपोनेंट
किसी भी पेज पर, जैसे निजी साइट या ब्लॉग, कार्ड लाइव रेंडर करने के लिए <profile-graphics> इस्तेमाल करें। CDN से सिंगल-फ़ाइल बंडल लोड करें या बंडलर में import "profile-graphics/element" लिखें। card कॉमा से अलग एक या ज़्यादा कार्ड नाम लेता है (डिफ़ॉल्ट streak), और डिस्प्ले विकल्प अंडरस्कोर की जगह डैश वाले एट्रिब्यूट हैं: theme, locale, frame, frame-title, layout, langs-style और बाकी। config पूरा कॉन्फ़िग JSON के रूप में लेता है। नेटिव थीम के साथ कार्ड देखने वाले की कलर स्कीम के हिसाब से चलते हैं, और कोई एट्रिब्यूट बदलने पर फिर से बनते हैं।
<script type="module" src="https://cdn.jsdelivr.net/npm/profile-graphics/dist/profile-graphics.element.js"></script>
<profile-graphics user="octocat" card="streak,langs" theme="tokyo-night" frame="window"
frame-title="{user} · {total} contributions"></profile-graphics>
<profile-graphics provider="forgejo" user="forgejo" card="calendar"></profile-graphics>
<profile-graphics stats-src="https://cards.example.com/github/octocat.json" card="summary"></profile-graphics>
<profile-graphics views-src="https://cards.example.com/github/octocat/views.json" card="views"></profile-graphics>
डेटा के बिना एलिमेंट डेमो आँकड़े रेंडर करता है। provider="forgejo" सीधे एक सार्वजनिक Codeberg प्रोफ़ाइल लोड करता है, और base-url उसे किसी दूसरे सर्वर की ओर मोड़ता है। stats-src आँकड़ों का JSON लोड करता है, जैसे profile-graphics serve से /<provider>/<user>.json पर। views-src, views कार्ड के लिए /<provider>/<user>/views.json से व्यू की गिनती पढ़ता है, बिना व्यू गिने। stats और token प्रॉपर्टी किसी स्क्रिप्ट से डेटा या टोकन लेती हैं। HTML में कभी टोकन न डालें।
मुख्य टाइप
| टाइप | मान |
|---|---|
ProviderType
|
github, gitlab, forgejo, wakatime, file
|
ChartType
|
activity, langs, streak, summary, productive-time, punch-card, calendar, calendar-3d, profile-details, profile-card, trophy, repos, coding-time, coding-languages, coding-editors, coding-os, coding-projects, views
|
OutputFormat
|
svg, png, webp
|
OutputLayout
|
separate, dashboard
|
CalendarMode
|
current-year, last-year, rolling
|
FrameStyle
|
none, border, window
|
LangsStyle
|
bar, donut, donut-compact
|
ThemeMode
|
auto, light, dark, fixed
|