UNPKG

@twilio/mcs-client

Version:

Twilio Media Content Service client library

214 lines (205 loc) 9.32 kB
/* @license Copyright (c) 2018, Twilio, Inc. Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ 'use strict'; var global = typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}; Object.defineProperty(exports, '__esModule', { value: true }); var tslib_es6 = require('./node_modules/tslib/tslib.es6.js'); var logger = require('./logger.js'); var configuration = require('./configuration.js'); var media = require('./media.js'); var transport = require('./services/transport.js'); var network = require('./services/network.js'); var _package = require('./packages/mcs-client/package.json.js'); var declarativeTypeValidator = require('@twilio/declarative-type-validator'); var cancellablePromise = require('./cancellable-promise.js'); const log = logger.Logger.scope(""); /** * @classdesc A Client provides an interface for Media Content Service */ exports.Client = class Client { /** * Base URLs must be full URLs with host. If host is not provided it will be generated from a default configuration * template using options.region. * * @param {String} token - Access token * @param {String} baseUrl - Base URL for Media Content Service Media resource, i.e. /v1/Services/{serviceSid}/Media * @param {String} baseSetUrl - Base URL for Media Content Service MediaSet resource, i.e. /v1/Services/{serviceSid}/MediaSet * @param {Client#ClientOptions} [options] - Options to customize the Client */ constructor(token, baseUrl, baseSetUrl, options = {}) { var _a, _b; this.options = options; this.options.logLevel = (_a = this.options.logLevel) !== null && _a !== void 0 ? _a : "silent"; this.config = new configuration.Configuration(token, baseUrl, baseSetUrl, this.options); log.setLevel(this.options.logLevel); this.options.transport = (_b = this.options.transport) !== null && _b !== void 0 ? _b : new transport.Transport(); this.transport = this.options.transport; this.network = new network.Network(this.config, this.transport); } /** * These options can be passed to Client constructor * @typedef {Object} Client#ClientOptions * @property {String} [logLevel='silent'] - The level of logging to enable. Valid options * (from strictest to broadest): ['silent', 'error', 'warn', 'info', 'debug', 'trace'] */ /** * Update the token used for Client operations * @param {String} token - The JWT string of the new token * @returns {void} */ updateToken(token) { log.info("updateToken"); this.config.updateToken(token); } /** * Gets media from media service * @param {String} sid - Media's SID */ get(sid) { return new cancellablePromise.CancellablePromise(async (resolve, reject, onCancel) => { const request = this.network.get(`${this.config.mediaUrl}/${sid}`); onCancel(() => request.cancel()); try { const response = await request; resolve(new media.Media(this.config, this.network, response.body)); } catch (e) { reject(e); } }); } /** * Posts raw content to media service * @param {String} contentType - content type of media * @param {String|Buffer|Blob} media - content to post * @param {MediaCategory|null} category - category for the media */ post(contentType, media$1, category, filename) { return new cancellablePromise.CancellablePromise(async (resolve, reject, onCancel) => { const request = this.network.post(this.config.mediaUrl, category !== null && category !== void 0 ? category : "media", media$1, contentType, filename); onCancel(() => request.cancel()); try { const response = await request; resolve(new media.Media(this.config, this.network, response.body)); } catch (e) { reject(e); } }); } /** * Posts FormData to media service. Can be used only with browser engine's FormData. * In non-browser FormData case the method will do promise reject with * new TypeError("Posting FormData supported only with browser engine's FormData") * @param {FormData} formData - form data to post * @param {MediaCategory|null} category - category for the media */ postFormData(formData, category) { return new cancellablePromise.CancellablePromise(async (resolve, reject, onCancel) => { const request = this.network.post(this.config.mediaUrl, category !== null && category !== void 0 ? category : "media", formData); onCancel(() => request.cancel()); try { const response = await request; resolve(new media.Media(this.config, this.network, response.body)); } catch (e) { reject(e); } }); } /** * Retrieve information about multiple media SIDs at the same time. * @param mediaSids Array of Media SIDs to get information from. */ mediaSetGet(mediaSids) { return new cancellablePromise.CancellablePromise(async (resolve, reject, onCancel) => { const query = { command: "get", list: mediaSids.map((sid) => ({ media_sid: sid })), }; const request = this.network.post(`${this.config.mediaSetUrl}`, null, query, "application/json"); onCancel(() => request.cancel()); try { const response = await request; const media$1 = response.body.map((item) => { if (item.code !== 200) { reject(`Failed to obtain detailed information about Media items (failed SID ${item.media_record.sid})`); return; } return new media.Media(this.config, this.network, item.media_record); }); resolve(media$1); } catch (e) { reject(e); } }); } /** * Retrieve temporary URLs for a set of media SIDs. * @param mediaSids array of the media SIDs to get URLs from. */ mediaSetGetContentUrls(mediaSids) { return new cancellablePromise.CancellablePromise(async (resolve, reject, onCancel) => { const query = { command: "get", list: mediaSids.map((sid) => ({ media_sid: sid })), }; const request = this.network.post(`${this.config.mediaSetUrl}`, null, query, "application/json"); onCancel(() => request.cancel()); try { const response = await request; const urls = new Map(); response.body.forEach((item) => { if (item.code !== 200) { reject(`Failed to obtain detailed information about Media items (failed SID ${item.media_record.sid})`); return; } urls.set(item.media_record.sid, item.media_record.links.content_direct_temporary); }); resolve(urls); } catch (e) { reject(e); } }); } }; exports.Client.version = _package.version; tslib_es6.__decorate([ declarativeTypeValidator.validateTypes(declarativeTypeValidator.nonEmptyString), tslib_es6.__metadata("design:type", Function), tslib_es6.__metadata("design:paramtypes", [String]), tslib_es6.__metadata("design:returntype", void 0) ], exports.Client.prototype, "updateToken", null); tslib_es6.__decorate([ declarativeTypeValidator.validateTypesAsync(declarativeTypeValidator.nonEmptyString), tslib_es6.__metadata("design:type", Function), tslib_es6.__metadata("design:paramtypes", [String]), tslib_es6.__metadata("design:returntype", cancellablePromise.CancellablePromise) ], exports.Client.prototype, "get", null); exports.Client = tslib_es6.__decorate([ declarativeTypeValidator.validateConstructorTypes(declarativeTypeValidator.nonEmptyString, declarativeTypeValidator.nonEmptyString, [declarativeTypeValidator.nonEmptyString, declarativeTypeValidator.literal(null)], [declarativeTypeValidator.pureObject, "undefined"]), tslib_es6.__metadata("design:paramtypes", [String, String, Object, Object]) ], exports.Client); exports.Media = media.Media; //# sourceMappingURL=client.js.map