UNPKG

tickethead-sdk

Version:

SDK for the Tickethead API

108 lines 5.09 kB
"use strict"; 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.UploadService = void 0; const upload_1 = require("../utils/upload"); class UploadService { constructor(client, version) { this.client = client; this.version = version; } /** * Returns true if the service is reachable * * @returns Services' online status */ health() { return __awaiter(this, void 0, void 0, function* () { try { const res = yield this.client.get(`upload/health`); if (res.data.status === 'ok') { return { online: true }; } } catch (e) { // Do nothing } return { online: false }; }); } uploadProfileImage(fileData) { return __awaiter(this, void 0, void 0, function* () { const boundary = (0, upload_1.generateBoundary)(); const body = (0, upload_1.writeMultipartForm)(fileData.filename, fileData.content, boundary, 'profile'); const response = yield this.client.post(`${this.client.defaults.baseURL}/upload/${this.version}/profile`, body, { headers: { 'Content-Type': `multipart/form-data; boundary=${boundary}`, }, }); return { url: response.data.data[0].url }; }); } uploadImage(fileData) { return __awaiter(this, void 0, void 0, function* () { const boundary = (0, upload_1.generateBoundary)(); const body = (0, upload_1.writeMultipartForm)(fileData.filename, fileData.content, boundary, 'file'); const response = yield this.client.post(`${this.client.defaults.baseURL}/upload/${this.version}/send`, body, { headers: { 'Content-Type': `multipart/form-data; boundary=${boundary}`, }, }); return { url: response.data.url }; }); } uploadApiVideo(fileData) { return __awaiter(this, void 0, void 0, function* () { const boundary = (0, upload_1.generateBoundary)(); const body = (0, upload_1.writeMultipartForm)(fileData.filename, fileData.content, boundary, 'file'); const response = yield this.client.post(`${this.client.defaults.baseURL}/upload/${this.version}/video/api-video`, body, { headers: { 'Content-Type': `multipart/form-data; boundary=${boundary}`, }, }); return { url: response.data.data[0].url }; }); } /** * Upload the event images with different formats: 1x1, 4x3, and 16x9 * @param uploadImageData.image1x1 Content and filename of the 1x1 event image * @param uploadImageData.image4x3 Content and filename of the 4x3 event image * @param uploadImageData.image16x9 Content and filename of the 16x9 event image * @returns Urls of the 1x1, 4x3, and 16x9 images */ uploadEventImage(uploadImageData) { return __awaiter(this, void 0, void 0, function* () { const formData = new FormData(); const blob1x1 = this.bufferToBlob(uploadImageData.image1x1.content); formData.append('1x1', blob1x1, uploadImageData.image1x1.filename); const blob4x3 = this.bufferToBlob(uploadImageData.image4x3.content); formData.append('4x3', blob4x3, uploadImageData.image4x3.filename); const blob16x9 = this.bufferToBlob(uploadImageData.image16x9.content); formData.append('16x9', blob16x9, uploadImageData.image16x9.filename); const response = yield this.client.post(`${this.client.defaults.baseURL}/upload/${this.version}/image`, formData, { headers: { 'Content-Type': `multipart/form-data;`, }, }); const data = response.data.data; return { file1x1Url: data.find((item) => item.format === '1x1').url, file4x3Url: data.find((item) => item.format === '4x3').url, file16x9Url: data.find((item) => item.format === '16x9').url, }; }); } bufferToBlob(buffer) { return new Blob([new Uint8Array(buffer)]); } } exports.UploadService = UploadService; //# sourceMappingURL=service.js.map