Skip to content

Commit

Permalink
PR review fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitrov-d committed Feb 1, 2024
1 parent 44f8b1d commit a87db59
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 157 deletions.
10 changes: 10 additions & 0 deletions packages/cli/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { exceptionHandler } from '@apillon/sdk';

export function enumValues(enumType: any): string[] {
return Object.values(enumType)
.filter((value) => typeof value === 'number')
.map((value) => value.toString());
}

export async function withErrorHandler(handler: () => Promise<any>) {
try {
await handler();
} catch (err) {
exceptionHandler(err);
}
}
2 changes: 1 addition & 1 deletion packages/cli/src/modules/hosting/hosting.commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ export function createHostingCommands(cli: Command) {
'--status <deployment-status>',
'Status of the deployment (optional) Choose from:\n' +
` ${DeploymentStatus.INITIATED}: Initiated\n` +
` ${DeploymentStatus.IN_PROGRESS}: In process\n` +
` ${DeploymentStatus.IN_PROGRESS}: In progress\n` +
` ${DeploymentStatus.IN_REVIEW}: In review\n` +
` ${DeploymentStatus.APPROVED}: Approved\n` +
` ${DeploymentStatus.SUCCESSFUL}: Successful\n` +
Expand Down
68 changes: 29 additions & 39 deletions packages/cli/src/modules/hosting/hosting.service.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
import {
Hosting,
exceptionHandler,
DeployToEnvironment,
toInteger,
} from '@apillon/sdk';
import { Hosting, DeployToEnvironment, toInteger } from '@apillon/sdk';
import { GlobalOptions } from '../../lib/types';
import { paginate } from '../../lib/options';
import { withErrorHandler } from '../../lib/utils';

export async function listWebsites(optsWithGlobals: GlobalOptions) {
await withErrorHandler(async (hosting) => {
const data = await hosting.listWebsites(paginate(optsWithGlobals));
await withErrorHandler(async () => {
const data = await new Hosting(optsWithGlobals).listWebsites(
paginate(optsWithGlobals),
);
data.items = data.items.map((w) => w.serialize());
console.log(data);
}, optsWithGlobals);
});
}

export async function getWebsite(optsWithGlobals: GlobalOptions) {
await withErrorHandler(async (hosting) => {
const website = await hosting.website(optsWithGlobals.uuid).get();
await withErrorHandler(async () => {
const website = await new Hosting(optsWithGlobals)
.website(optsWithGlobals.uuid)
.get();
console.log(website.serialize());
}, optsWithGlobals);
});
}

export async function deployWebsite(
path: string,
optsWithGlobals: GlobalOptions,
) {
await withErrorHandler(async (hosting) => {
const website = hosting.website(optsWithGlobals.uuid);
await withErrorHandler(async () => {
const website = new Hosting(optsWithGlobals).website(optsWithGlobals.uuid);
console.log(`Uploading files from folder: ${path}`);
await website.uploadFromFolder(path);
const deployment = await website.deploy(
Expand All @@ -38,60 +38,50 @@ export async function deployWebsite(
console.log(`Deployment started!`);
const deploymentData = await website.deployment(deployment.uuid).get();
console.log(deploymentData.serialize());
}, optsWithGlobals);
});
}

export async function uploadWebsiteFiles(
path: string,
optsWithGlobals: GlobalOptions,
) {
await withErrorHandler(async (hosting) => {
await hosting.website(optsWithGlobals.uuid).uploadFromFolder(path);
}, optsWithGlobals);
await withErrorHandler(async () => {
await new Hosting(optsWithGlobals)
.website(optsWithGlobals.uuid)
.uploadFromFolder(path);
});
}

export async function deployToEnvironment(optsWithGlobals: GlobalOptions) {
await withErrorHandler(async (hosting) => {
await hosting
await withErrorHandler(async () => {
await new Hosting(optsWithGlobals)
.website(optsWithGlobals.uuid)
.deploy(toInteger(optsWithGlobals.env));
console.log('Deploy successful');
}, optsWithGlobals);
});
}

export async function listDeployments(optsWithGlobals: GlobalOptions) {
await withErrorHandler(async (hosting) => {
await withErrorHandler(async () => {
const params = {
...paginate(optsWithGlobals),
environment: toInteger(optsWithGlobals.env),
deploymentStatus: toInteger(optsWithGlobals.status),
};
const data = await hosting
const data = await new Hosting(optsWithGlobals)
.website(optsWithGlobals.uuid)
.listDeployments(params);
data.items = data.items.map((w) => w.serialize());
console.log(data);
}, optsWithGlobals);
});
}

export async function getDeployment(optsWithGlobals: GlobalOptions) {
await withErrorHandler(async (hosting) => {
const deployment = await hosting
await withErrorHandler(async () => {
const deployment = await new Hosting(optsWithGlobals)
.website(optsWithGlobals.websiteUuid)
.deployment(optsWithGlobals.deploymentUuid)
.get();
console.log(deployment.serialize());
}, optsWithGlobals);
}

async function withErrorHandler(
handler: (module: Hosting) => Promise<any>,
optsWithGlobals: GlobalOptions,
) {
try {
const module = new Hosting(optsWithGlobals);
await handler(module);
} catch (err) {
exceptionHandler(err);
}
});
}
75 changes: 32 additions & 43 deletions packages/cli/src/modules/nfts/nft.service.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
import {
exceptionHandler,
ICreateCollection,
Nft,
toInteger,
} from '@apillon/sdk';
import { ICreateCollection, Nft, toInteger } from '@apillon/sdk';
import { readAndParseJson } from '../../lib/files';
import { GlobalOptions } from '../../lib/types';
import { paginate } from '../../lib/options';
import { withErrorHandler } from '../../lib/utils';

export async function listCollections(optsWithGlobals: GlobalOptions) {
await withErrorHandler(async (nftService: Nft) => {
const data = await nftService.listCollections({
await withErrorHandler(async () => {
const data = await new Nft(optsWithGlobals).listCollections({
...paginate(optsWithGlobals),
collectionStatus: toInteger(optsWithGlobals.status),
});
console.log(data.items.map((d) => d.serialize()));
}, optsWithGlobals);
});
}

export async function getCollection(optsWithGlobals: GlobalOptions) {
await withErrorHandler(async (nftService: Nft) => {
const data = await nftService.collection(optsWithGlobals.uuid).get();
await withErrorHandler(async () => {
const data = await new Nft(optsWithGlobals)
.collection(optsWithGlobals.uuid)
.get();
console.log(data.serialize());
}, optsWithGlobals);
});
}

export async function createCollection(
filePath: string,
optsWithGlobals: GlobalOptions,
) {
await withErrorHandler(async (nftService: Nft) => {
await withErrorHandler(async () => {
let createCollectionData;
try {
createCollectionData = readAndParseJson(filePath) as ICreateCollection;
Expand All @@ -49,26 +47,29 @@ export async function createCollection(
return;
}

const data = await nftService.create(createCollectionData);
const data = await new Nft(optsWithGlobals).create(createCollectionData);
console.log(data.serialize());
console.log('NFT collection created successfully!');
}, optsWithGlobals);
});
}

export async function mintCollectionNft(optsWithGlobals: GlobalOptions) {
await withErrorHandler(async (nftService: Nft) => {
const data = await nftService
await withErrorHandler(async () => {
const data = await new Nft(optsWithGlobals)
.collection(optsWithGlobals.uuid)
.mint(optsWithGlobals.address, toInteger(optsWithGlobals.quantity));
.mint({
receivingAddress: optsWithGlobals.address,
quantity: toInteger(optsWithGlobals.quantity),
});
if (data.success) {
console.log('NFT minted successfully');
}
}, optsWithGlobals);
});
}

export async function nestMintCollectionNft(optsWithGlobals: GlobalOptions) {
await withErrorHandler(async (nftService: Nft) => {
const data = await nftService
await withErrorHandler(async () => {
const data = await new Nft(optsWithGlobals)
.collection(optsWithGlobals.uuid)
.nestMint(
optsWithGlobals.parentCollection,
Expand All @@ -78,54 +79,42 @@ export async function nestMintCollectionNft(optsWithGlobals: GlobalOptions) {
if (data.success) {
console.log('NFT nest minted successfully');
}
}, optsWithGlobals);
});
}

export async function burnCollectionNft(optsWithGlobals: GlobalOptions) {
await withErrorHandler(async (nftService: Nft) => {
const data = await nftService
await withErrorHandler(async () => {
const data = await new Nft(optsWithGlobals)
.collection(optsWithGlobals.uuid)
.burn(optsWithGlobals.tokenId);
if (data.success) {
console.log('NFT burned successfully');
}
}, optsWithGlobals);
});
}

export async function transferCollectionOwnership(
optsWithGlobals: GlobalOptions,
) {
await withErrorHandler(async (nftService: Nft) => {
await nftService
await withErrorHandler(async () => {
await new Nft(optsWithGlobals)
.collection(optsWithGlobals.uuid)
.transferOwnership(optsWithGlobals.address);
console.log('NFT ownership transferred successfully');
}, optsWithGlobals);
});
}

export async function listCollectionTransactions(
optsWithGlobals: GlobalOptions,
) {
await withErrorHandler(async (nftService: Nft) => {
const data = await nftService
await withErrorHandler(async () => {
const data = await new Nft(optsWithGlobals)
.collection(optsWithGlobals.uuid)
.listTransactions({
...paginate(optsWithGlobals),
transactionStatus: toInteger(optsWithGlobals.status),
transactionType: toInteger(optsWithGlobals.type),
});
console.log(data);
}, optsWithGlobals);
}

async function withErrorHandler(
handler: (module: Nft) => Promise<any>,
optsWithGlobals: GlobalOptions,
) {
try {
const module = new Nft(optsWithGlobals);
await handler(module);
} catch (err) {
exceptionHandler(err);
}
});
}
55 changes: 23 additions & 32 deletions packages/cli/src/modules/storage/ipns.service.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,59 @@
import { Storage, exceptionHandler } from '@apillon/sdk';
import { Storage } from '@apillon/sdk';
import { GlobalOptions } from '../../lib/types';
import { paginate } from '../../lib/options';
import { withErrorHandler } from '../../lib/utils';

export async function listIpnsNames(optsWithGlobals: GlobalOptions) {
await withErrorHandler(async (storage: Storage) => {
const data = await storage
await withErrorHandler(async () => {
const data = await new Storage(optsWithGlobals)
.bucket(optsWithGlobals.bucketUuid)
.listIpnsNames(paginate(optsWithGlobals));
data.items = data.items.map((w) => w.serialize());
console.log(data);
}, optsWithGlobals);
});
}

export async function createIpns(optsWithGlobals: GlobalOptions) {
await withErrorHandler(async (storage: Storage) => {
const ipns = await storage.bucket(optsWithGlobals.bucketUuid).createIpns({
name: optsWithGlobals.name,
description: optsWithGlobals.description,
cid: optsWithGlobals.cid,
});
await withErrorHandler(async () => {
const ipns = await new Storage(optsWithGlobals)
.bucket(optsWithGlobals.bucketUuid)
.createIpns({
name: optsWithGlobals.name,
description: optsWithGlobals.description,
cid: optsWithGlobals.cid,
});
console.log('IPNS record created successfully');
console.log(ipns.serialize());
}, optsWithGlobals);
});
}

export async function getIpns(optsWithGlobals: GlobalOptions) {
await withErrorHandler(async (storage: Storage) => {
const ipns = await storage
await withErrorHandler(async () => {
const ipns = await new Storage(optsWithGlobals)
.bucket(optsWithGlobals.bucketUuid)
.ipns(optsWithGlobals.ipnsUuid)
.get();
console.log(ipns.serialize());
}, optsWithGlobals);
});
}

export async function publishIpns(optsWithGlobals: GlobalOptions) {
await withErrorHandler(async (storage: Storage) => {
const ipns = await storage
await withErrorHandler(async () => {
const ipns = await new Storage(optsWithGlobals)
.bucket(optsWithGlobals.bucketUuid)
.ipns(optsWithGlobals.ipnsUuid)
.publish(optsWithGlobals.cid);
console.log('IPNS published successfully');
console.log(ipns.serialize());
}, optsWithGlobals);
});
}

export async function deleteIpns(optsWithGlobals: GlobalOptions) {
await withErrorHandler(async (storage: Storage) => {
await storage
await withErrorHandler(async () => {
await new Storage(optsWithGlobals)
.bucket(optsWithGlobals.bucketUuid)
.ipns(optsWithGlobals.ipnsUuid)
.delete();
console.log('IPNS record deleted successfully');
}, optsWithGlobals);
}

async function withErrorHandler(
handler: (module: Storage) => Promise<any>,
optsWithGlobals: GlobalOptions,
) {
try {
const module = new Storage(optsWithGlobals);
await handler(module);
} catch (err) {
exceptionHandler(err);
}
});
}
Loading

0 comments on commit a87db59

Please sign in to comment.