Skip to content

Commit

Permalink
Merge pull request #33 from dimitrov-d/short-url
Browse files Browse the repository at this point in the history
Short URL
  • Loading branch information
MoMannn authored Jul 22, 2024
2 parents 9baf62a + e492153 commit 409bf73
Show file tree
Hide file tree
Showing 16 changed files with 59 additions and 88 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@apillon/sdk",
"description": "▶◀ Apillon SDK for NodeJS ▶◀",
"version": "3.1.0",
"version": "3.2.0",
"author": "Apillon",
"license": "MIT",
"main": "./dist/index.js",
Expand Down
9 changes: 9 additions & 0 deletions packages/sdk/src/lib/apillon.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ export class ApillonModel {
this.uuid = uuid;
}

/**
* Gets and populates the object details of this entity
* @returns A new instance of this object
*/
async get(): Promise<this> {
const data = await ApillonApi.get<this>(this.API_PREFIX);
return this.populate(data);
}

/**
* Populates class properties via data object.
* @param data Data object.
Expand Down
9 changes: 0 additions & 9 deletions packages/sdk/src/modules/computing/computing-contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,6 @@ export class ComputingContract extends ApillonModel {
this.config = config;
}

/**
* Gets a computing contract's details.
* @returns ComputingContract instance
*/
async get(): Promise<ComputingContract> {
const data = await ApillonApi.get<ComputingContract>(this.API_PREFIX);
return this.populate(data);
}

/**
* Gets list of transactions for this computing contract.
* @param {ITransactionListFilters} params Query filters.
Expand Down
10 changes: 0 additions & 10 deletions packages/sdk/src/modules/hosting/deployment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { ApillonModel } from '../../lib/apillon';

import { ApillonApi } from '../../lib/apillon-api';
import { DeployToEnvironment, DeploymentStatus } from '../../types/hosting';

export class Deployment extends ApillonModel {
Expand Down Expand Up @@ -56,14 +54,6 @@ export class Deployment extends ApillonModel {
this.populate(data);
}

/**
* Gets deployment details.
*/
async get(): Promise<Deployment> {
const data = await ApillonApi.get<Deployment>(this.API_PREFIX);
return this.populate(data);
}

protected override serializeFilter(key: string, value: any) {
const serialized = super.serializeFilter(key, value);
const enums = {
Expand Down
9 changes: 0 additions & 9 deletions packages/sdk/src/modules/hosting/hosting-website.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,6 @@ export class HostingWebsite extends ApillonModel {
this.populate(data);
}

/**
* Gets information about the website and fills properties with it.
* @returns An instance of HostingWebsite class with filled properties.
*/
public async get(): Promise<HostingWebsite> {
const data = await ApillonApi.get<HostingWebsite>(this.API_PREFIX);
return this.populate(data);
}

/**
* Uploads website files inside a folder via path.
* @param folderPath Path to the folder to upload.
Expand Down
21 changes: 18 additions & 3 deletions packages/sdk/src/modules/hosting/hosting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@ import { ApillonModule } from '../../lib/apillon';
import { ApillonApi } from '../../lib/apillon-api';
import { constructUrlWithQueryParams } from '../../lib/common';
import { IApillonList } from '../../types/apillon';
import { IWebsiteFilters } from '../../types/hosting';
import { IWebsiteFilters, ShortUrl } from '../../types/hosting';
import { HostingWebsite } from './hosting-website';

export class Hosting extends ApillonModule {
/**
* Base API url for hosting.
*/
private API_PREFIX = '/hosting/websites';
private API_PREFIX = '/hosting';

/**
* List all websites
* @param {IWebsiteFilters} params Query filters for listing websites
* @returns A list of HostingWebsite instances.
*/
public async listWebsites(
params?: IWebsiteFilters,
): Promise<IApillonList<HostingWebsite>> {
const url = constructUrlWithQueryParams(this.API_PREFIX, params);
const url = constructUrlWithQueryParams(
`${this.API_PREFIX}/websites`,
params,
);

const data = await ApillonApi.get<IApillonList<HostingWebsite>>(url);

Expand All @@ -37,4 +41,15 @@ export class Hosting extends ApillonModule {
public website(uuid: string): HostingWebsite {
return new HostingWebsite(uuid);
}

/**
* Generate a short link for a given target URL
* @param {string} targetUrl - The targer URL to generate a shortened URL for
* @returns {ShortUrl}
*/
public async generateShortUrl(targetUrl: string): Promise<ShortUrl> {
return await ApillonApi.post<any>(`${this.API_PREFIX}/short-url`, {
targetUrl,
});
}
}
9 changes: 0 additions & 9 deletions packages/sdk/src/modules/nft/nft-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,6 @@ export class NftCollection extends ApillonModel {
this.populate(data);
}

/**
* Gets and populates collection information.
* @returns Collection instance.
*/
public async get(): Promise<NftCollection> {
const data = await ApillonApi.get<NftCollection>(this.API_PREFIX);
return this.populate(data);
}

/**
* @param {IMintNftData} params - NFT mint parameters
* @returns {INftActionResponse} - success status and transaction hash of the mint
Expand Down
10 changes: 0 additions & 10 deletions packages/sdk/src/modules/social/social-channel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ApillonApi } from '../../lib/apillon-api';
import { ApillonModel } from '../../lib/apillon';
import { HubStatus } from '../../types/social';

Expand Down Expand Up @@ -33,13 +32,4 @@ export class SocialChannel extends ApillonModel {
this.API_PREFIX = `/social/channels/${uuid}`;
this.populate(data);
}

/**
* Fetches and populates the channel details from the API.
* @returns An instance of Channel class with filled properties.
*/
public async get(): Promise<SocialChannel> {
const data = await ApillonApi.get<SocialChannel>(this.API_PREFIX);
return this.populate(data);
}
}
10 changes: 0 additions & 10 deletions packages/sdk/src/modules/social/social-hub.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ApillonApi } from '../../lib/apillon-api';
import { ApillonModel } from '../../lib/apillon';
import { HubStatus } from '../../types/social';

Expand Down Expand Up @@ -39,13 +38,4 @@ export class SocialHub extends ApillonModel {
this.API_PREFIX = `/social/hubs/${uuid}`;
this.populate(data);
}

/**
* Fetches and populates the hub details from the API.
* @returns An instance of Hub class with filled properties.
*/
public async get(): Promise<SocialHub> {
const data = await ApillonApi.get<SocialHub>(this.API_PREFIX);
return this.populate(data);
}
}
8 changes: 3 additions & 5 deletions packages/sdk/src/modules/storage/directory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ export class Directory extends ApillonModel {
* Gets contents of a directory.
* @returns Directory data and content (files and subfolders)
*/
async get(
params: IStorageBucketContentRequest = {},
): Promise<(Directory | File)[]> {
override async get(params: IStorageBucketContentRequest = {}) {
this.content = [];
params.directoryUuid = this.uuid;
const url = constructUrlWithQueryParams(
Expand All @@ -76,7 +74,7 @@ export class Directory extends ApillonModel {

for (const item of data.items) {
if (item.type == StorageContentType.FILE) {
const file = item as File;
const file = item as File & { fileStatus: number };
this.content.push(
new File(this.bucketUuid, file.directoryUuid, file.uuid, file),
);
Expand All @@ -87,7 +85,7 @@ export class Directory extends ApillonModel {
}
}

return this.content;
return this.content as any;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/modules/storage/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export class File extends ApillonModel {
* Gets file details.
* @returns File instance
*/
async get(): Promise<File> {
public override async get(): Promise<this> {
const data = await ApillonApi.get<File & { fileStatus: number }>(
this.API_PREFIX,
);
Expand Down
9 changes: 0 additions & 9 deletions packages/sdk/src/modules/storage/ipns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,6 @@ export class Ipns extends ApillonModel {
this.populate(data);
}

/**
* Gets IPNS details.
* @returns IPNS record
*/
async get(): Promise<Ipns> {
const data = await ApillonApi.get<Ipns>(this.API_PREFIX);
return this.populate(data);
}

/**
* Publish an IPNS record to IPFS and link it to a CID.
* @param {string} cid - CID to which this ipns name will point.
Expand Down
13 changes: 2 additions & 11 deletions packages/sdk/src/modules/storage/storage-bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,6 @@ export class StorageBucket extends ApillonModel {
this.populate(data);
}

/**
* Gets bucket details.
* @returns Bucket instance
*/
async get(): Promise<StorageBucket> {
const data = await ApillonApi.get<StorageBucket>(this.API_PREFIX);
return this.populate(data);
}

/**
* Gets contents of a bucket.
* @returns A a list of File and Directory objects.
Expand All @@ -80,7 +71,7 @@ export class StorageBucket extends ApillonModel {
const data = await ApillonApi.get<IApillonList<File | Directory>>(url);
for (const item of data.items) {
if (item.type == StorageContentType.FILE) {
const file = item as File;
const file = item as File & { fileStatus: number };
content.push(new File(this.uuid, file.directoryUuid, file.uuid, file));
} else {
const directory = new Directory(
Expand All @@ -103,7 +94,7 @@ export class StorageBucket extends ApillonModel {
async listFiles(params?: IBucketFilesRequest): Promise<IApillonList<File>> {
const url = constructUrlWithQueryParams(`${this.API_PREFIX}/files`, params);
const data = await ApillonApi.get<
IApillonList<File & { fileUuid: string }>
IApillonList<File & { fileUuid: string; fileStatus: number }>
>(url);

return {
Expand Down
9 changes: 9 additions & 0 deletions packages/sdk/src/tests/hosting.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,13 @@ describe('Hosting tests', () => {
const deployment = await website.deployment(deploymentUuid).get();
expect(deployment.environment).toEqual(DeployToEnvironment.TO_STAGING);
});

test('generate short url', async () => {
const targertUrl = 'https://ipfs.apillon.io/ipfs/abc';

const shortUrl = await hosting.generateShortUrl(targertUrl);
expect(shortUrl.id).toBeDefined();
expect(shortUrl.targetUrl).toBe(targertUrl);
expect(shortUrl.url).toContain(shortUrl.id);
});
});
15 changes: 15 additions & 0 deletions packages/sdk/src/types/hosting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,18 @@ export interface IDeploymentFilters extends IApillonPagination {
deploymentStatus?: DeploymentStatus;
environment?: DeployToEnvironment;
}

export interface ShortUrl {
/**
* the short URL slug
*/
id: string;
/**
* the short URL
*/
url: string;
/**
* the target URL which the short link points to
*/
targetUrl: string;
}

0 comments on commit 409bf73

Please sign in to comment.