import { withAsyncPolling, TurnkeyActivityError } from "@turnkey/http";
// Use `withAsyncPolling(...)` to wrap & create a fetcher with built-in async polling support
const fetcher = withAsyncPolling({
request: client.createPrivateKeys,
});
// The fetcher remains fully typed. After submitting the request,
// it'll poll until the activity reaches a terminal state.
try {
const activity = await fetcher({
body: {
/* ... */
},
});
// Success!
console.log(
activity.result.createPrivateKeysResultV2?.privateKeys?.[0]?.privateKeyId,
);
} catch (error) {
if (error instanceof TurnkeyActivityError) {
// In case the activity is rejected, failed, or requires consensus,
// a rich `TurnkeyActivityError` will be thrown. You can read from
// `TurnkeyActivityError` to find out why the activity didn't succeed.
//
// For instance, if your activity requires consensus and doesn't have
// enough approvals, you can get the `activityId` from `TurnkeyActivityError`,
// store it somewhere, then re-fetch the activity via `.postGetActivity(...)`
// when the required approvals/rejections are in place.
}
}