UNPKG

uppy

Version:

Extensible JavaScript file upload widget with support for drag&drop, resumable uploads, previews, restrictions, file processing/encoding, remote providers like Instagram, Dropbox, Google Drive, S3 and more :dog:

106 lines (88 loc) 3.88 kB
'use strict'; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } require('whatwg-fetch'); var _getName = function _getName(id) { return id.split('-').map(function (s) { return s.charAt(0).toUpperCase() + s.slice(1); }).join(' '); }; module.exports = function () { function Provider(uppy, opts) { _classCallCheck(this, Provider); this.uppy = uppy; this.opts = opts; this.provider = opts.provider; this.id = this.provider; this.authProvider = opts.authProvider || this.provider; this.name = this.opts.name || _getName(this.id); this.onReceiveResponse = this.onReceiveResponse.bind(this); } Provider.prototype.onReceiveResponse = function onReceiveResponse(response) { var uppyServer = this.uppy.state.uppyServer || {}; var host = this.opts.host; var headers = response.headers; // Store the self-identified domain name for the uppy-server we just hit. if (headers.has('i-am') && headers.get('i-am') !== uppyServer[host]) { var _extends2; this.uppy.setState({ uppyServer: _extends({}, uppyServer, (_extends2 = {}, _extends2[host] = headers.get('i-am'), _extends2)) }); } return response; }; Provider.prototype.checkAuth = function checkAuth() { return fetch(this.hostname + '/' + this.id + '/authorized', { method: 'get', credentials: 'include', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' } }).then(this.onReceiveResponse).then(function (res) { return res.json().then(function (payload) { return payload.authenticated; }); }); }; Provider.prototype.authUrl = function authUrl() { return this.hostname + '/' + this.id + '/connect'; }; Provider.prototype.fileUrl = function fileUrl(id) { return this.hostname + '/' + this.id + '/get/' + id; }; Provider.prototype.list = function list(directory) { return fetch(this.hostname + '/' + this.id + '/list/' + (directory || ''), { method: 'get', credentials: 'include', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' } }).then(this.onReceiveResponse).then(function (res) { return res.json(); }); }; Provider.prototype.logout = function logout() { var redirect = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : location.href; return fetch(this.hostname + '/' + this.id + '/logout?redirect=' + redirect, { method: 'get', credentials: 'include', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' } }); }; _createClass(Provider, [{ key: 'hostname', get: function get() { var uppyServer = this.uppy.state.uppyServer || {}; var host = this.opts.host; return uppyServer[host] || host; } }]); return Provider; }(); //# sourceMappingURL=index.js.map