Skip to content
Open
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,20 @@ NLU_URL=<NLU_URL>
NLU_API_KEY=<NLU_API_KEY>
```

In the same `.env` file, add a comma separated list of origins that will be allowed to make cross site requests.

If any origin can make the requests, just use the "*" value.

```
ALLOWED_ORIGINS=<ALLOWED_ORIGINS>
```

For example:
```
ALLOWED_ORIGINS=http://localhost:3001
```


Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of setting ALLOWED_ORIGINS as mandatory, can we set it to http://localhost:3001 by default and move the update instruction to the Customize section bellow?

### Customize

Create an `.env` file to start the application in [cluster mode](https://nodejs.org/api/cluster.html#cluster_cluster), change its running port, log level, directory and maximum size. The variable names and default values are:
Expand Down
10 changes: 6 additions & 4 deletions src/controllers/translations/retrieve.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ const mongo = require('src/databases/mongo')
const NotFoundError = require('src/errors/not-found')

module.exports = async (language, signed) => {
const translationData = { signed, spoken: null }
const signedProcessedText = await text.process(signed)

const translation = await retrieveData(language, 'translations', { signed: signedProcessedText.hash })

if (!translation) throw new NotFoundError()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's important to keep this validation error.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, shouldn't we just return no translation and let the backend handle this and show an alert to the user that no translation was found?

As our database is still small, most phrases will return no translation.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the backend repo and NotFoundError is equal to no translation was found.

if (!translation) {
throw new NotFoundError()
}

const spoken = await retrieveData(language, 'spoken', { hash: translation.spoken })

return { signed, spoken: spoken.text }
translationData.spoken = spoken ? spoken.text : null
return translationData
}

function retrieveData (language, collection, query) {
Expand Down
13 changes: 13 additions & 0 deletions src/routes/translate/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
const joi = require('joi')
const translations = require('src/controllers/translations')
const BadRequestError = require('src/errors/bad-request')
require('dotenv').config()

const schema = joi.object({
params: {
Expand All @@ -47,6 +48,18 @@ module.exports = (req, res, next) => {
translations.retrieve(value.params.language, sanitize(value.query.text))
.then(response => {
res.locals = response
const allowedOriginsString = process.env.ALLOWED_ORIGINS
if (allowedOriginsString) {
if (allowedOriginsString === '*') {
res.set('Access-Control-Allow-Origin', '*')
} else {
const allowedOriginsArray = allowedOriginsString.split(',')
const requestOrigin = req.headers.origin
if (allowedOriginsArray.includes(requestOrigin)) {
res.set('Access-Control-Allow-Origin', requestOrigin)
}
}
}
next(null, req, res)
})
.catch(error => next(error))
Expand Down