yichong-canva2 2.0.0 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (39) hide show
  1. package/README.md +124 -24
  2. package/dist/api/error.js +6 -0
  3. package/dist/api/predictions.js +25 -6
  4. package/dist/api/reporting.js +4 -2
  5. package/dist/api/request.js +1 -0
  6. package/dist/api/uploads.js +121 -11
  7. package/dist/api/usage.js +3 -4
  8. package/dist/login/account.js +69 -0
  9. package/dist/login/context.js +1 -0
  10. package/dist/login/login/apis/index.js +17 -13
  11. package/dist/login/login/apis/request.js +33 -4
  12. package/dist/login/login/login.js +111 -103
  13. package/dist/login/privider.js +191 -125
  14. package/dist/types/api/chat.d.ts +6 -1
  15. package/dist/types/api/content.d.ts +5 -1
  16. package/dist/types/api/error.d.ts +14 -2
  17. package/dist/types/api/index.d.ts +6 -1
  18. package/dist/types/api/moderation.d.ts +5 -1
  19. package/dist/types/api/predictions.d.ts +5 -1
  20. package/dist/types/api/reporting.d.ts +7 -0
  21. package/dist/types/api/reporting_payload.d.ts +6 -1
  22. package/dist/types/api/request.d.ts +40 -2
  23. package/dist/types/api/transport.d.ts +7 -1
  24. package/dist/types/api/types.d.ts +231 -22
  25. package/dist/types/api/uploads.d.ts +7 -0
  26. package/dist/types/api/usage.d.ts +7 -1
  27. package/dist/types/index.d.ts +5 -0
  28. package/dist/types/login/account.d.ts +36 -0
  29. package/dist/types/login/context.d.ts +36 -35
  30. package/dist/types/login/index.d.ts +1 -0
  31. package/dist/types/login/interface.d.ts +35 -2
  32. package/dist/types/login/login/apis/index.d.ts +35 -17
  33. package/dist/types/login/login/apis/request.d.ts +10 -6
  34. package/dist/types/login/login/interface.d.ts +10 -1
  35. package/dist/types/login/login/login.d.ts +43 -21
  36. package/dist/types/login/privider.d.ts +34 -21
  37. package/dist/types/login/use_canva.d.ts +5 -2
  38. package/dist/types/login/version.d.ts +6 -0
  39. package/package.json +1 -1
@@ -3,3 +3,4 @@ export type { YiChongCanvaProviderProps } from './privider';
3
3
  export * from './interface';
4
4
  export * from './context';
5
5
  export * from './use_canva';
6
+ export type { AccountError, AccountStage, AccountOperation } from './account';
@@ -1,49 +1,78 @@
1
+ /**
2
+ * 账户服务返回的状态及 Provider 派生字段。首次登录前允许为空,使用嵌套字段前需判断是否存在。
3
+ * jwt 用于 AI 接口;账户 passport 由 Provider 私有保存,不放在此公开对象中。
4
+ */
1
5
  export interface UserStatus {
6
+ /** 账户服务颁发的 AI 接口 JWT;不同于 Canva 身份 token 和账户服务的 passport。 */
2
7
  jwt?: string;
8
+ /** 后端附带的鉴权时间信息,SDK 原样保留;仅读取这些值不会自动刷新 JWT。 */
3
9
  api_auth?: {
4
10
  time: number;
5
11
  expire_at: number;
6
12
  };
13
+ /** 系统账户资料;登录/刷新轮询以取得 profile 作为账户就绪条件。 */
7
14
  profile?: {
15
+ /** 我们系统的用户 ID,作为通用请求参数 uid;不是 Canva 的 userId。 */
8
16
  id: string;
9
17
  username: string;
10
18
  email: string;
11
19
  avatar: string;
20
+ /** 后端账户资料中的原始 token 字段;新 AI API 鉴权使用外层 jwt。 */
12
21
  token: string;
13
22
  from_app: string;
14
23
  expire_at: number;
24
+ /** 账户配置的免费与月度上限;对应已用量在 subscription 中。 */
15
25
  usage_limits: {
26
+ /** 免费额度基数;值为 -1 时 Provider 视作免费无限使用。 */
16
27
  free_limit: number;
28
+ /** 月额度上限;值为 -1 时对外额度 getter 用 null 表示无限。 */
17
29
  monthly_usage_limit: number;
18
30
  };
19
31
  };
32
+ /** 个人订阅和额度计数;字段保留后端命名,避免把积分余额和月用量混为一谈。 */
20
33
  subscription?: {
34
+ /** Provider 根据详情和 ends_at 派生 isSubscribed;无详情通常按非订阅处理。 */
21
35
  detail?: {
36
+ /** 后端提供的到期时间;Provider 用 Date 比较,无到期值时视作持续有效。 */
22
37
  ends_at: string | null | number;
38
+ /** 后端套餐名称;Provider 只按 month/year/free 关键词派生展示用 planName。 */
23
39
  name: string;
24
40
  };
41
+ /** 后端可返回此值,Provider 也会根据详情写回派生状态。 */
25
42
  is_subscribed?: boolean;
43
+ /** 积分/免费次数字段;normal 模式下负值按 free_limit + free_cnt 计算余额。 */
26
44
  free_cnt?: number;
45
+ /** 当前月已使用额度,不是剩余额度。 */
27
46
  monthly_usage_cnt?: number;
47
+ /** 已使用的免费额度,server_credits 模式用于计算免费用户余额。 */
28
48
  free_usage_cnt?: number;
49
+ /** 后端试用标记,保留数字形态;SDK 不将它转换为布尔值。 */
29
50
  on_trial?: number;
30
51
  };
52
+ /** 团队订阅原始数据;当前订阅派生逻辑只在个人 subscription.detail 存在时检查此字段。 */
31
53
  team_subscription?: {
32
54
  detail: {
33
55
  name: string;
34
56
  ends_at: string | null | number;
35
57
  };
36
58
  };
59
+ /** 历史云盘授权数据,Provider 原样保留;不是 AI 接口鉴权来源。 */
37
60
  drive?: {
38
61
  fresh_token: string;
39
62
  };
63
+ /** Canva 验证服务返回的用户 ID,作为 platform_uid;不同于 profile.id。 */
40
64
  userId?: string;
65
+ /** Provider 派生的展示标签;不能用标签代替实际订阅有效性或额度检查。 */
41
66
  planName?: 'Free' | 'Pro Monthly plan' | 'Pro Yearly plan' | 'Premium';
42
67
  }
68
+ /** 历史应用读取的全局兼容字段;新组件应使用 useYiChongCanva,避免直接依赖 window。 */
43
69
  declare global {
44
70
  interface Window {
71
+ /** 历史轮询入口;该旧声明未描述 Promise,完整参数与返回类型以 client.pollUserStatus 为准。 */
45
72
  pollUserSubscribe: () => void;
73
+ /** 从 Canva SDK 获得的身份 token,仅用于换取 Canva 用户身份。 */
46
74
  g_canva_token: string;
75
+ /** 初次登录写入的 JWT 兼容副本;后续刷新不依赖此字段,新 API 使用 Provider 内部状态。 */
47
76
  g_token: string;
48
77
  }
49
78
  }
@@ -53,7 +82,8 @@ declare global {
53
82
  * 语义:打开外部定价页后进入一段有 TTL 的轮询会话 ——
54
83
  * 快阶段每 fastIntervalMs 拉一次 status,持续 fastPhaseMs 后降频到 slowIntervalMs,
55
84
  * 检测到「相对打开定价页前基线」的订阅/额度变化即成功;
56
- * 会话满 sessionTtlMs 或手动 stopCurrentPolling() 时补拉一次 status 后 resolve。
85
+ * 在轮次之间发现 sessionTtlMs 到期,或手动 stopCurrentPolling() 后,补拉一次 status 再 resolve。
86
+ * 已发出的请求不会因 TTL 或停止操作而被取消;返回状态仍需检查,resolve 本身不表示支付成功。
57
87
  * 注意:回到 Canva 侧栏(focus / visibilitychange)不会终止会话 ——
58
88
  * Canva 拿不到定价页关闭事件,回侧栏往往表示定价页还开着。
59
89
  */
@@ -64,9 +94,12 @@ export interface CheckoutSessionOptions {
64
94
  fastPhaseMs?: number;
65
95
  /** 慢阶段轮询间隔,默认 15000ms */
66
96
  slowIntervalMs?: number;
67
- /** 会话总时长(TTL),默认 300000ms(5 分钟),到期补刷一次并 resolve */
97
+ /** 轮询调度时长(TTL),默认 300000ms(5 分钟);不限制单次在途请求,结束后另有补刷等待。 */
68
98
  sessionTtlMs?: number;
69
99
  }
100
+ /** 本地模拟模式:noTest 使用真实数据;其余分别模拟高积分、有效年订阅、已过期年订阅。 */
70
101
  export type TestMode = 'noTest' | 'credits' | 'subscribed' | 'expired';
102
+ /** 历史余额计算模式;server_credits 使用服务端用量字段和 maxAvailablePoints 派生本地余额。 */
71
103
  export type Mode = 'normal' | 'server_credits';
104
+ /** canUseLimit 的本地判断原因;不是后端错误码。undefined 表示没有拒绝原因。 */
72
105
  export type canUseError = 'userStatus_error' | 'monthly_usage_limit' | 'no_credits' | undefined;
@@ -1,29 +1,47 @@
1
+ import { AccountRequestOptions } from '../../account';
2
+ import { UserStatus } from '../../interface';
1
3
  import { CommonRequestContext } from '../../../api/request';
2
4
  /**
3
- * 由token和appId获取用户唯一的id
4
- * @param token canva颁发的token
5
- * @param appId 当前应用的appId
6
- * @returns
5
+ * 将 Canva 身份 token 和应用 ID 交给验证服务,读取 verified.userId。
6
+ * 返回的是 Canva 平台身份,不能与账户服务 profile.id 混用;此请求尚不需要 AI JWT。
7
+ * @param token Canva SDK 当前颁发的用户 token。
8
+ * @param appId 当前 Canva 应用 ID。
9
+ * @param context 登录阶段已知的通用参数和当前语言;身份未验证前可能没有 uid/platform_uid。
10
+ * @returns 验证服务的原始 JSON,结构校验由调用方读取 verified.userId 时体现。
7
11
  */
8
- declare function getUserIds(token: string, appId: string, context: CommonRequestContext): Promise<any>;
12
+ declare function getUserIds(token: string, appId: string, context: CommonRequestContext, options?: AccountRequestOptions): Promise<{
13
+ verified: {
14
+ userId: string;
15
+ };
16
+ }>;
9
17
  /**
10
- * 利用id登录用户
11
- * @param userId 获取到的用户唯一id
18
+ * 用已验证的 Canva ID 登录产品对应的账户;接口可能建立该应用的账户关系。
19
+ * @param userId Canva 用户 ID,写入请求体 canva_id。
20
+ * @param productName 后端渠道路径片段,必须与应用登记值一致。
21
+ * @returns 账户服务原始 JSON;保留响应形状;HTTP/业务错误保留安全的 AccountRequestError 诊断。
12
22
  */
13
- declare function handleUserLoginByCanvaId(userId: string, productName: string, context: CommonRequestContext): Promise<any>;
23
+ declare function handleUserLoginByCanvaId(userId: string, productName: string, context: CommonRequestContext, options?: AccountRequestOptions): Promise<Record<string, unknown>>;
14
24
  /**
15
- * 由id获取通行证
16
- * @param userId
17
- * @param appName
18
- * @returns
25
+ * 获取账户服务 passport,供 status 和消费请求的 X-Access-Ticket 使用。
26
+ * 优先读取 localStorage 缓存;缓存键历史上只包含 Canva userId,不包含 productName。
27
+ * 此处不主动检查过期;后续 status 缺少 profile 时,getUserProfile 会删除缓存再申请。
28
+ *
29
+ * imgkits 使用不带产品前缀的历史路径,其余产品使用产品前缀。
30
+ * @returns 命中缓存时仅返回 { data: { passport } };网络请求时返回原始 JSON 并缓存 passport。
19
31
  */
20
- declare function getPassport(userId: string, productName: string, context: CommonRequestContext): Promise<any>;
32
+ declare function getPassport(userId: string, productName: string, context: CommonRequestContext, options?: AccountRequestOptions): Promise<{
33
+ data: {
34
+ passport: string;
35
+ };
36
+ }>;
21
37
  /**
22
- * 由通行证获取用户信息
23
- * @param passport
24
- * @returns
38
+ * 以账户 passport 读取产品状态,响应 data 包含 profile、subscription 和 AI 请求用的 jwt。
39
+ * @param passport 账户票据;不是 Canva token,也不是 status 响应中的 jwt。
40
+ * @returns 账户服务原始 JSON;轮询层负责判断 data.profile 是否已就绪。
25
41
  */
26
- declare function getUserStatusByPassport(passport: string, productName: string, context: CommonRequestContext): Promise<any>;
42
+ declare function getUserStatusByPassport(passport: string, productName: string, context: CommonRequestContext, options?: AccountRequestOptions): Promise<{
43
+ data: UserStatus;
44
+ }>;
27
45
  declare const apis: {
28
46
  getUserIds: typeof getUserIds;
29
47
  handleUserLoginByCanvaId: typeof handleUserLoginByCanvaId;
@@ -1,10 +1,14 @@
1
1
  import { CommonRequestContext } from '../../../api/request';
2
+ import { AccountStage, AccountRequestOptions } from '../../account';
2
3
  /**
3
- * 请求json数据
4
- * @param option RequestInit & { url: string }
5
- * @returns
4
+ * 账户请求保留旧接口的响应形状,但检查 HTTP、JSON 和 ok/code/success。
5
+ * 每次请求含读取正文最多 10 秒,接收整个登录/刷新流程的取消信号。
6
+ * validate 是各接口的必要字段校验;诊断只记录 origin/path,绝不记录 token 查询参数或响应正文。
6
7
  */
7
- declare const requestUrl: (option: RequestInit & {
8
+ export declare function accountRequest<T>(option: RequestInit & {
8
9
  url: string;
9
- }, context?: CommonRequestContext) => Promise<any>;
10
- export default requestUrl;
10
+ }, context: CommonRequestContext, stage: AccountStage, options?: AccountRequestOptions, validate?: (body: Record<string, unknown>) => boolean): Promise<T>;
11
+ /** 远程版本查询仍沿用旧响应协议;账户登录使用上面的有界 accountRequest。 */
12
+ export default function requestUrl(option: RequestInit & {
13
+ url: string;
14
+ }, context?: CommonRequestContext): Promise<any>;
@@ -1,16 +1,25 @@
1
1
  import { ReactNode } from 'react';
2
2
  import { UserStatus } from '../interface';
3
3
  /**
4
- * @interface 登录组件props
4
+ * 旧登录组件的兼容类型;当前公开入口使用 YiChongCanvaProviderProps。
5
+ * 保留旧类型形状便于历史代码引用,不表示 Provider 会读取同名的 onLogin 参数。
5
6
  */
6
7
  export interface UseCanvaLoginProps {
8
+ /** 旧登录组件使用的账户就绪回调。 */
7
9
  onLogin?: (user: UserStatus) => void;
10
+ /** Canva 应用 ID,用于平台身份验证。 */
8
11
  appId: string;
12
+ /** 旧组件包裹的子节点。 */
9
13
  children?: ReactNode;
14
+ /** 后端产品渠道。 */
10
15
  productName: string;
11
16
  }
17
+ /** 历史登录 Context 声明;当前 useYiChongCanva 的完整能力见 YiChongCanvaContextType。 */
12
18
  export interface CanvaLoginContextProps {
19
+ /** 历史账户快照,未登录时可能为空。 */
13
20
  userStatus?: UserStatus;
21
+ /** Canva 平台用户 ID。 */
14
22
  userId?: string;
23
+ /** 旧版用于接收登录回调的接口;当前 Provider 使用 onUpdateUserStatus。 */
15
24
  onLogin?: (userStatus: (userStatus: UserStatus) => void) => void;
16
25
  }
@@ -1,6 +1,12 @@
1
1
  import { UserStatus } from './../interface';
2
+ import { AccountRequestOptions } from '../account';
2
3
  import { CommonRequestContext } from '../../api/request';
3
- export declare function handleLogin({ appId, productName, onPassport, getRequestContext, }: LoginProps): Promise<UserStatus>;
4
+ /**
5
+ * Canva token → 平台身份 → 产品账户 → passport/status。
6
+ * 单步超时由 accountStep 处理,整个登录及重试的 30 秒预算由 Provider 管理。
7
+ * 保留具体错误;只在产品登录成功后缓存 userId,失败重试不能跳过尚未成功的 canvaid-login。
8
+ */
9
+ export declare function handleLogin({ appId, productName, onPassport, getRequestContext, signal, onStep }: LoginProps): Promise<UserStatus>;
4
10
  /**
5
11
  * 订阅比较基线:打开定价页「之前」的用户状态快照。
6
12
  * 由 Provider 在会话开始时用当时的 userStatus 生成(takeSubscribeBaseline),
@@ -8,8 +14,11 @@ export declare function handleLogin({ appId, productName, onPassport, getRequest
8
14
  * 且 detail 为 undefined 时会误判成功(1.1.26 的现网 bug)。
9
15
  */
10
16
  export interface SubscribeBaseline {
17
+ /** 开始检查前的积分字段;缺失为 null,与 0 区分。 */
11
18
  free_cnt: number | null;
19
+ /** 订阅详情的序列化快照;无详情统一为 null,便于判断是否发生变化。 */
12
20
  detailJson: string | null;
21
+ /** 开始检查前的可用额度;无限额或无法用于数值比较时为 null。 */
13
22
  available: number | null;
14
23
  }
15
24
  /**
@@ -21,58 +30,71 @@ export interface SubscribeBaseline {
21
30
  export declare function takeSubscribeBaseline(userStatus: UserStatus): SubscribeBaseline;
22
31
  /**
23
32
  * 用「原始响应体」计算可用额度的纯函数。
24
- * 不要用 Provider 的 getLimitUsage():它读的是 React state,轮询期间不会更新。
33
+ * 不要用 Provider 的 getLimitUsage():它读的是 React state,无法代表本轮尚未合并的原始响应。
25
34
  * 口径对齐 Provider:订阅生效 = 月额度剩余(clamp 到 >=0)+ 积分;否则 = 积分。
26
- * monthly_usage_limit 为 -1(无限额度)时返回 null。
35
+ * monthly_usage_limit <= -1(无限额度)时返回 null。本函数仅比较个人订阅,不读取团队订阅。
27
36
  */
28
37
  export declare function computeAvailableLimit(userStatus: UserStatus): number | null;
29
38
  /**
30
- * 默认的「相对基线是否已付费/到账」判断(对齐 Chrome 插件 loopSubscribe 思路):
39
+ * 默认的「相对基线账户是否变化」判断,用于决定何时结束支付状态检查:
31
40
  * 1. subscription.detail 出现或变化(订阅、换套餐、续费改 ends_at,JSON 全量比较)
32
41
  * 2. free_cnt 变多(买积分包;只认「增加」——会话中在侧栏消耗积分导致的减少不算成功)
33
42
  * 3. 可用额度比基线变大(月订用户 free_cnt 常为 0,只盯它会把会话打满)
34
- * 注意:monthly_usage_cnt 自增(用量上报)不构成成功。
43
+ * 注意:monthly_usage_cnt 自增(用量上报)不构成成功;详情删除等变化也会返回 true,
44
+ * 因而此结果只表示需要更新账户视图,不是支付服务发出的交易成功确认。
35
45
  */
36
46
  export declare function isSubscribeChanged(current: UserStatus, baseline: SubscribeBaseline): boolean;
37
47
  /**
38
- * 拉取 / 轮询用户信息。
39
- *
40
- * 两种模式(isSubscribe 区分):
41
- * - isSubscribe = false(handleLogin / refreshUserStatus):拿到带 profile 的
42
- * userStatus 立刻 successCallback 并返回,行为与 1.1.26 一致
43
- * (1.1.26 会在 finally 里多睡一轮 3s 才 resolve,属实现缺陷,已去掉;请求次数不变)。
44
- * - isSubscribe = true(结账会话):相对 baseline 检测订阅/额度变化,变了才成功。
45
- * 未传 baseline 时保持旧语义:用第一轮成功响应拍快照(但 detail 快照 bug 已修)。
46
- *
47
- * 结束方式:
48
- * 1. 成功:successCallback + return userStatus
49
- * 2. 手动停止(onStop 交出的函数被调用):打断等待中的 sleep,return undefined(不算失败)
50
- * 3. maxCount 次数耗尽或 maxDurationMs 到期:failCallback + throw
48
+ * 普通读取默认最多两次;支付轮询仍按订阅基线判断,连续三次网络/服务端错误即结束。
49
+ * 一次无 profile 的响应允许刷新 passport 后再读一次;不把任意服务器故障当作票据失效。
50
+ * 总预算覆盖 passport、status、等待和响应体读取;主动停止/健康的支付 TTL 到期正常返回 undefined。
51
51
  */
52
- export declare function getUserProfile({ maxCount, interval, maxDurationMs, productName, userId, successCallback, failCallback, isSubscribe, baseline, isChanged, onStop, onPassport, getRequestContext, }: UserProfileProps): Promise<UserStatus | undefined>;
53
- export interface UserProfileProps {
52
+ export declare function getUserProfile({ maxCount, interval, maxDurationMs, productName, userId, successCallback, failCallback, isSubscribe, baseline, isChanged, onStop, onPassport, getRequestContext, signal, onStep, }: UserProfileProps): Promise<UserStatus | undefined>;
53
+ /** 内部账户读取/支付轮询配置;应用通常通过 client.refreshUserStatus 或 pollUserStatus 使用。 */
54
+ export interface UserProfileProps extends AccountRequestOptions {
55
+ /** status 尝试次数上限,默认 2;passport 请求另行发生,不包含在此计数内。 */
54
56
  maxCount?: number;
57
+ /** 两轮 status 之间的等待毫秒数,默认 3000;函数形式在每轮计算,用于支付快慢阶段切换。 */
55
58
  interval?: number | (() => number);
59
+ /** 从函数开始计算的轮询预算;默认读取 30 秒、支付检查 5 分钟;到时取消在途请求。 */
56
60
  maxDurationMs?: number;
61
+ /** 后端登记的产品渠道,用于账户接口路径和请求上下文。 */
57
62
  productName: string;
63
+ /** Canva 用户 ID;系统用户 ID 从 status.profile.id 取得。 */
58
64
  userId: string;
65
+ /** 达到本次模式的成功条件后调用;入参尚未由此函数补写 Canva userId。 */
59
66
  successCallback?: (userStatus: UserStatus) => void;
67
+ /** 轮询预算耗尽时调用;手动停止或首次 passport 请求失败不会走此回调。 */
60
68
  failCallback?: () => void;
69
+ /** false=profile 就绪即可返回;true=持续检查相对 baseline 的订阅/额度变化,默认 false。 */
61
70
  isSubscribe?: boolean;
71
+ /** 支付前快照;提供后不被第一轮覆盖,省略时首个有效 status 仅用于建立快照。 */
62
72
  baseline?: SubscribeBaseline;
73
+ /** 订阅模式的自定义结束判定;省略时使用 isSubscribeChanged。 */
63
74
  isChanged?: (current: UserStatus, baseline: SubscribeBaseline) => boolean;
75
+ /** 取得停止调度函数;立即取消等待和 status/passport 网络请求。 */
64
76
  onStop?: (fn: () => void) => void;
77
+ /** status 具有 profile 后传出已验证可用的账户票据、Canva ID 和系统 ID,供 Provider 私有缓存。 */
65
78
  onPassport?: (passport: string, userId: string, uid: string) => void;
79
+ /** 每次账户请求读取最新语言/身份上下文,避免轮询闭包捕获过期值。 */
66
80
  getRequestContext?: () => CommonRequestContext;
67
81
  }
68
- export interface LoginProps {
82
+ /** 内部登录参数;callback 和 requestConfig 为保留字段,当前 handleLogin 不读取它们。 */
83
+ export interface LoginProps extends AccountRequestOptions {
84
+ /** 历史兼容声明;当前登录结果由 Promise 返回,此回调不被调用。 */
69
85
  callback?: (state: LoginState) => void;
86
+ /** Canva 应用 ID,用于 token 验证。 */
70
87
  appId: string;
88
+ /** 后端登记的产品渠道。 */
71
89
  productName: string;
90
+ /** 历史兼容声明;当前不透传到 getUserProfile。 */
72
91
  requestConfig?: UserProfileProps;
92
+ /** 转交账户 passport 的内部回调。 */
73
93
  onPassport?: UserProfileProps["onPassport"];
94
+ /** 转交各登录阶段使用的实时请求上下文。 */
74
95
  getRequestContext?: UserProfileProps["getRequestContext"];
75
96
  }
97
+ /** 旧阶段式登录回调的数据类型,保留兼容声明;当前 handleLogin 不发出此类事件。 */
76
98
  export interface LoginState {
77
99
  status: "finish" | "error" | "start" | "user_id";
78
100
  data?: {
@@ -1,48 +1,61 @@
1
1
  import { default as React } from 'react';
2
2
  import { Mode, TestMode, UserStatus } from './interface';
3
3
  /**
4
- * @interface 参数
5
- * @property {React.ReactNode} children 子节点
6
- * @property {string} appId 应用ID
7
- * @property {string} productName 产品名称
8
- * @property {(userStatus: UserStatus) => void} onUpdateUserStatus 用户状态更新回调, 一般可以不用
9
- * @property {number} freeTimes 免费次数, 默认为1
10
- * @property {string} baseUrl 服务器地址
11
- * @property {boolean} autoLogin 自动登录, 默认为true
4
+ * 应用入口的账户与 API 配置。不同服务有独立根地址,修改其中一个不会自动改写其他服务地址。
5
+ * productName 应使用后端登记的应用渠道名称,而不是界面中可随意修改的标题。
12
6
  */
13
7
  export interface YiChongCanvaProviderProps {
8
+ /** 被包裹的业务组件;这些后代组件可通过 useYiChongCanva 读取账户和调用接口。 */
14
9
  children?: React.ReactNode;
10
+ /** Canva 应用 ID,用于验证 Canva 身份,并组成远程应用版本配置键 canva_<appId>。 */
15
11
  appId: string;
12
+ /** 后端应用渠道,影响登录路径、app_channel、X-Access-Channel 和定价页路径。 */
16
13
  productName: string;
14
+ /** 登录、成功刷新、支付检查发现变化时回调;接收该次响应,手动 updateUserStatus 不触发。 */
17
15
  onUpdateUserStatus?: (userStatus: UserStatus) => void;
16
+ /** 账户扣费地址(含所需应用路径),无默认值;登录/status 仍使用固定 usa.imgkits.com 地址。 */
18
17
  baseUrl: string;
19
- /** 预测与文本审核接口地址;baseUrl 仍用于原有账户扣费接口。 */
18
+ /** 预测与文本审核服务根地址,默认 https://api.imgkits.com;不覆盖账户、上传或 Chat 地址。 */
20
19
  apiBaseUrl?: string;
21
20
  /** 内容提取服务根地址,默认 https://ai.imgkits.com。 */
22
21
  contentBaseUrl?: string;
23
22
  /** 大模型 Chat 服务根地址,默认 https://ai.imgkits.com。 */
24
23
  chatBaseUrl?: string;
25
- /** R2 签名服务根地址,默认 https://ai.imgkits.com。 */
24
+ /** 既有 Cloudflare R2 签名/分片接口根地址,默认 https://ai.imgkits.com。 */
26
25
  uploadBaseUrl?: string;
27
- /** 每次 API 请求读取当前界面语言,默认使用浏览器语言。 */
26
+ /** 每次请求获取当前语言,用于 Accept-Language;缺省回退到浏览器语言,不要固定捕获过期语言。 */
28
27
  getLocale?: () => string;
28
+ /** 默认 true,挂载后自动登录;false 时由 client.handleLogin/manualLogin 显式启动。 */
29
29
  autoLogin?: boolean;
30
+ /** 初始模拟模式,默认 noTest;挂载 effect 会将开发环境设为 credits、其他环境设为 noTest。 */
30
31
  testMode?: TestMode;
32
+ /** 应用自身的数字发布版本(不是 npm 包版本);与远程版本比较以判断审核/测试阶段,运行时默认 1。 */
31
33
  version: number;
34
+ /** 本地余额计算模式,默认 normal;server_credits 按账户类型结合服务端用量计算。 */
32
35
  mode?: Mode;
36
+ /** server_credits 模式下订阅用户余额计算的总额度,默认 1;不是后端月限额的覆盖配置。 */
33
37
  maxAvailablePoints?: number;
38
+ /** 自动登录失败后的额外重试次数,默认 3;首次请求不计入,重试间隔 3 秒,整个登录受 30 秒总上限约束。 */
34
39
  retry?: number;
35
40
  }
36
41
  /**
37
- * YiChongCanvaProvider 全局登录,积分等方法提供
38
- * @param {object} params0
39
- * @param {children} params0.children 子元素
40
- * @param {string} params0.appId 应用id
41
- * @param {string} params0.productName 产品名称
42
- * @param {Function} params0.onUpdateUserStatus 用户信息更新后的回调
43
- * @param {string} params0.baseUrl 链接
44
- * @param {boolean} params0.autoLogin 是否自动登录
45
- * @param {string} params0.mode 测试模式
46
- * @returns
42
+ * 在应用入口包裹一次,为后代组件提供登录、账户额度、支付检查和后端同名 API。
43
+ *
44
+ * 登录流程依赖 Canva SDK、window 和 localStorage,应在 Canva 浏览器应用中使用。
45
+ * AI 请求使用 userStatus.jwt;账户消费使用登录过程中取得的私有 passport,两种凭据不可互换。
46
+ * Provider 只提供业务原语:生成/上传 API 不会顺带扣费,应用自行组织审核、生成和消费的先后关系。
47
+ *
48
+ * @example
49
+ * // 入口包裹已有的 App;替换为自己的 Canva App ID 和后端登记的渠道。
50
+ * <YiChongCanvaProvider
51
+ * appId="YOUR_CANVA_APP_ID"
52
+ * productName="your_channel"
53
+ * baseUrl="https://usa.imgkits.com/your_channel"
54
+ * version={1}
55
+ * >
56
+ * <App />
57
+ * </YiChongCanvaProvider>
58
+ * // App 或其子组件内:const client = useYiChongCanva();
59
+ * // 账号就绪后,直接调用 client.createSpecifyPrediction({ owner, name, input })。
47
60
  */
48
61
  export declare const YiChongCanvaProvider: ({ children, appId, productName, onUpdateUserStatus, baseUrl, apiBaseUrl, contentBaseUrl, chatBaseUrl, uploadBaseUrl, getLocale, autoLogin, testMode: _testMode, version, mode, retry, maxAvailablePoints, }: YiChongCanvaProviderProps) => React.JSX.Element;
@@ -1,6 +1,9 @@
1
1
  import { YiChongCanvaContextType } from './context';
2
2
  /**
3
- * 全局钩子,用于获取上下文
4
- * @returns
3
+ * 在 React 函数组件或自定义 Hook 中读取最近一层 YiChongCanvaProvider。
4
+ * Provider 应在应用入口包裹业务组件;从返回的 client 直接调用后端同名方法即可。
5
+ *
6
+ * @returns 账户状态及 API 客户端。状态会触发组件重渲染,调用 API 时会使用最新的账户凭据。
7
+ * @remarks 缺少 Provider 时当前 Context 有默认对象:账户字段为空,后端 API 调用拒绝 provider_missing。
5
8
  */
6
9
  export declare const useYiChongCanva: () => YiChongCanvaContextType;
@@ -1,5 +1,11 @@
1
1
  import { CommonRequestContext } from '../api/request';
2
+ /** 远程配置中的应用数字版本,用于 Provider 判断审核/测试阶段,不是 npm 包的语义版本。 */
2
3
  export interface AppVersion {
3
4
  version: number;
4
5
  }
6
+ /**
7
+ * 从账户服务站点配置读取版本;Provider 使用 canva_<appId> 作为 key。
8
+ * success=false 时运行时返回空对象,Provider 会对缺少数字 version 的情况按非测试处理。
9
+ * 网络失败或配置 value 不是有效 JSON 时会 reject;此函数不重试、不缓存,也不归一化为 ApiError。
10
+ */
5
11
  export declare function getAppVersion(key: string, context?: CommonRequestContext): Promise<AppVersion>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "yichong-canva2",
3
- "version": "2.0.0",
3
+ "version": "2.2.0",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",