-
Notifications
You must be signed in to change notification settings - Fork 2
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
Showing
2 changed files
with
224 additions
and
0 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,117 @@ | ||
const Joi = require('@hapi/joi'); | ||
const { validate, prepare } = require('../../utils/options'); | ||
|
||
// Validation | ||
const _id = Joi.number().min(1); | ||
const _data = Joi.object(); | ||
|
||
// Initialize Endpoint | ||
module.exports = (options = {}) => { | ||
const { error } = validate(options); | ||
if (error) throw new Error(error.details[0].message); | ||
|
||
const { url, headers } = prepare(options); | ||
|
||
return { | ||
/** | ||
* List Locales | ||
* | ||
* GET /api/v2/locales.json | ||
* https://developer.zendesk.com/rest_api/docs/support/locales#list-locales | ||
*/ | ||
list: () => { | ||
// Ignore any options | ||
return { | ||
method: 'GET', | ||
url: `${url}/api/v2/locales.json`, | ||
headers | ||
}; | ||
}, | ||
|
||
/** | ||
* List Available Public Locales | ||
* | ||
* GET /api/v2/locales/public.json | ||
* https://developer.zendesk.com/rest_api/docs/support/locales#list-available-public-locales | ||
*/ | ||
public: () => { | ||
// Ignore any options | ||
return { | ||
method: 'GET', | ||
url: `${url}/api/v2/locales/public.json`, | ||
headers | ||
}; | ||
}, | ||
|
||
/** | ||
* List Locales for Agent | ||
* | ||
* GET /api/v2/locales/agent.json | ||
* https://developer.zendesk.com/rest_api/docs/support/locales#list-locales-for-agent | ||
*/ | ||
agent: () => { | ||
// Ignore any options | ||
return { | ||
method: 'GET', | ||
url: `${url}/api/v2/locales/agent.json`, | ||
headers | ||
}; | ||
}, | ||
|
||
/** | ||
* Show Locale | ||
* | ||
* GET /api/v2/locales/{id}.json | ||
* https://developer.zendesk.com/rest_api/docs/support/locales#show-locale | ||
*/ | ||
show: (options = {}) => { | ||
const { error } = Joi.object({ | ||
id: _id.required() | ||
}).validate(options); | ||
if (error) throw new Error(error.details[0].message); | ||
|
||
const { id } = options; | ||
return { | ||
method: 'GET', | ||
url: `${url}/api/v2/locales/${id}.json`, | ||
headers | ||
}; | ||
}, | ||
|
||
/** | ||
* Show Current Locale | ||
* | ||
* GET /api/v2/locales/current.json | ||
* https://developer.zendesk.com/rest_api/docs/support/locales#show-current-locale | ||
*/ | ||
current: () => { | ||
// Ignore any options | ||
return { | ||
method: 'GET', | ||
url: `${url}/api/v2/locales/current.json`, | ||
headers | ||
}; | ||
}, | ||
|
||
/** | ||
* Detect best language for user | ||
* | ||
* GET /api/v2/locales/detect_best_locale.json | ||
* https://developer.zendesk.com/rest_api/docs/support/locales#detect-best-language-for-user | ||
*/ | ||
detect: (options = {}) => { | ||
const { error } = Joi.object({ | ||
data: _data.required() | ||
}).validate(options); | ||
if (error) throw new Error(error.details[0].message); | ||
|
||
const { data } = options; | ||
return { | ||
method: 'GET', | ||
url: `${url}/api/v2/locales/detect_best_locale.json`, | ||
headers, | ||
data | ||
}; | ||
} | ||
}; | ||
}; |
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,107 @@ | ||
const endpoint = require('../../../../src/api/support/locales'); | ||
const { prepare } = require('../../../../src/utils/options'); | ||
|
||
describe('Locales', () => { | ||
let endPoint, options, url, headers; | ||
|
||
beforeEach(() => { | ||
options = { | ||
instance: 'instance', | ||
email: 'user@email.com', | ||
token: 'token' | ||
}; | ||
endPoint = endpoint(options); | ||
({ url, headers } = prepare(options)); | ||
}); | ||
|
||
afterEach(() => { | ||
options = null; | ||
endPoint = null; | ||
url = null; | ||
headers = null; | ||
}); | ||
|
||
describe('init', () => { | ||
it('should setup endpoint object', () => { | ||
expect(endpoint(options)).toBeTruthy(); | ||
}); | ||
|
||
it('should fail with invalid input', () => { | ||
expect(() => endpoint()).toThrowError(); | ||
expect(() => endpoint({})).toThrowError(); | ||
}); | ||
}); | ||
|
||
describe('List Locales', () => { | ||
it('should process w/ valid input', () => { | ||
expect(endPoint.list()).toEqual({ | ||
method: 'GET', | ||
url: `${url}/api/v2/locales.json`, | ||
headers | ||
}); | ||
}); | ||
}); | ||
|
||
describe('List Available Public Locales', () => { | ||
it('should process w/ valid input', () => { | ||
expect(endPoint.public()).toEqual({ | ||
method: 'GET', | ||
url: `${url}/api/v2/locales/public.json`, | ||
headers | ||
}); | ||
}); | ||
}); | ||
|
||
describe('List Locales for Agent', () => { | ||
it('should process w/ valid input', () => { | ||
expect(endPoint.agent()).toEqual({ | ||
method: 'GET', | ||
url: `${url}/api/v2/locales/agent.json`, | ||
headers | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Show Locale', () => { | ||
it('should process w/ valid input', () => { | ||
expect(endPoint.show({ id: 123 })).toEqual({ | ||
method: 'GET', | ||
url: `${url}/api/v2/locales/123.json`, | ||
headers | ||
}); | ||
}); | ||
|
||
it('should throw error w/ invalid input', () => { | ||
expect(() => endPoint.show()).toThrowError(); | ||
expect(() => endPoint.show({})).toThrowError(); | ||
expect(() => endPoint.show({ id: 'invalid' })).toThrowError(); | ||
}); | ||
}); | ||
|
||
describe('Show Current Locale', () => { | ||
it('should process w/ valid input', () => { | ||
expect(endPoint.current()).toEqual({ | ||
method: 'GET', | ||
url: `${url}/api/v2/locales/current.json`, | ||
headers | ||
}); | ||
}); | ||
}); | ||
|
||
describe('Detect best language for user', () => { | ||
it('should process w/ valid input', () => { | ||
expect(endPoint.detect({ data: {} })).toEqual({ | ||
method: 'GET', | ||
url: `${url}/api/v2/locales/detect_best_locale.json`, | ||
headers, | ||
data: {} | ||
}); | ||
}); | ||
|
||
it('should throw error w/ invalid input', () => { | ||
expect(() => endPoint.detect()).toThrowError(); | ||
expect(() => endPoint.detect({})).toThrowError(); | ||
expect(() => endPoint.detect({ data: 'invalid' })).toThrowError(); | ||
}); | ||
}); | ||
}); |