Provider.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. 'use strict'
  2. const RequestClient = require('./RequestClient')
  3. const tokenStorage = require('./tokenStorage')
  4. const _getName = (id) => {
  5. return id.split('-').map((s) => s.charAt(0).toUpperCase() + s.slice(1)).join(' ')
  6. }
  7. module.exports = class Provider extends RequestClient {
  8. static VERSION = require('../package.json').version
  9. constructor (uppy, opts) {
  10. super(uppy, opts)
  11. this.provider = opts.provider
  12. this.id = this.provider
  13. this.authProvider = opts.authProvider || this.provider
  14. this.name = this.opts.name || _getName(this.id)
  15. this.pluginId = this.opts.pluginId
  16. this.tokenKey = `companion-${this.pluginId}-auth-token`
  17. }
  18. headers () {
  19. return new Promise((resolve, reject) => {
  20. super.headers().then((headers) => {
  21. this.getAuthToken().then((token) => {
  22. resolve(Object.assign({}, headers, { 'uppy-auth-token': token }))
  23. })
  24. }).catch(reject)
  25. })
  26. }
  27. onReceiveResponse (response) {
  28. response = super.onReceiveResponse(response)
  29. const authenticated = response.status !== 401
  30. this.uppy.getPlugin(this.pluginId).setPluginState({ authenticated })
  31. return response
  32. }
  33. // @todo(i.olarewaju) consider whether or not this method should be exposed
  34. setAuthToken (token) {
  35. return this.uppy.getPlugin(this.pluginId).storage.setItem(this.tokenKey, token)
  36. }
  37. getAuthToken () {
  38. return this.uppy.getPlugin(this.pluginId).storage.getItem(this.tokenKey)
  39. }
  40. authUrl () {
  41. return `${this.hostname}/${this.id}/connect`
  42. }
  43. fileUrl (id) {
  44. return `${this.hostname}/${this.id}/get/${id}`
  45. }
  46. list (directory) {
  47. return this.get(`${this.id}/list/${directory || ''}`)
  48. }
  49. logout (redirect = location.href) {
  50. return new Promise((resolve, reject) => {
  51. this.get(`${this.id}/logout?redirect=${redirect}`)
  52. .then((res) => {
  53. this.uppy.getPlugin(this.pluginId).storage.removeItem(this.tokenKey)
  54. .then(() => resolve(res))
  55. .catch(reject)
  56. }).catch(reject)
  57. })
  58. }
  59. static initPlugin (plugin, opts, defaultOpts) {
  60. plugin.type = 'acquirer'
  61. plugin.files = []
  62. if (defaultOpts) {
  63. plugin.opts = Object.assign({}, defaultOpts, opts)
  64. }
  65. if (opts.serverUrl || opts.serverPattern) {
  66. throw new Error('`serverUrl` and `serverPattern` have been renamed to `companionUrl` and `companionAllowedHosts` respectively in the 0.30.5 release. Please consult the docs (for example, https://uppy.io/docs/instagram/ for the Instagram plugin) and use the updated options.`')
  67. }
  68. if (opts.companionAllowedHosts) {
  69. const pattern = opts.companionAllowedHosts
  70. // validate companionAllowedHosts param
  71. if (typeof pattern !== 'string' && !Array.isArray(pattern) && !(pattern instanceof RegExp)) {
  72. throw new TypeError(`${plugin.id}: the option "companionAllowedHosts" must be one of string, Array, RegExp`)
  73. }
  74. plugin.opts.companionAllowedHosts = pattern
  75. } else {
  76. // does not start with https://
  77. if (/^(?!https?:\/\/).*$/i.test(opts.companionUrl)) {
  78. plugin.opts.companionAllowedHosts = `https://${opts.companionUrl.replace(/^\/\//, '')}`
  79. } else {
  80. plugin.opts.companionAllowedHosts = opts.companionUrl
  81. }
  82. }
  83. plugin.storage = plugin.opts.storage || tokenStorage
  84. }
  85. }