diff --git a/package.json b/package.json index 2f2ded8e..e3f53091 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@internxt/sdk", "author": "Internxt ", - "version": "1.9.10", + "version": "1.9.11", "description": "An sdk for interacting with Internxt's services", "repository": { "type": "git", diff --git a/src/drive/index.ts b/src/drive/index.ts index abdad678..8cbb4bf8 100644 --- a/src/drive/index.ts +++ b/src/drive/index.ts @@ -3,5 +3,6 @@ export * from './share'; export * from './users'; export * from './referrals'; export * from './payments'; +export * from './payments/object-storage'; export * from './backups'; -export * from './trash'; \ No newline at end of file +export * from './trash'; diff --git a/src/drive/payments/object-storage.ts b/src/drive/payments/object-storage.ts new file mode 100644 index 00000000..677cc4d1 --- /dev/null +++ b/src/drive/payments/object-storage.ts @@ -0,0 +1,93 @@ +import { ApiUrl, AppDetails } from '../../shared'; +import { basicHeaders } from '../../shared/headers'; +import { HttpClient } from '../../shared/http/client'; +import { CreatedSubscriptionData } from './types'; + +interface ObjectStoragePlan { + id: string; + bytes: number; + interval: 'year' | 'month' | 'lifetime'; + amount: number; + currency: string; + decimalAmount: number; +} + +export class ObjectStorage { + private readonly client: HttpClient; + private readonly appDetails: AppDetails; + + public static client(apiUrl: ApiUrl, appDetails: AppDetails) { + return new ObjectStorage(apiUrl, appDetails); + } + + private constructor(apiUrl: ApiUrl, appDetails: AppDetails) { + this.client = HttpClient.create(apiUrl); + this.appDetails = appDetails; + } + + public getObjectStoragePlanById(priceId: string, currency?: string): Promise { + const query = new URLSearchParams(); + priceId !== undefined && query.set('planId', priceId); + currency !== undefined && query.set('currency', currency); + return this.client.get(`/object-storage-plan-by-id?${query.toString()}`, this.headers()); + } + + public createCustomerForObjectStorage({ + name, + email, + country, + companyVatId, + }: { + name: string; + email: string; + country?: string; + companyVatId?: string; + }): Promise<{ customerId: string; token: string }> { + return this.client.post( + '/create-customer-for-object-storage', + { + name, + email, + country, + companyVatId, + }, + this.headers(), + ); + } + + public createObjectStorageSubscription({ + customerId, + plan, + token, + companyName, + vatId, + promoCodeId, + }: { + customerId: string; + plan: ObjectStoragePlan; + token: string; + companyName: string; + vatId: string; + promoCodeId?: string; + }): Promise { + const { id: priceId, currency } = plan; + + return this.client.post( + '/create-subscription-for-object-storage', + { + customerId, + priceId, + token, + currency, + companyName, + companyVatId: vatId, + promoCodeId, + }, + this.headers(), + ); + } + + private headers() { + return basicHeaders(this.appDetails.clientName, this.appDetails.clientVersion); + } +} diff --git a/test/drive/payments/object-storage.test.ts b/test/drive/payments/object-storage.test.ts new file mode 100644 index 00000000..2d6e5283 --- /dev/null +++ b/test/drive/payments/object-storage.test.ts @@ -0,0 +1,105 @@ +import sinon from 'sinon'; +import { AppDetails } from '../../../src/shared'; +import { basicHeaders } from '../../../src/shared/headers'; +import { ObjectStorage } from '../../../src/drive'; +import { HttpClient } from '../../../src/shared/http/client'; + +const httpClient = HttpClient.create(''); + +describe('Object Storage service', () => { + beforeEach(() => { + sinon.stub(HttpClient, 'create').returns(httpClient); + }); + + afterEach(() => { + sinon.restore(); + }); + + describe('Get object storage plan by id', () => { + it('When get object storage plan by id is requested, then it should call with right params & return data', async () => { + const callStub = sinon.stub(httpClient, 'get').resolves({ + id: 'plan_123', + }); + const { client, headers } = basicHeadersAndClient(); + + const body = await client.getObjectStoragePlanById('plan_123', 'eur'); + + expect(callStub.firstCall.args).toEqual(['/object-storage-plan-by-id?planId=plan_123¤cy=eur', headers]); + expect(body).toEqual({ id: 'plan_123' }); + }); + }); + + describe('Create customer for object storage', () => { + it('When create customer is requested, then it should call with right params & return data', async () => { + const callStub = sinon.stub(httpClient, 'post').resolves({ + customerId: 'cus_123', + }); + const { client, headers } = basicHeadersAndClient(); + + const body = await client.createCustomerForObjectStorage({ + name: 'test', + email: 'test@test.com', + country: 'US', + companyVatId: '1234567890', + }); + + expect(callStub.firstCall.args).toEqual([ + '/create-customer-for-object-storage', + { name: 'test', email: 'test@test.com', country: 'US', companyVatId: '1234567890' }, + headers, + ]); + expect(body).toEqual({ customerId: 'cus_123' }); + }); + }); + + describe('Create object storage subscription', () => { + it('When create subscription is requested, then it should call with right params & return data', async () => { + const callStub = sinon.stub(httpClient, 'post').resolves({ + subscriptionId: 'sub_123', + }); + + const { client, headers } = basicHeadersAndClient(); + + await client.createObjectStorageSubscription({ + customerId: 'cus_123', + plan: { id: 'plan_123', bytes: 1000, interval: 'month', amount: 1000, currency: 'eur', decimalAmount: 10.0 }, + token: 'token', + companyName: 'test', + vatId: '1234567890', + promoCodeId: 'promo_123', + }); + + expect(callStub.firstCall.args).toEqual([ + '/create-subscription-for-object-storage', + { + customerId: 'cus_123', + priceId: 'plan_123', + token: 'token', + currency: 'eur', + companyName: 'test', + companyVatId: '1234567890', + promoCodeId: 'promo_123', + }, + headers, + ]); + }); + }); +}); + +function basicHeadersAndClient( + apiUrl = '', + clientName = 'c-name', + clientVersion = '0.1', +): { + client: ObjectStorage; + headers: object; +} { + const appDetails: AppDetails = { + clientName: clientName, + clientVersion: clientVersion, + }; + + const client = ObjectStorage.client(apiUrl, appDetails); + const headers = basicHeaders(clientName, clientVersion); + return { client, headers }; +}