Skip to content

Commit

Permalink
feat: adds examples; refactors for auth response
Browse files Browse the repository at this point in the history
  • Loading branch information
eljefedelrodeodeljefe committed Sep 18, 2018
1 parent 794c5a7 commit 7057e2a
Show file tree
Hide file tree
Showing 16 changed files with 61 additions and 18 deletions.
1 change: 1 addition & 0 deletions examples/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package-lock.json
17 changes: 17 additions & 0 deletions examples/mq/subscribe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const Tillhub = require('@tillhub/node-sdk')

const th = new Tillhub({
base: process.env.TILLHUB_BASE,
credentials: {
id: process.env.CLIENT_ACCOUNT,
apiKey: process.env.API_KEY
}
})

function login () {
th.init((err, authResponse, client, authInstance) => {
if (err) throw err
})
}

login()
18 changes: 18 additions & 0 deletions examples/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"name": "examples",
"version": "0.1.0",
"description": "",
"main": "index.js",
"scripts": {
"run": "node -r dotenv/config",
"mq:subscribe": "npm run run -- mq/subscribe.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"@tillhub/node-sdk": "*"
},
"devDependencies": {
"node": "^10.10.0"
}
}
10 changes: 5 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ class Tillhub extends EventEmitter {
/**
* Initialise the SDK instance by authenticating the client
*
* @param {Function?} callback optional callback. I fnot specified a Promise will be returned
* @param {Function?} callback optional callback. If not specified and credentials are set we return a Promise. When called synchronously, we will only prepare the SDK
*/
init (callback) {
if (!this.options.credentials) {
this.auth = new Auth({ credentials: this.options.credentials })

if (!this.options.credentials && callback) {
return callback(errors.generate(errors.constants.INSUFFICIENT_CREDENTIALS))
}

this.auth = new Auth({ credentials: this.options.credentials })

this.auth.authenticate((err, authResponse) => {
if (err) return callback(err)

Expand All @@ -69,7 +69,7 @@ class Tillhub extends EventEmitter {
}
})

return callback(null, this, this.auth)
return callback(null, authResponse, this, this.auth)
})
}

Expand Down
3 changes: 3 additions & 0 deletions lib/v0/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const errors = require('../errors')
class Auth extends EventEmitter {
constructor (options = {}) {
super()

this.subUser = null

this.options = {
credentials: {},
base: process.env.TILLHUB_BASE || 'https://api.tillhub.com',
Expand Down
6 changes: 5 additions & 1 deletion lib/v1/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const errors = require('../errors')
class Auth extends AuthV0 {
constructor (options = {}) {
super()

this.subUser = null

this.options = {
credentials: {},
base: process.env.TILLHUB_BASE || 'https://api.tillhub.com',
Expand Down Expand Up @@ -80,8 +83,9 @@ class Auth extends AuthV0 {
if (resp.statusCode === 200) {
this.token = body.token
this.user = body.user.legacy_id || body.user.id
this.subUser = body.sub_user

return callback(null, { token: this.token, user: this.user })
return callback(null, { token: this.token, user: this.user, subUser: this.subUser, key: this.subUser ? this.subUser.key : null })
}

return callback(new Error('tillhub: could not get serviceaccount login info'), body, resp)
Expand Down
2 changes: 1 addition & 1 deletion test/auth/class-auth.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ test('root: Can init with auth credentials', function (t) {
})
}

th.init((err, client, authInstance) => {
th.init((err, authResponse, client, authInstance) => {
t.error(err)

t.ok(th.auth)
Expand Down
2 changes: 1 addition & 1 deletion test/carts/create.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ test('v1: carts: can create cart from existing products', function (t) {
})
}

th.init((err, client, authInstance) => {
th.init((err, authResponse, client, authInstance) => {
t.error(err)

const carts = th.carts()
Expand Down
2 changes: 1 addition & 1 deletion test/carts/get.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ test('v1: carts: can fetch some carts', function (t) {
})
}

th.init((err, client, authInstance) => {
th.init((err, authResponse, client, authInstance) => {
t.error(err)

const carts = th.carts()
Expand Down
4 changes: 2 additions & 2 deletions test/products/get.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ test('v1: products: can fetch some products', function (t) {
})
}

th.init((err, client, authInstance) => {
th.init((err, authResponse, client, authInstance) => {
t.error(err)

const products = th.products()
Expand Down Expand Up @@ -126,7 +126,7 @@ test('v1: Products: can fetch paged products', function (t) {
})
}

th.init((err, client, authInstance) => {
th.init((err, authResponse, client, authInstance) => {
t.error(err)

const products = th.products()
Expand Down
2 changes: 1 addition & 1 deletion test/templates/create.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ test('v1: Templates: can create template', function (t) {
})
}

th.init((err, client, authInstance) => {
th.init((err, authResponse, client, authInstance) => {
t.error(err)

const templates = th.templates()
Expand Down
2 changes: 1 addition & 1 deletion test/templates/get.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ test('v1: Templates: can fetch some templates', function (t) {
})
}

th.init((err, client, authInstance) => {
th.init((err, authResponse, client, authInstance) => {
t.error(err)

const templates = th.templates()
Expand Down
4 changes: 2 additions & 2 deletions test/transactions/get.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ test('v0: Transactions: can fetch some transactions', function (t) {
})
}

th.init((err, client, authInstance) => {
th.init((err, authResponse, client, authInstance) => {
t.error(err)

const transactions = th.transactions()
Expand Down Expand Up @@ -126,7 +126,7 @@ test('v0: Transactions: can fetch paged transactions', function (t) {
})
}

th.init((err, client, authInstance) => {
th.init((err, authResponse, client, authInstance) => {
t.error(err)

const transactions = th.transactions()
Expand Down
2 changes: 1 addition & 1 deletion test/vouchers/create.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ test('v0: Vouchers: can create voucher', function (t) {
})
}

th.init((err, client, authInstance) => {
th.init((err, authResponse, client, authInstance) => {
t.error(err)

const vouchers = th.vouchers()
Expand Down
2 changes: 1 addition & 1 deletion test/vouchers/get.logs.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ test('v1: Vouchers: can fetch some vouchers', function (t) {
})
}

th.init((err, client, authInstance) => {
th.init((err, authResponse, client, authInstance) => {
t.error(err)

const vouchers = th.vouchers()
Expand Down
2 changes: 1 addition & 1 deletion test/vouchers/get.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ test('v0: Vouchers: can fetch some vouchers', function (t) {
})
}

th.init((err, client, authInstance) => {
th.init((err, authResponse, client, authInstance) => {
t.error(err)

const vouchers = th.vouchers()
Expand Down

0 comments on commit 7057e2a

Please sign in to comment.