Skip to content

Commit

Permalink
Merge pull request #134 from kosssi/version
Browse files Browse the repository at this point in the history
feat: Add cozyVersion parameter to remove '/status/' request (offline) 🛀
  • Loading branch information
kosssi authored Apr 21, 2017
2 parents e33543f + 114e323 commit 967a8af
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,13 @@ It does not return a value.
* `cozyURL`: absolute url of the cozy stack
* `disablePromises`: boolean to make function that returns promise used with a classical "callback as last argument" (default value is *false*)
* `oauth`: an object with the OAuth parameters, see [OAuth](./oauth.md) for details
* `version`: the version of Cozy (2 or 3), it's optional, by default with a request to the server it can deduce automatically the version. (To be specified for an offline mode)

```javascript
cozy.client.init({
cozyURL: 'http://cozy.tools:8080',
disablePromises: false,
version: 3,
oauth: {
clientParams: {/*...*/},
scopes: ["io.cozy.files:GET"],
Expand Down
11 changes: 7 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class Client {
this._authstate = AuthNone
this._authcreds = null
this._storage = null
this._version = null
this._version = options.version || null
this._offline = null

const token = options.token
Expand Down Expand Up @@ -235,17 +235,20 @@ class Client {

isV2 () {
if (!this._version) {
this._version = retry(() => fetch(`${this._url}/status/`), 3)()
return retry(() => fetch(`${this._url}/status/`), 3)()
.then((res) => {
if (!res.ok) {
throw new Error('Could not fetch cozy status')
} else {
return res.json()
}
})
.then((status) => status.datasystem !== undefined)
.then((status) => {
this._version = status.datasystem !== undefined ? 2 : 3
return this.isV2()
})
}
return this._version
return Promise.resolve(this._version === 2)
}
}

Expand Down

0 comments on commit 967a8af

Please sign in to comment.