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

rewrite companion.app() to return an object #3827

Merged
merged 1 commit into from Jun 27, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion examples/aws-companion/server.js
Expand Up @@ -46,7 +46,9 @@ process.on('exit', () => {
fs.rmSync(DATA_DIR, { recursive: true, force: true })
})

app.use(companion.app(options))
const { app: companionApp } = companion.app(options)

app.use(companionApp)

const server = app.listen(3020, () => {
console.log('listening on port 3020')
Expand Down
6 changes: 4 additions & 2 deletions examples/digitalocean-spaces/server.js
Expand Up @@ -27,7 +27,7 @@ const app = router()
app.use(require('cors')())
app.use(require('body-parser').json())

app.use('/companion', companion.app({
const { app: companionApp } = companion.app({
providerOptions: {
s3: {
// This is the crucial part; set an endpoint template for the service you want to use.
Expand All @@ -41,7 +41,9 @@ app.use('/companion', companion.app({
},
},
server: { serverUrl: `localhost:${PORT}` },
}))
})

app.use('/companion', companionApp)

// Serve the built CSS file.
app.get('/uppy.min.css', (req, res) => {
Expand Down
3 changes: 2 additions & 1 deletion examples/uppy-with-companion/server/index.js
Expand Up @@ -53,7 +53,8 @@ const uppyOptions = {
debug: true,
}

app.use(companion.app(uppyOptions))
const { app: companionApp } = companion.app(uppyOptions)
app.use(companionApp)

// handle 404
app.use((req, res) => {
Expand Down
3 changes: 2 additions & 1 deletion packages/@uppy/companion/README.md
Expand Up @@ -48,7 +48,8 @@ const options = {
filePath: '/path/to/folder/',
}

app.use(companion.app(options))
const { app: companionApp } = companion.app(options)
app.use(companionApp)
```

To enable companion socket for realtime feed to the client while upload is going on, you call the `socket` method like so.
Expand Down
7 changes: 2 additions & 5 deletions packages/@uppy/companion/src/companion.js
Expand Up @@ -57,7 +57,7 @@ module.exports.socket = require('./server/socket')
* Entry point into initializing the Companion app.
*
* @param {object} optionsArg
* @returns {import('express').Express}
* @returns {{ app: import('express').Express, emitter: any }}}
*/
module.exports.app = (optionsArg = {}) => {
validateConfig(optionsArg)
Expand Down Expand Up @@ -152,8 +152,5 @@ module.exports.app = (optionsArg = {}) => {
processId,
})

// todo split emitter from app in next major
// @ts-ignore
app.companionEmitter = emitter
return app
return { app, emitter }
}
2 changes: 1 addition & 1 deletion packages/@uppy/companion/src/standalone/index.js
Expand Up @@ -167,7 +167,7 @@ module.exports = function server (inputCompanionOptions = {}) {
}

// initialize companion
const companionApp = companion.app(companionOptions)
const { app: companionApp } = companion.app(companionOptions)

// add companion to server middleware
router.use(companionApp)
Expand Down
11 changes: 6 additions & 5 deletions website/src/docs/companion.md
Expand Up @@ -54,7 +54,7 @@ Companion may either be used as a pluggable express app, which you plug into you

### Plugging into an existing express server

To plug Companion into an existing server, call its `.app` method, passing in an [options](#Options) object as a parameter. This returns a server instance that you can mount on a subpath in your Express or app.
To plug Companion into an existing server, call its `.app()` method, passing in an [options](#Options) object as a parameter. This returns an object with an `app` property which is a server instance that you can mount on a subpath in your Express or app.

```js
import express from 'express'
Expand Down Expand Up @@ -88,7 +88,9 @@ const options = {
filePath: '/path/to/folder/',
}

app.use('/companion', companion.app(options))
const { app: companionApp } = companion.app(options)

app.use('/companion', companionApp)
```

See [Options](#Options) for valid configuration options.
Expand All @@ -105,7 +107,7 @@ This takes your `server` instance as an argument.

#### Events

The object returned by `companion.app()` also has a property `companionEmitter` which is an `EventEmitter` that emits the following events:
The object returned by `companion.app()` also has a property `emitter` which is an `EventEmitter` that emits the following events:

* `upload-start` - When an upload starts, this event is emitted with an object containing the property `token`, which is a unique ID for the upload.
* **token** - The event name is the token from `upload-start`. The event has an object with the following properties:
Expand All @@ -117,8 +119,7 @@ The object returned by `companion.app()` also has a property `companionEmitter`
Example code for using the `EventEmitter` to handle a finished file upload:

```js
const companionApp = companion.app(options)
const { companionEmitter: emitter } = companionApp
const { app, emitter } = companion.app(options)

emitter.on('upload-start', ({ token }) => {
console.log('Upload started', token)
Expand Down