Skip to content

Commit

Permalink
chore: update xo and fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
blakmatrix committed Aug 2, 2024
1 parent 2cfe5dc commit c9b52ca
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 84 deletions.
25 changes: 20 additions & 5 deletions examples/groupmemberships-list-group.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
25 changes: 20 additions & 5 deletions examples/groupmemberships-list-user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
25 changes: 20 additions & 5 deletions examples/ticketaudits-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
31 changes: 21 additions & 10 deletions examples/users-listbygroup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
31 changes: 21 additions & 10 deletions examples/usertags-list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
41 changes: 25 additions & 16 deletions src/clients/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<void|object>} - 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);
}
Expand All @@ -141,24 +141,24 @@ 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<void|object>} - 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);
}

async getAll(resource) {
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);
}
Expand All @@ -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<module:client.ApiResponse<T>>} - 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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 &&
Expand Down
24 changes: 12 additions & 12 deletions src/clients/core/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Array<User>>} 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);
}

Expand Down Expand Up @@ -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<void>}
* @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);
}

Expand Down
8 changes: 4 additions & 4 deletions src/clients/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
[],
);
}
Expand All @@ -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);

Expand Down Expand Up @@ -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];
Expand Down
15 changes: 8 additions & 7 deletions src/clients/throttle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -24,8 +24,9 @@ module.exports = throttle;
*
* Credit: Original inspiration from https://github.com/brianloveswords/throttle-function "Brian J Brennan" <brianloveswords@gmail.com>
*/
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;
Expand All @@ -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,
Expand Down
Loading

0 comments on commit c9b52ca

Please sign in to comment.