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 configuration patch route #4

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions lib/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ const constants = {
UNINSTANTIATED_HTTP_CLIENT: {
code: 400,
message: 'cannot instantiate API without instantiated HTTP client'
},
CONFLICT_OBJECTS: {
code: 409,
message: 'could not generate JSON patch'
}
}

Expand Down
16 changes: 14 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { Auth } = require('./v1/auth')
const { Transactions } = require('./v0/transactions')
const { Products } = require('./v1/products')
const { Carts } = require('./v1/carts')
const { Configurations } = require('./v1/configurations')

/**
* Tillhub SDK instance. The instance handles auth autonomously.
Expand Down Expand Up @@ -46,7 +47,7 @@ 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 a Promise will be returned
*/
init (callback) {
if (!this.options.credentials) {
Expand Down Expand Up @@ -93,12 +94,23 @@ class Tillhub extends EventEmitter {
* Create an authenticated carts instance
*
* @param {Object} options options object
* @return {Cart} cart instance
* @return {Carts} cart instance
*/
carts (options) {
if (!this.http) throw errors.generate(errors.constants.UNINSTANTIATED_HTTP_CLIENT)
return new Carts({ user: this.user, base: this.options.base, ...options }, this.http)
}

/**
* Create an authenticated configurations instance
*
* @param {Object} options options object
* @return {Configurations} configuration instance
*/
configurations (options) {
if (!this.http) throw errors.generate(errors.constants.UNINSTANTIATED_HTTP_CLIENT)
return new Configurations({ user: this.user, base: this.options.base, ...options }, this.http)
}
}

module.exports = Tillhub
Expand Down
105 changes: 105 additions & 0 deletions lib/v1/configurations.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
const EventEmitter = require('events')
const typeOf = require('just-typeof')
const errors = require('../errors')

const { diff, jsonPatchPathConverter } = require('just-diff')

/**
* Handles configurations from the v1 Model
*
* @class
*
* @example
* const Tillhub = require('@tillhub/node-sdk')
*
* const th = new Tillhub({
* credentials: {
* username: 'user@example.com',
* password: '123455'
* }
* })
*
* await th.init()
* const config = th.configurations()
*
* const { data, metadata } = await config.patch()
* console.log(data) // [...]
*/
class Configurations extends EventEmitter {
constructor (options = {}, http) {
super()
this.options = {
...options
}

this.endpoint = '/api/v1/configurations'
this.http = http
}

/**
* Update configuration partially
*
* @param {originalObject} originalObject target object to modify
* @param {modifiedObject} modifiedObject expected object after being modified
* @param {objects|Function} [queryOrCallback] query for configurations with allowed parameters, or specify an optional callback
* @param {Function} [callback] optional callback. If not specified, this function returns a promise
*/
patch (originalObject, modifiedObject, queryOrCallback, callback) {
if (!callback && typeof queryOrCallback === 'function') {
callback = queryOrCallback
queryOrCallback = {}
}

if (typeOf(queryOrCallback) !== 'object') {
return callback(new TypeError('Tillhub: query parameter must be object'))
}

if (!queryOrCallback.id) {
return callback(new ReferenceError(`Tillhub: query 'id' must be provided`))
}

let patchBody
try {
patchBody = diff(originalObject, modifiedObject, jsonPatchPathConverter)
} catch (e) {
return callback(errors.generate(errors.constants.CONFLICT_OBJECTS))
}

let requestOptions = {
headers: {
'content-type': 'application/json-patch+json'
},
method: 'PATCH',
json: true
}

if (!queryOrCallback.uri) {
requestOptions = {
...requestOptions,
uri: `${this.endpoint}/${this.options.user}/${queryOrCallback.id}`,
baseUrl: this.options.base,
body: patchBody
}
} else {
requestOptions = {
...requestOptions,
uri: queryOrCallback.uri,
body: patchBody
}
}

this.http(requestOptions, (err, resp, body) => {
if (err) return callback(err)

if (resp.statusCode === 200) {
return callback(null, body, resp)
}

return callback(new Error('Tillhub: could not get configurations patch'), body, resp)
})
}
}

module.exports = {
Configurations
}
3 changes: 2 additions & 1 deletion lib/v1/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module.exports = {
Auth: require('./auth').Auth,
Products: require('./products').Products
Products: require('./products').Products,
Configurations: require('./configurations').Configurations
}
2 changes: 1 addition & 1 deletion lib/v1/products.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class Products extends EventEmitter {
/**
* Get all products from client account.
*
* @param {Object|Function} [queryOrCallback] query for products with allowed paramaters, or specify an optional callback
* @param {Object|Function} [queryOrCallback] query for products with allowed parameters, or specify an optional callback
* @param {Function} [callback] optional callback. If not specified, this function returns a promise
*/
getAll (queryOrCallback, callback) {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@
"@google-cloud/pubsub": "^0.18.0",
"http-errors": "^1.6.3",
"is": "^3.2.1",
"just-diff": "^2.2.0",
"just-safe-set": "^2.0.1",
"just-typeof": "^1.0.5",
"pump": "^3.0.0",
Expand Down
Loading