Skip to content

Commit cb51106

Browse files
author
Olaf Kwant
committed
v2 sdk client
1 parent 23eaab8 commit cb51106

File tree

3 files changed

+142
-3
lines changed

3 files changed

+142
-3
lines changed

lib/client_v2.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import Client from './client'
2+
3+
const _cache = {}
4+
export const tools = {
5+
unqiue: function (obj) {
6+
return Object.keys(obj).map((key) => {
7+
const value = obj[key]
8+
return (typeof value === 'object') ? tools.unqiue(value) : `${key}:${value}`
9+
}).join(' ')
10+
},
11+
12+
cache: function (key, value) {
13+
if (value === undefined) {
14+
return _cache[key]
15+
} else {
16+
_cache[key] = value
17+
return value
18+
}
19+
}
20+
}
21+
22+
export default class ClientV2 extends Client {
23+
get (stringOrArray) {
24+
if (typeof stringOrArray !== 'string' && !Array.isArray(stringOrArray)) {
25+
throw new Error('Type for get not supported, get expects String or Array of Strings')
26+
}
27+
28+
return super.get(stringOrArray).then((data) => {
29+
let error, str, arr
30+
31+
if (typeof stringOrArray === 'string') {
32+
str = stringOrArray
33+
} else {
34+
arr = stringOrArray
35+
}
36+
37+
if (str) {
38+
if (data[str]) {
39+
return data[str]
40+
} else if (data.errors[str]) {
41+
error = new Error(data.errors[str].message)
42+
return error
43+
}
44+
} else {
45+
return arr.reduce((returnValue, key) => {
46+
if (data[key]) {
47+
returnValue.push(data[key])
48+
} else if (data.errors[key]) {
49+
error = new Error(data.errors[key].message)
50+
returnValue.push(error)
51+
} else {
52+
returnValue.push(undefined)
53+
}
54+
return returnValue
55+
}, [])
56+
}
57+
})
58+
}
59+
60+
request (obj) {
61+
const isCached = !!obj.cachable
62+
delete obj.cachable
63+
const cacheName = tools.unqiue(obj)
64+
65+
if (isCached) return tools.cache(cacheName) || tools.cache(cacheName, super.request(obj))
66+
else return super.request(obj)
67+
}
68+
}

lib/index.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import Client from './client'
2+
import ClientV2 from './client_v2'
23
import { queryParameters } from './utils'
34

45
/**
@@ -35,10 +36,10 @@ const ZAFClient = {
3536
const appGuid = queryParams.app_guid || hashParams.app_guid
3637
if (!origin || !appGuid) { return false }
3738

38-
const client = new Client({ origin, appGuid })
39+
const client = (callbackOrV2 === 'v2') ? new ClientV2({ origin, appGuid }) : new Client({ origin, appGuid })
3940

40-
if (typeof callback === 'function') {
41-
client.on('app.registered', callback.bind(client))
41+
if (typeof callbackOrV2 === 'function') {
42+
client.on('app.registered', callbackOrV2.bind(client))
4243
}
4344

4445
return client

spec/client_v2_spec.js

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/* eslint-env mocha */
2+
/* globals assert */
3+
import ClientV2, { tools } from '../lib/client_v2'
4+
import sinon from 'sinon'
5+
6+
describe('ClientV2', () => {
7+
const sandbox = sinon.createSandbox()
8+
const origin = 'https://foo.zendesk.com'
9+
const appGuid = 'ABC123'
10+
let subject, source
11+
12+
before(() => {
13+
sandbox.stub(window, 'addEventListener')
14+
sandbox.stub(window, 'postMessage')
15+
source = { postMessage: sandbox.stub() }
16+
subject = new ClientV2({ origin, appGuid, source })
17+
})
18+
19+
after(() => {
20+
sandbox.restore()
21+
})
22+
23+
describe('request', () => {
24+
let cacheSpy
25+
26+
before(() => {
27+
cacheSpy = sandbox.stub(tools, 'cache')
28+
})
29+
30+
it('1', () => {
31+
subject.request({
32+
url: '/api/v2/tickets',
33+
cachable: true
34+
})
35+
assert(cacheSpy.withArgs('url:/api/v2/tickets').called)
36+
})
37+
38+
it('2', () => {
39+
subject.request({
40+
type: 'get',
41+
url: '/api/v2/tickets',
42+
cachable: true
43+
})
44+
assert(cacheSpy.withArgs('type:get url:/api/v2/tickets').called)
45+
})
46+
47+
it('3', () => {
48+
subject.request({
49+
url: '/api/v2/tickets',
50+
data: {
51+
test: true
52+
},
53+
cachable: true
54+
})
55+
assert(cacheSpy.withArgs('url:/api/v2/tickets test:true').called)
56+
})
57+
58+
it('4', () => {
59+
subject.request({
60+
url: '/api/v2/tickets',
61+
data: {
62+
test: true
63+
},
64+
other: function () {},
65+
cachable: true
66+
})
67+
assert(cacheSpy.withArgs('url:/api/v2/tickets test:true other:function () {}').called)
68+
})
69+
})
70+
})

0 commit comments

Comments
 (0)