@cognigy/rest-api-client
Version:
Cognigy REST-Client
69 lines • 2.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GenericTusFn = void 0;
const uuid = require("uuid");
/** Node Modules */
const tus_js_client_1 = require("tus-js-client");
const GenericTusFn = (url, _this) => {
return (data, options = {}) => {
var _a;
options.withAuthentication = (_a = options.withAuthentication) !== null && _a !== void 0 ? _a : true;
// create a uuid for the uploadId
const uploadId = uuid.v4();
if (!tus_js_client_1.isSupported) {
alert("This browser does not support uploads. Please use a modern browser instead.");
}
if (!tus_js_client_1.canStoreURLs) {
alert("This Client can not store url. Please use a modern browser instead.");
}
const baseUrl = _this.getBaseUrl();
const upload = new tus_js_client_1.Upload(data.file, {
endpoint: `${baseUrl}${url}`,
retryDelays: [0, 1000, 1000, 1000, 1000],
removeFingerprintOnSuccess: true,
metadata: {
filename: data.file.name,
uploadId,
filetype: "multipart/form-data",
projectId: data.projectId,
organisationid: data.organisationId,
userId: data.userId,
uploadType: data.uploadType
},
onError: data.onError,
onProgress: data.onProgress,
onSuccess: data.onSuccess,
onChunkComplete: data.onChunkComplete,
onShouldRetry: data.onShouldRetry,
onBeforeRequest: async (request) => {
var _a;
request.setHeader("x-cognigy-project-id", data.projectId);
if (options.withAuthentication) {
const authenticationHeaders = await ((_a = _this.authenticationHandler) === null || _a === void 0 ? void 0 : _a.getAuthenticationHeaders());
Object.entries(authenticationHeaders).forEach(([key, value]) => {
request.setHeader(key, value);
});
}
return;
},
onAfterResponse: data.onAfterResponse
});
upload.start();
upload.findPreviousUploads().then((previousUploads) => {
if (previousUploads.length === 0)
return;
const index = 0;
if (!Number.isNaN(index) && previousUploads[index]) {
upload.resumeFromPreviousUpload(previousUploads[index]);
}
upload.start();
});
return {
fileName: upload.file.name,
url: upload.url,
uploadId
};
};
};
exports.GenericTusFn = GenericTusFn;
//# sourceMappingURL=GenericTusFn.js.map