Skip to content

Commit

Permalink
feat: options.PouchDB
Browse files Browse the repository at this point in the history
BREAKING CHANGE:

`options.PouchDB` is now a required options for the [`Store` constructor](https://github.com/hoodiehq/hoodie-client#constructor)
  • Loading branch information
gr2m committed Nov 27, 2016
1 parent 948b35c commit 026b206
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
30 changes: 16 additions & 14 deletions lib/get-api.js
Expand Up @@ -17,25 +17,27 @@ function getApi (state) {
}

var account = mergeOptionsAndCreate(internals.Account, { url: api.url + '/account/api' }, state.account)
var CustomPouchDB = state.PouchDB.defaults({
ajax: {
headers: {
get authorization () {
var session = api.account.get('session')
if (!session) {
return
}

return 'Session ' + session.id
}
}
}
})

var CustomStore = internals.Store.defaults({
PouchDB: CustomPouchDB,
remoteBaseUrl: api.url + '/store/api'
})
var dbName = 'user/' + account.id
var store = new CustomStore(dbName, {
ajax: function () {
var session = api.account.get('session')
if (!session) {
return
}

return {
headers: {
authorization: 'Session ' + session.id
}
}
}
})
var store = new CustomStore(dbName)

// core modules
api.account = account
Expand Down
5 changes: 5 additions & 0 deletions lib/get-state.js
Expand Up @@ -10,6 +10,11 @@ var constants = require('./constants')

function getState (options) {
options = options || {}

if (typeof options.PouchDB !== 'function') {
throw new TypeError('options.PouchDB is required (see https://github.com/hoodiehq/hoodie-client#constructor)')
}

var defaultOptions = {
url: getCurrentOrigin()
}
Expand Down

0 comments on commit 026b206

Please sign in to comment.