Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@internxt/sdk",
"author": "Internxt <[email protected]>",
"version": "1.9.10",
"version": "1.9.11",
"description": "An sdk for interacting with Internxt's services",
"repository": {
"type": "git",
Expand Down
3 changes: 2 additions & 1 deletion src/drive/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
export * from './trash';
93 changes: 93 additions & 0 deletions src/drive/payments/object-storage.ts
Original file line number Diff line number Diff line change
@@ -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<ObjectStoragePlan> {
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<CreatedSubscriptionData> {
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);
}
}
105 changes: 105 additions & 0 deletions test/drive/payments/object-storage.test.ts
Original file line number Diff line number Diff line change
@@ -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&currency=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: '[email protected]',
country: 'US',
companyVatId: '1234567890',
});

expect(callStub.firstCall.args).toEqual([
'/create-customer-for-object-storage',
{ name: 'test', email: '[email protected]', 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 };
}