@bitbybit-dev/core
Version:
Bit By Bit Developers Core CAD API to Program Geometry
109 lines (108 loc) • 3.69 kB
JavaScript
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
import { AssetManager } from "../../asset-manager";
export class Asset {
constructor() {
this.assetManager = new AssetManager();
}
/**
* Gets the asset file
* @param inputs file name to get from project assets
* @returns Blob of asset
* @group get
* @shortname cloud file
*/
getFile(inputs) {
return this.assetManager.getAsset(inputs.fileName);
}
/**
* Gets the local asset file stored in your browser.
* @param inputs asset name to get from local assets
* @returns Blob of asset
* @group get
* @shortname local file
*/
getLocalFile(inputs) {
return this.assetManager.getLocalAsset(inputs.fileName);
}
/**
* Fetches the blob from the given url, must be CORS enabled accessible endpoint
* @param inputs url of the asset
* @returns Blob
* @group fetch
* @shortname fetch blob
*/
fetchBlob(inputs) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield fetch(inputs.url);
return res.blob();
});
}
/**
* Fetches the file from the given url, must be CORS enabled accessible endpoint
* @param inputs url of the asset
* @returns File
* @group fetch
* @shortname fetch file
*/
fetchFile(inputs) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield fetch(inputs.url);
const blob = yield res.blob();
return new File([blob], inputs.url.split("/").pop().split("?")[0]);
});
}
/**
* Fetches the json from the given url, must be CORS enabled accessible endpoint
* @param inputs url of the asset
* @returns JSON
* @group fetch
* @shortname fetch json
*/
fetchJSON(inputs) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield fetch(inputs.url);
return res.json();
});
}
/**
* Fetches the json from the given url, must be CORS enabled accessible endpoint
* @param inputs url of the asset
* @returns Text
* @group fetch
* @shortname fetch text
*/
fetchText(inputs) {
return __awaiter(this, void 0, void 0, function* () {
const res = yield fetch(inputs.url);
return res.text();
});
}
/**
* Gets and creates the url string path to your file stored in your memory.
* @param File or a blob
* @returns URL string of a file
* @group create
* @shortname object url
*/
createObjectURL(inputs) {
return URL.createObjectURL(inputs.file);
}
/**
* Gets and creates the url string paths to your files stored in your memory.
* @param Files or a blobs
* @returns URL strings for given files
* @group create
* @shortname object urls
*/
createObjectURLs(inputs) {
return inputs.files.map(f => URL.createObjectURL(f));
}
}