站点只给本人用,云乐坊 SSO 授权、收藏/历史云同步、获客归因和账号切换记账都没有收益,却被登录状态检查和同步失败提示持续打扰。 - 删除 useYunlefunAuth / yunlefunAuth / yunlefunSsoConfig / yunlefunAuth 适配器、useUserDataSync / userDataSync / userData 适配器、三个 client 插件与 /auth/callback 回调页 - 抽出 adapters/cloudbase.ts,让「反馈一下」和匿名筛选信号不再依赖登录态;cook-feedback 云函数保留 - 删除 cook-user-data 云函数与 cook_user_data 权限规则,移除 @yunlefun/sso 依赖 - 「我的」页删掉登录按钮、账号卡、四个统计格和退出登录;「关于」页删掉项目作者、致谢名单和上游宣传链接 - 本地收藏/历史与导出导入保留,删除同步专用的 lastUid 记账 取舍见 docs/decisions/0005-drop-login-and-sync.md
72 lines
2.2 KiB
TypeScript
72 lines
2.2 KiB
TypeScript
/** Calls a cloud function and resolves its `result`. */
|
|
export type CloudbaseFunctionCaller = (name: string, data: Record<string, unknown>) => Promise<unknown>
|
|
|
|
interface CloudbaseApp {
|
|
auth: (options?: { persistence: 'session' }) => unknown
|
|
callFunction: (options: { name: string, data: Record<string, unknown> }) => Promise<{
|
|
errCode?: number | string
|
|
errMsg?: string
|
|
result?: unknown
|
|
} | undefined>
|
|
}
|
|
|
|
interface CloudbaseModule {
|
|
default?: {
|
|
init: (config: {
|
|
auth: { detectSessionInUrl: false }
|
|
env: string
|
|
region: 'ap-shanghai'
|
|
}) => CloudbaseApp
|
|
}
|
|
init?: (config: {
|
|
auth: { detectSessionInUrl: false }
|
|
env: string
|
|
region: 'ap-shanghai'
|
|
}) => CloudbaseApp
|
|
}
|
|
|
|
let cachedCloudbaseEnv = ''
|
|
let cachedCloudbaseApp: Promise<CloudbaseApp> | undefined
|
|
|
|
function getCloudbaseApp(env: string): Promise<CloudbaseApp> {
|
|
if (cachedCloudbaseApp) {
|
|
if (cachedCloudbaseEnv !== env)
|
|
return Promise.reject(new Error('CloudBase environment cannot change at runtime'))
|
|
return cachedCloudbaseApp
|
|
}
|
|
|
|
cachedCloudbaseEnv = env
|
|
cachedCloudbaseApp = (async () => {
|
|
const cloudbaseModule = await import('@cloudbase/js-sdk') as unknown as CloudbaseModule
|
|
const cloudbase = cloudbaseModule.default ?? cloudbaseModule
|
|
if (!cloudbase.init)
|
|
throw new Error('@cloudbase/js-sdk does not expose init()')
|
|
|
|
return cloudbase.init({
|
|
auth: { detectSessionInUrl: false },
|
|
env,
|
|
region: 'ap-shanghai',
|
|
})
|
|
})()
|
|
cachedCloudbaseApp.catch(() => {
|
|
cachedCloudbaseApp = undefined
|
|
})
|
|
return cachedCloudbaseApp
|
|
}
|
|
|
|
/** Call a cloud function on the configured CloudBase environment. */
|
|
export async function callCloudbaseFunction(
|
|
env: string,
|
|
name: string,
|
|
data: Record<string, unknown>,
|
|
): Promise<unknown> {
|
|
const app = await getCloudbaseApp(env)
|
|
// The auth client is created first so the call carries the app's session.
|
|
app.auth({ persistence: 'session' })
|
|
const response = await app.callFunction({ name, data })
|
|
// A missing result means the call itself failed (not deployed, crashed, …).
|
|
if (!response || response.result === undefined)
|
|
throw new Error(`CloudBase function ${name} failed: ${response?.errCode ?? response?.errMsg ?? 'no result'}`)
|
|
return response.result
|
|
}
|