diff --git a/examples/.gitignore b/examples/.gitignore new file mode 100644 index 0000000..d8b83df --- /dev/null +++ b/examples/.gitignore @@ -0,0 +1 @@ +package-lock.json diff --git a/examples/mq/subscribe.js b/examples/mq/subscribe.js new file mode 100644 index 0000000..493f5c3 --- /dev/null +++ b/examples/mq/subscribe.js @@ -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() diff --git a/examples/package.json b/examples/package.json new file mode 100644 index 0000000..c3e42c5 --- /dev/null +++ b/examples/package.json @@ -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" + } +} diff --git a/lib/index.js b/lib/index.js index d2c8d2a..87a6e4e 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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) @@ -69,7 +69,7 @@ class Tillhub extends EventEmitter { } }) - return callback(null, this, this.auth) + return callback(null, authResponse, this, this.auth) }) } diff --git a/lib/v0/auth.js b/lib/v0/auth.js index 33c8e37..978c277 100644 --- a/lib/v0/auth.js +++ b/lib/v0/auth.js @@ -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', diff --git a/lib/v1/auth.js b/lib/v1/auth.js index 9fe61e9..021396d 100644 --- a/lib/v1/auth.js +++ b/lib/v1/auth.js @@ -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', @@ -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) diff --git a/test/auth/class-auth.spec.js b/test/auth/class-auth.spec.js index 6cd32f2..671d853 100644 --- a/test/auth/class-auth.spec.js +++ b/test/auth/class-auth.spec.js @@ -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) diff --git a/test/carts/create.spec.js b/test/carts/create.spec.js index fb51f9a..40352e3 100644 --- a/test/carts/create.spec.js +++ b/test/carts/create.spec.js @@ -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() diff --git a/test/carts/get.spec.js b/test/carts/get.spec.js index 4d19ed8..0c5d0c0 100644 --- a/test/carts/get.spec.js +++ b/test/carts/get.spec.js @@ -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() diff --git a/test/products/get.spec.js b/test/products/get.spec.js index c807108..3389038 100644 --- a/test/products/get.spec.js +++ b/test/products/get.spec.js @@ -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() @@ -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() diff --git a/test/templates/create.spec.js b/test/templates/create.spec.js index cb71b9c..7f25aab 100644 --- a/test/templates/create.spec.js +++ b/test/templates/create.spec.js @@ -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() diff --git a/test/templates/get.spec.js b/test/templates/get.spec.js index 342ec95..91754ac 100644 --- a/test/templates/get.spec.js +++ b/test/templates/get.spec.js @@ -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() diff --git a/test/transactions/get.spec.js b/test/transactions/get.spec.js index dc07772..4f262d8 100644 --- a/test/transactions/get.spec.js +++ b/test/transactions/get.spec.js @@ -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() @@ -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() diff --git a/test/vouchers/create.spec.js b/test/vouchers/create.spec.js index 0ab02aa..5c7c127 100644 --- a/test/vouchers/create.spec.js +++ b/test/vouchers/create.spec.js @@ -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() diff --git a/test/vouchers/get.logs.spec.js b/test/vouchers/get.logs.spec.js index 32fe4b8..42c90bb 100644 --- a/test/vouchers/get.logs.spec.js +++ b/test/vouchers/get.logs.spec.js @@ -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() diff --git a/test/vouchers/get.spec.js b/test/vouchers/get.spec.js index f505f0e..9f7badb 100644 --- a/test/vouchers/get.spec.js +++ b/test/vouchers/get.spec.js @@ -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()