Skip to content

Commit

Permalink
v2 client
Browse files Browse the repository at this point in the history
  • Loading branch information
Olaf Kwant committed Oct 23, 2018
1 parent 3cb0c30 commit a189957
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 4 deletions.
68 changes: 68 additions & 0 deletions lib/client_v2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import Client from './client'

const _cache = {}
export const tools = {
unqiue: function (obj) {
return Object.keys(obj).map((key) => {
const value = obj[key]
return (typeof value === 'object') ? tools.unqiue(value) : `${key}:${value}`
}).join(' ')
},

cache: function (key, value) {
if (value === undefined) {
return _cache[key]
} else {
_cache[key] = value
return value
}
}
}

export default class ClientV2 extends Client {
get (stringOrArray) {
if (typeof stringOrArray !== 'string' && !Array.isArray(stringOrArray)) {
throw new Error('Type for get not supported, get expects String or Array of Strings')
}

return super.get(stringOrArray).then((data) => {
let error, str, arr

if (typeof stringOrArray === 'string') {
str = stringOrArray
} else {
arr = stringOrArray
}

if (str) {
if (data[str]) {
return data[str]
} else if (data.errors[str]) {
error = new Error(data.errors[str].message)
return error
}
} else {
return arr.reduce((returnValue, key) => {
if (data[key]) {
returnValue.push(data[key])
} else if (data.errors[key]) {
error = new Error(data.errors[key].message)
returnValue.push(error)
} else {
returnValue.push(undefined)
}
return returnValue
}, [])
}
})
}

request (obj) {
const isCached = !!obj.cachable
delete obj.cachable
const cacheName = tools.unqiue(obj)

if (isCached) return tools.cache(cacheName) || tools.cache(cacheName, super.request(obj))
else return super.request(obj)
}
}
9 changes: 5 additions & 4 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Client from './client'
import ClientV2 from './client_v2'
import { queryParameters } from './utils'

/**
Expand Down Expand Up @@ -26,18 +27,18 @@ import { queryParameters } from './utils'
* ```
*/
const ZAFClient = {
init: function (callback, loc) {
init: function (callbackOrV2, loc) {
loc = loc || window.location
const queryParams = queryParameters(loc.search)
const hashParams = queryParameters(loc.hash)
const origin = queryParams.origin || hashParams.origin
const appGuid = queryParams.app_guid || hashParams.app_guid
if (!origin || !appGuid) { return false }

const client = new Client({ origin, appGuid })
const client = (callbackOrV2 === 'v2') ? new ClientV2({ origin, appGuid }) : new Client({ origin, appGuid })

if (typeof callback === 'function') {
client.on('app.registered', callback.bind(client))
if (typeof callbackOrV2 === 'function') {
client.on('app.registered', callbackOrV2.bind(client))
}

return client
Expand Down
70 changes: 70 additions & 0 deletions spec/client_v2_spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/* eslint-env mocha */
/* globals assert */
import ClientV2, { tools } from '../lib/client_v2'
import sinon from 'sinon'

describe('ClientV2', () => {
const sandbox = sinon.createSandbox()
const origin = 'https://foo.zendesk.com'
const appGuid = 'ABC123'
let subject, source

before(() => {
sandbox.stub(window, 'addEventListener')
sandbox.stub(window, 'postMessage')
source = { postMessage: sandbox.stub() }
subject = new ClientV2({ origin, appGuid, source })
})

after(() => {
sandbox.restore()
})

describe('request', () => {
let cacheSpy

before(() => {
cacheSpy = sandbox.stub(tools, 'cache')
})

it('1', () => {
subject.request({
url: '/api/v2/tickets',
cachable: true
})
assert(cacheSpy.withArgs('url:/api/v2/tickets').called)
})

it('2', () => {
subject.request({
type: 'get',
url: '/api/v2/tickets',
cachable: true
})
assert(cacheSpy.withArgs('type:get url:/api/v2/tickets').called)
})

it('3', () => {
subject.request({
url: '/api/v2/tickets',
data: {
test: true
},
cachable: true
})
assert(cacheSpy.withArgs('url:/api/v2/tickets test:true').called)
})

it('4', () => {
subject.request({
url: '/api/v2/tickets',
data: {
test: true
},
other: function () {},
cachable: true
})
assert(cacheSpy.withArgs('url:/api/v2/tickets test:true other:function () {}').called)
})
})
})

0 comments on commit a189957

Please sign in to comment.