npm API
ใช้ไปป์ไลน์เดียวกันจากสคริปต์ เครื่องมือบิลด์ เบราว์เซอร์ edge function หรือเซิร์ฟเวอร์ของคุณเอง
นำเข้า
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);
เบราว์เซอร์และรันไทม์ Edge
profile-graphics/core มีสคีมา ธีม คำแปลของการ์ด และตัวเรนเดอร์ โดยไม่ใช้ API ของ Node.js เลย จึงทำงานในเบราว์เซอร์ worker และ edge function ได้ renderBrowserCards เรนเดอร์สถิติตัวอย่างเมื่อคุณไม่ส่ง stats และยังส่งออก 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 ซึ่งเป็นแฮนด์เลอร์ Fetch API เบื้องหลัง profile-graphics serve พร้อมตัวเลือก allowedUsers, cacheSeconds, cacheEntries, rateLimit, clientKey และ counter รวมถึงไทป์ RateLimit โดยค่าเริ่มต้น ไคลเอนต์คือที่อยู่แรกใน x-forwarded-for และคำขอที่ไม่มีที่อยู่นี้จะไม่ถูกจำกัด listen รันแฮนด์เลอร์บนเซิร์ฟเวอร์ HTTP ของ Node.js
createVercelHandler ห่อแฮนด์เลอร์สำหรับฟังก์ชันของ Vercel: ตัดพรีฟิกซ์ /api ตอบ /api/stats?username=… ด้วย JSON สถิติ GitHub และจำกัดแต่ละไคลเอนต์ที่ 30 คำขอต่อ 60 วินาที ตั้ง 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 คุยกับ REST API ของ Upstash Redis ผ่าน fetch โดยเก็บ hash หนึ่งรายการต่อผู้ใช้ ส่งที่เก็บของคุณเองเพื่อเชื่อมต่อฐานข้อมูลอื่น
createVercelHandler จะเลือก UpstashCounterStore เองเมื่อตั้งค่า UPSTASH_REDIS_REST_URL และ UPSTASH_REDIS_REST_TOKEN (หรือ KV_REST_API_URL และ KV_REST_API_TOKEN) ไว้
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 อ่านยอดเข้าชมจาก /<provider>/<user>/views.json สำหรับการ์ด views โดยไม่นับการเข้าชม พร็อพเพอร์ตี 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
|