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

v2 sdk client (proposal) #72

Open
wants to merge 1 commit 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
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 @@ -27,18 +28,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)
})
})
})