@swype-org/react-sdk 0.1.100 → 0.1.102
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/dist/index.cjs +83 -11
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +5 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.js +83 -11
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.cts
CHANGED
|
@@ -297,7 +297,10 @@ declare function useSwypeDepositAmount(): {
|
|
|
297
297
|
declare function fetchProviders(apiBaseUrl: string, token: string): Promise<Provider[]>;
|
|
298
298
|
declare function fetchChains(apiBaseUrl: string, token: string): Promise<Chain[]>;
|
|
299
299
|
declare function fetchAccounts(apiBaseUrl: string, token: string, credentialId: string): Promise<Account[]>;
|
|
300
|
+
declare function fetchAccount(apiBaseUrl: string, token: string, accountId: string, credentialId: string): Promise<Account>;
|
|
300
301
|
interface CreateAccountParams {
|
|
302
|
+
/** Caller-supplied UUID v4 for idempotency. If omitted, a random UUID is generated. */
|
|
303
|
+
id?: string;
|
|
301
304
|
name: string;
|
|
302
305
|
credentialId: string;
|
|
303
306
|
providerId: string;
|
|
@@ -362,6 +365,7 @@ type api_CreateAccountParams = CreateAccountParams;
|
|
|
362
365
|
type api_CreateTransferParams = CreateTransferParams;
|
|
363
366
|
declare const api_createAccount: typeof createAccount;
|
|
364
367
|
declare const api_createTransfer: typeof createTransfer;
|
|
368
|
+
declare const api_fetchAccount: typeof fetchAccount;
|
|
365
369
|
declare const api_fetchAccounts: typeof fetchAccounts;
|
|
366
370
|
declare const api_fetchAuthorizationSession: typeof fetchAuthorizationSession;
|
|
367
371
|
declare const api_fetchChains: typeof fetchChains;
|
|
@@ -376,7 +380,7 @@ declare const api_signTransfer: typeof signTransfer;
|
|
|
376
380
|
declare const api_updateUserConfig: typeof updateUserConfig;
|
|
377
381
|
declare const api_updateUserConfigBySession: typeof updateUserConfigBySession;
|
|
378
382
|
declare namespace api {
|
|
379
|
-
export { type api_CreateAccountParams as CreateAccountParams, type api_CreateTransferParams as CreateTransferParams, api_createAccount as createAccount, api_createTransfer as createTransfer, api_fetchAccounts as fetchAccounts, api_fetchAuthorizationSession as fetchAuthorizationSession, api_fetchChains as fetchChains, api_fetchMerchantPublicKey as fetchMerchantPublicKey, api_fetchProviders as fetchProviders, api_fetchTransfer as fetchTransfer, api_fetchUserConfig as fetchUserConfig, api_registerPasskey as registerPasskey, api_reportActionCompletion as reportActionCompletion, api_reportPasskeyActivity as reportPasskeyActivity, api_signTransfer as signTransfer, api_updateUserConfig as updateUserConfig, api_updateUserConfigBySession as updateUserConfigBySession };
|
|
383
|
+
export { type api_CreateAccountParams as CreateAccountParams, type api_CreateTransferParams as CreateTransferParams, api_createAccount as createAccount, api_createTransfer as createTransfer, api_fetchAccount as fetchAccount, api_fetchAccounts as fetchAccounts, api_fetchAuthorizationSession as fetchAuthorizationSession, api_fetchChains as fetchChains, api_fetchMerchantPublicKey as fetchMerchantPublicKey, api_fetchProviders as fetchProviders, api_fetchTransfer as fetchTransfer, api_fetchUserConfig as fetchUserConfig, api_registerPasskey as registerPasskey, api_reportActionCompletion as reportActionCompletion, api_reportPasskeyActivity as reportPasskeyActivity, api_signTransfer as signTransfer, api_updateUserConfig as updateUserConfig, api_updateUserConfigBySession as updateUserConfigBySession };
|
|
380
384
|
}
|
|
381
385
|
|
|
382
386
|
interface SwypePaymentProps {
|
package/dist/index.d.ts
CHANGED
|
@@ -297,7 +297,10 @@ declare function useSwypeDepositAmount(): {
|
|
|
297
297
|
declare function fetchProviders(apiBaseUrl: string, token: string): Promise<Provider[]>;
|
|
298
298
|
declare function fetchChains(apiBaseUrl: string, token: string): Promise<Chain[]>;
|
|
299
299
|
declare function fetchAccounts(apiBaseUrl: string, token: string, credentialId: string): Promise<Account[]>;
|
|
300
|
+
declare function fetchAccount(apiBaseUrl: string, token: string, accountId: string, credentialId: string): Promise<Account>;
|
|
300
301
|
interface CreateAccountParams {
|
|
302
|
+
/** Caller-supplied UUID v4 for idempotency. If omitted, a random UUID is generated. */
|
|
303
|
+
id?: string;
|
|
301
304
|
name: string;
|
|
302
305
|
credentialId: string;
|
|
303
306
|
providerId: string;
|
|
@@ -362,6 +365,7 @@ type api_CreateAccountParams = CreateAccountParams;
|
|
|
362
365
|
type api_CreateTransferParams = CreateTransferParams;
|
|
363
366
|
declare const api_createAccount: typeof createAccount;
|
|
364
367
|
declare const api_createTransfer: typeof createTransfer;
|
|
368
|
+
declare const api_fetchAccount: typeof fetchAccount;
|
|
365
369
|
declare const api_fetchAccounts: typeof fetchAccounts;
|
|
366
370
|
declare const api_fetchAuthorizationSession: typeof fetchAuthorizationSession;
|
|
367
371
|
declare const api_fetchChains: typeof fetchChains;
|
|
@@ -376,7 +380,7 @@ declare const api_signTransfer: typeof signTransfer;
|
|
|
376
380
|
declare const api_updateUserConfig: typeof updateUserConfig;
|
|
377
381
|
declare const api_updateUserConfigBySession: typeof updateUserConfigBySession;
|
|
378
382
|
declare namespace api {
|
|
379
|
-
export { type api_CreateAccountParams as CreateAccountParams, type api_CreateTransferParams as CreateTransferParams, api_createAccount as createAccount, api_createTransfer as createTransfer, api_fetchAccounts as fetchAccounts, api_fetchAuthorizationSession as fetchAuthorizationSession, api_fetchChains as fetchChains, api_fetchMerchantPublicKey as fetchMerchantPublicKey, api_fetchProviders as fetchProviders, api_fetchTransfer as fetchTransfer, api_fetchUserConfig as fetchUserConfig, api_registerPasskey as registerPasskey, api_reportActionCompletion as reportActionCompletion, api_reportPasskeyActivity as reportPasskeyActivity, api_signTransfer as signTransfer, api_updateUserConfig as updateUserConfig, api_updateUserConfigBySession as updateUserConfigBySession };
|
|
383
|
+
export { type api_CreateAccountParams as CreateAccountParams, type api_CreateTransferParams as CreateTransferParams, api_createAccount as createAccount, api_createTransfer as createTransfer, api_fetchAccount as fetchAccount, api_fetchAccounts as fetchAccounts, api_fetchAuthorizationSession as fetchAuthorizationSession, api_fetchChains as fetchChains, api_fetchMerchantPublicKey as fetchMerchantPublicKey, api_fetchProviders as fetchProviders, api_fetchTransfer as fetchTransfer, api_fetchUserConfig as fetchUserConfig, api_registerPasskey as registerPasskey, api_reportActionCompletion as reportActionCompletion, api_reportPasskeyActivity as reportPasskeyActivity, api_signTransfer as signTransfer, api_updateUserConfig as updateUserConfig, api_updateUserConfigBySession as updateUserConfigBySession };
|
|
380
384
|
}
|
|
381
385
|
|
|
382
386
|
interface SwypePaymentProps {
|
package/dist/index.js
CHANGED
|
@@ -158,6 +158,7 @@ var api_exports = {};
|
|
|
158
158
|
__export(api_exports, {
|
|
159
159
|
createAccount: () => createAccount,
|
|
160
160
|
createTransfer: () => createTransfer,
|
|
161
|
+
fetchAccount: () => fetchAccount,
|
|
161
162
|
fetchAccounts: () => fetchAccounts,
|
|
162
163
|
fetchAuthorizationSession: () => fetchAuthorizationSession,
|
|
163
164
|
fetchChains: () => fetchChains,
|
|
@@ -204,14 +205,28 @@ async function fetchAccounts(apiBaseUrl, token, credentialId) {
|
|
|
204
205
|
const data = await res.json();
|
|
205
206
|
return data.items;
|
|
206
207
|
}
|
|
208
|
+
async function fetchAccount(apiBaseUrl, token, accountId, credentialId) {
|
|
209
|
+
const params = new URLSearchParams({ credentialId });
|
|
210
|
+
const res = await fetch(`${apiBaseUrl}/v1/accounts/${accountId}?${params.toString()}`, {
|
|
211
|
+
headers: { Authorization: `Bearer ${token}` }
|
|
212
|
+
});
|
|
213
|
+
if (!res.ok) await throwApiError(res);
|
|
214
|
+
return await res.json();
|
|
215
|
+
}
|
|
207
216
|
async function createAccount(apiBaseUrl, token, params) {
|
|
217
|
+
const body = {
|
|
218
|
+
id: params.id ?? crypto.randomUUID(),
|
|
219
|
+
name: params.name,
|
|
220
|
+
credentialId: params.credentialId,
|
|
221
|
+
providerId: params.providerId
|
|
222
|
+
};
|
|
208
223
|
const res = await fetch(`${apiBaseUrl}/v1/accounts`, {
|
|
209
224
|
method: "POST",
|
|
210
225
|
headers: {
|
|
211
226
|
"Content-Type": "application/json",
|
|
212
227
|
Authorization: `Bearer ${token}`
|
|
213
228
|
},
|
|
214
|
-
body: JSON.stringify(
|
|
229
|
+
body: JSON.stringify(body)
|
|
215
230
|
});
|
|
216
231
|
if (!res.ok) await throwApiError(res);
|
|
217
232
|
return await res.json();
|
|
@@ -1861,6 +1876,7 @@ function createInitialState(config) {
|
|
|
1861
1876
|
accounts: [],
|
|
1862
1877
|
chains: [],
|
|
1863
1878
|
loadingData: false,
|
|
1879
|
+
selectedProviderId: null,
|
|
1864
1880
|
selectedAccountId: null,
|
|
1865
1881
|
selectedWalletId: null,
|
|
1866
1882
|
amount: config.depositAmount != null ? config.depositAmount.toString() : "",
|
|
@@ -1955,7 +1971,9 @@ function paymentReducer(state, action) {
|
|
|
1955
1971
|
case "SELECT_PROVIDER":
|
|
1956
1972
|
return {
|
|
1957
1973
|
...state,
|
|
1958
|
-
|
|
1974
|
+
selectedProviderId: action.providerId,
|
|
1975
|
+
selectedAccountId: null,
|
|
1976
|
+
selectedWalletId: null
|
|
1959
1977
|
};
|
|
1960
1978
|
case "SELECT_ACCOUNT":
|
|
1961
1979
|
return {
|
|
@@ -2024,7 +2042,7 @@ function paymentReducer(state, action) {
|
|
|
2024
2042
|
case "MOBILE_SETUP_COMPLETE":
|
|
2025
2043
|
return {
|
|
2026
2044
|
...state,
|
|
2027
|
-
transfer: action.transfer,
|
|
2045
|
+
transfer: action.transfer ?? state.transfer,
|
|
2028
2046
|
error: null,
|
|
2029
2047
|
mobileFlow: false,
|
|
2030
2048
|
deeplinkUri: null,
|
|
@@ -2046,6 +2064,7 @@ function paymentReducer(state, action) {
|
|
|
2046
2064
|
...state,
|
|
2047
2065
|
mobileFlow: true,
|
|
2048
2066
|
deeplinkUri: action.deeplinkUri,
|
|
2067
|
+
selectedProviderId: action.providerId ?? state.selectedProviderId,
|
|
2049
2068
|
error: action.error ?? null,
|
|
2050
2069
|
step: "open-wallet"
|
|
2051
2070
|
};
|
|
@@ -2101,6 +2120,7 @@ function paymentReducer(state, action) {
|
|
|
2101
2120
|
amount: action.depositAmount != null ? action.depositAmount.toString() : "",
|
|
2102
2121
|
mobileFlow: false,
|
|
2103
2122
|
deeplinkUri: null,
|
|
2123
|
+
selectedProviderId: null,
|
|
2104
2124
|
selectedWalletId: null,
|
|
2105
2125
|
selectedAccountId: action.firstAccountId
|
|
2106
2126
|
};
|
|
@@ -5118,6 +5138,7 @@ function SwypePaymentInner({
|
|
|
5118
5138
|
);
|
|
5119
5139
|
const loadingDataRef = useRef(false);
|
|
5120
5140
|
const pollingTransferIdRef = useRef(null);
|
|
5141
|
+
const setupAccountIdRef = useRef(null);
|
|
5121
5142
|
const mobileSetupFlowRef = useRef(false);
|
|
5122
5143
|
const handlingMobileReturnRef = useRef(false);
|
|
5123
5144
|
const processingStartedAtRef = useRef(null);
|
|
@@ -5438,6 +5459,15 @@ function SwypePaymentInner({
|
|
|
5438
5459
|
amount: payAmount
|
|
5439
5460
|
});
|
|
5440
5461
|
dispatch({ type: "TRANSFER_CREATED", transfer: t });
|
|
5462
|
+
if (t.status === "COMPLETED") {
|
|
5463
|
+
dispatch({ type: "TRANSFER_COMPLETED", transfer: t });
|
|
5464
|
+
onComplete?.(t);
|
|
5465
|
+
return;
|
|
5466
|
+
}
|
|
5467
|
+
if (t.status === "FAILED") {
|
|
5468
|
+
dispatch({ type: "TRANSFER_FAILED", transfer: t, error: "Transfer failed." });
|
|
5469
|
+
return;
|
|
5470
|
+
}
|
|
5441
5471
|
const signedTransfer = await transferSigning.signTransfer(t.id);
|
|
5442
5472
|
dispatch({ type: "TRANSFER_SIGNED", transfer: signedTransfer });
|
|
5443
5473
|
polling.startPolling(t.id);
|
|
@@ -5465,6 +5495,7 @@ function SwypePaymentInner({
|
|
|
5465
5495
|
transferSigning,
|
|
5466
5496
|
polling,
|
|
5467
5497
|
onError,
|
|
5498
|
+
onComplete,
|
|
5468
5499
|
idempotencyKey,
|
|
5469
5500
|
merchantAuthorization
|
|
5470
5501
|
]);
|
|
@@ -5495,7 +5526,6 @@ function SwypePaymentInner({
|
|
|
5495
5526
|
}
|
|
5496
5527
|
}
|
|
5497
5528
|
const t = await createTransfer(apiBaseUrl, token, {
|
|
5498
|
-
id: idempotencyKey,
|
|
5499
5529
|
credentialId: state.activeCredentialId,
|
|
5500
5530
|
merchantAuthorization,
|
|
5501
5531
|
sourceType: effectiveSourceType,
|
|
@@ -5522,7 +5552,6 @@ function SwypePaymentInner({
|
|
|
5522
5552
|
getAccessToken,
|
|
5523
5553
|
polling,
|
|
5524
5554
|
onError,
|
|
5525
|
-
idempotencyKey,
|
|
5526
5555
|
merchantAuthorization,
|
|
5527
5556
|
destination
|
|
5528
5557
|
]);
|
|
@@ -5567,7 +5596,9 @@ function SwypePaymentInner({
|
|
|
5567
5596
|
try {
|
|
5568
5597
|
const token = await getAccessToken();
|
|
5569
5598
|
if (!token) throw new Error("Not authenticated");
|
|
5599
|
+
const accountId = crypto.randomUUID();
|
|
5570
5600
|
const account = await createAccount(apiBaseUrl, token, {
|
|
5601
|
+
id: accountId,
|
|
5571
5602
|
name: providerName,
|
|
5572
5603
|
credentialId: state.activeCredentialId,
|
|
5573
5604
|
providerId
|
|
@@ -5581,6 +5612,7 @@ function SwypePaymentInner({
|
|
|
5581
5612
|
if (isMobile) {
|
|
5582
5613
|
handlingMobileReturnRef.current = false;
|
|
5583
5614
|
mobileSetupFlowRef.current = true;
|
|
5615
|
+
setupAccountIdRef.current = account.id;
|
|
5584
5616
|
persistMobileFlowState({
|
|
5585
5617
|
accountId: account.id,
|
|
5586
5618
|
sessionId: session.id,
|
|
@@ -5616,13 +5648,13 @@ function SwypePaymentInner({
|
|
|
5616
5648
|
const handleContinueConnection = useCallback(
|
|
5617
5649
|
(accountId) => {
|
|
5618
5650
|
const acct = state.accounts.find((a) => a.id === accountId);
|
|
5619
|
-
|
|
5620
|
-
|
|
5621
|
-
|
|
5622
|
-
|
|
5623
|
-
}
|
|
5651
|
+
if (!acct) return;
|
|
5652
|
+
const matchedProvider = state.providers.find((p) => p.name === acct.name);
|
|
5653
|
+
if (matchedProvider) {
|
|
5654
|
+
handleSelectProvider(matchedProvider.id);
|
|
5655
|
+
}
|
|
5624
5656
|
},
|
|
5625
|
-
[state.accounts]
|
|
5657
|
+
[state.accounts, state.providers, handleSelectProvider]
|
|
5626
5658
|
);
|
|
5627
5659
|
const handleNewPayment = useCallback(() => {
|
|
5628
5660
|
clearMobileFlowState();
|
|
@@ -5992,6 +6024,46 @@ function SwypePaymentInner({
|
|
|
5992
6024
|
if (!polledTransfer || polledTransfer.status !== "AUTHORIZED") return;
|
|
5993
6025
|
void handleAuthorizedMobileReturn(polledTransfer, mobileSetupFlowRef.current);
|
|
5994
6026
|
}, [state.mobileFlow, polling.transfer, handleAuthorizedMobileReturn]);
|
|
6027
|
+
useEffect(() => {
|
|
6028
|
+
if (!state.mobileFlow || !mobileSetupFlowRef.current) return;
|
|
6029
|
+
if (state.step !== "open-wallet") return;
|
|
6030
|
+
if (!state.activeCredentialId || !setupAccountIdRef.current) return;
|
|
6031
|
+
const accountId = setupAccountIdRef.current;
|
|
6032
|
+
const credentialId = state.activeCredentialId;
|
|
6033
|
+
let cancelled = false;
|
|
6034
|
+
const POLL_INTERVAL_MS = 3e3;
|
|
6035
|
+
const poll = async () => {
|
|
6036
|
+
try {
|
|
6037
|
+
const token = await getAccessTokenRef.current();
|
|
6038
|
+
if (!token || cancelled) return;
|
|
6039
|
+
const acct = await fetchAccount(apiBaseUrl, token, accountId, credentialId);
|
|
6040
|
+
if (cancelled) return;
|
|
6041
|
+
const hasActive = acct.wallets.some((w) => w.status === "ACTIVE");
|
|
6042
|
+
if (hasActive) {
|
|
6043
|
+
cancelled = true;
|
|
6044
|
+
mobileSetupFlowRef.current = false;
|
|
6045
|
+
setupAccountIdRef.current = null;
|
|
6046
|
+
clearMobileFlowState();
|
|
6047
|
+
await reloadAccounts();
|
|
6048
|
+
dispatch({ type: "MOBILE_SETUP_COMPLETE" });
|
|
6049
|
+
}
|
|
6050
|
+
} catch {
|
|
6051
|
+
}
|
|
6052
|
+
};
|
|
6053
|
+
poll();
|
|
6054
|
+
const intervalId = window.setInterval(poll, POLL_INTERVAL_MS);
|
|
6055
|
+
const handleVisibility = () => {
|
|
6056
|
+
if (document.visibilityState === "visible" && !cancelled) {
|
|
6057
|
+
poll();
|
|
6058
|
+
}
|
|
6059
|
+
};
|
|
6060
|
+
document.addEventListener("visibilitychange", handleVisibility);
|
|
6061
|
+
return () => {
|
|
6062
|
+
cancelled = true;
|
|
6063
|
+
window.clearInterval(intervalId);
|
|
6064
|
+
document.removeEventListener("visibilitychange", handleVisibility);
|
|
6065
|
+
};
|
|
6066
|
+
}, [state.mobileFlow, state.step, state.activeCredentialId, apiBaseUrl, reloadAccounts]);
|
|
5995
6067
|
useEffect(() => {
|
|
5996
6068
|
if (!state.mobileFlow) return;
|
|
5997
6069
|
if (handlingMobileReturnRef.current) return;
|