@@ -2,6 +2,16 @@ import type { ColorScheme } from "~/hooks/useDark";
22import type { HistoryItem } from "~/atoms/historyAtom" ;
33import type { PrimitiveMetadata } from "@shared/types" ;
44import type { ReadingState } from "@shared/reading-state" ;
5+ import type { LatestNewsSyncHubCollection } from "@shared/synchub-contract" ;
6+
7+ import {
8+ syncHubHeaders ,
9+ latestNewsSyncHubURL ,
10+ defaultSyncHubEndpoint ,
11+ normalizeSyncHubEndpoint ,
12+ } from "@shared/synchub-contract" ;
13+
14+ export { defaultSyncHubEndpoint } from "@shared/synchub-contract" ;
515
616export interface UserPreferences {
717 colorScheme : ColorScheme ;
@@ -12,29 +22,32 @@ export interface SyncHubConfig {
1222 endpoint : string ;
1323 apiKey : string ;
1424}
15- const storageKey = "latestnews-synchub-sync" ;
1625
17- function endpoint ( value : string ) {
18- return value . trim ( ) . replace ( / \/ + $ / , "" ) ;
19- }
26+ const storageKey = "latestnews-synchub-sync" ;
2027
2128export function getSyncHubConfig ( ) : SyncHubConfig {
2229 try {
2330 const raw = localStorage . getItem ( storageKey ) ;
2431 const value = raw ? ( JSON . parse ( raw ) as Partial < SyncHubConfig > ) : { } ;
2532 return {
26- endpoint : typeof value . endpoint === "string" ? endpoint ( value . endpoint ) : "" ,
33+ endpoint :
34+ typeof value . endpoint === "string" && value . endpoint . trim ( )
35+ ? normalizeSyncHubEndpoint ( value . endpoint )
36+ : defaultSyncHubEndpoint ,
2737 apiKey : typeof value . apiKey === "string" ? value . apiKey . trim ( ) : "" ,
2838 } ;
2939 } catch {
30- return { endpoint : "" , apiKey : "" } ;
40+ return { endpoint : defaultSyncHubEndpoint , apiKey : "" } ;
3141 }
3242}
3343
3444export function saveSyncHubConfig ( config : SyncHubConfig ) {
3545 localStorage . setItem (
3646 storageKey ,
37- JSON . stringify ( { endpoint : endpoint ( config . endpoint ) , apiKey : config . apiKey . trim ( ) } )
47+ JSON . stringify ( {
48+ endpoint : normalizeSyncHubEndpoint ( config . endpoint ) ,
49+ apiKey : config . apiKey . trim ( ) ,
50+ } )
3851 ) ;
3952}
4053export function clearSyncHubConfig ( ) {
@@ -44,14 +57,12 @@ export function isSyncHubConfigured(config = getSyncHubConfig()) {
4457 return / ^ h t t p s ? : \/ \/ / i. test ( config . endpoint ) && config . apiKey . startsWith ( "shk_" ) ;
4558}
4659
47- type Collection = "reading-history" | "favorites" | "preferences" ;
48-
49- async function request < T > ( collection : Collection , init ?: RequestInit ) : Promise < T | null > {
60+ async function request < T > ( collection : LatestNewsSyncHubCollection , init ?: RequestInit ) : Promise < T | null > {
5061 const config = getSyncHubConfig ( ) ;
5162 if ( ! isSyncHubConfigured ( config ) ) return null ;
52- const response = await fetch ( ` ${ config . endpoint } /api/v1/metadata/latestnews/ ${ collection } ` , {
63+ const response = await fetch ( latestNewsSyncHubURL ( config . endpoint , collection ) , {
5364 ...init ,
54- headers : { "Content-Type" : "application/json" , "X-API-Key" : config . apiKey , ...init ?. headers } ,
65+ headers : { ... syncHubHeaders ( config . apiKey ) , ...init ?. headers } ,
5566 } ) ;
5667 const result = ( await response . json ( ) ) as { code : number ; message ?: string ; data ?: { payload : T | null } } ;
5768 if ( ! response . ok || result . code !== 0 ) throw new Error ( result . message || "SyncHub request failed" ) ;
0 commit comments