@happyvertical/smrt-social

中文文档
0.62.1发布于 2026/11/17MITTypeScriptES Module近 30 天下载 1.5万

Social media account management for multi-platform publishing in the SMRT ecosystem

原始使用文档
文档目录8 节
  1. Installation
  2. Usage
  3. Connecting accounts (OAuth)
  4. API
  5. Models
  6. Types
  7. Key Computed Properties
  8. Dependencies

@happyvertical/smrt-social

Social media account management for multi-platform publishing in the s-m-r-t ecosystem. Manages OAuth connections, post scheduling, and analytics tracking across YouTube, Threads, X (Twitter), and Bluesky.

Installation

pnpm add @happyvertical/smrt-social

Usage

import { SocialAccount, SocialPost, OAuthState } from '@happyvertical/smrt-social';

// Connect a social account
const account = new SocialAccount({
  name: 'Bentley News YouTube',
  platform: 'youtube',
  platformUsername: 'Bentley News',
  accessToken: 'encrypted-token',
  refreshToken: 'encrypted-refresh',
  tokenExpiresAt: new Date('2026-06-01'),
  defaultHashtags: ['news', 'local'],
  linkBehavior: 'description',
});
await account.save();

// Check readiness before publishing (active + connected + token present + not expired)
if (account.isReady) {
  const post = new SocialPost({
    socialAccountId: account.id,
    title: 'Breaking News from Bentley',
    description: 'Latest updates from the town council meeting.',
    hashtags: ['news', 'local', 'bentley'],
    linkUrl: 'https://example.com/article',
    scheduledAt: new Date('2026-03-05T18:00:00Z'),
    status: 'scheduled',
  });
  await post.save();
}

// OAuth flow: create state, redirect user, verify callback
const state = new OAuthState({
  platform: 'youtube',
  state: OAuthState.generateState(),
  codeVerifier: OAuthState.generateCodeVerifier(),
  redirectUri: 'https://app.example.com/oauth/callback',
  scopes: ['youtube.upload', 'youtube.readonly'],
  createdByUserId: currentUser.id, // reject a different user on callback
  returnTo: '/settings/social', // host-validated same-origin path
});
await state.save();
// On callback: state.verifyState(callbackState), check the user, then
// `await states.consume(state)` (false = already used) before exchanging the code

Connecting accounts (OAuth)

@happyvertical/smrt-social/connect owns the connect flow so hosts mount two routes and write no platform code. X is the first provider (OAuth 2.0 authorization code with PKCE, confidential client); register more with registerSocialConnectProvider.

import {
  completeSocialConnect,
  startSocialConnect,
} from '@happyvertical/smrt-social/connect';

// GET /connect/x  (host has authorized the caller and set the tenant)
const { authorizeUrl } = await startSocialConnect({
  db,
  platform: 'x',
  tenantId,
  redirectUri: 'https://app.example.com/social/callback/x',
});
// redirect to authorizeUrl

// GET /callback/x
const { account } = await completeSocialConnect({
  db,
  platform: 'x',
  tenantId,
  redirectUri: 'https://app.example.com/social/callback/x',
  query: { code, state, error },
});

Client credentials come from the provider's env names (X: X_CLIENT_ID, X_CLIENT_SECRET, falling back to TWITTER_CLIENT_ID/TWITTER_CLIENT_SECRET) or the client option. Tokens are stored through SocialAccount.setCredentials (smrt-secrets, per tenant) as a JSON payload including expiresAt; nothing is written to the plain token columns or logs. The state is single-use, tenant-bound, and checked against the registered redirect URI. New accounts start in the platform's default publish mode (dry_run for X); a reconnect updates the same account and never widens its mode. Failures throw SocialConnectError with a safe, user-showable message. Pass userId to both calls: the state is bound to the initiating user, so a forwarded authorization link is rejected for anyone else. No role checks live in the package. Register the same redirectUri with the platform. YouTube, Facebook Pages and Threads providers are not built yet.

API

Models

ExportDescription
SocialAccountConnected platform account with OAuth credentials and publishing settings
SocialPostScheduled or published post with analytics tracking; createdByUserId records the poster
OAuthStateTemporary OAuth flow state with CSRF protection and PKCE support; createdByUserId and returnTo bind it to its initiator and return page

Types

ExportDescription
SocialPlatformTypePlatform identifier: youtube, threads, x, bluesky
AccountStatusConnection status: connected, expired, error
PostStatusLifecycle status: draft, scheduled, publishing, published, failed
LinkBehaviorLink handling strategy: description, reply, none
PostAnalyticsEngagement metrics: views, likes, comments, shares, clicks
SocialAccountOptionsAccount creation options
SocialPostOptionsPost creation options
OAuthStateOptionsOAuth state creation options

Key Computed Properties

  • SocialAccount.isReady -- gate check: active + connected + token present + not expired
  • SocialAccount.isTokenExpired -- checks with 5-minute buffer before actual expiry
  • SocialPost.isEditable -- true when draft or failed
  • SocialPost.fullText -- description + formatted hashtags
  • OAuthState.isValid -- not expired and state token present
  • OAuthState.verifyState(callback) -- CSRF verification for OAuth callbacks
  • OAuthState.generateCodeChallenge(verifier) -- PKCE S256 challenge generation
  • OAuthStateCollection.consume(state) -- single-use consumption; false when another caller consumed it first

Dependencies

  • @happyvertical/smrt-core -- ORM and code generation
  • @happyvertical/smrt-config -- configuration loading
  • @happyvertical/smrt-content -- content models
  • @happyvertical/smrt-tenancy -- multi-tenant scoping
  • @happyvertical/smrt-video -- video content references

版本与发布记录

@happyvertical/smrt-social 共发布 365 个版本,首次发布于 2026/06/23;过去 12 个月发布了 365 个版本。

版本发布日期距今
0.62.1latest2026/11/17今天
0.62.02026/11/17今天
0.61.62026/11/161 天前
0.61.52026/11/161 天前
0.61.42026/11/161 天前
0.61.32026/11/152 天前

关于 @happyvertical/smrt-social 的常见问题

@happyvertical/smrt-social 是什么?

Social media account management for multi-platform publishing in the SMRT ecosystem

如何安装 @happyvertical/smrt-social?

在项目目录运行 npm install @happyvertical/smrt-social,或使用 pnpm add / yarn add / bun add。

@happyvertical/smrt-social 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@happyvertical/smrt-social 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@happyvertical/smrt-social 可以商用吗?

@happyvertical/smrt-social 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@happyvertical/smrt-social 的下载量有多少?

根据 npm 官方下载统计,@happyvertical/smrt-social 在 2026/10/17 – 2026/11/15 这 30 天内被下载 14,654 次,日均约 488 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@happyvertical/smrt-social 还在维护吗?

最新版本 0.62.1 发布于 2026/11/17(今天);至今共发布 365 个版本,其中过去 12 个月发布了 365 个;首次发布于 2026/06/23。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。