also looking at this
feat: crossposting — media (LinkedIn/IG/Threads), OAuth token refresh, and X/Twitter
#30
28 files
+1810
-109
| @@ -7,6 +7,29 @@ export function now(): string { | ||
| 7 | 7 | return new Date().toISOString().slice(0, 19).replace('T', ' ') |
| 8 | 8 | } |
| 9 | 9 | |
| 10 | /** | |
| 11 | * A SQLite `YYYY-MM-DD HH:MM:SS` (UTC) timestamp `secondsFromNow` in the | |
| 12 | * future — used to record when an OAuth access token will expire. Returns null | |
| 13 | * when the provider didn't report a lifetime, so callers can store NULL. | |
| 14 | */ | |
| 15 | export function expiresAt(secondsFromNow: number | null | undefined): string | null { | |
| 16 | if (!secondsFromNow || secondsFromNow <= 0) return null | |
| 17 | return new Date(Date.now() + secondsFromNow * 1000).toISOString().slice(0, 19).replace('T', ' ') | |
| 18 | } | |
| 19 | ||
| 20 | /** | |
| 21 | * Whether a stored `token_expires_at` (SQLite `YYYY-MM-DD HH:MM:SS`, UTC) is | |
| 22 | * already past or falls within `withinSeconds` of now — the trigger for a | |
| 23 | * proactive token refresh before publishing. Unknown/blank/unparseable expiry | |
| 24 | * returns false: there's nothing to refresh against, so leave the token alone. | |
| 25 | */ | |
| 26 | export function isExpiringSoon(value: string | null | undefined, withinSeconds = 7 * 24 * 60 * 60): boolean { | |
| 27 | if (!value) return false | |
| 28 | const expiry = Date.parse(`${String(value).replace(' ', 'T')}Z`) | |
| 29 | if (Number.isNaN(expiry)) return false | |
| 30 | return expiry - Date.now() <= withinSeconds * 1000 | |
| 31 | } | |
| 32 | ||
| 10 | 33 | export function uuid(): string { |
| 11 | 34 | return crypto.randomUUID() |
| 12 | 35 | } |