mirror of
https://github.com/filecoin-project/slate.git
synced 2024-11-27 01:03:08 +03:00
small refactor to getBucketAPIFromUserToken
This commit is contained in:
parent
ec921ac782
commit
ad7c573752
@ -137,6 +137,32 @@ export const getPowergateAPIFromUserToken = async ({ user }) => {
|
||||
};
|
||||
};
|
||||
|
||||
export const setupWithThread = async ({ buckets }) => {
|
||||
const client = new Client(buckets.context);
|
||||
|
||||
try {
|
||||
const res = await client.getThread("buckets");
|
||||
|
||||
buckets.withThread(res.id.toString());
|
||||
|
||||
console.log(`[ buckets ] getThread success`);
|
||||
} catch (error) {
|
||||
if (error.message !== "Thread not found") {
|
||||
throw new Error(error.message);
|
||||
}
|
||||
|
||||
const newId = ThreadID.fromRandom();
|
||||
await client.newDB(newId, "buckets");
|
||||
const threadID = newId.toString();
|
||||
|
||||
buckets.withThread(threadID);
|
||||
|
||||
console.log(`[ buckets ] newDB success`);
|
||||
}
|
||||
|
||||
return buckets;
|
||||
};
|
||||
|
||||
// NOTE(jim): Requires @textile/hub
|
||||
export const getBucketAPIFromUserToken = async ({
|
||||
user,
|
||||
@ -151,28 +177,20 @@ export const getBucketAPIFromUserToken = async ({
|
||||
await buckets.getToken(identity);
|
||||
|
||||
let root = null;
|
||||
|
||||
console.log(`[ buckets ] getOrCreate init ${name}`);
|
||||
|
||||
// NOTE(jim): captures `withThread` cases.
|
||||
try {
|
||||
const client = new Client(buckets.context);
|
||||
try {
|
||||
const res = await client.getThread("buckets");
|
||||
buckets = await setupWithThread({ buckets });
|
||||
} catch (e) {
|
||||
console.log(`[ textile ] warning: ${e.message}`);
|
||||
}
|
||||
|
||||
buckets.withThread(res.id.toString());
|
||||
console.log(`[ buckets ] getThread success`);
|
||||
} catch (error) {
|
||||
if (error.message !== "Thread not found") {
|
||||
throw new Error(error.message);
|
||||
}
|
||||
|
||||
const newId = ThreadID.fromRandom();
|
||||
await client.newDB(newId, "buckets");
|
||||
const threadID = newId.toString();
|
||||
|
||||
buckets.withThread(threadID);
|
||||
console.log(`[ buckets ] newDB success`);
|
||||
}
|
||||
console.log(`[ buckets ] getOrCreate thread found for ${name}`);
|
||||
|
||||
// NOTE(jim): captures finding your bucket and or creating a new one.
|
||||
try {
|
||||
const roots = await buckets.list();
|
||||
root = roots.find((bucket) => bucket.name === name);
|
||||
if (!root) {
|
||||
|
Loading…
Reference in New Issue
Block a user