@cognigy/rest-api-client
Version:
Cognigy REST-Client
66 lines • 2.72 kB
JavaScript
import { __awaiter } from "tslib";
import * as uuid from "uuid";
/** Node Modules */
import { Upload, isSupported, canStoreURLs } from "tus-js-client";
export 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 (!isSupported) {
alert("This browser does not support uploads. Please use a modern browser instead.");
}
if (!canStoreURLs) {
alert("This Client can not store url. Please use a modern browser instead.");
}
const baseUrl = _this.getBaseUrl();
const upload = new 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: (request) => __awaiter(void 0, void 0, void 0, function* () {
var _b;
request.setHeader("x-cognigy-project-id", data.projectId);
if (options.withAuthentication) {
const authenticationHeaders = yield ((_b = _this.authenticationHandler) === null || _b === void 0 ? void 0 : _b.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
};
};
};
//# sourceMappingURL=GenericTusFn.js.map