Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@uppy/companion-client: refactor to ESM #3693

Merged
merged 1 commit into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
4 changes: 4 additions & 0 deletions packages/@uppy/companion-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"license": "MIT",
"main": "lib/index.js",
"types": "types/index.d.ts",
"type": "module",
"keywords": [
"file uploader",
"uppy",
Expand All @@ -23,5 +24,8 @@
"dependencies": {
"@uppy/utils": "workspace:^",
"namespace-emitter": "^2.0.1"
},
"devDependencies": {
"@jest/globals": "^27.4.2"
}
}
2 changes: 1 addition & 1 deletion packages/@uppy/companion-client/src/AuthError.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ class AuthError extends Error {
}
}

module.exports = AuthError
export default AuthError
10 changes: 6 additions & 4 deletions packages/@uppy/companion-client/src/Provider.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -131,5 +132,6 @@ module.exports = class Provider extends RequestClient {
}

plugin.storage = plugin.opts.storage || tokenStorage
/* eslint-enable no-param-reassign */
}
}
13 changes: 7 additions & 6 deletions packages/@uppy/companion-client/src/RequestClient.js
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down Expand Up @@ -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))

Expand Down
3 changes: 2 additions & 1 deletion packages/@uppy/companion-client/src/RequestClient.test.js
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down
7 changes: 3 additions & 4 deletions packages/@uppy/companion-client/src/SearchProvider.js
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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}` : ''}`)
}
}
4 changes: 2 additions & 2 deletions packages/@uppy/companion-client/src/Socket.js
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
3 changes: 2 additions & 1 deletion packages/@uppy/companion-client/src/Socket.test.js
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 5 additions & 5 deletions packages/@uppy/companion-client/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions packages/@uppy/companion-client/src/tokenStorage.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down