@nexle-soft/quick-desk-client
Version:
Typescript Client for Quick desk's APIs
141 lines • 5.85 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());
});
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
import JsZip from "jszip";
import FileSaver from "file-saver";
import { HttpClient } from "./axios";
import { formatDate } from "../utils/date-time";
const basePath = "/media";
const downloadPath = "/media-download";
export class MediaApi extends HttpClient {
constructor() {
super();
}
downloadFile(url) {
return __awaiter(this, void 0, void 0, function* () {
const fetchUrl = yield fetch(url);
const result = {
name: url.substring(url.indexOf("_") + 1, url.lastIndexOf(".")),
type: url.substring(url.lastIndexOf(".") + 1),
blob: fetchUrl.blob(),
};
return result;
});
}
downloadByGroup(urls) {
return Promise.all(urls.map((url) => this.downloadFile(url)));
}
exportZip(blobs) {
return new Promise((resolve) => {
const zip = JsZip();
for (const blob of blobs) {
zip.file(`${blob.name}.${blob.type}`, blob.blob);
}
resolve(zip.generateAsync({ type: "blob" }).then((zipFile) => {
const fileName = `${formatDate(new Date())}.zip`;
return FileSaver.saveAs(zipFile, fileName);
}));
});
}
downloadAndZip(urls) {
return this.downloadByGroup(urls).then(this.exportZip);
}
// Method to fetch a single media by its ID
getById(mediaId) {
return __awaiter(this, void 0, void 0, function* () {
return this.get({
url: `${basePath}/${mediaId}`,
});
});
}
// User for FE only
download({ mediaIds, fileName, fileUrl, downloadSpecify, mediaUrls, }) {
return __awaiter(this, void 0, void 0, function* () {
// Extract the file extension and normalize to lowercase
const fileExtension = fileName === null || fileName === void 0 ? void 0 : fileName.slice(fileName.lastIndexOf(".") + 1).toLowerCase();
// If mediaUrls is provided, zip and download those files
if (mediaUrls) {
yield this.downloadAndZip(mediaUrls);
}
else {
// Otherwise, fetch and download the specified media by ID
const response = (yield this.get({
url: `${downloadPath}/${mediaIds === null || mediaIds === void 0 ? void 0 : mediaIds.join("_")}`,
config: {
responseType: "blob",
},
}));
if (response) {
const url = window.URL.createObjectURL(response);
const link = document.createElement("a");
link.href = url;
link.setAttribute("download", fileName);
document.body.appendChild(link);
link.click();
// Remove the DOM element after the download
document.body.removeChild(link);
window.URL.revokeObjectURL(url);
}
}
});
}
// Method to create a new media
upload({ files, isPublic = false, context, activityLogContext, }) {
return __awaiter(this, void 0, void 0, function* () {
return yield Promise.allSettled(Object.values(files).map((file) => __awaiter(this, void 0, void 0, function* () {
const formData = new FormData();
formData.append("file", file);
let validUrl = `${basePath}?is_public=${isPublic}`;
if (context) {
validUrl += `&context=${context}`;
}
if (activityLogContext) {
validUrl += `&activity_context=${activityLogContext}`;
}
return yield this.post({
url: validUrl,
data: formData,
config: {
headers: {
"content-type": "multipart/form-data",
},
},
});
})));
});
}
updateMedia(_a) {
var { id } = _a, mediaData = __rest(_a, ["id"]);
return __awaiter(this, void 0, void 0, function* () {
return this.put({
url: `${basePath}/${id}`,
data: mediaData,
});
});
}
deleteTicket(mediaId) {
return __awaiter(this, void 0, void 0, function* () {
return this.delete({
url: `${basePath}/${mediaId}`,
});
});
}
}
//# sourceMappingURL=mediaApi.js.map