Procházet zdrojové kódy

@uppy/dropbox: refactor to TypeScript (#4979)

Murderlon před 1 rokem
rodič
revize
f72ec1b7ef

+ 5 - 2
packages/@uppy/box/src/Box.tsx

@@ -97,8 +97,11 @@ export default class Box<M extends Meta, B extends Body> extends UIPlugin<
     this.unmount()
   }
 
-  onFirstRender(): void {
-    Promise.all([this.provider.fetchPreAuthToken(), this.view.getFolder()])
+  async onFirstRender(): Promise<void> {
+    await Promise.all([
+      this.provider.fetchPreAuthToken(),
+      this.view.getFolder(),
+    ])
   }
 
   render(state: unknown): ComponentChild {

+ 1 - 0
packages/@uppy/dropbox/.npmignore

@@ -0,0 +1 @@
+tsconfig.*

+ 0 - 71
packages/@uppy/dropbox/src/Dropbox.jsx

@@ -1,71 +0,0 @@
-import { UIPlugin } from '@uppy/core'
-import { Provider, tokenStorage, getAllowedHosts } from '@uppy/companion-client'
-import { ProviderViews } from '@uppy/provider-views'
-import { h } from 'preact'
-
-import packageJson from '../package.json'
-import locale from './locale.js'
-
-export default class Dropbox extends UIPlugin {
-  static VERSION = packageJson.version
-
-  constructor (uppy, opts) {
-    super(uppy, opts)
-    this.id = this.opts.id || 'Dropbox'
-    this.type = 'acquirer'
-    this.storage = this.opts.storage || tokenStorage
-    this.files = []
-    this.icon = () => (
-      <svg className="uppy-DashboardTab-iconDropbox" aria-hidden="true" focusable="false" width="32" height="32" viewBox="0 0 32 32">
-        <path d="M10.5 7.5L5 10.955l5.5 3.454 5.5-3.454 5.5 3.454 5.5-3.454L21.5 7.5 16 10.955zM10.5 21.319L5 17.864l5.5-3.455 5.5 3.455zM16 17.864l5.5-3.455 5.5 3.455-5.5 3.455zM16 25.925l-5.5-3.455 5.5-3.454 5.5 3.454z" fill="currentcolor" fillRule="nonzero" />
-      </svg>
-    )
-
-    this.opts.companionAllowedHosts = getAllowedHosts(this.opts.companionAllowedHosts, this.opts.companionUrl)
-    this.provider = new Provider(uppy, {
-      companionUrl: this.opts.companionUrl,
-      companionHeaders: this.opts.companionHeaders,
-      companionKeysParams: this.opts.companionKeysParams,
-      companionCookiesRule: this.opts.companionCookiesRule,
-      provider: 'dropbox',
-      pluginId: this.id,
-      supportsRefreshToken: true,
-    })
-
-    this.defaultLocale = locale
-
-    this.i18nInit()
-    this.title = this.opts.title || this.i18n('pluginNameDropbox')
-
-    this.onFirstRender = this.onFirstRender.bind(this)
-    this.render = this.render.bind(this)
-  }
-
-  install () {
-    this.view = new ProviderViews(this, {
-      provider: this.provider,
-      loadAllFiles: true,
-    })
-
-    const { target } = this.opts
-    if (target) {
-      this.mount(target, this)
-    }
-  }
-
-  uninstall () {
-    this.view.tearDown()
-    this.unmount()
-  }
-
-  onFirstRender () {
-    return Promise.all([
-      this.provider.fetchPreAuthToken(),
-      this.view.getFolder(),
-    ])
-  }
-
-  render (state) {
-    return this.view.render(state)
-  }
-}

+ 111 - 0
packages/@uppy/dropbox/src/Dropbox.tsx

@@ -0,0 +1,111 @@
+import {
+  Provider,
+  getAllowedHosts,
+  tokenStorage,
+  type CompanionPluginOptions,
+} from '@uppy/companion-client'
+import { UIPlugin, Uppy } from '@uppy/core'
+import { ProviderViews } from '@uppy/provider-views'
+import { h, type ComponentChild } from 'preact'
+
+import type { UppyFile, Body, Meta } from '@uppy/utils/lib/UppyFile'
+import type { UnknownProviderPluginState } from '@uppy/core/lib/Uppy.ts'
+import locale from './locale.ts'
+// eslint-disable-next-line @typescript-eslint/ban-ts-comment
+// @ts-ignore We don't want TS to generate types for the package.json
+import packageJson from '../package.json'
+
+export type DropboxOptions = CompanionPluginOptions
+
+export default class Dropbox<M extends Meta, B extends Body> extends UIPlugin<
+  DropboxOptions,
+  M,
+  B,
+  UnknownProviderPluginState
+> {
+  static VERSION = packageJson.version
+
+  icon: () => JSX.Element
+
+  provider: Provider<M, B>
+
+  view: ProviderViews<M, B>
+
+  storage: typeof tokenStorage
+
+  files: UppyFile<M, B>[]
+
+  constructor(uppy: Uppy<M, B>, opts: DropboxOptions) {
+    super(uppy, opts)
+    this.id = this.opts.id || 'Dropbox'
+    this.type = 'acquirer'
+    this.storage = this.opts.storage || tokenStorage
+    this.files = []
+    this.icon = () => (
+      <svg
+        className="uppy-DashboardTab-iconDropbox"
+        aria-hidden="true"
+        focusable="false"
+        width="32"
+        height="32"
+        viewBox="0 0 32 32"
+      >
+        <path
+          d="M10.5 7.5L5 10.955l5.5 3.454 5.5-3.454 5.5 3.454 5.5-3.454L21.5 7.5 16 10.955zM10.5 21.319L5 17.864l5.5-3.455 5.5 3.455zM16 17.864l5.5-3.455 5.5 3.455-5.5 3.455zM16 25.925l-5.5-3.455 5.5-3.454 5.5 3.454z"
+          fill="currentcolor"
+          fillRule="nonzero"
+        />
+      </svg>
+    )
+
+    this.opts.companionAllowedHosts = getAllowedHosts(
+      this.opts.companionAllowedHosts,
+      this.opts.companionUrl,
+    )
+    this.provider = new Provider(uppy, {
+      companionUrl: this.opts.companionUrl,
+      companionHeaders: this.opts.companionHeaders,
+      companionKeysParams: this.opts.companionKeysParams,
+      companionCookiesRule: this.opts.companionCookiesRule,
+      provider: 'dropbox',
+      pluginId: this.id,
+      supportsRefreshToken: true,
+    })
+
+    this.defaultLocale = locale
+
+    this.i18nInit()
+    this.title = this.opts.title || this.i18n('pluginNameDropbox')
+
+    this.onFirstRender = this.onFirstRender.bind(this)
+    this.render = this.render.bind(this)
+  }
+
+  install(): void {
+    this.view = new ProviderViews(this, {
+      provider: this.provider,
+      loadAllFiles: true,
+    })
+
+    const { target } = this.opts
+    if (target) {
+      this.mount(target, this)
+    }
+  }
+
+  uninstall(): void {
+    this.view.tearDown()
+    this.unmount()
+  }
+
+  async onFirstRender(): Promise<void> {
+    await Promise.all([
+      this.provider.fetchPreAuthToken(),
+      this.view.getFolder(),
+    ])
+  }
+
+  render(state: unknown): ComponentChild {
+    return this.view.render(state)
+  }
+}

+ 0 - 1
packages/@uppy/dropbox/src/index.js

@@ -1 +0,0 @@
-export { default } from './Dropbox.jsx'

+ 1 - 0
packages/@uppy/dropbox/src/index.ts

@@ -0,0 +1 @@
+export { default } from './Dropbox.tsx'

+ 0 - 0
packages/@uppy/dropbox/src/locale.js → packages/@uppy/dropbox/src/locale.ts


+ 35 - 0
packages/@uppy/dropbox/tsconfig.build.json

@@ -0,0 +1,35 @@
+{
+  "extends": "../../../tsconfig.shared",
+  "compilerOptions": {
+    "noImplicitAny": false,
+    "outDir": "./lib",
+    "paths": {
+      "@uppy/companion-client": ["../companion-client/src/index.js"],
+      "@uppy/companion-client/lib/*": ["../companion-client/src/*"],
+      "@uppy/provider-views": ["../provider-views/src/index.js"],
+      "@uppy/provider-views/lib/*": ["../provider-views/src/*"],
+      "@uppy/utils/lib/*": ["../utils/src/*"],
+      "@uppy/core": ["../core/src/index.js"],
+      "@uppy/core/lib/*": ["../core/src/*"]
+    },
+    "resolveJsonModule": false,
+    "rootDir": "./src",
+    "skipLibCheck": true
+  },
+  "include": ["./src/**/*.*"],
+  "exclude": ["./src/**/*.test.ts"],
+  "references": [
+    {
+      "path": "../companion-client/tsconfig.build.json"
+    },
+    {
+      "path": "../provider-views/tsconfig.build.json"
+    },
+    {
+      "path": "../utils/tsconfig.build.json"
+    },
+    {
+      "path": "../core/tsconfig.build.json"
+    }
+  ]
+}

+ 31 - 0
packages/@uppy/dropbox/tsconfig.json

@@ -0,0 +1,31 @@
+{
+  "extends": "../../../tsconfig.shared",
+  "compilerOptions": {
+    "emitDeclarationOnly": false,
+    "noEmit": true,
+    "paths": {
+      "@uppy/companion-client": ["../companion-client/src/index.js"],
+      "@uppy/companion-client/lib/*": ["../companion-client/src/*"],
+      "@uppy/provider-views": ["../provider-views/src/index.js"],
+      "@uppy/provider-views/lib/*": ["../provider-views/src/*"],
+      "@uppy/utils/lib/*": ["../utils/src/*"],
+      "@uppy/core": ["../core/src/index.js"],
+      "@uppy/core/lib/*": ["../core/src/*"],
+    },
+  },
+  "include": ["./package.json", "./src/**/*.*"],
+  "references": [
+    {
+      "path": "../companion-client/tsconfig.build.json",
+    },
+    {
+      "path": "../provider-views/tsconfig.build.json",
+    },
+    {
+      "path": "../utils/tsconfig.build.json",
+    },
+    {
+      "path": "../core/tsconfig.build.json",
+    },
+  ],
+}