diff --git a/package-lock.json b/package-lock.json index 84a162e0..7391d4b6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6320,6 +6320,26 @@ "typescript": "^4.9.5" } }, + "packages/browser-upload/node_modules/@spheron/core": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@spheron/core/-/core-2.0.0.tgz", + "integrity": "sha512-BkOxHoUVGH6AE1FzyO1Ut7lSoXWxMUKWDlnann5kCQ3qJYPOZ38MpUNt4UxuKqlyYuk6mvX2lI08HX6iRgO4Cw==", + "dependencies": { + "axios": "1.1.2", + "eventsource": "^2.0.2", + "p-limit": "^3.0.0" + } + }, + "packages/browser-upload/node_modules/axios": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.1.2.tgz", + "integrity": "sha512-bznQyETwElsXl2RK7HLLwb5GPpOLlycxHCtrpDR/4RqqBzjARaOTo3jz4IgtntWUYee7Ne4S8UHd92VCuzPaWA==", + "dependencies": { + "follow-redirects": "^1.15.0", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + }, "packages/cli": { "name": "@spheron/cli", "version": "1.0.16", @@ -6363,9 +6383,9 @@ } }, "packages/cli/node_modules/@spheron/core": { - "version": "1.0.9", - "resolved": "https://registry.npmjs.org/@spheron/core/-/core-1.0.9.tgz", - "integrity": "sha512-FUuD7mlWPUotN+lpT7XfKRBxr5geYLonDi+I66+ECpJuwUS7EYE1k0QsGEXG4tu8OLcW1wxSlarNzcejh0DZIg==", + "version": "1.0.10", + "resolved": "https://registry.npmjs.org/@spheron/core/-/core-1.0.10.tgz", + "integrity": "sha512-FaB1a+Sjf/1yo+yK/uXpAcg9DIag7ZNfc4CBiyGBVtgRGluiToXOCx+XJKrrdENqHOakAiA3yEeXIYp/bSdAoQ==", "dependencies": { "axios": "1.1.2", "eventsource": "^2.0.2", @@ -6393,34 +6413,17 @@ "multiformats": "^9.9.0" } }, - "packages/compute": { - "name": "@spheron/compute", - "version": "1.0.4", - "license": "Apache-2.0", - "dependencies": { - "@spheron/core": "^1.0.10" - }, - "devDependencies": { - "@types/node": "^18.13.0", - "@typescript-eslint/eslint-plugin": "^5.51.0", - "@typescript-eslint/parser": "^5.51.0", - "eslint": "^8.33.0", - "prettier": "^2.8.3", - "tsup": "^6.5.0", - "typescript": "^4.9.5" - } - }, - "packages/compute/node_modules/@spheron/core": { - "version": "1.0.10", - "resolved": "https://registry.npmjs.org/@spheron/core/-/core-1.0.10.tgz", - "integrity": "sha512-FaB1a+Sjf/1yo+yK/uXpAcg9DIag7ZNfc4CBiyGBVtgRGluiToXOCx+XJKrrdENqHOakAiA3yEeXIYp/bSdAoQ==", + "packages/cli/node_modules/@spheron/storage/node_modules/@spheron/core": { + "version": "1.0.9", + "resolved": "https://registry.npmjs.org/@spheron/core/-/core-1.0.9.tgz", + "integrity": "sha512-FUuD7mlWPUotN+lpT7XfKRBxr5geYLonDi+I66+ECpJuwUS7EYE1k0QsGEXG4tu8OLcW1wxSlarNzcejh0DZIg==", "dependencies": { "axios": "1.1.2", "eventsource": "^2.0.2", "p-limit": "^3.0.0" } }, - "packages/compute/node_modules/axios": { + "packages/cli/node_modules/@spheron/storage/node_modules/axios": { "version": "1.1.2", "resolved": "https://registry.npmjs.org/axios/-/axios-1.1.2.tgz", "integrity": "sha512-bznQyETwElsXl2RK7HLLwb5GPpOLlycxHCtrpDR/4RqqBzjARaOTo3jz4IgtntWUYee7Ne4S8UHd92VCuzPaWA==", @@ -6430,11 +6433,29 @@ "proxy-from-env": "^1.1.0" } }, + "packages/compute": { + "name": "@spheron/compute", + "version": "1.0.5", + "license": "Apache-2.0", + "dependencies": { + "@spheron/core": "^2.0.0" + }, + "devDependencies": { + "@types/node": "^18.13.0", + "@typescript-eslint/eslint-plugin": "^5.51.0", + "@typescript-eslint/parser": "^5.51.0", + "eslint": "^8.33.0", + "prettier": "^2.8.3", + "tsup": "^6.5.0", + "typescript": "^4.9.5" + } + }, "packages/core": { "name": "@spheron/core", - "version": "2.0.0", + "version": "2.0.1", "license": "Apache-2.0", "dependencies": { + "async-lock": "^1.4.0", "axios": "1.1.2", "eventsource": "^2.0.2", "p-limit": "^3.0.0" @@ -6527,11 +6548,10 @@ }, "packages/site": { "name": "@spheron/site", - "version": "1.0.1", + "version": "1.0.2", "license": "Apache-2.0", "dependencies": { - "@spheron/core": "1.0.9", - "async-lock": "^1.4.0" + "@spheron/core": "2.0.1" }, "devDependencies": { "@types/async-lock": "^1.4.0", @@ -6544,32 +6564,12 @@ "typescript": "^4.9.5" } }, - "packages/site/node_modules/@spheron/core": { - "version": "1.0.9", - "resolved": "https://registry.npmjs.org/@spheron/core/-/core-1.0.9.tgz", - "integrity": "sha512-FUuD7mlWPUotN+lpT7XfKRBxr5geYLonDi+I66+ECpJuwUS7EYE1k0QsGEXG4tu8OLcW1wxSlarNzcejh0DZIg==", - "dependencies": { - "axios": "1.1.2", - "eventsource": "^2.0.2", - "p-limit": "^3.0.0" - } - }, - "packages/site/node_modules/axios": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.1.2.tgz", - "integrity": "sha512-bznQyETwElsXl2RK7HLLwb5GPpOLlycxHCtrpDR/4RqqBzjARaOTo3jz4IgtntWUYee7Ne4S8UHd92VCuzPaWA==", - "dependencies": { - "follow-redirects": "^1.15.0", - "form-data": "^4.0.0", - "proxy-from-env": "^1.1.0" - } - }, "packages/storage": { "name": "@spheron/storage", - "version": "2.0.0", + "version": "2.0.1", "license": "Apache-2.0", "dependencies": { - "@spheron/core": "2.0.0", + "@spheron/core": "2.0.1", "@spheron/encryption": "1.0.0", "form-data": "^4.0.0", "multiformats": "^9.9.0" @@ -7738,6 +7738,28 @@ "prettier": "^2.8.3", "tsup": "^6.5.0", "typescript": "^4.9.5" + }, + "dependencies": { + "@spheron/core": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/@spheron/core/-/core-2.0.0.tgz", + "integrity": "sha512-BkOxHoUVGH6AE1FzyO1Ut7lSoXWxMUKWDlnann5kCQ3qJYPOZ38MpUNt4UxuKqlyYuk6mvX2lI08HX6iRgO4Cw==", + "requires": { + "axios": "1.1.2", + "eventsource": "^2.0.2", + "p-limit": "^3.0.0" + } + }, + "axios": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.1.2.tgz", + "integrity": "sha512-bznQyETwElsXl2RK7HLLwb5GPpOLlycxHCtrpDR/4RqqBzjARaOTo3jz4IgtntWUYee7Ne4S8UHd92VCuzPaWA==", + "requires": { + "follow-redirects": "^1.15.0", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + } } }, "@spheron/cli": { @@ -7775,9 +7797,9 @@ }, "dependencies": { "@spheron/core": { - "version": "1.0.9", - "resolved": "https://registry.npmjs.org/@spheron/core/-/core-1.0.9.tgz", - "integrity": "sha512-FUuD7mlWPUotN+lpT7XfKRBxr5geYLonDi+I66+ECpJuwUS7EYE1k0QsGEXG4tu8OLcW1wxSlarNzcejh0DZIg==", + "version": "1.0.10", + "resolved": "https://registry.npmjs.org/@spheron/core/-/core-1.0.10.tgz", + "integrity": "sha512-FaB1a+Sjf/1yo+yK/uXpAcg9DIag7ZNfc4CBiyGBVtgRGluiToXOCx+XJKrrdENqHOakAiA3yEeXIYp/bSdAoQ==", "requires": { "axios": "1.1.2", "eventsource": "^2.0.2", @@ -7805,6 +7827,28 @@ "@spheron/encryption": "1.0.0", "form-data": "^4.0.0", "multiformats": "^9.9.0" + }, + "dependencies": { + "@spheron/core": { + "version": "1.0.9", + "resolved": "https://registry.npmjs.org/@spheron/core/-/core-1.0.9.tgz", + "integrity": "sha512-FUuD7mlWPUotN+lpT7XfKRBxr5geYLonDi+I66+ECpJuwUS7EYE1k0QsGEXG4tu8OLcW1wxSlarNzcejh0DZIg==", + "requires": { + "axios": "1.1.2", + "eventsource": "^2.0.2", + "p-limit": "^3.0.0" + } + }, + "axios": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/axios/-/axios-1.1.2.tgz", + "integrity": "sha512-bznQyETwElsXl2RK7HLLwb5GPpOLlycxHCtrpDR/4RqqBzjARaOTo3jz4IgtntWUYee7Ne4S8UHd92VCuzPaWA==", + "requires": { + "follow-redirects": "^1.15.0", + "form-data": "^4.0.0", + "proxy-from-env": "^1.1.0" + } + } } } } @@ -7812,7 +7856,7 @@ "@spheron/compute": { "version": "file:packages/compute", "requires": { - "@spheron/core": "^1.0.10", + "@spheron/core": "^2.0.0", "@types/node": "^18.13.0", "@typescript-eslint/eslint-plugin": "^5.51.0", "@typescript-eslint/parser": "^5.51.0", @@ -7820,28 +7864,6 @@ "prettier": "^2.8.3", "tsup": "^6.5.0", "typescript": "^4.9.5" - }, - "dependencies": { - "@spheron/core": { - "version": "1.0.10", - "resolved": "https://registry.npmjs.org/@spheron/core/-/core-1.0.10.tgz", - "integrity": "sha512-FaB1a+Sjf/1yo+yK/uXpAcg9DIag7ZNfc4CBiyGBVtgRGluiToXOCx+XJKrrdENqHOakAiA3yEeXIYp/bSdAoQ==", - "requires": { - "axios": "1.1.2", - "eventsource": "^2.0.2", - "p-limit": "^3.0.0" - } - }, - "axios": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.1.2.tgz", - "integrity": "sha512-bznQyETwElsXl2RK7HLLwb5GPpOLlycxHCtrpDR/4RqqBzjARaOTo3jz4IgtntWUYee7Ne4S8UHd92VCuzPaWA==", - "requires": { - "follow-redirects": "^1.15.0", - "form-data": "^4.0.0", - "proxy-from-env": "^1.1.0" - } - } } }, "@spheron/core": { @@ -7851,6 +7873,7 @@ "@types/node": "^18.13.0", "@typescript-eslint/eslint-plugin": "^5.51.0", "@typescript-eslint/parser": "^5.51.0", + "async-lock": "^1.4.0", "axios": "1.1.2", "eslint": "^8.33.0", "eventsource": "^2.0.2", @@ -7934,44 +7957,21 @@ "@spheron/site": { "version": "file:packages/site", "requires": { - "@spheron/core": "1.0.9", + "@spheron/core": "2.0.1", "@types/async-lock": "^1.4.0", "@types/node": "^18.13.0", "@typescript-eslint/eslint-plugin": "^5.51.0", "@typescript-eslint/parser": "^5.51.0", - "async-lock": "^1.4.0", "eslint": "^8.33.0", "prettier": "^2.8.3", "tsup": "^6.5.0", "typescript": "^4.9.5" - }, - "dependencies": { - "@spheron/core": { - "version": "1.0.9", - "resolved": "https://registry.npmjs.org/@spheron/core/-/core-1.0.9.tgz", - "integrity": "sha512-FUuD7mlWPUotN+lpT7XfKRBxr5geYLonDi+I66+ECpJuwUS7EYE1k0QsGEXG4tu8OLcW1wxSlarNzcejh0DZIg==", - "requires": { - "axios": "1.1.2", - "eventsource": "^2.0.2", - "p-limit": "^3.0.0" - } - }, - "axios": { - "version": "1.1.2", - "resolved": "https://registry.npmjs.org/axios/-/axios-1.1.2.tgz", - "integrity": "sha512-bznQyETwElsXl2RK7HLLwb5GPpOLlycxHCtrpDR/4RqqBzjARaOTo3jz4IgtntWUYee7Ne4S8UHd92VCuzPaWA==", - "requires": { - "follow-redirects": "^1.15.0", - "form-data": "^4.0.0", - "proxy-from-env": "^1.1.0" - } - } } }, "@spheron/storage": { "version": "file:packages/storage", "requires": { - "@spheron/core": "2.0.0", + "@spheron/core": "2.0.1", "@spheron/encryption": "1.0.0", "@types/node": "^18.13.0", "@typescript-eslint/eslint-plugin": "^5.51.0", diff --git a/packages/core/package.json b/packages/core/package.json index 93ceef54..ab81cef0 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -1,6 +1,6 @@ { "name": "@spheron/core", - "version": "2.0.0", + "version": "2.0.1", "description": "Shared core package for all sdk packages", "keywords": [ "Storage", @@ -35,6 +35,7 @@ "dist/" ], "dependencies": { + "async-lock": "^1.4.0", "axios": "1.1.2", "eventsource": "^2.0.2", "p-limit": "^3.0.0" diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index ba9ccfa3..09e7f103 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -4,3 +4,4 @@ export * from "./spheron-api/interfaces"; export * from "./upload-manager"; export * from "./spheron-api/request-interfaces"; export * from "./spheron-api/response-interfaces"; +export * from "./scope-extractor"; diff --git a/packages/core/src/scope-extractor.ts b/packages/core/src/scope-extractor.ts new file mode 100644 index 00000000..c3be67d2 --- /dev/null +++ b/packages/core/src/scope-extractor.ts @@ -0,0 +1,40 @@ +import asyncLock from "async-lock"; +import SpheronApi from "./spheron-api"; +import { TokenScope } from "./spheron-api/interfaces"; +import { AppTypeEnum } from "./spheron-api/enums"; + +abstract class ScopeExtractor { + private readonly api: SpheronApi; + private scope: TokenScope | null = null; + private lock: asyncLock; + + constructor(spheronApi: SpheronApi) { + this.api = spheronApi; + this.lock = new asyncLock(); + } + + protected async getScopeFromToken(): Promise { + if (!this.scope) { + await this.lock.acquire("getScopeFromToken", async () => { + if (!this.scope) { + this.scope = await this.api.getTokenScope(); + } + }); + } + + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + return this.scope!; + } + + protected async getOrganizationIdFromToken(): Promise { + const scope = await this.getScopeFromToken(); + return scope.organizations[0].id; + } + + protected async getOrganizationTypeFromToken(): Promise { + const scope = await this.getScopeFromToken(); + return scope.organizations[0].appType; + } +} + +export { ScopeExtractor }; diff --git a/packages/core/src/spheron-api/enums.ts b/packages/core/src/spheron-api/enums.ts index 37a92801..8565cd96 100644 --- a/packages/core/src/spheron-api/enums.ts +++ b/packages/core/src/spheron-api/enums.ts @@ -76,6 +76,7 @@ enum DeploymentStatusEnum { enum AppTypeEnum { WEB_APP = "app", COMPUTE = "compute", + STORAGE = "storage", } enum ProviderEnum { diff --git a/packages/core/src/spheron-api/interfaces.ts b/packages/core/src/spheron-api/interfaces.ts index 9e78a69d..78a4bc83 100644 --- a/packages/core/src/spheron-api/interfaces.ts +++ b/packages/core/src/spheron-api/interfaces.ts @@ -30,6 +30,7 @@ interface TokenScope { id: string; name: string; username: string; + appType: AppTypeEnum; }[]; } diff --git a/packages/site/README.md b/packages/site/README.md index c81ba924..a504f1ca 100644 --- a/packages/site/README.md +++ b/packages/site/README.md @@ -14,7 +14,7 @@

- + diff --git a/packages/site/package.json b/packages/site/package.json index 0e4e7eb8..8c711490 100644 --- a/packages/site/package.json +++ b/packages/site/package.json @@ -1,6 +1,6 @@ { "name": "@spheron/site", - "version": "1.0.1", + "version": "1.0.2", "description": "Typescript library for working with Spheron Web-App Organizations.", "keywords": [ "Storage", @@ -35,8 +35,7 @@ "dist/" ], "dependencies": { - "@spheron/core": "1.0.9", - "async-lock": "^1.4.0" + "@spheron/core": "2.0.1" }, "license": "Apache-2.0", "repository": { diff --git a/packages/site/src/deployment-manager.ts b/packages/site/src/deployment-manager.ts index caf88e73..a38149ae 100644 --- a/packages/site/src/deployment-manager.ts +++ b/packages/site/src/deployment-manager.ts @@ -1,4 +1,4 @@ -import { SpheronApi } from "@spheron/core"; +import { SpheronApi, ScopeExtractor } from "@spheron/core"; import { CancelDeploymentResponse, Deployment, @@ -6,10 +6,9 @@ import { DeploymentResponse, StartDeploymentConfiguration, } from "./interfaces"; -import OrganizationIdExtractor from "./organizationId-extractor"; import { mapCoreDeployment, mapCoreDeploymentLogs } from "./interface-mappers"; -class DeploymentManger extends OrganizationIdExtractor { +class DeploymentManger extends ScopeExtractor { private readonly spheronApi: SpheronApi; constructor(spheronApi: SpheronApi) { diff --git a/packages/site/src/organization-manager.ts b/packages/site/src/organization-manager.ts index fd90f50d..54d6f44c 100644 --- a/packages/site/src/organization-manager.ts +++ b/packages/site/src/organization-manager.ts @@ -1,13 +1,12 @@ -import { ProjectStateEnum, SpheronApi } from "@spheron/core"; +import { ProjectStateEnum, ScopeExtractor, SpheronApi } from "@spheron/core"; import { Organization, Project, UsageWithLimits } from "./interfaces"; -import OrganizationIdExtractor from "./organizationId-extractor"; import { mapCoreProject, mapCoreOrganization, mapCoreUsageWithLimits, } from "./interface-mappers"; -class OrganizationManager extends OrganizationIdExtractor { +class OrganizationManager extends ScopeExtractor { private readonly spheronApi: SpheronApi; constructor(spheronApi: SpheronApi) { diff --git a/packages/site/src/organizationId-extractor.ts b/packages/site/src/organizationId-extractor.ts deleted file mode 100644 index 0d3b0294..00000000 --- a/packages/site/src/organizationId-extractor.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { SpheronApi } from "@spheron/core"; -import asyncLock from "async-lock"; - -abstract class OrganizationIdExtractor { - private readonly api: SpheronApi; - private organizationId = ""; - private lock: asyncLock; - - constructor(spheronApi: SpheronApi) { - this.api = spheronApi; - this.lock = new asyncLock(); - } - - protected async getOrganizationIdFromToken(): Promise { - if (!this.organizationId) { - await this.lock.acquire("getOrganizationIdFromToken", async () => { - if (!this.organizationId) { - const scope = await this.api.getTokenScope(); - this.organizationId = scope.organizations[0].id; - } - }); - } - - return this.organizationId; - } -} - -export default OrganizationIdExtractor; diff --git a/packages/storage/README.md b/packages/storage/README.md index 2bdc9965..12ee69ad 100644 --- a/packages/storage/README.md +++ b/packages/storage/README.md @@ -14,7 +14,7 @@

- + diff --git a/packages/storage/package.json b/packages/storage/package.json index ca9790d9..28f33a39 100644 --- a/packages/storage/package.json +++ b/packages/storage/package.json @@ -1,6 +1,6 @@ { "name": "@spheron/storage", - "version": "2.0.0", + "version": "2.0.1", "description": "Typescript library for uploading files or directory to IPFS, Filecoin or Arweave via Spheron", "keywords": [ "Storage", @@ -35,7 +35,7 @@ "dist/" ], "dependencies": { - "@spheron/core": "2.0.0", + "@spheron/core": "2.0.1", "@spheron/encryption": "1.0.0", "form-data": "^4.0.0", "multiformats": "^9.9.0" diff --git a/packages/storage/src/index.ts b/packages/storage/src/index.ts index 66189a46..2ac50a15 100644 --- a/packages/storage/src/index.ts +++ b/packages/storage/src/index.ts @@ -13,6 +13,8 @@ import { UploadManager, UploadResult, PinStatus, + ScopeExtractor, + AppTypeEnum, } from "@spheron/core"; import { createPayloads } from "./fs-payload-creator"; import { ipfs } from "./ipfs.utils"; @@ -47,18 +49,20 @@ export interface SpheronClientConfiguration { apiUrl?: string; } -export class SpheronClient { +export class SpheronClient extends ScopeExtractor { private readonly configuration: SpheronClientConfiguration; private readonly spheronApi: SpheronApi; private readonly bucketManager: BucketManager; private readonly uploadManager: UploadManager; constructor(configuration: SpheronClientConfiguration) { - this.configuration = configuration; - this.spheronApi = new SpheronApi( - this.configuration.token, + const spheronApi = new SpheronApi( + configuration.token, configuration?.apiUrl ); + super(spheronApi); + this.spheronApi = spheronApi; + this.configuration = configuration; this.bucketManager = new BucketManager(this.spheronApi); this.uploadManager = new UploadManager(configuration?.apiUrl); } @@ -73,6 +77,8 @@ export class SpheronClient { onChunkUploaded?: (uploadedSize: number, totalSize: number) => void; } ): Promise { + await this.validateStorageOrganizationType(); + const { uploadId, payloadSize, parallelUploadCount } = await this.uploadManager.initiateUpload({ protocol: configuration.protocol, @@ -144,6 +150,8 @@ export class SpheronClient { litNodeClient, configuration, }: EncryptToIpfsProps): Promise { + await this.validateStorageOrganizationType(); + if (!string && !filePath) { throw new Error(`Either string or filePath must be provided`); } @@ -266,6 +274,8 @@ export class SpheronClient { ipfsCid, litNodeClient, }: DecryptFromIpfsProps): Promise { + await this.validateStorageOrganizationType(); + const metadata = await ( await fetch(`https://${ipfsCid}.ipfs.sphn.link/data.json`).catch(() => { throw new Error("Error finding metadata from IPFS CID"); @@ -292,6 +302,8 @@ export class SpheronClient { name: string; protocol: ProtocolEnum; }): Promise<{ uploadToken: string }> { + await this.validateStorageOrganizationType(); + const { singleUseToken } = await this.uploadManager.initiateUpload({ protocol: configuration.protocol, name: configuration.name, @@ -309,6 +321,8 @@ export class SpheronClient { protocolLink: string; dynamicLinks: string[]; }> { + await this.validateStorageOrganizationType(); + return await this.uploadManager.pinCID({ name: configuration.name, token: this.configuration.token, @@ -325,6 +339,8 @@ export class SpheronClient { limit: number; } ): Promise { + await this.validateStorageOrganizationType(); + return await this.bucketManager.getOrganizationBuckets( organizationId, options @@ -338,6 +354,8 @@ export class SpheronClient { state?: BucketStateEnum; } ): Promise { + await this.validateStorageOrganizationType(); + return await this.bucketManager.getOrganizationBucketCount( organizationId, options @@ -345,14 +363,20 @@ export class SpheronClient { } async getBucket(bucketId: string): Promise { + await this.validateStorageOrganizationType(); + return await this.bucketManager.getBucket(bucketId); } async getCIDStatus(CID: string): Promise<{ pinStatus: PinStatus }> { + await this.validateStorageOrganizationType(); + return await this.uploadManager.getCIDStatus(CID); } async getBucketDomains(bucketId: string): Promise { + await this.validateStorageOrganizationType(); + return await this.bucketManager.getBucketDomains(bucketId); } @@ -360,6 +384,8 @@ export class SpheronClient { bucketId: string, domainIdentifier: string ): Promise { + await this.validateStorageOrganizationType(); + return await this.bucketManager.getBucketDomain(bucketId, domainIdentifier); } @@ -377,6 +403,8 @@ export class SpheronClient { name: string; } ): Promise { + await this.validateStorageOrganizationType(); + return await this.bucketManager.addBucketDomain(bucketId, options); } @@ -388,6 +416,8 @@ export class SpheronClient { name: string; } ): Promise { + await this.validateStorageOrganizationType(); + return await this.bucketManager.updateBucketDomain( bucketId, domainIdentifier, @@ -399,6 +429,8 @@ export class SpheronClient { bucketId: string, domainIdentifier: string ): Promise { + await this.validateStorageOrganizationType(); + return await this.bucketManager.verifyBucketDomain( bucketId, domainIdentifier @@ -409,6 +441,8 @@ export class SpheronClient { cdnARecords: string; cdnCnameRecords: string; }> { + await this.validateStorageOrganizationType(); + const { recordIpv4V2, recordCnameV2 } = await this.spheronApi.getCdnRecords(); return { @@ -421,6 +455,8 @@ export class SpheronClient { bucketId: string, domainIdentifier: string ): Promise { + await this.validateStorageOrganizationType(); + return await this.bucketManager.deleteBucketDomain( bucketId, domainIdentifier @@ -428,14 +464,20 @@ export class SpheronClient { } async archiveBucket(bucketId: string): Promise { + await this.validateStorageOrganizationType(); + await this.bucketManager.archiveBucket(bucketId); } async unarchiveBucket(bucketId: string): Promise { + await this.validateStorageOrganizationType(); + await this.bucketManager.unarchiveBucket(bucketId); } async getBucketUploadCount(bucketId: string): Promise { + await this.validateStorageOrganizationType(); + return await this.bucketManager.getBucketUploadCount(bucketId); } @@ -446,14 +488,20 @@ export class SpheronClient { limit: number; } ): Promise { + await this.validateStorageOrganizationType(); + return await this.bucketManager.getBucketUploads(bucketId, options); } async getUpload(uploadId: string): Promise { + await this.validateStorageOrganizationType(); + return await this.bucketManager.getUpload(uploadId); } async getOrganizationUsage(organizationId: string): Promise { + await this.validateStorageOrganizationType(); + const usage = await this.spheronApi.getOrganizationUsage( organizationId, "storage" @@ -489,10 +537,14 @@ export class SpheronClient { } async getTokenScope(): Promise { - return await this.spheronApi.getTokenScope(); + await this.validateStorageOrganizationType(); + + return await this.getScopeFromToken(); } async getBucketIpnsRecords(bucketId: string): Promise { + await this.validateStorageOrganizationType(); + return await this.bucketManager.getBucketIpnsRecords(bucketId); } @@ -500,6 +552,8 @@ export class SpheronClient { bucketId: string, ipnsRecordId: string ): Promise { + await this.validateStorageOrganizationType(); + return await this.bucketManager.getBucketIpnsRecord(bucketId, ipnsRecordId); } @@ -507,6 +561,8 @@ export class SpheronClient { bucketId: string, uploadId: string ): Promise { + await this.validateStorageOrganizationType(); + return await this.bucketManager.addBucketIpnsRecord(bucketId, uploadId); } @@ -515,6 +571,8 @@ export class SpheronClient { ipnsRecordId: string, uploadId: string ): Promise { + await this.validateStorageOrganizationType(); + return await this.bucketManager.updateBucketIpnsRecord( bucketId, ipnsRecordId, @@ -526,6 +584,8 @@ export class SpheronClient { bucketId: string, ipnsRecordId: string ): Promise { + await this.validateStorageOrganizationType(); + await this.spheronApi.deleteBucketIpnsRecord(bucketId, ipnsRecordId); } @@ -536,11 +596,22 @@ export class SpheronClient { numberOfBuckets: number; numberOfUploads: number; }> { + await this.validateStorageOrganizationType(); + return await this.spheronApi.migrateStaticSiteOrgToStorage( webappOrganizationId, storageOrganizationId ); } + + private async validateStorageOrganizationType(): Promise { + const type = await this.getOrganizationTypeFromToken(); + if (type != AppTypeEnum.STORAGE) { + throw new Error( + "The token used won't work with version >2.0.0 of SDK, please create a new token from your storage organisation." + ); + } + } } export default SpheronClient;