diff --git a/library/src/@types/personal-storage.d.ts b/library/src/@types/personal-storage.d.ts deleted file mode 100644 index b38ba87..0000000 --- a/library/src/@types/personal-storage.d.ts +++ /dev/null @@ -1,11 +0,0 @@ -import type { PersonalStorage } from '@fairdatasociety/fdp-storage' -import type { Pod } from '@fairdatasociety/fdp-storage/dist/pod/types' - -declare module '@fairdatasociety/fdp-storage' { - class PersonalStorage { - /** - * Creates a pod for dapp. Pod name is generated by the Blossom extension. - */ - create(): Promise - } -} diff --git a/library/src/blossom.ts b/library/src/blossom.ts index f7bf0ac..ea737b4 100644 --- a/library/src/blossom.ts +++ b/library/src/blossom.ts @@ -1,7 +1,7 @@ -import type { FdpStorage } from '@fairdatasociety/fdp-storage' import { ApiActions } from './constants/api-actions.enum' import { BlossomMessages } from './messages/blossom-messages' import { createBlossomMessages } from './messages/blossom-messages.factory' +import { FdpStorage } from './model/fdp-storage.model' import createFdpStorageProxy from './proxy/fdp-storage.proxy.factory' import { getDappId } from './utils/dapp.util' diff --git a/library/src/model/fdp-storage.model.ts b/library/src/model/fdp-storage.model.ts new file mode 100644 index 0000000..2daa3ed --- /dev/null +++ b/library/src/model/fdp-storage.model.ts @@ -0,0 +1,13 @@ +import type { + FdpStorage as OriginalFdpStorage, + PersonalStorage as OriginalPersonalStorage, +} from '@fairdatasociety/fdp-storage' + +export type PersonalStorage = OriginalPersonalStorage & { + /** + * Checks whether dapp's pod is already created + */ + isDappPodCreated(): Promise +} + +export type FdpStorage = Omit & { personalStorage: PersonalStorage } diff --git a/library/src/proxy/fdp-storage.proxy.factory.ts b/library/src/proxy/fdp-storage.proxy.factory.ts index 4ea6d7b..8d87b90 100644 --- a/library/src/proxy/fdp-storage.proxy.factory.ts +++ b/library/src/proxy/fdp-storage.proxy.factory.ts @@ -1,7 +1,8 @@ -import type { FdpStorage, AccountData, Directory, PersonalStorage } from '@fairdatasociety/fdp-storage' +import type { AccountData, Directory } from '@fairdatasociety/fdp-storage' import { ApiActions } from '../constants/api-actions.enum' import { BlossomMessages } from '../messages/blossom-messages' import { FdpStorageRequest } from '../model/fdp-storage-request.model' +import { FdpStorage, PersonalStorage } from '../model/fdp-storage.model' function createProxy(path: string, messages: BlossomMessages): T { return new Proxy( diff --git a/src/services/fdp-storage/extended-personal-storage.ts b/src/services/fdp-storage/extended-personal-storage.ts new file mode 100644 index 0000000..5ee3aa6 --- /dev/null +++ b/src/services/fdp-storage/extended-personal-storage.ts @@ -0,0 +1,30 @@ +import { PersonalStorage } from '@fairdatasociety/fdp-storage' + +export async function isDappPodCreated(podName: string): Promise { + const personalStorage = this as PersonalStorage + + const pods = await personalStorage.list() + + return pods.getPods().some((pod) => pod.name === podName) +} + +export type PersonalStorageExtension = { + isDappPodCreated(podName: string): Promise +} + +export type ExtendedPersonalStorage = PersonalStorage & PersonalStorageExtension + +export function createPersonalStorageProxy(personalStorage: PersonalStorage): ExtendedPersonalStorage { + return new Proxy( + personalStorage as ExtendedPersonalStorage, + { + get(target: PersonalStorage, property: string) { + if (property === 'isDappPodCreated') { + return isDappPodCreated.bind(target) + } + + return target[property] + }, + } as ProxyHandler, + ) +} diff --git a/src/services/fdp-storage/fdp-storage-access.ts b/src/services/fdp-storage/fdp-storage-access.ts index 5629888..7aec7d7 100644 --- a/src/services/fdp-storage/fdp-storage-access.ts +++ b/src/services/fdp-storage/fdp-storage-access.ts @@ -36,7 +36,7 @@ function fileHandler(file: File, method: string, parameters: unknown[]) { const proxy: Record = { personalStorage: { handler: personalStorageHandler, - allowedMethods: ['create'], + allowedMethods: ['create', 'isDappPodCreated'], requiresPod: false, }, directory: { diff --git a/src/services/fdp-storage/fdp-storage.provider.ts b/src/services/fdp-storage/fdp-storage.provider.ts index 64488c0..649bacb 100644 --- a/src/services/fdp-storage/fdp-storage.provider.ts +++ b/src/services/fdp-storage/fdp-storage.provider.ts @@ -5,6 +5,7 @@ import { Swarm } from '../../model/storage/swarm.model' import { SwarmExtension } from '../../swarm-api/swarm-extension' import { getBatchId } from '../../utils/bee' import { AsyncConfigService } from '../async-config.service' +import { createPersonalStorageProxy } from './extended-personal-storage' export abstract class FdpStorageProvider extends AsyncConfigService { public getService(): Promise { @@ -45,7 +46,13 @@ export abstract class FdpStorageProvider extends AsyncConfigService // TODO cannot cast to BatchId because it's not exported // eslint-disable-next-line @typescript-eslint/no-explicit-any - return new FdpStorage(beeApiUrl, batchId as any, options as unknown) + const fdpStorage = new FdpStorage(beeApiUrl, batchId as any, options as unknown) + + // Replaces the personalStorage property with a proxy object that extends its functionalities + // eslint-disable-next-line @typescript-eslint/no-explicit-any + ;(fdpStorage as unknown as any).personalStorage = createPersonalStorageProxy(fdpStorage.personalStorage) + + return fdpStorage } private async getBeeAddresses(swarm: Swarm): Promise<{