-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Olaf Kwant
committed
Dec 3, 2018
1 parent
23eaab8
commit f05cda7
Showing
3 changed files
with
143 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) | ||
}) | ||
}) |