@shapediver/sdk.geometry-api-sdk-v2
Version:
SDK to communicate with the Geometry API version 2
201 lines • 10.9 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());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.UtilsApi = void 0;
const common_1 = require("./client/common");
const client_1 = require("./client");
const base_1 = require("./client/base");
const utils_1 = require("./utils");
const error_1 = require("./error");
const apiAssetExportUri = /.+\/session\/.+\/export\/.+/;
const apiAssetOutputUri = /.+\/session\/.+\/output\/.+/;
const apiAssetTextureUri = /.+\/session\/.+\/texture\/.+/;
const cdnAssetUri = /.+\/cdn-asset-(exports|outputs|textures)\/.+/;
const cdnAssetExportUri = /.+\/cdn-asset-exports\/.+/;
const cdnAssetOutputUri = /.+\/cdn-asset-outputs\/.+/;
const cdnAssetTextureUri = /.+\/cdn-asset-textures\/.+/;
const directDownloadUri = /^(http[s]?:\/\/)?(viewer|textures|downloads)\.shapediver\.com(\/.*)?$/;
class UtilsApi extends base_1.BaseAPI {
constructor(configuration, basePath, axios) {
super(configuration, basePath, axios);
}
upload(url, data, contentType, filename, options) {
const reqHeaders = {
Authorization: undefined,
'Content-Type': contentType,
'X-ShapeDiver-Origin': undefined,
'X-ShapeDiver-SessionEngineId': undefined,
'X-ShapeDiver-BuildVersion': undefined,
'X-ShapeDiver-BuildDate': undefined,
'X-ShapeDiver-UserAgent': undefined,
};
if (filename)
reqHeaders['Content-Disposition'] = (0, utils_1.contentDispositionFromFilename)(filename);
const reqOptions = Object.assign({}, options);
reqOptions.headers = Object.assign(Object.assign({}, reqHeaders), options === null || options === void 0 ? void 0 : options.headers);
const request = this.buildRequest('PUT', url, data, reqOptions)();
return request();
}
uploadAsset(url, data, headers, options) {
const reqHeaders = {
Authorization: undefined,
'Content-Type': headers.contentType,
'X-ShapeDiver-Origin': undefined,
'X-ShapeDiver-SessionEngineId': undefined,
'X-ShapeDiver-BuildVersion': undefined,
'X-ShapeDiver-BuildDate': undefined,
'X-ShapeDiver-UserAgent': undefined,
};
if (headers.contentDisposition)
reqHeaders['Content-Disposition'] = headers.contentDisposition;
const reqOptions = Object.assign({}, options);
reqOptions.headers = Object.assign(Object.assign({}, reqHeaders), options === null || options === void 0 ? void 0 : options.headers);
const request = this.buildRequest('PUT', url, data, reqOptions)();
return request();
}
download(url, options) {
const request = this.buildRequest('GET', url, undefined, options)();
return request();
}
downloadAsset(url, options) {
let type;
this.disableAuthHeaderForShapeDiverUris(url, options);
if (apiAssetExportUri.test(url) || cdnAssetExportUri.test(url))
type = 'export';
else if (apiAssetOutputUri.test(url) || cdnAssetOutputUri.test(url))
type = 'output';
else if (apiAssetTextureUri.test(url) || cdnAssetTextureUri.test(url))
type = 'texture';
else {
throw new error_1.IllegalArgumentError(`Cannot fetch asset: Invalid URL '${url}' - Only ShapeDiver asset URLs are allowed.`);
}
return [this.download(url, options), type];
}
downloadImage(sessionId, url, options) {
this.disableAuthHeaderForShapeDiverUris(url, options);
if (apiAssetTextureUri.test(url) ||
cdnAssetTextureUri.test(url) ||
directDownloadUri.test(url)) {
return this.download(url, options);
}
else {
const encodedUrl = typeof window !== 'undefined' && window.btoa
? window.btoa(encodeURIComponent(url).replace(/%([0-9A-F]{2})/g, (_, p1) => String.fromCharCode(parseInt(p1, 16))))
: Buffer.from(url, 'utf-8').toString('base64');
return new client_1.AssetsApi(this.configuration).downloadImage(sessionId, encodedUrl, options);
}
}
submitAndWaitForOutput(sessionId_1, body_1) {
return __awaiter(this, arguments, void 0, function* (sessionId, body, maxWaitMsec = -1, ignoreUnknownParams, options) {
const startMsec = Date.now();
const dto = (yield new client_1.OutputApi(this.configuration).computeOutputs(sessionId, body, ignoreUnknownParams, options)).data;
const waitMsec = Date.now() - startMsec;
maxWaitMsec = maxWaitMsec < 0 ? maxWaitMsec : Math.max(0, maxWaitMsec - waitMsec);
return this.waitForOutputResult(sessionId, dto, maxWaitMsec, options);
});
}
submitAndWaitForExport(sessionId_1, body_1) {
return __awaiter(this, arguments, void 0, function* (sessionId, body, maxWaitMsec = -1, ignoreUnknownParams, options) {
const startMsec = Date.now();
const dto = (yield new client_1.ExportApi(this.configuration).computeExports(sessionId, body, ignoreUnknownParams, options)).data;
const waitMsec = Date.now() - startMsec;
maxWaitMsec = maxWaitMsec < 0 ? maxWaitMsec : Math.max(0, maxWaitMsec - waitMsec);
return this.waitForExportResult(sessionId, body, dto, maxWaitMsec, options);
});
}
waitForOutputResult(sessionId, dto, maxWaitMsec, options) {
return __awaiter(this, void 0, void 0, function* () {
if (!dto.outputs)
return dto;
const outputVersions = {};
Object.keys(dto.outputs).forEach((id) => (outputVersions[id] = dto.outputs[id].version));
let delay = this.getMaxOutputDelay(dto);
const startMsec = Date.now();
while (delay > 0) {
if (maxWaitMsec >= 0) {
const waitMsec = Date.now() - startMsec;
if (waitMsec >= maxWaitMsec) {
throw new error_1.TimeoutError(`Maximum wait time of ${maxWaitMsec} ms reached`);
}
if (waitMsec + delay > maxWaitMsec) {
delay = maxWaitMsec - waitMsec;
}
}
yield (0, utils_1.sleep)(delay);
dto = (yield new client_1.OutputApi(this.configuration).getCachedOutputs(sessionId, outputVersions, options)).data;
delay = this.getMaxOutputDelay(dto);
}
return dto;
});
}
waitForExportResult(sessionId, body, dto, maxWaitMsec, options) {
return __awaiter(this, void 0, void 0, function* () {
let delay = this.getMaxExportDelay(body, dto);
const startMsec = Date.now();
while (delay > 0) {
if (maxWaitMsec >= 0) {
const waitMsec = Date.now() - startMsec;
if (waitMsec >= maxWaitMsec) {
throw new error_1.TimeoutError(`Maximum wait time of ${maxWaitMsec} ms reached`);
}
if (waitMsec + delay > maxWaitMsec) {
delay = maxWaitMsec - waitMsec;
}
}
yield (0, utils_1.sleep)(delay);
dto = (yield new client_1.ExportApi(this.configuration).getCachedExports(sessionId, body, options)).data;
delay = this.getMaxExportDelay(body, dto);
}
return dto;
});
}
getMaxOutputDelay(dto) {
var _a;
return Math.max(...Object.values((_a = dto.outputs) !== null && _a !== void 0 ? _a : {}).map((output) => { var _a; return (_a = output.delay) !== null && _a !== void 0 ? _a : -1; }), -1);
}
getMaxExportDelay(body, dto) {
var _a, _b, _c, _d;
const exports = (_a = body.exports) !== null && _a !== void 0 ? _a : [];
const outputs = (_b = body.outputs) !== null && _b !== void 0 ? _b : [];
return Math.max(...Object.values((_c = dto.exports) !== null && _c !== void 0 ? _c : {})
.filter((e) => exports.includes(e.id))
.map((e) => { var _a; return (_a = e.delay) !== null && _a !== void 0 ? _a : -1; }), ...Object.values((_d = dto.outputs) !== null && _d !== void 0 ? _d : {})
.filter((o) => outputs.includes(o.id))
.map((o) => { var _a; return (_a = o.delay) !== null && _a !== void 0 ? _a : -1; }), -1);
}
buildRequest(method, url, data, options = {}) {
return (basePath = '') => {
var _a, _b, _c;
const baseOptions = (_b = (_a = this.configuration) === null || _a === void 0 ? void 0 : _a.baseOptions) !== null && _b !== void 0 ? _b : {};
const baseHeaders = baseOptions && baseOptions.headers ? baseOptions.headers : {};
const reqOptions = Object.assign(Object.assign({ method }, baseOptions), options);
reqOptions.headers = Object.assign(Object.assign({}, baseHeaders), options.headers);
if (!reqOptions.data && data) {
const configuration = (_c = this.configuration) !== null && _c !== void 0 ? _c : new client_1.Configuration();
reqOptions.data = (0, common_1.serializeDataIfNeeded)(data, reqOptions, configuration);
}
const configuration = this.configuration && url.startsWith('http')
? new client_1.Configuration(Object.assign(Object.assign({}, this.configuration), { basePath: undefined }))
: this.configuration;
const axiosArgs = { url, options: reqOptions };
return (0, common_1.createRequestFunction)(axiosArgs, this.axios, basePath, configuration).bind(this.axios);
};
}
disableAuthHeaderForShapeDiverUris(url, options) {
var _a;
options = Object.assign({}, options);
if (!((_a = options.headers) === null || _a === void 0 ? void 0 : _a.Authorization) &&
(cdnAssetUri.test(url) || directDownloadUri.test(url)))
options.headers = Object.assign({ Authorization: undefined }, options.headers);
}
}
exports.UtilsApi = UtilsApi;
//# sourceMappingURL=UtilsApi.js.map