-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #132 from dhirajudhani/feat/128-wrap-api-calls-in-…
…try-catch feat:#128 wrapped all the api calls into try catch block
- Loading branch information
Showing
1 changed file
with
65 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,86 +1,93 @@ | ||
import * as Pieces from "@pieces.app/pieces-os-client"; | ||
import {SeededAsset, SeedTypeEnum} from "@pieces.app/pieces-os-client"; | ||
import {Application} from "@pieces.app/pieces-os-client"; | ||
|
||
import { SeededAsset, SeedTypeEnum } from "@pieces.app/pieces-os-client"; | ||
import { Application } from "@pieces.app/pieces-os-client"; | ||
|
||
type LocalAsset = { | ||
name: string, | ||
id: string, | ||
classification: Pieces.ClassificationSpecificEnum, | ||
} | ||
name: string, | ||
id: string, | ||
classification: Pieces.ClassificationSpecificEnum, | ||
} | ||
|
||
//==============================[/create]==================================// | ||
export function createAsset(applicationData: Application, data: string, name: string) { | ||
|
||
export async function createAsset(applicationData: Application, data: string, name: string) { | ||
let _seededAsset: SeededAsset = { | ||
application: applicationData, | ||
format: { | ||
fragment: { | ||
string: { raw: data }, | ||
}, | ||
application: applicationData, | ||
format: { | ||
fragment: { | ||
string: { raw: data }, | ||
}, | ||
metadata: { | ||
name: name | ||
} | ||
}, | ||
metadata: { | ||
name: name | ||
} | ||
} | ||
|
||
// create your seed | ||
let _seed: Pieces.Seed = { | ||
asset: _seededAsset, | ||
type: SeedTypeEnum.Asset | ||
asset: _seededAsset, | ||
type: SeedTypeEnum.Asset | ||
} | ||
|
||
console.log("seed:", _seed) | ||
|
||
// make your api call. | ||
new Pieces.AssetsApi().assetsCreateNewAsset({seed: _seed}).then(_a => { | ||
console.log("well howdy", _a); | ||
}) | ||
|
||
try { | ||
const _a = await new Pieces.AssetsApi().assetsCreateNewAsset({ seed: _seed }); | ||
console.log("well howdy", _a); | ||
} catch (error) { | ||
console.error("Error creating asset:", error); | ||
} | ||
} | ||
//==============================[.end /create]==================================// | ||
|
||
export function deleteAsset(_id: String,setArray:Function){ | ||
|
||
const newAssetsList : Array<LocalAsset> = []; | ||
|
||
new Pieces.AssetsApi().assetsSnapshot({}).then(_assetList => { | ||
for (let i = 0; i < _assetList.iterable.length; i++) { | ||
if (_assetList.iterable[i].id == _id) { | ||
new Pieces.AssetsApi().assetsDeleteAsset({asset: _assetList.iterable[i].id }).then(()=>console.log(_id)); | ||
} | ||
else{ | ||
newAssetsList.push( { | ||
id: _assetList.iterable[i].id, | ||
name: _assetList.iterable[i].name, | ||
classification: _assetList.iterable[i].original.reference.classification.specific | ||
}) | ||
} | ||
export async function deleteAsset(_id: String, setArray: Function) { | ||
const newAssetsList: Array<LocalAsset> = []; | ||
try { | ||
const _assetList = await new Pieces.AssetsApi().assetsSnapshot({}); | ||
for (let i = 0; i < _assetList.iterable.length; i++) { | ||
if (_assetList.iterable[i].id == _id) { | ||
try { | ||
await new Pieces.AssetsApi().assetsDeleteAsset({ asset: _assetList.iterable[i].id }); | ||
console.log(_id); | ||
} catch (error) { | ||
console.error("Error deleting asset:", error); | ||
} | ||
} else { | ||
newAssetsList.push({ | ||
id: _assetList.iterable[i].id, | ||
name: _assetList.iterable[i].name, | ||
classification: _assetList.iterable[i].original.reference.classification.specific | ||
}); | ||
} | ||
window.alert("selected snippet got deleted"); | ||
setArray(newAssetsList); | ||
}) | ||
} | ||
window.alert("selected snippet got deleted"); | ||
setArray(newAssetsList); | ||
} catch (error) { | ||
console.error("Error fetching asset list:", error); | ||
} | ||
} | ||
|
||
// used to rename an asset, takes in an _id and _name that comes from the input fields on | ||
// NameInput + DataInput fields. | ||
// | ||
// this uses your asset snapshot to get your list of snippets, then() to get the snippet list back, | ||
// then use the _id to select the snippet from the list of all snippets. | ||
export function renameAsset(_name: string, _id: String){ | ||
|
||
new Pieces.AssetsApi().assetsSnapshot({}).then(_assetList => { | ||
for (let i = 0; i < _assetList.iterable.length; i++) { | ||
if (_assetList.iterable[i].id == _id) { | ||
|
||
let _asset = _assetList.iterable[i]; | ||
|
||
_asset.name = _name; | ||
|
||
new Pieces.AssetApi().assetUpdate({asset: _asset}).then(_updated => { | ||
console.log("updated:", _updated); | ||
}) | ||
} | ||
export async function renameAsset(_name: string, _id: String) { | ||
try { | ||
const _assetList = await new Pieces.AssetsApi().assetsSnapshot({}); | ||
for (let i = 0; i < _assetList.iterable.length; i++) { | ||
if (_assetList.iterable[i].id == _id) { | ||
let _asset = _assetList.iterable[i]; | ||
_asset.name = _name; | ||
try { | ||
const _updated = await new Pieces.AssetApi().assetUpdate({ asset: _asset }); | ||
console.log("updated:", _updated); | ||
} catch (error) { | ||
console.error("Error updating asset:", error); | ||
} | ||
} | ||
}) | ||
} | ||
} | ||
} catch (error) { | ||
console.error("Error fetching asset list:", error); | ||
} | ||
} |