Files
geo/apps/browser-extension/src/adapters/juejin.ts
T

47 lines
1.5 KiB
TypeScript
Raw Normal View History

import type { StoredPlatformState } from "../platforms";
import { connectedPlatform, disconnectedPlatform, fetchJson, unsupportedPublishResult } from "./common";
import type { AdapterContext, PlatformAdapter, PlatformPublishResult } from "./types";
type JuejinProfileResponse = {
data?: {
bui_user?: {
user_id?: string;
screen_name?: string;
avatar_large?: string;
avatar_url?: string;
};
};
};
async function detectJuejin(platform: StoredPlatformState): Promise<StoredPlatformState> {
try {
const response = await fetchJson<JuejinProfileResponse>("https://api.juejin.cn/user_api/v1/user/get_profile", {
method: "POST",
headers: {
"content-type": "application/json",
accept: "application/json",
},
body: "{}",
});
const user = response.data?.bui_user;
if (!user?.user_id || !user.screen_name) {
return disconnectedPlatform(platform, "未检测到掘金登录态");
}
return connectedPlatform(platform, user.user_id, user.screen_name, user.avatar_url ?? user.avatar_large ?? null);
} catch (error) {
return disconnectedPlatform(platform, error instanceof Error ? error.message : "掘金登录检测失败");
}
}
async function publishJuejin(): Promise<PlatformPublishResult> {
return unsupportedPublishResult("掘金");
}
export const juejinAdapter: PlatformAdapter = {
platformId: "juejin",
detect: detectJuejin,
publish: (_context: AdapterContext) => publishJuejin(),
};