From 9f95053e51c873ee55e95b0dc15f69a2b9dbed11 Mon Sep 17 00:00:00 2001 From: Murderlon Date: Mon, 4 Mar 2024 11:30:54 +0100 Subject: [PATCH] @uppy/unsplash: refactor to TypeScript --- packages/@uppy/unsplash/.npmignore | 1 + packages/@uppy/unsplash/src/Unsplash.jsx | 69 ------------- packages/@uppy/unsplash/src/Unsplash.tsx | 108 ++++++++++++++++++++ packages/@uppy/unsplash/src/index.js | 1 - packages/@uppy/unsplash/src/index.ts | 1 + packages/@uppy/unsplash/tsconfig.build.json | 35 +++++++ packages/@uppy/unsplash/tsconfig.json | 31 ++++++ 7 files changed, 176 insertions(+), 70 deletions(-) create mode 100644 packages/@uppy/unsplash/.npmignore delete mode 100644 packages/@uppy/unsplash/src/Unsplash.jsx create mode 100644 packages/@uppy/unsplash/src/Unsplash.tsx delete mode 100644 packages/@uppy/unsplash/src/index.js create mode 100644 packages/@uppy/unsplash/src/index.ts create mode 100644 packages/@uppy/unsplash/tsconfig.build.json create mode 100644 packages/@uppy/unsplash/tsconfig.json diff --git a/packages/@uppy/unsplash/.npmignore b/packages/@uppy/unsplash/.npmignore new file mode 100644 index 00000000000..6c816673f08 --- /dev/null +++ b/packages/@uppy/unsplash/.npmignore @@ -0,0 +1 @@ +tsconfig.* diff --git a/packages/@uppy/unsplash/src/Unsplash.jsx b/packages/@uppy/unsplash/src/Unsplash.jsx deleted file mode 100644 index 605c1a34e2d..00000000000 --- a/packages/@uppy/unsplash/src/Unsplash.jsx +++ /dev/null @@ -1,69 +0,0 @@ -import { h } from 'preact' -import { UIPlugin } from '@uppy/core' -import { SearchProvider, tokenStorage, getAllowedHosts } from '@uppy/companion-client' -import { SearchProviderViews } from '@uppy/provider-views' - -import packageJson from '../package.json' - -export default class Unsplash extends UIPlugin { - static VERSION = packageJson.version - - constructor (uppy, opts) { - super(uppy, opts) - this.type = 'acquirer' - this.files = [] - this.storage = this.opts.storage || tokenStorage - this.id = this.opts.id || 'Unsplash' - this.title = this.opts.title || 'Unsplash' - - this.icon = () => ( - - ) - - if (!this.opts.companionUrl) { - throw new Error('Companion hostname is required, please consult https://uppy.io/docs/companion') - } - - this.hostname = this.opts.companionUrl - - this.opts.companionAllowedHosts = getAllowedHosts(this.opts.companionAllowedHosts, this.opts.companionUrl) - this.provider = new SearchProvider(uppy, { - companionUrl: this.opts.companionUrl, - companionHeaders: this.opts.companionHeaders, - companionCookiesRule: this.opts.companionCookiesRule, - provider: 'unsplash', - pluginId: this.id, - }) - } - - install () { - this.view = new SearchProviderViews(this, { - provider: this.provider, - viewType: 'unsplash', - showFilter: true, - }) - - const { target } = this.opts - if (target) { - this.mount(target, this) - } - } - - // eslint-disable-next-line class-methods-use-this - onFirstRender () { - // do nothing - } - - render (state) { - return this.view.render(state) - } - - uninstall () { - this.unmount() - } -} diff --git a/packages/@uppy/unsplash/src/Unsplash.tsx b/packages/@uppy/unsplash/src/Unsplash.tsx new file mode 100644 index 00000000000..a5c8d5403ba --- /dev/null +++ b/packages/@uppy/unsplash/src/Unsplash.tsx @@ -0,0 +1,108 @@ +import { + getAllowedHosts, + tokenStorage, + type CompanionPluginOptions, + SearchProvider, +} from '@uppy/companion-client' +import { UIPlugin, Uppy } from '@uppy/core' +import { SearchProviderViews } from '@uppy/provider-views' +import { h, type ComponentChild } from 'preact' + +import type { UppyFile, Body, Meta } from '@uppy/utils/lib/UppyFile' +import type { UnknownSearchProviderPluginState } from '@uppy/core/lib/Uppy.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 UnsplashOptions = CompanionPluginOptions + +export default class Unsplash extends UIPlugin< + UnsplashOptions, + M, + B, + UnknownSearchProviderPluginState +> { + static VERSION = packageJson.version + + icon: () => JSX.Element + + provider: SearchProvider + + view: SearchProviderViews + + storage: typeof tokenStorage + + files: UppyFile[] + + hostname: string + + constructor(uppy: Uppy, opts: UnsplashOptions) { + super(uppy, opts) + this.type = 'acquirer' + this.files = [] + this.storage = this.opts.storage || tokenStorage + this.id = this.opts.id || 'Unsplash' + this.title = this.opts.title || 'Unsplash' + + this.icon = () => ( + + ) + + if (!this.opts.companionUrl) { + throw new Error( + 'Companion hostname is required, please consult https://uppy.io/docs/companion', + ) + } + + this.hostname = this.opts.companionUrl + + this.opts.companionAllowedHosts = getAllowedHosts( + this.opts.companionAllowedHosts, + this.opts.companionUrl, + ) + this.provider = new SearchProvider(uppy, { + companionUrl: this.opts.companionUrl, + companionHeaders: this.opts.companionHeaders, + companionCookiesRule: this.opts.companionCookiesRule, + provider: 'unsplash', + pluginId: this.id, + }) + } + + install(): void { + this.view = new SearchProviderViews(this, { + provider: this.provider, + viewType: 'unsplash', + showFilter: true, + }) + + const { target } = this.opts + if (target) { + this.mount(target, this) + } + } + + // eslint-disable-next-line class-methods-use-this + async onFirstRender(): Promise { + // do nothing + } + + render(state: unknown): ComponentChild { + return this.view.render(state) + } + + uninstall(): void { + this.unmount() + } +} diff --git a/packages/@uppy/unsplash/src/index.js b/packages/@uppy/unsplash/src/index.js deleted file mode 100644 index 94ed1f63e60..00000000000 --- a/packages/@uppy/unsplash/src/index.js +++ /dev/null @@ -1 +0,0 @@ -export { default } from './Unsplash.jsx' diff --git a/packages/@uppy/unsplash/src/index.ts b/packages/@uppy/unsplash/src/index.ts new file mode 100644 index 00000000000..f88c464959a --- /dev/null +++ b/packages/@uppy/unsplash/src/index.ts @@ -0,0 +1 @@ +export { default } from './Unsplash.tsx' diff --git a/packages/@uppy/unsplash/tsconfig.build.json b/packages/@uppy/unsplash/tsconfig.build.json new file mode 100644 index 00000000000..99aaf378deb --- /dev/null +++ b/packages/@uppy/unsplash/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" + } + ] +} diff --git a/packages/@uppy/unsplash/tsconfig.json b/packages/@uppy/unsplash/tsconfig.json new file mode 100644 index 00000000000..e5220fb5ab1 --- /dev/null +++ b/packages/@uppy/unsplash/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", + }, + ], +}