diff --git a/src/v2/index.js b/src/v2/index.js index 23b16dc..c7b28df 100644 --- a/src/v2/index.js +++ b/src/v2/index.js @@ -1,3 +1,4 @@ module.exports = ({ instance, headers }) => ({ + sunshine: require('./sunshine')({ instance, headers }), support: require('./support')({ instance, headers }) }); diff --git a/src/v2/sunshine/index.js b/src/v2/sunshine/index.js new file mode 100644 index 0000000..63879e8 --- /dev/null +++ b/src/v2/sunshine/index.js @@ -0,0 +1,3 @@ +module.exports = ({ instance, headers }) => ({ + object_types: require('./object_types')({ instance, headers }) +}); diff --git a/src/v2/sunshine/object_types.js b/src/v2/sunshine/object_types.js new file mode 100644 index 0000000..260d9b9 --- /dev/null +++ b/src/v2/sunshine/object_types.js @@ -0,0 +1,87 @@ +const Joi = require('@hapi/joi'); + +const _key = Joi.string() + .min(2) + .max(32); +const _data = Joi.object({ + key: _key, + schema: Joi.object({ + properties: Joi.object(), + required: Joi.array() + }), + end_users_can_read: Joi.bool() +}); + +module.exports = ({ instance, headers }) => { + const url = `https://${instance}.zendesk.com`; + + return { + list: () => { + // Ignore any options + return { + method: 'GET', + url: `${url}/api/sunshine/objects/types`, + headers + }; + }, + + show: (options = {}) => { + const { error } = Joi.object({ + key: _key.required() + }).validate(options); + if (error) throw new Error(error.details[0].message); + + const { key } = options; + return { + method: 'GET', + url: `${url}/api/sunshine/objects/types/${key}`, + headers + }; + }, + + create: (options = {}) => { + const { error } = Joi.object({ + data: _data.required() + }).validate(options); + if (error) throw new Error(error.details[0].message); + + const { data } = options; + return { + method: 'POST', + url: `${url}/api/sunshine/objects/types`, + headers, + data + }; + }, + + update: (options = {}) => { + const { error } = Joi.object({ + key: _key.required(), + data: _data.required() + }).validate(options); + if (error) throw new Error(error.details[0].message); + + const { key, data } = options; + return { + method: 'PUT', + url: `${url}/api/sunshine/objects/types/${key}`, + headers, + data + }; + }, + + delete: (options = {}) => { + const { error } = Joi.object({ + key: _key.required() + }).validate(options); + if (error) throw new Error(error.details[0].message); + + const { key } = options; + return { + method: 'DELETE', + url: `${url}/api/sunshine/objects/types/${key}`, + headers + }; + } + }; +}; diff --git a/tests/src/v2/sunshine/object_types.test.js b/tests/src/v2/sunshine/object_types.test.js new file mode 100644 index 0000000..db03737 --- /dev/null +++ b/tests/src/v2/sunshine/object_types.test.js @@ -0,0 +1,107 @@ +const endpoint = require('../../../../src/v2/sunshine/object_types'); + +const instance = 'instance'; +const url = `https://${instance}.zendesk.com`; +const headers = { + 'Content-Type': 'application/json', + Authorization: 'Basic <64bit_encoded_credentials>' +}; + +describe('object types', () => { + let objectTypes; + + beforeEach(() => (objectTypes = endpoint({ instance, headers }))); + afterEach(() => (objectTypes = null)); + + describe('list object types', () => { + it('should process, ignores additional input', () => { + expect(objectTypes.list()).toEqual({ + method: 'GET', + url: `${url}/api/sunshine/objects/types`, + headers + }); + }); + }); + + describe('show object type', () => { + it('should process w/ valid input', () => { + expect(objectTypes.show({ key: 'valid' })).toEqual({ + method: 'GET', + url: `${url}/api/sunshine/objects/types/valid`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => objectTypes.show()).toThrowError(); + expect(() => objectTypes.show({})).toThrowError(); + expect(() => objectTypes.show('invalid')).toThrowError(); + expect(() => objectTypes.show({ key: 123 })).toThrowError(); + }); + }); + + describe('create object type', () => { + it('should process w/ valid input', () => { + const data = { + key: 'valid', + schema: { + properties: {}, + required: ['valid'] + }, + end_users_can_read: false + }; + + expect(objectTypes.create({ data })).toEqual({ + method: 'POST', + url: `${url}/api/sunshine/objects/types`, + headers, + data + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => objectTypes.create()).toThrowError(); + expect(() => objectTypes.create({})).toThrowError(); + expect(() => objectTypes.create('invalid')).toThrowError(); + expect(() => objectTypes.create({ data: 'invalid' })).toThrowError(); + }); + }); + + describe('update custom role', () => { + it('should process w/ valid input', () => { + expect(objectTypes.update({ key: 'valid', data: {} })).toEqual({ + method: 'PUT', + url: `${url}/api/sunshine/objects/types/valid`, + headers, + data: {} + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => objectTypes.update()).toThrowError(); + expect(() => objectTypes.update({})).toThrowError(); + expect(() => objectTypes.update('invalid')).toThrowError(); + expect(() => objectTypes.update({ key: 0 })).toThrowError(); + expect(() => + objectTypes.update({ key: 'valid', data: 'invalid' }) + ).toThrowError(); + }); + }); + + describe('delete custom role', () => { + it('should process w/ valid input', () => { + expect(objectTypes.delete({ key: 'valid' })).toEqual({ + method: 'DELETE', + url: `${url}/api/sunshine/objects/types/valid`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => objectTypes.delete()).toThrowError(); + expect(() => objectTypes.delete({})).toThrowError(); + expect(() => objectTypes.delete('invalid')).toThrowError(); + expect(() => objectTypes.delete({ key: 123 })).toThrowError(); + }); + }); +});