From 1a8f80ed406b02f1e5f9b479b1970a25109dd86a Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Wed, 4 May 2022 21:43:24 +0200 Subject: [PATCH] @uppy/companion-client: refactor to ESM --- .eslintrc.js | 1 + packages/@uppy/companion-client/package.json | 4 ++++ packages/@uppy/companion-client/src/AuthError.js | 2 +- packages/@uppy/companion-client/src/Provider.js | 10 ++++++---- .../@uppy/companion-client/src/RequestClient.js | 13 +++++++------ .../companion-client/src/RequestClient.test.js | 3 ++- .../@uppy/companion-client/src/SearchProvider.js | 7 +++---- packages/@uppy/companion-client/src/Socket.js | 4 ++-- packages/@uppy/companion-client/src/Socket.test.js | 3 ++- packages/@uppy/companion-client/src/index.js | 10 +++++----- packages/@uppy/companion-client/src/tokenStorage.js | 6 +++--- yarn.lock | 1 + 12 files changed, 37 insertions(+), 27 deletions(-) diff --git a/.eslintrc.js b/.eslintrc.js index 457749e034..3d5f14b017 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -198,6 +198,7 @@ module.exports = { // Packages that have switched to ESM sources: 'packages/@uppy/audio/src/**/*.js', 'packages/@uppy/box/src/**/*.js', + 'packages/@uppy/companion-client/src/**/*.js', 'packages/@uppy/compressor/src/**/*.js', 'packages/@uppy/drag-drop/src/**/*.js', 'packages/@uppy/drop-target/src/**/*.js', diff --git a/packages/@uppy/companion-client/package.json b/packages/@uppy/companion-client/package.json index 199c7614c3..73363751e4 100644 --- a/packages/@uppy/companion-client/package.json +++ b/packages/@uppy/companion-client/package.json @@ -5,6 +5,7 @@ "license": "MIT", "main": "lib/index.js", "types": "types/index.d.ts", + "type": "module", "keywords": [ "file uploader", "uppy", @@ -23,5 +24,8 @@ "dependencies": { "@uppy/utils": "workspace:^", "namespace-emitter": "^2.0.1" + }, + "devDependencies": { + "@jest/globals": "^27.4.2" } } diff --git a/packages/@uppy/companion-client/src/AuthError.js b/packages/@uppy/companion-client/src/AuthError.js index e0c02e2309..14517d3400 100644 --- a/packages/@uppy/companion-client/src/AuthError.js +++ b/packages/@uppy/companion-client/src/AuthError.js @@ -8,4 +8,4 @@ class AuthError extends Error { } } -module.exports = AuthError +export default AuthError diff --git a/packages/@uppy/companion-client/src/Provider.js b/packages/@uppy/companion-client/src/Provider.js index a4ec88dad0..ec3cbaab9d 100644 --- a/packages/@uppy/companion-client/src/Provider.js +++ b/packages/@uppy/companion-client/src/Provider.js @@ -1,13 +1,13 @@ 'use strict' -const RequestClient = require('./RequestClient') -const tokenStorage = require('./tokenStorage') +import RequestClient from './RequestClient.js' +import tokenStorage from './tokenStorage.js' const getName = (id) => { return id.split('-').map((s) => s.charAt(0).toUpperCase() + s.slice(1)).join(' ') } -module.exports = class Provider extends RequestClient { +export default class Provider extends RequestClient { constructor (uppy, opts) { super(uppy, opts) this.provider = opts.provider @@ -37,7 +37,7 @@ module.exports = class Provider extends RequestClient { } onReceiveResponse (response) { - response = super.onReceiveResponse(response) + response = super.onReceiveResponse(response) // eslint-disable-line no-param-reassign const plugin = this.uppy.getPlugin(this.pluginId) const oldAuthenticated = plugin.getPluginState().authenticated const authenticated = oldAuthenticated ? response.status !== 401 : response.status < 400 @@ -106,6 +106,7 @@ module.exports = class Provider extends RequestClient { } static initPlugin (plugin, opts, defaultOpts) { + /* eslint-disable no-param-reassign */ plugin.type = 'acquirer' plugin.files = [] if (defaultOpts) { @@ -131,5 +132,6 @@ module.exports = class Provider extends RequestClient { } plugin.storage = plugin.opts.storage || tokenStorage + /* eslint-enable no-param-reassign */ } } diff --git a/packages/@uppy/companion-client/src/RequestClient.js b/packages/@uppy/companion-client/src/RequestClient.js index b87ba9c60f..36430b22e8 100644 --- a/packages/@uppy/companion-client/src/RequestClient.js +++ b/packages/@uppy/companion-client/src/RequestClient.js @@ -1,8 +1,10 @@ 'use strict' -const fetchWithNetworkError = require('@uppy/utils/lib/fetchWithNetworkError') -const ErrorWithCause = require('@uppy/utils/lib/ErrorWithCause') -const AuthError = require('./AuthError') +import fetchWithNetworkError from '@uppy/utils/lib/fetchWithNetworkError' +import ErrorWithCause from '@uppy/utils/lib/ErrorWithCause' +import AuthError from './AuthError.js' + +import packageJson from '../package.json' // Remove the trailing slash so we can always safely append /xyz. function stripSlash (url) { @@ -30,9 +32,8 @@ async function handleJSONResponse (res) { return jsonPromise } -module.exports = class RequestClient { - // eslint-disable-next-line global-require - static VERSION = require('../package.json').version +export default class RequestClient { + static VERSION = packageJson.version #getPostResponseFunc = skip => response => (skip ? response : this.onReceiveResponse(response)) diff --git a/packages/@uppy/companion-client/src/RequestClient.test.js b/packages/@uppy/companion-client/src/RequestClient.test.js index 252dbef79a..750809013b 100644 --- a/packages/@uppy/companion-client/src/RequestClient.test.js +++ b/packages/@uppy/companion-client/src/RequestClient.test.js @@ -1,4 +1,5 @@ -const RequestClient = require('./RequestClient') +import { describe, it, expect } from '@jest/globals' +import RequestClient from './RequestClient.js' describe('RequestClient', () => { it('has a hostname without trailing slash', () => { diff --git a/packages/@uppy/companion-client/src/SearchProvider.js b/packages/@uppy/companion-client/src/SearchProvider.js index 748350c6b2..19bc051d74 100644 --- a/packages/@uppy/companion-client/src/SearchProvider.js +++ b/packages/@uppy/companion-client/src/SearchProvider.js @@ -1,12 +1,12 @@ 'use strict' -const RequestClient = require('./RequestClient') +import RequestClient from './RequestClient.js' const getName = (id) => { return id.split('-').map((s) => s.charAt(0).toUpperCase() + s.slice(1)).join(' ') } -module.exports = class SearchProvider extends RequestClient { +export default class SearchProvider extends RequestClient { constructor (uppy, opts) { super(uppy, opts) this.provider = opts.provider @@ -20,7 +20,6 @@ module.exports = class SearchProvider extends RequestClient { } search (text, queries) { - queries = queries ? `&${queries}` : '' - return this.get(`search/${this.id}/list?q=${encodeURIComponent(text)}${queries}`) + return this.get(`search/${this.id}/list?q=${encodeURIComponent(text)}${queries ? `&${queries}` : ''}`) } } diff --git a/packages/@uppy/companion-client/src/Socket.js b/packages/@uppy/companion-client/src/Socket.js index 0fc6fb684c..620fee1c2a 100644 --- a/packages/@uppy/companion-client/src/Socket.js +++ b/packages/@uppy/companion-client/src/Socket.js @@ -1,6 +1,6 @@ -const ee = require('namespace-emitter') +import ee from 'namespace-emitter' -module.exports = class UppySocket { +export default class UppySocket { #queued = [] #emitter = ee() diff --git a/packages/@uppy/companion-client/src/Socket.test.js b/packages/@uppy/companion-client/src/Socket.test.js index b4e92599aa..3f6db3559e 100644 --- a/packages/@uppy/companion-client/src/Socket.test.js +++ b/packages/@uppy/companion-client/src/Socket.test.js @@ -1,4 +1,5 @@ -const UppySocket = require('./Socket') +import { jest, describe, it, expect } from '@jest/globals' +import UppySocket from './Socket.js' describe('Socket', () => { let webSocketConstructorSpy diff --git a/packages/@uppy/companion-client/src/index.js b/packages/@uppy/companion-client/src/index.js index 0f93d661b4..4cc8e0241e 100644 --- a/packages/@uppy/companion-client/src/index.js +++ b/packages/@uppy/companion-client/src/index.js @@ -4,12 +4,12 @@ * Manages communications with Companion */ -const RequestClient = require('./RequestClient') -const Provider = require('./Provider') -const SearchProvider = require('./SearchProvider') -const Socket = require('./Socket') +import RequestClient from './RequestClient.js' +import Provider from './Provider.js' +import SearchProvider from './SearchProvider.js' +import Socket from './Socket.js' -module.exports = { +export { RequestClient, Provider, SearchProvider, diff --git a/packages/@uppy/companion-client/src/tokenStorage.js b/packages/@uppy/companion-client/src/tokenStorage.js index 6baebf6e62..2bfc9f5873 100644 --- a/packages/@uppy/companion-client/src/tokenStorage.js +++ b/packages/@uppy/companion-client/src/tokenStorage.js @@ -3,18 +3,18 @@ /** * This module serves as an Async wrapper for LocalStorage */ -module.exports.setItem = (key, value) => { +export function setItem (key, value) { return new Promise((resolve) => { localStorage.setItem(key, value) resolve() }) } -module.exports.getItem = (key) => { +export function getItem (key) { return Promise.resolve(localStorage.getItem(key)) } -module.exports.removeItem = (key) => { +export function removeItem (key) { return new Promise((resolve) => { localStorage.removeItem(key) resolve() diff --git a/yarn.lock b/yarn.lock index 422aab9f78..a18dd1c693 100644 --- a/yarn.lock +++ b/yarn.lock @@ -9713,6 +9713,7 @@ __metadata: version: 0.0.0-use.local resolution: "@uppy/companion-client@workspace:packages/@uppy/companion-client" dependencies: + "@jest/globals": ^27.4.2 "@uppy/utils": "workspace:^" namespace-emitter: ^2.0.1 languageName: unknown