From c9b52ca05c0209a8d2f36df8fcf7b787801b703c Mon Sep 17 00:00:00 2001 From: Farrin Reid Date: Thu, 1 Aug 2024 18:10:26 -0700 Subject: [PATCH] chore: update xo and fix lint --- examples/groupmemberships-list-group.js | 25 ++++++++++++--- examples/groupmemberships-list-user.js | 25 ++++++++++++--- examples/ticketaudits-list.js | 25 ++++++++++++--- examples/users-listbygroup.js | 31 +++++++++++++------ examples/usertags-list.js | 31 +++++++++++++------ package.json | 6 ++-- src/clients/client.js | 41 +++++++++++++++---------- src/clients/core/users.js | 24 +++++++-------- src/clients/helpers.js | 8 ++--- src/clients/throttle.js | 15 ++++----- src/index.js | 12 +++----- 11 files changed, 159 insertions(+), 84 deletions(-) diff --git a/examples/groupmemberships-list-group.js b/examples/groupmemberships-list-group.js index 50089349..a8a2a5f7 100644 --- a/examples/groupmemberships-list-group.js +++ b/examples/groupmemberships-list-group.js @@ -8,9 +8,24 @@ const client = zd.createClient({ remoteUri: process.env.ZENDESK_TEST_REMOTEURI || exampleConfig.auth.remoteUri, }); -client.groups.list().then(function (groups) { - const group = groups[0]; - client.groupmemberships.listByGroup(group.id).then(function (memberships) { +/** + * + */ +async function listGroupMemberships() { + try { + const groups = await client.groups.list(); + const group = groups[0]; + + if (!group) { + console.log('No groups found.'); + return; + } + + const memberships = await client.groupmemberships.listByGroup(group.id); console.log(JSON.stringify(memberships)); - }); -}); + } catch (error) { + console.error('Error fetching group memberships:', error); + } +} + +listGroupMemberships(); diff --git a/examples/groupmemberships-list-user.js b/examples/groupmemberships-list-user.js index e593d86b..9dbdc4c0 100644 --- a/examples/groupmemberships-list-user.js +++ b/examples/groupmemberships-list-user.js @@ -8,9 +8,24 @@ const client = zd.createClient({ remoteUri: process.env.ZENDESK_TEST_REMOTEURI || exampleConfig.auth.remoteUri, }); -client.users.list().then(function (users) { - const user = users[0]; - client.groupmemberships.listByUser(user.id).then(function (memberships) { +/** + * + */ +async function listUserMemberships() { + try { + const users = await client.users.list(); + const user = users[0]; + + if (!user) { + console.log('No users found.'); + return; + } + + const memberships = await client.groupmemberships.listByUser(user.id); console.log(JSON.stringify(memberships)); - }); -}); + } catch (error) { + console.error('Error fetching user memberships:', error); + } +} + +listUserMemberships(); diff --git a/examples/ticketaudits-list.js b/examples/ticketaudits-list.js index 9f32728e..4a2dc1c0 100644 --- a/examples/ticketaudits-list.js +++ b/examples/ticketaudits-list.js @@ -8,9 +8,24 @@ const client = zd.createClient({ remoteUri: process.env.ZENDESK_TEST_REMOTEURI || exampleConfig.auth.remoteUri, }); -client.tickets.list().then(function (tickets) { - const ticket = tickets[0]; - client.ticketaudits.list(ticket.id).then(function (audits) { +/** + * + */ +async function listTicketAudits() { + try { + const tickets = await client.tickets.list(); + const ticket = tickets[0]; + + if (!ticket) { + console.log('No tickets found.'); + return; + } + + const audits = await client.ticketaudits.list(ticket.id); console.log(JSON.stringify(audits)); - }); -}); + } catch (error) { + console.error('Error fetching ticket audits:', error); + } +} + +listTicketAudits(); diff --git a/examples/users-listbygroup.js b/examples/users-listbygroup.js index fe6ef176..35526f1a 100644 --- a/examples/users-listbygroup.js +++ b/examples/users-listbygroup.js @@ -8,13 +8,24 @@ const client = zd.createClient({ remoteUri: process.env.ZENDESK_TEST_REMOTEURI || exampleConfig.auth.remoteUri, }); -client.groups - .list() - .then(function (result) { - client.users.listByGroup(result[0].id).then(function (users) { - console.log(users); - }); - }) - .catch(function (error) { - console.log(error); - }); +/** + * + */ +async function listUsersByFirstGroup() { + try { + const groups = await client.groups.list(); + const firstGroup = groups[0]; + + if (!firstGroup) { + console.log('No groups found.'); + return; + } + + const users = await client.users.listByGroup(firstGroup.id); + console.log(users); + } catch (error) { + console.error(error); + } +} + +listUsersByFirstGroup(); diff --git a/examples/usertags-list.js b/examples/usertags-list.js index c4951eaf..11f9a71f 100644 --- a/examples/usertags-list.js +++ b/examples/usertags-list.js @@ -8,13 +8,24 @@ const client = zd.createClient({ remoteUri: process.env.ZENDESK_TEST_REMOTEURI || exampleConfig.auth.remoteUri, }); -client.users - .list() - .then(function (result) { - client.users.listTags(result[0].id).then(function (tags) { - console.log(tags); - }); - }) - .catch(function (error) { - console.log(error); - }); +/** + * + */ +async function listTagsForFirstUser() { + try { + const users = await client.users.list(); + const firstUser = users[0]; + + if (!firstUser) { + console.log('No users found.'); + return; + } + + const tags = await client.users.listTags(firstUser.id); + console.log(tags); + } catch (error) { + console.error(error); + } +} + +listTagsForFirstUser(); diff --git a/package.json b/package.json index ffd387d2..082c7fc6 100644 --- a/package.json +++ b/package.json @@ -157,10 +157,10 @@ "nock": "^13.5.0", "typedoc": "^0.26.5", "typedoc-plugin-markdown": "^4.2.3", - "vitepress": "1.3.1", - "vitepress-sidebar": "1.24.1", + "vitepress": "^1.3.1", + "vitepress-sidebar": "^1.24.1", "vitest": "^1.2.1", "vue-github-button": "^3.1.0", - "xo": "^0.56.0" + "xo": "^0.59.3" } } diff --git a/src/clients/client.js b/src/clients/client.js index ce50fdf3..5a595e52 100644 --- a/src/clients/client.js +++ b/src/clients/client.js @@ -121,12 +121,12 @@ class Client { /** * Patches a resource. - * @param {...any} args - The resources or parts of the resource path followed by the body. + * @param {...any} arguments_ - The resources or parts of the resource path followed by the body. * @returns {Promise} - Either void or response object */ - async patch(...args) { - const body = args.pop(); - const resource = Array.isArray(args[0]) ? args[0] : args; + async patch(...arguments_) { + const body = arguments_.pop(); + const resource = Array.isArray(arguments_[0]) ? arguments_[0] : arguments_; return this.request('PATCH', resource, body); } @@ -141,12 +141,12 @@ class Client { /** * Deletes a resource. - * @param {...any} args - The resources or parts of the resource path. + * @param {...any} arguments_ - The resources or parts of the resource path. * @returns {Promise} - Either void or response object */ - async delete(...args) { + async delete(...arguments_) { // Check if the first argument is an array - const resource = Array.isArray(args[0]) ? args[0] : args; + const resource = Array.isArray(arguments_[0]) ? arguments_[0] : arguments_; return this.request('DELETE', resource); } @@ -154,11 +154,11 @@ class Client { return this.requestAll('GET', resource); } - async _rawRequest(method, uri, ...args) { + async _rawRequest(method, uri, ...arguments_) { const body = - typeof args.at(-1) === 'object' && - !Array.isArray(args.at(-1)) && - args.pop(); + typeof arguments_.at(-1) === 'object' && + !Array.isArray(arguments_.at(-1)) && + arguments_.pop(); return this.transporter.request(method, uri, body); } @@ -175,12 +175,16 @@ class Client { * @template T * @param {string} method - HTTP method (e.g., 'GET', 'POST'). * @param {string} uri - The URI for the request. - * @param {...any} args - Additional arguments for the request. + * @param {...any} arguments_ - Additional arguments for the request. * @returns {Promise>} - The API response. */ - async request(method, uri, ...args) { + async request(method, uri, ...arguments_) { try { - const {response, result} = await this._rawRequest(method, uri, ...args); + const {response, result} = await this._rawRequest( + method, + uri, + ...arguments_, + ); const responseBody = processResponseBody( checkRequestResponse(response, result), this, @@ -209,7 +213,7 @@ class Client { } // Request method for fetching multiple pages of results - async requestAll(method, uri, ...args) { + async requestAll(method, uri, ...arguments_) { const bodyList = []; const throttle = this.options.get('throttle'); let __request = this._rawRequest; // Use _rawRequest directly @@ -238,7 +242,12 @@ class Client { const fetchPagesRecursively = async (pageUri) => { const isIncremental = pageUri.includes('incremental'); - const responseData = await __request.call(this, method, pageUri, ...args); + const responseData = await __request.call( + this, + method, + pageUri, + ...arguments_, + ); const nextPage = processPage(responseData); if ( nextPage && diff --git a/src/clients/core/users.js b/src/clients/core/users.js index 0136e9fd..6b705c15 100644 --- a/src/clients/core/users.js +++ b/src/clients/core/users.js @@ -217,22 +217,22 @@ class Users extends Client { /** * Updates multiple users. - * @param {...*} args - Arguments including optional IDs and user details. + * @param {...*} arguments_ - Arguments including optional IDs and user details. * @returns {Promise>} An array of updated user details. * @throws {Error} Throws an error if not enough arguments are provided. * @see {@link https://developer.zendesk.com/api-reference/ticketing/users/users/#update-many-users} * @example * const updatedUsers = await client.users.updateMany([12345, 67890], [{name: 'John Doe'}, {name: 'Jane Smith'}]); */ - async updateMany(...args /* Optional ids, users, cb */) { - if (args.length < 2) { + async updateMany(...arguments_ /* Optional ids, users, cb */) { + if (arguments_.length < 2) { throw new Error('Not enough arguments; at least two expected.'); } - const ids = args[0]; - const users = args.length === 2 ? args[0] : args[1]; + const ids = arguments_[0]; + const users = arguments_.length === 2 ? arguments_[0] : arguments_[1]; - if (args.length === 2) { + if (arguments_.length === 2) { return this.put(['users', 'update_many'], users); } @@ -314,22 +314,22 @@ class Users extends Client { /** * Deletes multiple users. - * @param {...*} args - Arguments including optional IDs and user details. + * @param {...any} arguments_ - Arguments including optional IDs and user details. * @returns {Promise} * @throws {Error} Throws an error if not enough arguments are provided. * @see {@link https://developer.zendesk.com/api-reference/ticketing/users/users/#delete-many-users} * @example * await client.users.destroyMany([12345, 67890]); */ - async destroyMany(...args) { - if (args.length < 2) { + async destroyMany(...arguments_) { + if (arguments_.length < 2) { throw new Error('Not enough arguments; at least two expected.'); } - const ids = args[0]; - const users = args.length === 2 ? args[0] : args[1]; + const ids = arguments_[0]; + const users = arguments_.length === 2 ? arguments_[0] : arguments_[1]; - if (args.length === 2) { + if (arguments_.length === 2) { return super.delete(['users', 'destroy_many'], users); } diff --git a/src/clients/helpers.js b/src/clients/helpers.js index d76fcaf3..5226cce1 100644 --- a/src/clients/helpers.js +++ b/src/clients/helpers.js @@ -20,9 +20,9 @@ const failCodes = { function flatten(array) { // eslint-disable-next-line unicorn/no-array-reduce return array.reduce( - (acc, element) => + (accumulator, element) => // eslint-disable-next-line unicorn/prefer-spread - acc.concat(Array.isArray(element) ? flatten(element) : element), + accumulator.concat(Array.isArray(element) ? flatten(element) : element), [], ); } @@ -45,7 +45,7 @@ function populateFields(data, response, map) { const populateRecord = (record) => { for (const {field, name, dataset, all, dataKey, array} of map) { - if (Object.hasOwnProperty.call(record, field)) { + if (Object.hasOwn(record, field)) { const responseDataset = datasetCache.get(dataset) || response[dataset]; datasetCache.set(dataset, responseDataset); @@ -281,7 +281,7 @@ function findBody(result_, self) { if (self.jsonAPINames) { const apiName = self.jsonAPINames.find((api) => - Object.hasOwnProperty.call(result_, api), + Object.hasOwn(result_, api), ); if (apiName) { return result_[apiName]; diff --git a/src/clients/throttle.js b/src/clients/throttle.js index 08f220ac..a1e4121a 100644 --- a/src/clients/throttle.js +++ b/src/clients/throttle.js @@ -4,7 +4,7 @@ module.exports = throttle; * Creates a throttled function that limits the rate of execution of the provided function. * The throttled function ensures that the wrapped function is not invoked more frequently * than a specified time interval. - * @param {...any} args - The arguments for the throttled function. This can include: + * @param {...any} arguments_ - The arguments for the throttled function. This can include: * - `fn` (Function): The function to be throttled. * - `options` (object, optional): Throttling options. * - `options.interval` (number|string, optional): The time interval in milliseconds between function calls. @@ -24,8 +24,9 @@ module.exports = throttle; * * Credit: Original inspiration from https://github.com/brianloveswords/throttle-function "Brian J Brennan" */ -function throttle(...args) { - const [thisArg, fn, options = {}] = args.length > 1 ? args : [null, ...args]; +function throttle(...arguments_) { + const [thisArgument, function_, options = {}] = + arguments_.length > 1 ? arguments_ : [null, ...arguments_]; const msBetweenCalls = getMsBetweenCalls(options); const queue = []; let timer; @@ -47,12 +48,12 @@ function throttle(...args) { */ function runQueue() { if (queue.length === 0) clearInterval(timer); - return queue.shift() ? fn.apply(thisArg, queue.shift()) : null; + return queue.shift() ? function_.apply(thisArgument, queue.shift()) : null; } - return function (...args) { - queue.push(args); - if (!timer) timer = setInterval(runQueue, msBetweenCalls); + return function (...arguments_) { + queue.push(arguments_); + timer ||= setInterval(runQueue, msBetweenCalls); return { position: queue.length, diff --git a/src/index.js b/src/index.js index 181e626c..22667a68 100644 --- a/src/index.js +++ b/src/index.js @@ -65,7 +65,7 @@ class ZendeskClient { /** * @private */ - this.instances = this.instances ?? {}; + this.instances ??= {}; // If the instance already exists, return it if (this.instances[ServiceClass]) { @@ -336,16 +336,14 @@ class ZendeskClient { } /** - * @param {object} args - Arguments for debugging. + * @param {object} arguments_ - Arguments for debugging. * @private */ - _debug(args) { + _debug(arguments_) { if (this.config.debug) { - if (args.result) { - args.result = String(args.result); - } + arguments_.result = String(arguments_.result); - this.logger.debug(args); + this.logger.debug(arguments_); } } }