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

Add install suggestion service #592

Merged
merged 1 commit into from
Apr 5, 2019
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
"eslint-config-prettier": "4.1.0",
"focus-trap-react": "4.0.1",
"lodash.get": "4.4.2",
"node-fetch": "^2.3.0",
"prop-types": "15.7.2",
"react-proptypes": "1.0.0",
"react-redux": "5.1.1",
Expand Down
43 changes: 43 additions & 0 deletions src/targets/services/installSuggestion.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import fetch from 'node-fetch'
import CozyClient from 'cozy-client'

global.fetch = fetch

const schema = {
suggestion: {
doctype: 'io.cozy.suggestions'
},
konnectors: {
doctype: 'io.cozy.konnectors'
}
}

let suggestion = {}
try {
suggestion = JSON.parse(process.env.COZY_COUCH_DOC)
} catch (e) {
throw new Error(`Wrong formatted suggestion doc: ${e}`)
}

const client = new CozyClient({
uri: process.env.COZY_URL.trim(),
schema,
token: process.env.COZY_CREDENTIALS.trim()
}).getStackClient()

const handleError = (e, slug) => {
if (e.status === 409) return
throw new Error(`Error when installing ${slug}: ${e}`)
}

async function install(suggestion) {
if (suggestion.silenced) return // silenced, so not concerned
const slug = suggestion.slug
try {
await client.fetchJSON('POST', `/konnectors/${slug}`)
} catch (e) {
handleError(e, slug)
}
}

install(suggestion)
CPatchane marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

Since the install promise is not awaited, thrown errors (for example if suggestion is null or undefined) will result in UnhandledPromiseRejection, this is why I had put an await and a try catch.

2 changes: 1 addition & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7906,7 +7906,7 @@ node-fetch@^1.0.1:
encoding "^0.1.11"
is-stream "^1.0.1"

node-fetch@^2.0.0:
node-fetch@^2.0.0, node-fetch@^2.3.0:
version "2.3.0"
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.3.0.tgz#1a1d940bbfb916a1d3e0219f037e89e71f8c5fa5"
integrity sha512-MOd8pV3fxENbryESLgVIeaGKrdl+uaYhCSSVkjeOb/31/njTpcis5aWfdqgNlHIrKOLRbMnfPINPOML2CIFeXA==
Expand Down