53 lines
1.2 KiB
TypeScript
53 lines
1.2 KiB
TypeScript
import { extractLinkIdFromUrl, extractUserIdFromUrl } from './extractors.js';
|
|
|
|
interface FeedTargetInput {
|
|
link_id?: string;
|
|
url?: string;
|
|
}
|
|
|
|
interface UserTargetInput {
|
|
user_id?: string;
|
|
url?: string;
|
|
}
|
|
|
|
export interface FeedTargetResolved {
|
|
linkId: string;
|
|
}
|
|
|
|
export interface UserTargetResolved {
|
|
userId: string;
|
|
}
|
|
|
|
function normalizeUrl(url: string): string {
|
|
const trimmed = url.trim();
|
|
if (!trimmed) {
|
|
throw new Error('url cannot be empty');
|
|
}
|
|
return trimmed;
|
|
}
|
|
|
|
export function resolveFeedTarget(input: FeedTargetInput): FeedTargetResolved {
|
|
const direct = input.link_id?.trim();
|
|
if (direct) return { linkId: direct };
|
|
|
|
if (input.url) {
|
|
const parsed = extractLinkIdFromUrl(normalizeUrl(input.url));
|
|
if (parsed) return { linkId: parsed };
|
|
}
|
|
|
|
throw new Error('xhh_get_feed_detail requires link_id or url containing link_id');
|
|
}
|
|
|
|
export function resolveUserTarget(input: UserTargetInput): UserTargetResolved {
|
|
const direct = input.user_id?.trim();
|
|
if (direct) return { userId: direct };
|
|
|
|
if (input.url) {
|
|
const parsed = extractUserIdFromUrl(normalizeUrl(input.url));
|
|
if (parsed) return { userId: parsed };
|
|
}
|
|
|
|
throw new Error('xhh_get_user_profile requires user_id or url containing user_id');
|
|
}
|
|
|