diff --git a/src/api/support/support_addresses.js b/src/api/support/support_addresses.js new file mode 100644 index 0000000..720375e --- /dev/null +++ b/src/api/support/support_addresses.js @@ -0,0 +1,134 @@ +const Joi = require('@hapi/joi'); +const { validate, prepare } = require('../../utils/options'); + +// Validation +const _id = Joi.number().positive(); +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 Support Addresses + * + * GET /api/v2/recipient_addresses.json + * https://developer.zendesk.com/rest_api/docs/support/support_addresses#list-support-addresses + */ + list: () => { + // Ignore any options + return { + method: 'GET', + url: `${url}/api/v2/recipient_addresses.json`, + headers + }; + }, + + /** + * Show Support Address + * + * GET /api/v2/recipient_addresses/{id}.json + * https://developer.zendesk.com/rest_api/docs/support/support_addresses#show-support-address + */ + 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/recipient_addresses/${id}.json`, + headers + }; + }, + + /** + * Create Support Address + * + * POST /api/v2/recipient_addresses.json + * https://developer.zendesk.com/rest_api/docs/support/support_addresses#create-support-address + */ + 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/v2/recipient_addresses.json`, + headers, + data + }; + }, + + /** + * Update Support Address + * + * PUT /api/v2/recipient_addresses/{id}.json + * https://developer.zendesk.com/rest_api/docs/support/support_addresses#update-support-address + */ + update: (options = {}) => { + const { error } = Joi.object({ + id: _id.required(), + data: _data.required() + }).validate(options); + if (error) throw new Error(error.details[0].message); + + const { id, data } = options; + return { + method: 'PUT', + url: `${url}/api/v2/recipient_addresses/${id}.json`, + headers, + data + }; + }, + + /** + * Verify Support Address Forwarding + * + * PUT /api/v2/recipient_addresses/{id}/verify.json + * https://developer.zendesk.com/rest_api/docs/support/support_addresses#verify-support-address-forwarding + */ + verify: (options = {}) => { + const { error } = Joi.object({ + id: _id.required() + }).validate(options); + if (error) throw new Error(error.details[0].message); + + const { id } = options; + return { + method: 'PUT', + url: `${url}/api/v2/recipient_addresses/${id}/verify.json`, + headers + }; + }, + + /** + * Delete Recipient Address + * + * DELETE /api/v2/recipient_addresses/{id}.json + * https://developer.zendesk.com/rest_api/docs/support/support_addresses#delete-recipient-address + */ + delete: (options = {}) => { + const { error } = Joi.object({ + id: _id.required() + }).validate(options); + if (error) throw new Error(error.details[0].message); + + const { id } = options; + return { + method: 'DELETE', + url: `${url}/api/v2/recipient_addresses/${id}.json`, + headers + }; + } + }; +}; diff --git a/tests/src/api/support/support_addresses.test.js b/tests/src/api/support/support_addresses.test.js new file mode 100644 index 0000000..4d27508 --- /dev/null +++ b/tests/src/api/support/support_addresses.test.js @@ -0,0 +1,134 @@ +const endpoint = require('../../../../src/api/support/support_addresses'); +const { prepare } = require('../../../../src/utils/options'); + +describe('Support Addresses', () => { + 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 Support Addresses', () => { + it('should process w/ valid input', () => { + expect(endPoint.list()).toEqual({ + method: 'GET', + url: `${url}/api/v2/recipient_addresses.json`, + headers + }); + }); + }); + + describe('Show Support Address', () => { + it('should process w/ valid input', () => { + expect(endPoint.show({ id: 123 })).toEqual({ + method: 'GET', + url: `${url}/api/v2/recipient_addresses/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('Create Support Address', () => { + it('should process w/ valid input', () => { + expect(endPoint.create({ data: {} })).toEqual({ + method: 'POST', + url: `${url}/api/v2/recipient_addresses.json`, + headers, + data: {} + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => endPoint.create()).toThrowError(); + expect(() => endPoint.create('invalid')).toThrowError(); + expect(() => endPoint.create({})).toThrowError(); + expect(() => endPoint.create({ data: 'invalid' })).toThrowError(); + }); + }); + + describe('Update Support Address', () => { + it('should process w/ valid input', () => { + expect(endPoint.update({ id: 123, data: {} })).toEqual({ + method: 'PUT', + url: `${url}/api/v2/recipient_addresses/123.json`, + headers, + data: {} + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => endPoint.update()).toThrowError(); + expect(() => endPoint.update({})).toThrowError(); + expect(() => endPoint.update({ data: {} })).toThrowError(); + expect(() => endPoint.update({ id: 'invalid', data: {} })).toThrowError(); + expect(() => + endPoint.update({ id: 123, data: 'invalid' }) + ).toThrowError(); + }); + }); + + describe('Verify Support Address Forwarding', () => { + it('should process w/ valid input', () => { + expect(endPoint.verify({ id: 123 })).toEqual({ + method: 'PUT', + url: `${url}/api/v2/recipient_addresses/123/verify.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => endPoint.verify()).toThrowError(); + expect(() => endPoint.verify({})).toThrowError(); + expect(() => endPoint.verify({ data: {} })).toThrowError(); + expect(() => endPoint.verify({ id: 'invalid', data: {} })).toThrowError(); + }); + }); + + describe('Delete Recipient Address', () => { + it('should process w/ valid input', () => { + expect(endPoint.delete({ id: 123 })).toEqual({ + method: 'DELETE', + url: `${url}/api/v2/recipient_addresses/123.json`, + headers + }); + }); + + it('should throw error w/ invalid input', () => { + expect(() => endPoint.delete()).toThrowError(); + expect(() => endPoint.delete('invalid')).toThrowError(); + expect(() => endPoint.delete({})).toThrowError(); + expect(() => endPoint.delete({ id: 'invalid' })).toThrowError(); + expect(() => endPoint.delete({ id: 0 })).toThrowError(); + }); + }); +});