UNPKG

tickethead-sdk

Version:

SDK for the Tickethead API

130 lines 6.08 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; 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 = this.generateBoundary(); const body = 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 = this.generateBoundary(); const body = 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 = this.generateBoundary(); const body = 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 = new Blob([uploadImageData.image1x1.content]); formData.append('1x1', blob1x1, uploadImageData.image1x1.filename); const blob4x3 = new Blob([uploadImageData.image4x3.content]); formData.append('4x3', blob4x3, uploadImageData.image4x3.filename); const blob16x9 = new Blob([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, }; }); } /** * Boundary is used to separate fields in multipart, a value that should not appear in the data. * This value is added in the `Content-Type` header. */ generateBoundary() { return `----CustomBoundary${Date.now()}`; } } exports.UploadService = UploadService; /** * Method creates multipart body with provided file. * * @param fileName * @param fileData * @param boundary Parameter used for the multipart boundary. * @returns Multipart body as Uint8Array. */ function WriteMultipartForm(fileName, fileData, boundary, name) { // Header contains metadata for file and the initial boundary const header = new Uint8Array(Buffer.from(`--${boundary}\r\nContent-Disposition: form-data; name="${name}"; filename="${fileName}"\r\nContent-Type: "application/octet-stream"\r\n\r\n`)); const lastBoundary = new Uint8Array(Buffer.from(`\r\n--${boundary}--`)); const finalData = new Uint8Array(header.byteLength + lastBoundary.byteLength + fileData.byteLength); // Put together header, data and last boundary in one array. finalData.set(header, 0); finalData.set(fileData, header.byteLength); finalData.set(lastBoundary, header.byteLength + fileData.byteLength); return finalData; } //# sourceMappingURL=service.js.map