UNPKG

@stackend/api

Version:

JS bindings to api.stackend.com

552 lines (547 loc) 24.6 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; 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 __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.constructImageThumbnail = exports.searchUses = exports.embed = exports.get = exports.remove = exports.list = exports.listMy = exports.upload = exports.uploadMediaFile = exports.getContextMediaUploadUrl = exports.getMediaUploadUrl = exports.getAbsoluteContextPrefix = exports.getContextPrefix = exports.getThumbnailUrl = exports.Orientation = exports.ThumbnailSize = exports.MediaListOrder = exports.MediaTypeName = exports.MediaTypeId = exports.MediaType = exports.MediaStatus = void 0; var Stackend = __importStar(require("../stackend")); var LoadJson_1 = require("../api/LoadJson"); var api_1 = require("../api"); var get_1 = __importDefault(require("lodash/get")); var AccessToken_1 = require("../api/AccessToken"); /** * Image */ var MediaStatus; (function (MediaStatus) { MediaStatus["OK"] = "OK"; MediaStatus["NOT_OK"] = "NOT_OK"; MediaStatus["NOT_PROCESSED"] = "NOT_PROCESSED"; MediaStatus["TEMPORARY"] = "TEMPORARY"; })(MediaStatus = exports.MediaStatus || (exports.MediaStatus = {})); /** * Media types */ var MediaType; (function (MediaType) { /** * Image files. Will be presented as an <img>-element */ MediaType["IMAGE"] = "IMAGE"; /** * Video files. Will be presented as a <video>-element */ MediaType["VIDEO"] = "VIDEO"; /** * Audio files. Will be presented as an <audio>-element. */ MediaType["AUDIO"] = "AUDIO"; /** * Other types of files that may be stored and will be presented as a link when inserted in text. */ MediaType["DOCUMENT"] = "DOCUMENT"; })(MediaType = exports.MediaType || (exports.MediaType = {})); /** * Media type ids */ var MediaTypeId; (function (MediaTypeId) { MediaTypeId[MediaTypeId["IMAGE"] = 1] = "IMAGE"; MediaTypeId[MediaTypeId["VIDEO"] = 2] = "VIDEO"; MediaTypeId[MediaTypeId["AUDIO"] = 3] = "AUDIO"; MediaTypeId[MediaTypeId["DOCUMENT"] = 4] = "DOCUMENT"; })(MediaTypeId = exports.MediaTypeId || (exports.MediaTypeId = {})); /** * Media type names */ exports.MediaTypeName = (_a = {}, _a[MediaTypeId.IMAGE] = 'IMAGE', _a[MediaTypeId.VIDEO] = 'VIDEO', _a[MediaTypeId.AUDIO] = 'AUDIO', _a[MediaTypeId.DOCUMENT] = 'DOCUMENT', _a); /** * Media list ordering */ var MediaListOrder; (function (MediaListOrder) { MediaListOrder[MediaListOrder["LATEST_UPDATED_DESC"] = 1] = "LATEST_UPDATED_DESC"; MediaListOrder[MediaListOrder["LATEST_UPDATED_ASC"] = 2] = "LATEST_UPDATED_ASC"; MediaListOrder[MediaListOrder["CREATED_DESC"] = 3] = "CREATED_DESC"; MediaListOrder[MediaListOrder["CREATED_ASC"] = 4] = "CREATED_ASC"; MediaListOrder[MediaListOrder["CATEGORY_DESC"] = 5] = "CATEGORY_DESC"; MediaListOrder[MediaListOrder["CATEGORY_ASC"] = 6] = "CATEGORY_ASC"; MediaListOrder[MediaListOrder["TITLE_DESC"] = 7] = "TITLE_DESC"; MediaListOrder[MediaListOrder["TITLE_ASC"] = 8] = "TITLE_ASC"; MediaListOrder[MediaListOrder["USER_DESC"] = 9] = "USER_DESC"; MediaListOrder[MediaListOrder["USER_ASC"] = 10] = "USER_ASC"; MediaListOrder[MediaListOrder["RANDOM"] = 11] = "RANDOM"; })(MediaListOrder = exports.MediaListOrder || (exports.MediaListOrder = {})); /** * Standard ImageThumbnailConfig names * @type {{TINY: string, SMALL: string, MEDIUM: string, LARGE: string}} */ exports.ThumbnailSize = { // Guaranteed to be available TINY: 'tiny', SMALL: 'small', MEDIUM: 'medium', LARGE: 'large' }; /** * Orientation: Landscape or portrait? */ var Orientation; (function (Orientation) { Orientation["LANDSCAPE"] = "LANDSCAPE"; Orientation["PORTRAIT"] = "PORTRAIT"; })(Orientation = exports.Orientation || (exports.Orientation = {})); /** * Given an url, get an url to a thumbnail of the desired size. * Effectively inserting the size into the url. * @param url * @param size */ function getThumbnailUrl(_a) { var url = _a.url, _b = _a.size, size = _b === void 0 ? exports.ThumbnailSize.MEDIUM : _b; if (typeof url === 'undefined' || url === null) { return null; } var i = url.lastIndexOf('/'); var p = url.substring(0, i); var s = url.substring(i); return p + '/' + size + s; } exports.getThumbnailUrl = getThumbnailUrl; /** * Get the context prefix * @param config * @param communityPermalink */ function getContextPrefix(_a) { var config = _a.config, communityPermalink = _a.communityPermalink; var cp = ''; if (typeof communityPermalink === 'undefined') { cp = (0, api_1._getContextPath)(config); } else { if (Stackend.STACKEND_COMMUNITY === communityPermalink) { cp = (0, api_1._getContextPath)(config); } else { cp = (0, api_1._getContextPath)(config) + '/' + communityPermalink; } } return cp; } exports.getContextPrefix = getContextPrefix; /** * Get the absolute context prefix, including server. * @param config * @param communityPermalink * @param context */ function getAbsoluteContextPrefix(_a) { var config = _a.config, communityPermalink = _a.communityPermalink, context = _a.context; // Allows this to be overridden var server = (0, api_1._getConfig)({ config: config, key: 'media-upload-server', componentName: 'media', context: context, defaultValue: (0, api_1._getServer)(config) }); var cp = getContextPrefix({ config: config, communityPermalink: communityPermalink }); return server + cp; } exports.getAbsoluteContextPrefix = getAbsoluteContextPrefix; /** * Get the url used to upload a media file. * @param config API config * @param referenceId {number} Id of another object referencing this media object. * @param communityPermalink {string} Community permalink (optional, defaults to current community) * @param context {string} Community context * @param temporary {Boolean} Mark the media as temporary? Temporary images are subject to automatic removal. * @param thumbnail {String} Optional thumbnail configuration name. Determines the size of the returned image * @param maxWidth {number} Optional max width. Typically detected from the width of a container element. * @param maxHeight {number} Optional max height. Typically detected from the height of a container element. */ function getMediaUploadUrl(_a) { var config = _a.config, referenceId = _a.referenceId, communityPermalink = _a.communityPermalink, context = _a.context, _b = _a.temporary, temporary = _b === void 0 ? false : _b, thumbnail = _a.thumbnail, maxWidth = _a.maxWidth, maxHeight = _a.maxHeight; var cp = getAbsoluteContextPrefix({ config: config, communityPermalink: communityPermalink, context: context }); var p = cp + '/media/upload?context=' + context + '&temporary=' + (typeof temporary === 'undefined' ? 'false' : String(temporary)); if (typeof thumbnail !== 'undefined') { p += '&thumbnail=' + thumbnail; } if (typeof referenceId !== 'undefined') { p += '&referenceId=' + referenceId; } if (typeof maxWidth !== 'undefined') { p += '&maxWidth=' + maxWidth; } if (typeof maxHeight !== 'undefined') { p += '&maxHeight=' + maxHeight; } return p; } exports.getMediaUploadUrl = getMediaUploadUrl; /** * Get the url used to upload a media file. * @param referenceId {number} Id of another object referencing this media object. * @param config API config * @param communityPermalink {string} Community permalink (optional, defaults to current community) * @param context {string} Community context * @param temporary {Boolean} Mark the media as temporary? Temporary images are subject to automatic removal. * @param thumbnail {String} Optional thumbnail configuration name. Determines the size of the returned image * @param maxWidth {number} Optional max width. Typically detected from the width of a container element. * @param maxHeight {number} Optional max height. Typically detected from the height of a container element. * @param responsive {Boolean} Optional responsive. Sets widht:100%; max-width: <PICTURE WIDTH>px; height: auto */ function getContextMediaUploadUrl(_a) { var config = _a.config, _b = _a.referenceId, referenceId = _b === void 0 ? undefined : _b, _c = _a.communityPermalink, communityPermalink = _c === void 0 ? undefined : _c, _d = _a.context, context = _d === void 0 ? undefined : _d, _e = _a.temporary, temporary = _e === void 0 ? false : _e, _f = _a.thumbnail, thumbnail = _f === void 0 ? undefined : _f, _g = _a.maxWidth, maxWidth = _g === void 0 ? undefined : _g, _h = _a.maxHeight, maxHeight = _h === void 0 ? undefined : _h, _j = _a.responsive, responsive = _j === void 0 ? undefined : _j; var cp = getAbsoluteContextPrefix({ config: config, communityPermalink: communityPermalink, context: context }); var p = cp + '/media/upload'; p = (0, AccessToken_1.appendAccessToken)(p); var q = (0, LoadJson_1.urlEncodeParameters)({ context: context, temporary: typeof temporary === 'undefined' ? 'false' : String(temporary), thumbnail: thumbnail, referenceId: referenceId, maxWidth: maxWidth, maxHeight: maxHeight, responsive: typeof responsive !== 'undefined' }); /* p += '?context=' + context + '&temporary=' + (typeof temporary === 'undefined' ? 'false' : String(temporary)); if (typeof thumbnail !== 'undefined') { p += '&thumbnail=' + thumbnail; } if (typeof referenceId !== 'undefined') { p += '&referenceId=' + referenceId; } if (typeof maxWidth !== 'undefined') { p += '&maxWidth=' + maxWidth; } if (typeof maxHeight !== 'undefined') { p += '&maxHeight=' + maxHeight; } if (typeof responsive !== 'undefined') { p += '&responsive=true'; } return p; */ return (0, LoadJson_1.appendQueryString)(p, q); } exports.getContextMediaUploadUrl = getContextMediaUploadUrl; /** * Upload a media file. Browser only. * * @param config API config * @param file {File} file object * @param communityPermalink {string} Community permalink (optional, defaults to current community) * @param context {string} Community context * @param referenceId {number} Id of another object referencing this media object. * @param temporary {Boolean} Mark the media as temporary? Temporary images are subject to automatic removal. * @param thumbnail {String} Optional thumbnail configuration name. Determines the size of the returned image * @param maxWidth {number} Optional max width. Typically detected from the width of a container element. * @param maxHeight {number} Optional max height. Typically detected from the height of a container element. * @param responsive {Boolean} Optional responsive. Sets widht:100%; max-width: <PICTURE WIDTH>px; height: auto * @return {Promise} */ function uploadMediaFile(_a) { var config = _a.config, file = _a.file, _b = _a.communityPermalink, communityPermalink = _b === void 0 ? undefined : _b, _c = _a.context, context = _c === void 0 ? undefined : _c, _d = _a.referenceId, referenceId = _d === void 0 ? undefined : _d, _e = _a.temporary, temporary = _e === void 0 ? false : _e, thumbnail = _a.thumbnail, maxWidth = _a.maxWidth, maxHeight = _a.maxHeight, _f = _a.responsive, responsive = _f === void 0 ? undefined : _f; return __awaiter(this, arguments, void 0, function () { var url, data, r; return __generator(this, function (_g) { switch (_g.label) { case 0: // Allows the use of COMMUNITY_PARAMETER as well if (typeof communityPermalink === 'undefined') { // FIXME: This needs to be fixed // @ts-ignore communityPermalink = arguments[api_1.COMMUNITY_PARAMETER]; } url = getContextMediaUploadUrl({ config: config, communityPermalink: communityPermalink, context: context, referenceId: referenceId, temporary: temporary, thumbnail: thumbnail, maxWidth: maxWidth, maxHeight: maxHeight, responsive: responsive }); data = new FormData(); data.append('file', file); return [4 /*yield*/, (0, LoadJson_1.LoadJson)({ url: url, method: 'POST', parameters: {}, body: data })]; case 1: r = _g.sent(); // FIXME: Handle cookies // This json format includes it's own error messages. if (r.error) { return [2 /*return*/, { error: r.error, files: [], html: {}, thumbnails: {} }]; } return [2 /*return*/, r.json]; } }); }); } exports.uploadMediaFile = uploadMediaFile; /** * Upload a media file. Browser only. * * @param config API config * @param file {File} file object * @param communityPermalink {string} Community permalink (optional, defaults to current community) * @param context {string} Community context * @param referenceId {number} Id of another object referencing this media object. * @param temporary {Boolean} Mark the media as temporary? Temporary images are subject to automatic removal. * @param thumbnail {String} Optional thumbnail configuration name. Determines the size of the returned image * @param maxWidth {number} Optional max width. Typically detected from the width of a container element. * @param maxHeight {number} Optional max height. Typically detected from the height of a container element. * @param responsive {Boolean} Optional responsive. Sets widht:100%; max-width: <PICTURE WIDTH>px; height: auto * @return {Promise} */ function upload(_a) { var file = _a.file, _b = _a.context, context = _b === void 0 ? undefined : _b, _c = _a.referenceId, referenceId = _c === void 0 ? undefined : _c, _d = _a.temporary, temporary = _d === void 0 ? false : _d, thumbnail = _a.thumbnail, maxWidth = _a.maxWidth, maxHeight = _a.maxHeight, _e = _a.responsive, responsive = _e === void 0 ? undefined : _e, _f = _a.communityPermalink, communityPermalink = _f === void 0 ? undefined : _f; var cpl = communityPermalink; if (typeof cpl === 'undefined') { // @ts-ignore cpl = arguments[api_1.COMMUNITY_PARAMETER]; } return function (dispatch, getState) { var _a = getState(), config = _a.config, communities = _a.communities; if (!cpl) { cpl = (0, get_1.default)(communities, 'community.permalink'); } return uploadMediaFile({ config: config, file: file, communityPermalink: cpl, context: context, referenceId: referenceId, temporary: temporary, thumbnail: thumbnail, maxWidth: maxWidth, maxHeight: maxHeight, responsive: responsive }); }; } exports.upload = upload; /** * List my media files. * * @param context {string} Context (excluding community) * @param thumbnailConfigName {string} Thumbnail config name * @param mediaType {MediaTypeId} Media type id (optional) * @param referenceId {number} Reference id (optional) * @param categoryId {number} Category id (optional) * @param order {MediaListOrder} Sort order (optional) * @param p Page number (optional) * @param pageSize Page size (optional) */ function listMy(_a) { var context = _a.context, _b = _a.thumbnailConfigName, thumbnailConfigName = _b === void 0 ? 'medium' : _b, mediaType = _a.mediaType, referenceId = _a.referenceId, categoryId = _a.categoryId, _c = _a.order, order = _c === void 0 ? MediaListOrder.CREATED_DESC : _c, pageSize = _a.pageSize, _d = _a.p, p = _d === void 0 ? 1 : _d; return (0, api_1.getJson)({ url: '/media/list/my', parameters: arguments }); } exports.listMy = listMy; /** * List media files. * * @param context {string} Context (excluding community) * @param thumbnailConfigName {string} Thumbnail config name * @param mediaType {MediaTypeId} Media type id (optional) * @param referenceId {number} Reference id (optional) * @param categoryId {number} Category id (optional) * @param order {MediaListOrder} Sort order (optional) * @param p Page number (optional) * @param pageSize Page size (optional) */ function list(_a) { var context = _a.context, _b = _a.thumbnailConfigName, thumbnailConfigName = _b === void 0 ? 'medium' : _b, mediaType = _a.mediaType, referenceId = _a.referenceId, categoryId = _a.categoryId, _c = _a.order, order = _c === void 0 ? MediaListOrder.CREATED_DESC : _c, pageSize = _a.pageSize, _d = _a.p, p = _d === void 0 ? 1 : _d; return (0, api_1.getJson)({ url: '/media/list', parameters: arguments }); } exports.list = list; /** * Remove a media object (by setting modstatus). * * @param communityPermalink {string} Community permalink (optional, defaults to current community) * @param context {string} Context (excluding community) * @param id {number} Media id */ function remove(_a) { var communityPermalink = _a.communityPermalink, context = _a.context, id = _a.id; /* let url = Media.getAbsoluteContextPrefix({communityPermalink, context}) + "/media/remove"; console.log("url", url); let result = LoadJson(url, 'POST', { id, context }, null, null); return result; */ !!arguments[0].communityPermalink && delete arguments[0].communityPermalink; return (0, api_1.post)({ url: '/media/remove', parameters: arguments, community: communityPermalink }); } exports.remove = remove; /** * Get a media file. * * @param context {string} Context (excluding community) * @param thumbnailConfigName {string} Thumbnail config name (optional) * @param id {number} id (optional) * @param permalink {string} permalink (optional) * @param responsive {boolean} Generate responsive html * @return {Thunk<*>} */ function get(_a) { var context = _a.context, _b = _a.thumbnailConfigName, thumbnailConfigName = _b === void 0 ? 'medium' : _b, id = _a.id, permalink = _a.permalink, responsive = _a.responsive; return (0, api_1.getJson)({ url: '/media/get', parameters: arguments }); } exports.get = get; /** * Validate a html fragment, a link, iframe, video for embedding in rich content. * * Url - Create some sample data from the site. * Video url - the corresponding embed code is created (youtube, vimeo). * Iframe/img tags - The src is checked against the whitelist. * * @param context {string} Context (excluding community) * @param embedCode * @param thumbnailConfigName {string} Thumbnail config name (optional) * @param maxWidth (optional) * @param responsive Use responsive layout: 100% width, or smaller if smaller. * @param communityPermalink {string} Community permalink (optional, defaults to current community) */ function embed(_a) { var context = _a.context, embedCode = _a.embedCode, thumbnailConfigName = _a.thumbnailConfigName, maxWidth = _a.maxWidth, responsive = _a.responsive, communityPermalink = _a.communityPermalink; !!arguments[0].communityPermalink && delete arguments[0].communityPermalink; return (0, api_1.getJson)({ url: '/media/embed', parameters: arguments, community: communityPermalink }); } exports.embed = embed; /** * Search for media uses * * @param mediaId * @param context */ function searchUses(_a) { var context = _a.context, mediaId = _a.mediaId; return (0, api_1.getJson)({ url: '/media/search-uses', parameters: arguments }); } exports.searchUses = searchUses; /** * Construct a fake image thumbnail * @param image * @returns ImageThumbnail */ function constructImageThumbnail(image, thumbnailConfig) { if (!image) { return null; } return { id: -1, url: image.url, bytes: image.bytes, mediaId: image.id, createdDate: image.createdDate, height: image.height, width: image.width, mediaType: MediaType.IMAGE, config: { create: false, gravity: 'CENTER', height: image.height, method: 0, paddingColor: 'pink', type: 'DEFAULT', width: image.width } }; } exports.constructImageThumbnail = constructImageThumbnail; //# sourceMappingURL=index.js.map