yichong-canva2 2.1.0 → 2.3.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.
- package/README.md +91 -4
- package/dist/api/reporting.js +4 -2
- package/dist/api/request.js +5 -3
- package/dist/api/vtt.js +62 -0
- package/dist/login/account.js +69 -0
- package/dist/login/context.js +7 -0
- package/dist/login/login/apis/index.js +17 -13
- package/dist/login/login/apis/request.js +33 -4
- package/dist/login/login/login.js +111 -103
- package/dist/login/privider.js +194 -125
- package/dist/types/api/index.d.ts +1 -1
- package/dist/types/api/reporting.d.ts +1 -1
- package/dist/types/api/request.d.ts +2 -0
- package/dist/types/api/types.d.ts +59 -0
- package/dist/types/api/vtt.d.ts +9 -0
- package/dist/types/login/account.d.ts +36 -0
- package/dist/types/login/context.d.ts +10 -7
- package/dist/types/login/index.d.ts +1 -0
- package/dist/types/login/login/apis/index.d.ts +17 -5
- package/dist/types/login/login/apis/request.d.ts +10 -9
- package/dist/types/login/login/login.d.ts +14 -28
- package/dist/types/login/privider.d.ts +2 -2
- package/package.json +1 -1
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
import { auth } from "@canva/user";
|
|
2
2
|
import apis from "./apis/index.js";
|
|
3
|
+
import { isObject } from "../../api/request.js";
|
|
4
|
+
import { ApiError } from "../../api/error.js";
|
|
5
|
+
import { withRequest, throwIfAborted } from "../../api/transport.js";
|
|
6
|
+
import { AccountRequestError, retryableAccountError, accountDelay, accountStep } from "../account.js";
|
|
3
7
|
function accountContext(productName, userId, getContext) {
|
|
4
8
|
const context = getContext == null ? void 0 : getContext();
|
|
5
9
|
const current = context == null ? void 0 : context.userStatus;
|
|
@@ -10,35 +14,32 @@ function accountContext(productName, userId, getContext) {
|
|
|
10
14
|
};
|
|
11
15
|
}
|
|
12
16
|
auth.initOauth();
|
|
13
|
-
async function handleLogin({
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
const userIdResult = await apis.getUserIds(token, appId, accountContext(productName, userId, getRequestContext));
|
|
26
|
-
userId = userIdResult.verified.userId;
|
|
27
|
-
window.userId = userId;
|
|
28
|
-
await apis.handleUserLoginByCanvaId(userId, productName, accountContext(productName, userId, getRequestContext));
|
|
29
|
-
}
|
|
30
|
-
const userStatus = await getUserProfile({
|
|
31
|
-
userId,
|
|
32
|
-
productName,
|
|
33
|
-
onPassport,
|
|
34
|
-
getRequestContext
|
|
35
|
-
});
|
|
36
|
-
window.g_token = userStatus.jwt || "";
|
|
37
|
-
userStatus.userId = userId;
|
|
38
|
-
return userStatus;
|
|
39
|
-
} catch (e) {
|
|
40
|
-
throw new Error("login failed");
|
|
17
|
+
async function handleLogin({ appId, productName, onPassport, getRequestContext, signal, onStep }) {
|
|
18
|
+
const options = { signal, onStep };
|
|
19
|
+
let userId = window.userId;
|
|
20
|
+
if (!userId) {
|
|
21
|
+
const token = await accountStep("canva_token", options, () => auth.getCanvaUserToken());
|
|
22
|
+
if (signal) throwIfAborted(signal);
|
|
23
|
+
window.g_canva_token = token;
|
|
24
|
+
const identity = await apis.getUserIds(token, appId, accountContext(productName, userId, getRequestContext), options);
|
|
25
|
+
userId = identity.verified.userId;
|
|
26
|
+
await apis.handleUserLoginByCanvaId(userId, productName, accountContext(productName, userId, getRequestContext), options);
|
|
27
|
+
if (signal) throwIfAborted(signal);
|
|
28
|
+
window.userId = userId;
|
|
41
29
|
}
|
|
30
|
+
const userStatus = await getUserProfile({
|
|
31
|
+
userId,
|
|
32
|
+
productName,
|
|
33
|
+
onPassport,
|
|
34
|
+
getRequestContext,
|
|
35
|
+
...options,
|
|
36
|
+
maxCount: 1
|
|
37
|
+
});
|
|
38
|
+
if (signal) throwIfAborted(signal);
|
|
39
|
+
if (!userStatus) throw new AccountRequestError("Account request canceled.", "aborted", "status");
|
|
40
|
+
window.g_token = userStatus.jwt || "";
|
|
41
|
+
userStatus.userId = userId;
|
|
42
|
+
return userStatus;
|
|
42
43
|
}
|
|
43
44
|
function takeSubscribeBaseline(userStatus) {
|
|
44
45
|
const sub = userStatus == null ? void 0 : userStatus.subscription;
|
|
@@ -81,7 +82,7 @@ function isSubscribeChanged(current, baseline) {
|
|
|
81
82
|
return false;
|
|
82
83
|
}
|
|
83
84
|
async function getUserProfile({
|
|
84
|
-
maxCount =
|
|
85
|
+
maxCount = 2,
|
|
85
86
|
interval,
|
|
86
87
|
maxDurationMs,
|
|
87
88
|
productName,
|
|
@@ -93,87 +94,94 @@ async function getUserProfile({
|
|
|
93
94
|
isChanged,
|
|
94
95
|
onStop,
|
|
95
96
|
onPassport,
|
|
96
|
-
getRequestContext
|
|
97
|
+
getRequestContext,
|
|
98
|
+
signal,
|
|
99
|
+
onStep
|
|
97
100
|
}) {
|
|
98
|
-
|
|
99
|
-
let
|
|
100
|
-
const
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
const context = accountContext(productName, userId, getRequestContext);
|
|
104
|
-
return { ...context, userStatus: { ...context.userStatus, profile: latestProfile ?? context.userStatus.profile } };
|
|
105
|
-
};
|
|
106
|
-
let passportResult = await apis.getPassport(userId, productName, requestContext());
|
|
107
|
-
let passport = (_a = passportResult.data) == null ? void 0 : _a.passport;
|
|
108
|
-
let isStop = false;
|
|
109
|
-
let cancelSleep = null;
|
|
110
|
-
let snapshot = baseline ?? null;
|
|
101
|
+
const controller = new AbortController();
|
|
102
|
+
let stopped = false;
|
|
103
|
+
const cancel = () => controller.abort();
|
|
104
|
+
if (signal == null ? void 0 : signal.aborted) controller.abort();
|
|
105
|
+
signal == null ? void 0 : signal.addEventListener("abort", cancel, { once: true });
|
|
111
106
|
onStop == null ? void 0 : onStop(() => {
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
cancelSleep == null ? void 0 : cancelSleep();
|
|
107
|
+
stopped = true;
|
|
108
|
+
controller.abort();
|
|
115
109
|
});
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
latestProfile
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
110
|
+
let lastError;
|
|
111
|
+
let lastStage = "canva_passport";
|
|
112
|
+
let lastApi;
|
|
113
|
+
try {
|
|
114
|
+
return await withRequest({ signal: controller.signal, timeoutMs: maxDurationMs ?? (isSubscribe ? 3e5 : 3e4) }, async (requestSignal) => {
|
|
115
|
+
const options = { signal: requestSignal, onStep: (stage, api) => {
|
|
116
|
+
lastStage = stage;
|
|
117
|
+
lastApi = api;
|
|
118
|
+
onStep == null ? void 0 : onStep(stage, api);
|
|
119
|
+
} };
|
|
120
|
+
let latestProfile;
|
|
121
|
+
const requestContext = () => {
|
|
122
|
+
const context = accountContext(productName, userId, getRequestContext);
|
|
123
|
+
return { ...context, userStatus: { ...context.userStatus, profile: latestProfile ?? context.userStatus.profile } };
|
|
124
|
+
};
|
|
125
|
+
let passport = (await apis.getPassport(userId, productName, requestContext(), options)).data.passport;
|
|
126
|
+
let snapshot = baseline ?? null;
|
|
127
|
+
let consecutiveFailures = 0;
|
|
128
|
+
let renewedPassport = false;
|
|
129
|
+
for (let count = 0; count < maxCount; count++) {
|
|
130
|
+
throwIfAborted(requestSignal);
|
|
131
|
+
try {
|
|
132
|
+
let userStatus;
|
|
133
|
+
try {
|
|
134
|
+
userStatus = (await apis.getUserStatusByPassport(passport, productName, requestContext(), options)).data;
|
|
135
|
+
} catch (error) {
|
|
136
|
+
if (!(error instanceof ApiError && error.status === 401) || renewedPassport) throw error;
|
|
137
|
+
userStatus = {};
|
|
138
|
+
}
|
|
139
|
+
throwIfAborted(requestSignal);
|
|
140
|
+
if (!userStatus.profile && !renewedPassport) {
|
|
141
|
+
renewedPassport = true;
|
|
142
|
+
localStorage.removeItem(userId + "_passport");
|
|
143
|
+
passport = (await apis.getPassport(userId, productName, requestContext(), options)).data.passport;
|
|
144
|
+
userStatus = (await apis.getUserStatusByPassport(passport, productName, requestContext(), options)).data;
|
|
145
|
+
throwIfAborted(requestSignal);
|
|
146
|
+
}
|
|
147
|
+
if (!isObject(userStatus.profile) || typeof userStatus.profile.id !== "string" || !userStatus.profile.id || !isObject(userStatus.profile.usage_limits)) {
|
|
148
|
+
throw new AccountRequestError("Account status is missing a profile.", "invalid_response", "status", lastApi);
|
|
149
|
+
}
|
|
150
|
+
latestProfile = userStatus.profile;
|
|
151
|
+
onPassport == null ? void 0 : onPassport(passport, userId, userStatus.profile.id);
|
|
152
|
+
consecutiveFailures = 0;
|
|
153
|
+
lastError = void 0;
|
|
154
|
+
if (!isSubscribe) {
|
|
155
|
+
successCallback == null ? void 0 : successCallback(userStatus);
|
|
156
|
+
return userStatus;
|
|
157
|
+
}
|
|
158
|
+
if (!snapshot) snapshot = takeSubscribeBaseline(userStatus);
|
|
159
|
+
else if (isChanged ? isChanged(userStatus, snapshot) : isSubscribeChanged(userStatus, snapshot)) {
|
|
160
|
+
successCallback == null ? void 0 : successCallback(userStatus);
|
|
161
|
+
return userStatus;
|
|
162
|
+
}
|
|
163
|
+
} catch (error) {
|
|
164
|
+
throwIfAborted(requestSignal);
|
|
165
|
+
lastError = error;
|
|
166
|
+
consecutiveFailures++;
|
|
167
|
+
if (!retryableAccountError(error) || count + 1 >= maxCount || isSubscribe && consecutiveFailures >= 3) throw error;
|
|
140
168
|
}
|
|
169
|
+
if (count + 1 < maxCount) await accountDelay(typeof interval === "function" ? interval() : interval ?? 3e3, requestSignal);
|
|
141
170
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
try {
|
|
145
|
-
passportResult = await apis.getPassport(userId, productName, requestContext());
|
|
146
|
-
passport = (_b = passportResult.data) == null ? void 0 : _b.passport;
|
|
147
|
-
} catch (e) {
|
|
148
|
-
}
|
|
149
|
-
}
|
|
150
|
-
count++;
|
|
151
|
-
if (isStop || expired()) {
|
|
152
|
-
break;
|
|
153
|
-
}
|
|
154
|
-
let ms = typeof interval === "function" ? interval() : interval ?? 3e3;
|
|
155
|
-
if (maxDurationMs != null) {
|
|
156
|
-
ms = Math.min(ms, Math.max(maxDurationMs - (Date.now() - startedAt), 0));
|
|
157
|
-
}
|
|
158
|
-
await sleep(ms, (cancel) => {
|
|
159
|
-
cancelSleep = cancel;
|
|
171
|
+
if (isSubscribe && !lastError) return void 0;
|
|
172
|
+
throw lastError ?? new AccountRequestError("Account status is unavailable.", "invalid_response", "status", lastApi);
|
|
160
173
|
});
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
174
|
+
} catch (error) {
|
|
175
|
+
if (stopped) return void 0;
|
|
176
|
+
if (error instanceof ApiError && error.code === "timeout" && !(error instanceof AccountRequestError)) {
|
|
177
|
+
if (isSubscribe && !lastError) return void 0;
|
|
178
|
+
error = lastError ?? new AccountRequestError("Account request timed out.", "timeout", lastStage, lastApi);
|
|
179
|
+
}
|
|
180
|
+
if (!(error instanceof ApiError && error.code === "aborted")) failCallback == null ? void 0 : failCallback();
|
|
181
|
+
throw error;
|
|
182
|
+
} finally {
|
|
183
|
+
signal == null ? void 0 : signal.removeEventListener("abort", cancel);
|
|
165
184
|
}
|
|
166
|
-
failCallback == null ? void 0 : failCallback();
|
|
167
|
-
throw new Error("get user profile error");
|
|
168
|
-
}
|
|
169
|
-
function sleep(ms = 3e3, onCancel) {
|
|
170
|
-
return new Promise((resolve) => {
|
|
171
|
-
const id = setTimeout(resolve, ms);
|
|
172
|
-
onCancel == null ? void 0 : onCancel(() => {
|
|
173
|
-
clearTimeout(id);
|
|
174
|
-
resolve();
|
|
175
|
-
});
|
|
176
|
-
});
|
|
177
185
|
}
|
|
178
186
|
export {
|
|
179
187
|
computeAvailableLimit,
|
package/dist/login/privider.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { jsx } from "react/jsx-runtime";
|
|
2
2
|
import { useState, useRef, useEffect } from "react";
|
|
3
|
-
import {
|
|
3
|
+
import { getUserProfile, takeSubscribeBaseline, handleLogin } from "./login/login.js";
|
|
4
4
|
import { YiChongCanvaContext } from "./context.js";
|
|
5
5
|
import _ from "lodash";
|
|
6
6
|
import { getAppVersion } from "./version.js";
|
|
@@ -8,9 +8,13 @@ import { createPredictionApi } from "../api/predictions.js";
|
|
|
8
8
|
import { createTextModerationApi } from "../api/moderation.js";
|
|
9
9
|
import { createContentExtractionApi } from "../api/content.js";
|
|
10
10
|
import { createChatApi } from "../api/chat.js";
|
|
11
|
+
import { createVttApi } from "../api/vtt.js";
|
|
11
12
|
import { createUsageApi } from "../api/usage.js";
|
|
12
13
|
import { createUploadApi } from "../api/uploads.js";
|
|
13
14
|
import { createErrorReportingApi } from "../api/reporting.js";
|
|
15
|
+
import { ApiError } from "../api/error.js";
|
|
16
|
+
import { withRequest, throwIfAborted } from "../api/transport.js";
|
|
17
|
+
import { createAccountReporter, AccountRequestError, retryableAccountError, accountDelay } from "./account.js";
|
|
14
18
|
import { language, addContextQuery } from "../api/request.js";
|
|
15
19
|
function reduceFreeTimes(baseUrl, passport, cnt, context) {
|
|
16
20
|
const header = {
|
|
@@ -76,6 +80,11 @@ const YiChongCanvaProvider = ({
|
|
|
76
80
|
const stopCurrentPolling = useRef(null);
|
|
77
81
|
const sessionStopRef = useRef(null);
|
|
78
82
|
const [loginStatus, setLoginStatus] = useState("idle");
|
|
83
|
+
const [accountError, setAccountError] = useState(null);
|
|
84
|
+
const accountRunRef = useRef(null);
|
|
85
|
+
const loginPendingRef = useRef(false);
|
|
86
|
+
const mountedRef = useRef(true);
|
|
87
|
+
const identityScopeRef = useRef(`${appId}|${productName}`);
|
|
79
88
|
const userStatusRef = useRef({});
|
|
80
89
|
const apiConfigRef = useRef({ apiBaseUrl, contentBaseUrl, chatBaseUrl, baseUrl, uploadBaseUrl, productName, appId, version, getLocale });
|
|
81
90
|
apiConfigRef.current = { apiBaseUrl, contentBaseUrl, chatBaseUrl, baseUrl, uploadBaseUrl, productName, appId, version, getLocale };
|
|
@@ -89,6 +98,7 @@ const YiChongCanvaProvider = ({
|
|
|
89
98
|
});
|
|
90
99
|
const [predictionApi] = useState(() => createPredictionApi(getApiContext));
|
|
91
100
|
const [textModerationApi] = useState(() => createTextModerationApi(getApiContext));
|
|
101
|
+
const [vttApi] = useState(() => createVttApi(getApiContext));
|
|
92
102
|
const [contentExtractionApi] = useState(() => createContentExtractionApi(() => ({
|
|
93
103
|
...getApiContext(),
|
|
94
104
|
apiBaseUrl: apiConfigRef.current.contentBaseUrl
|
|
@@ -151,115 +161,146 @@ const YiChongCanvaProvider = ({
|
|
|
151
161
|
var _a;
|
|
152
162
|
return ((_a = userStatus.profile) == null ? void 0 : _a.usage_limits.free_limit) === -1;
|
|
153
163
|
};
|
|
154
|
-
const
|
|
164
|
+
const [reportAccountFailure] = useState(() => createAccountReporter(errorReportingApi.reportError));
|
|
165
|
+
const beginAccountRun = (operation) => {
|
|
166
|
+
var _a, _b;
|
|
167
|
+
(_a = sessionStopRef.current) == null ? void 0 : _a.call(sessionStopRef, false);
|
|
168
|
+
(_b = accountRunRef.current) == null ? void 0 : _b.controller.abort();
|
|
169
|
+
const run = {
|
|
170
|
+
operation,
|
|
171
|
+
controller: new AbortController(),
|
|
172
|
+
startedAt: Date.now(),
|
|
173
|
+
attempts: 0,
|
|
174
|
+
stage: operation === "login" ? "canva_token" : "status",
|
|
175
|
+
appKey: `${appId}|${productName}`
|
|
176
|
+
};
|
|
177
|
+
accountRunRef.current = run;
|
|
178
|
+
setAccountError(null);
|
|
179
|
+
return run;
|
|
180
|
+
};
|
|
181
|
+
const isCurrentRun = (run) => mountedRef.current && accountRunRef.current === run && !run.controller.signal.aborted && run.appKey === `${apiConfigRef.current.appId}|${apiConfigRef.current.productName}`;
|
|
182
|
+
const requestOptions = (run) => ({
|
|
183
|
+
signal: run.controller.signal,
|
|
184
|
+
onStep: (stage, api) => {
|
|
185
|
+
run.stage = stage;
|
|
186
|
+
run.api = api;
|
|
187
|
+
run.attempts++;
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
const failAccountRun = (run, error) => {
|
|
191
|
+
if (!isCurrentRun(run) || error instanceof ApiError && error.code === "aborted") return;
|
|
192
|
+
const failure = {
|
|
193
|
+
operation: run.operation,
|
|
194
|
+
stage: error instanceof AccountRequestError ? error.stage : run.stage,
|
|
195
|
+
code: error instanceof ApiError ? error.code : "account_error",
|
|
196
|
+
status: error instanceof ApiError ? error.status : void 0,
|
|
197
|
+
// 使用 SDK 生成的诊断文本;未知异常不直接发送任意异常对象/原始后端正文。
|
|
198
|
+
message: error instanceof ApiError ? error.message : "Account operation failed.",
|
|
199
|
+
attempts: run.attempts,
|
|
200
|
+
elapsedMs: Date.now() - run.startedAt,
|
|
201
|
+
api: error instanceof AccountRequestError ? error.api : run.api
|
|
202
|
+
};
|
|
203
|
+
setAccountError(failure);
|
|
204
|
+
reportAccountFailure(failure, run.appKey);
|
|
205
|
+
};
|
|
206
|
+
const login = (extraRetries = retry) => {
|
|
207
|
+
const run = beginAccountRun("login");
|
|
208
|
+
loginPendingRef.current = true;
|
|
155
209
|
setLoginStatus("loading");
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
setLoginStatus("success");
|
|
163
|
-
updateUserStatus(userStatus2);
|
|
164
|
-
onUpdateUserStatus == null ? void 0 : onUpdateUserStatus(userStatus2);
|
|
165
|
-
window.userId = userStatus2.userId || "";
|
|
166
|
-
window.pollUserSubscribe = pollUserStatus.current = (options) => {
|
|
167
|
-
var _a;
|
|
168
|
-
const {
|
|
169
|
-
fastIntervalMs = 5e3,
|
|
170
|
-
fastPhaseMs = 3e4,
|
|
171
|
-
slowIntervalMs = 15e3,
|
|
172
|
-
sessionTtlMs = 3e5
|
|
173
|
-
} = options || {};
|
|
174
|
-
setHasClickedUpgrade(true);
|
|
175
|
-
(_a = sessionStopRef.current) == null ? void 0 : _a.call(sessionStopRef, false);
|
|
176
|
-
return new Promise((resolve) => {
|
|
177
|
-
const userId = (window == null ? void 0 : window.userId) || "";
|
|
178
|
-
const baseline = takeSubscribeBaseline(userStatusRef.current);
|
|
179
|
-
const sessionStart = Date.now();
|
|
180
|
-
console.log("checkout session start", baseline);
|
|
181
|
-
let stopInner = null;
|
|
182
|
-
let settled = false;
|
|
183
|
-
const finish = (u) => {
|
|
184
|
-
if (settled) return;
|
|
185
|
-
settled = true;
|
|
186
|
-
if (stopCurrentPolling.current === exposedStop) {
|
|
187
|
-
stopCurrentPolling.current = null;
|
|
188
|
-
}
|
|
189
|
-
if (sessionStopRef.current === myStop) {
|
|
190
|
-
sessionStopRef.current = null;
|
|
191
|
-
}
|
|
192
|
-
resolve(u);
|
|
193
|
-
};
|
|
194
|
-
const finalizeWithRefresh = () => {
|
|
195
|
-
if (settled) return;
|
|
196
|
-
Promise.race([
|
|
197
|
-
refreshUserStatus(),
|
|
198
|
-
new Promise(
|
|
199
|
-
(r) => setTimeout(() => r(userStatusRef.current), 1e4)
|
|
200
|
-
)
|
|
201
|
-
]).catch(() => userStatusRef.current).then((u) => finish(u || userStatusRef.current));
|
|
202
|
-
};
|
|
203
|
-
const myStop = (withRefresh) => {
|
|
204
|
-
stopInner == null ? void 0 : stopInner();
|
|
205
|
-
if (stopCurrentPolling.current === exposedStop) {
|
|
206
|
-
stopCurrentPolling.current = null;
|
|
207
|
-
}
|
|
208
|
-
if (sessionStopRef.current === myStop) {
|
|
209
|
-
sessionStopRef.current = null;
|
|
210
|
-
}
|
|
211
|
-
if (withRefresh) {
|
|
212
|
-
finalizeWithRefresh();
|
|
213
|
-
} else {
|
|
214
|
-
finish(userStatusRef.current);
|
|
215
|
-
}
|
|
216
|
-
};
|
|
217
|
-
const exposedStop = () => myStop(true);
|
|
218
|
-
stopCurrentPolling.current = exposedStop;
|
|
219
|
-
sessionStopRef.current = myStop;
|
|
220
|
-
getUserProfile({
|
|
210
|
+
pollUserStatus.current = null;
|
|
211
|
+
void withRequest({ signal: run.controller.signal, timeoutMs: 3e4 }, async (signal) => {
|
|
212
|
+
for (let attempt = 0; ; attempt++) {
|
|
213
|
+
try {
|
|
214
|
+
return await handleLogin({
|
|
215
|
+
appId,
|
|
221
216
|
productName,
|
|
222
|
-
userId,
|
|
223
217
|
onPassport,
|
|
224
218
|
getRequestContext: getApiContext,
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
// 会话时长由 sessionTtlMs 主导;次数上限只作双保险
|
|
228
|
-
maxCount: 1e4,
|
|
229
|
-
maxDurationMs: sessionTtlMs,
|
|
230
|
-
// 快阶段过后降频
|
|
231
|
-
interval: () => Date.now() - sessionStart < fastPhaseMs ? fastIntervalMs : slowIntervalMs,
|
|
232
|
-
onStop: (fn) => {
|
|
233
|
-
stopInner = fn;
|
|
234
|
-
},
|
|
235
|
-
successCallback(u) {
|
|
236
|
-
u.userId = userId;
|
|
237
|
-
updateUserStatus(u);
|
|
238
|
-
finish(u);
|
|
239
|
-
onUpdateUserStatus == null ? void 0 : onUpdateUserStatus(u);
|
|
240
|
-
}
|
|
241
|
-
}).catch(() => {
|
|
242
|
-
console.log("checkout session ttl reached");
|
|
243
|
-
finalizeWithRefresh();
|
|
244
|
-
}).finally(() => {
|
|
245
|
-
if (stopCurrentPolling.current === exposedStop) {
|
|
246
|
-
stopCurrentPolling.current = null;
|
|
247
|
-
}
|
|
248
|
-
if (sessionStopRef.current === myStop) {
|
|
249
|
-
sessionStopRef.current = null;
|
|
250
|
-
}
|
|
219
|
+
...requestOptions(run),
|
|
220
|
+
signal
|
|
251
221
|
});
|
|
252
|
-
})
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
setTimeout(() => {
|
|
258
|
-
login(tryCount + 1);
|
|
259
|
-
}, 3e3);
|
|
222
|
+
} catch (error) {
|
|
223
|
+
throwIfAborted(signal);
|
|
224
|
+
if (attempt >= extraRetries || !retryableAccountError(error)) throw error;
|
|
225
|
+
await accountDelay(3e3, signal);
|
|
226
|
+
}
|
|
260
227
|
}
|
|
228
|
+
}).then((userStatus2) => {
|
|
229
|
+
if (!isCurrentRun(run)) return;
|
|
230
|
+
loginPendingRef.current = false;
|
|
231
|
+
setLoginStatus("success");
|
|
232
|
+
setAccountError(null);
|
|
233
|
+
updateUserStatus(userStatus2);
|
|
234
|
+
window.userId = userStatus2.userId || "";
|
|
235
|
+
window.pollUserSubscribe = pollUserStatus.current = startPolling;
|
|
236
|
+
onUpdateUserStatus == null ? void 0 : onUpdateUserStatus(userStatus2);
|
|
237
|
+
}).catch((error) => {
|
|
238
|
+
if (!isCurrentRun(run)) return;
|
|
239
|
+
loginPendingRef.current = false;
|
|
240
|
+
setLoginStatus("error");
|
|
241
|
+
failAccountRun(run, error);
|
|
261
242
|
});
|
|
262
243
|
};
|
|
244
|
+
function startPolling(options) {
|
|
245
|
+
const { fastIntervalMs = 5e3, fastPhaseMs = 3e4, slowIntervalMs = 15e3, sessionTtlMs = 3e5 } = options || {};
|
|
246
|
+
const run = beginAccountRun("poll");
|
|
247
|
+
setHasClickedUpgrade(true);
|
|
248
|
+
const userId = userStatusRef.current.userId || window.userId || "";
|
|
249
|
+
const baseline = takeSubscribeBaseline(userStatusRef.current);
|
|
250
|
+
return new Promise((resolve) => {
|
|
251
|
+
let settled = false;
|
|
252
|
+
let finishing = false;
|
|
253
|
+
const finish = (user) => {
|
|
254
|
+
if (settled) return;
|
|
255
|
+
settled = true;
|
|
256
|
+
if (stopCurrentPolling.current === exposedStop) stopCurrentPolling.current = null;
|
|
257
|
+
if (sessionStopRef.current === stop) sessionStopRef.current = null;
|
|
258
|
+
resolve(user);
|
|
259
|
+
};
|
|
260
|
+
const stop = (withRefresh) => {
|
|
261
|
+
if (settled || finishing) return;
|
|
262
|
+
run.controller.abort();
|
|
263
|
+
if (!withRefresh) {
|
|
264
|
+
finish(userStatusRef.current);
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
finishing = true;
|
|
268
|
+
if (sessionStopRef.current === stop) sessionStopRef.current = null;
|
|
269
|
+
if (stopCurrentPolling.current === exposedStop) stopCurrentPolling.current = null;
|
|
270
|
+
void refreshUserStatus(1e4).then(finish);
|
|
271
|
+
};
|
|
272
|
+
const exposedStop = () => stop(true);
|
|
273
|
+
sessionStopRef.current = stop;
|
|
274
|
+
stopCurrentPolling.current = exposedStop;
|
|
275
|
+
void getUserProfile({
|
|
276
|
+
productName,
|
|
277
|
+
userId,
|
|
278
|
+
onPassport,
|
|
279
|
+
getRequestContext: getApiContext,
|
|
280
|
+
...requestOptions(run),
|
|
281
|
+
isSubscribe: true,
|
|
282
|
+
baseline,
|
|
283
|
+
maxCount: 1e4,
|
|
284
|
+
maxDurationMs: sessionTtlMs,
|
|
285
|
+
interval: () => Date.now() - run.startedAt < fastPhaseMs ? fastIntervalMs : slowIntervalMs
|
|
286
|
+
}).then((user) => {
|
|
287
|
+
if (!isCurrentRun(run) || settled || finishing) return;
|
|
288
|
+
if (!user) {
|
|
289
|
+
stop(true);
|
|
290
|
+
return;
|
|
291
|
+
}
|
|
292
|
+
user.userId = userId;
|
|
293
|
+
setAccountError(null);
|
|
294
|
+
updateUserStatus(user);
|
|
295
|
+
finish(user);
|
|
296
|
+
onUpdateUserStatus == null ? void 0 : onUpdateUserStatus(user);
|
|
297
|
+
}).catch((error) => {
|
|
298
|
+
if (finishing) return;
|
|
299
|
+
failAccountRun(run, error);
|
|
300
|
+
finish(userStatusRef.current);
|
|
301
|
+
});
|
|
302
|
+
});
|
|
303
|
+
}
|
|
263
304
|
useEffect(() => {
|
|
264
305
|
if (!version && !productName && !baseUrl && !appId) {
|
|
265
306
|
throw new Error("version, productName, baseUrl, appId is required");
|
|
@@ -336,10 +377,27 @@ const YiChongCanvaProvider = ({
|
|
|
336
377
|
updateUserStatus({});
|
|
337
378
|
}, [test]);
|
|
338
379
|
useEffect(() => {
|
|
339
|
-
|
|
340
|
-
|
|
380
|
+
mountedRef.current = true;
|
|
381
|
+
const scope = `${appId}|${productName}`;
|
|
382
|
+
if (identityScopeRef.current !== scope) {
|
|
383
|
+
identityScopeRef.current = scope;
|
|
384
|
+
window.userId = "";
|
|
385
|
+
passportRef.current = null;
|
|
386
|
+
userStatusRef.current = {};
|
|
387
|
+
setUserStatus({});
|
|
388
|
+
pollUserStatus.current = null;
|
|
341
389
|
}
|
|
342
|
-
|
|
390
|
+
if (autoLogin) login();
|
|
391
|
+
else setLoginStatus("idle");
|
|
392
|
+
return () => {
|
|
393
|
+
var _a, _b;
|
|
394
|
+
mountedRef.current = false;
|
|
395
|
+
loginPendingRef.current = false;
|
|
396
|
+
(_a = sessionStopRef.current) == null ? void 0 : _a.call(sessionStopRef, false);
|
|
397
|
+
(_b = accountRunRef.current) == null ? void 0 : _b.controller.abort();
|
|
398
|
+
accountRunRef.current = null;
|
|
399
|
+
};
|
|
400
|
+
}, [autoLogin, appId, productName]);
|
|
343
401
|
const initCredits = () => {
|
|
344
402
|
var _a, _b;
|
|
345
403
|
if (!userStatus.subscription || !userStatus.profile) {
|
|
@@ -448,24 +506,34 @@ const YiChongCanvaProvider = ({
|
|
|
448
506
|
}
|
|
449
507
|
return "Premium";
|
|
450
508
|
};
|
|
451
|
-
function refreshUserStatus() {
|
|
452
|
-
return
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
509
|
+
function refreshUserStatus(timeoutMs = 3e4) {
|
|
510
|
+
if (loginPendingRef.current || !userStatusRef.current.userId || !userStatusRef.current.profile) return Promise.resolve(userStatusRef.current);
|
|
511
|
+
const existing = accountRunRef.current;
|
|
512
|
+
if ((existing == null ? void 0 : existing.operation) === "refresh" && existing.promise && isCurrentRun(existing)) return existing.promise;
|
|
513
|
+
const run = beginAccountRun("refresh");
|
|
514
|
+
const userId = userStatusRef.current.userId || window.userId || "";
|
|
515
|
+
run.promise = getUserProfile({
|
|
516
|
+
productName,
|
|
517
|
+
userId,
|
|
518
|
+
onPassport,
|
|
519
|
+
getRequestContext: getApiContext,
|
|
520
|
+
...requestOptions(run),
|
|
521
|
+
maxCount: 2,
|
|
522
|
+
maxDurationMs: timeoutMs
|
|
523
|
+
}).then((user) => {
|
|
524
|
+
if (!user || !isCurrentRun(run)) return userStatusRef.current;
|
|
525
|
+
user.userId = userId;
|
|
526
|
+
setAccountError(null);
|
|
527
|
+
updateUserStatus(user);
|
|
528
|
+
onUpdateUserStatus == null ? void 0 : onUpdateUserStatus(user);
|
|
529
|
+
return user;
|
|
530
|
+
}).catch((error) => {
|
|
531
|
+
failAccountRun(run, error);
|
|
532
|
+
return userStatusRef.current;
|
|
533
|
+
}).finally(() => {
|
|
534
|
+
run.promise = void 0;
|
|
468
535
|
});
|
|
536
|
+
return run.promise;
|
|
469
537
|
}
|
|
470
538
|
useEffect(() => {
|
|
471
539
|
initCredits();
|
|
@@ -618,6 +686,7 @@ const YiChongCanvaProvider = ({
|
|
|
618
686
|
...textModerationApi,
|
|
619
687
|
...contentExtractionApi,
|
|
620
688
|
...chatApi,
|
|
689
|
+
...vttApi,
|
|
621
690
|
...usageApi,
|
|
622
691
|
...uploadApi,
|
|
623
692
|
...errorReportingApi,
|
|
@@ -639,12 +708,13 @@ const YiChongCanvaProvider = ({
|
|
|
639
708
|
},
|
|
640
709
|
getPricingLink,
|
|
641
710
|
updateTestMode,
|
|
642
|
-
refreshUserStatus,
|
|
711
|
+
refreshUserStatus: () => refreshUserStatus(),
|
|
643
712
|
hasClickedUpgrade,
|
|
644
713
|
setHasClickedUpgrade: (v) => {
|
|
645
714
|
setHasClickedUpgrade(v);
|
|
646
715
|
},
|
|
647
716
|
loginStatus,
|
|
717
|
+
accountError,
|
|
648
718
|
updateCredits,
|
|
649
719
|
stopCurrentPolling: () => {
|
|
650
720
|
var _a;
|
|
@@ -655,8 +725,7 @@ const YiChongCanvaProvider = ({
|
|
|
655
725
|
return false;
|
|
656
726
|
},
|
|
657
727
|
manualLogin: (_retry = 1) => {
|
|
658
|
-
|
|
659
|
-
login(0);
|
|
728
|
+
login(_retry);
|
|
660
729
|
}
|
|
661
730
|
},
|
|
662
731
|
children
|
|
@@ -4,4 +4,4 @@
|
|
|
4
4
|
* 参数、返回值及方法的 JSDoc 定义在 types.ts,会随声明文件发布供编辑器提示。
|
|
5
5
|
*/
|
|
6
6
|
export { ApiError } from './error';
|
|
7
|
-
export type { ApiRequestOptions, ApiResponse, ChatApi, ChatParams, ChatPayload, ChatResult, CompleteUploadParams, CompleteUploadResponse, ContentExtractParams, ContentExtractResult, ContentExtractionApi, TextCheckParams, CreateSpecifyPredictionParams, ErrorReportingApi, ErrorReportSource, GetPredictionParams, GeneratePresignedUrlParams, GetUploadUrlParams, InitiateUploadParams, InitiateUploadResponse, JsonObject, JsonValue, Prediction, PredictionApi, ReportErrorParams, ReportErrorResult, PresignedUrlResponse, TextModerationApi, TextModerationCategory, TextModerationDecision, TextModerationResult, UploadApi, UploadFileParams, UploadFileResult, UploadedPart, UploadPartUrlResponse, UploadProgress, UsageApi, UsageParams, UsageResponse, } from './types';
|
|
7
|
+
export type { ApiRequestOptions, ApiResponse, ChatApi, ChatParams, ChatPayload, ChatResult, CreateVttParams, CreateVttResult, GetVttStatusParams, VttApi, VttStatusResult, VttTaskState, CompleteUploadParams, CompleteUploadResponse, ContentExtractParams, ContentExtractResult, ContentExtractionApi, TextCheckParams, CreateSpecifyPredictionParams, ErrorReportingApi, ErrorReportSource, GetPredictionParams, GeneratePresignedUrlParams, GetUploadUrlParams, InitiateUploadParams, InitiateUploadResponse, JsonObject, JsonValue, Prediction, PredictionApi, ReportErrorParams, ReportErrorResult, PresignedUrlResponse, TextModerationApi, TextModerationCategory, TextModerationDecision, TextModerationResult, UploadApi, UploadFileParams, UploadFileResult, UploadedPart, UploadPartUrlResponse, UploadProgress, UsageApi, UsageParams, UsageResponse, } from './types';
|
|
@@ -9,7 +9,7 @@ export interface ReportingContext extends Pick<ApiContext, "userStatus" | "produ
|
|
|
9
9
|
}
|
|
10
10
|
/**
|
|
11
11
|
* 绑定显式错误上报能力:整理诊断信息,交由现有 Worker 转发到指定飞书机器人。
|
|
12
|
-
* 返回 Worker
|
|
12
|
+
* 返回 Worker 的确认结果,不安装全局异常监听,也不在业务 API 失败时自动上报或重试;账户模块的最终失败由 Provider 调用。
|
|
13
13
|
* 登录失败本身也需要诊断,因此允许账号尚未就绪,用 unknown 标记缺失身份。
|
|
14
14
|
* 上报失败仍会抛出 ApiError,应用应单独捕获,避免它覆盖原来的业务异常。
|
|
15
15
|
*/
|