Skip to content

Commit

Permalink
feat(client): add sort by create time capability
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkelemen authored Jan 22, 2024
1 parent 621a625 commit 036b73e
Showing 6 changed files with 77 additions and 1 deletion.
8 changes: 8 additions & 0 deletions docs/Client.md
Original file line number Diff line number Diff line change
@@ -32,6 +32,14 @@ Here"s a list of the available options:
| usePriority | If false, task will be fetched arbitrarily instead of based on its priority. | boolean | | true |
| interceptors | Function(s) that will be called before a request is sent. Interceptors receive the configuration of the request and return a new configuration. | function or [function] | | |
| use | Function(s) that have access to the client instance as soon as it is created and before any polling happens. Check out [logger](/lib/logger.js) for a better understanding of the usage of middlewares. | function or [function] | | |
| sorting | Defines the sorting of the fetched tasks. It can be used together with `usePriority`, but the sorting will be based on priority first. | Array of `Sorting` objects | | |

`Sorting` object properties:

| Option | Description | Type | Required | Default |
|:---------:|:-------------------------------------------------------------------------:|--------|:--------:|:-------:|
| sortBy | Specifies the sorting by property. [Possible values.](/lib/Client.js#L67) | string || |
| sortOrder | Specifies the sorting direction. [Possible values.](/lib/Client.js#L71) | string || |

### About interceptors

7 changes: 7 additions & 0 deletions examples/order/index.js
Original file line number Diff line number Diff line change
@@ -28,6 +28,13 @@ import {
const config = {
baseUrl: "http://localhost:8080/engine-rest",
use: logger,
usePriority: false,
sorting: [
{
sortBy: Client.SortBy.CreateTime,
sortOrder: Client.SortOrder.DESC,
},
],
};

// create a Client instance with custom configuration
44 changes: 43 additions & 1 deletion lib/Client.js
Original file line number Diff line number Diff line change
@@ -27,6 +27,9 @@ import {
MISSING_HANDLER,
WRONG_INTERCEPTOR,
WRONG_MIDDLEWARES,
WRONG_SORTING,
WRONG_SORTING_SORT_ORDER,
WRONG_SORTING_SORT_BY,
} from "./__internal/errors.js";

import {
@@ -55,11 +58,21 @@ const defaultOptions = {
* @param customOptions.autoPoll
* @param customOptions.asyncResponseTimeout
* @param customOptions.interceptors
* @param customOptions.use
* @param customOptions.usePriority
* @param customOptions.maxParallelExecutions
* @param customOptions.sorting
* @constructor
*/
class Client extends events {
static SortBy = Object.freeze({
CreateTime: "createTime",
});

static SortOrder = Object.freeze({
ASC: "asc",
DESC: "desc",
});

constructor(customOptions) {
super();

@@ -129,6 +142,31 @@ class Client extends events {
if (isFunction(use)) {
this.options.use = [use];
}

if (this.options.sorting) {
// sanitize sorting
if (!Array.isArray(this.options.sorting)) {
throw new Error(WRONG_SORTING);
}

// sanitize sorting.sortBy
const sortByPredicate = (sorting) =>
!sorting.sortBy ||
!Object.values(Client.SortBy).includes(sorting.sortBy);
if (this.options.sorting.some(sortByPredicate)) {
throw new Error(WRONG_SORTING_SORT_BY + Object.values(Client.SortBy));
}

// sanitize sorting.sortOrder
const sortOrderPredicate = (sorting) =>
!sorting.sortOrder ||
!Object.values(Client.SortOrder).includes(sorting.sortOrder);
if (this.options.sorting.some(sortOrderPredicate)) {
throw new Error(
WRONG_SORTING_SORT_ORDER + Object.values(Client.SortOrder)
);
}
}
}

/**
@@ -153,6 +191,7 @@ class Client extends events {
const {
maxTasks,
usePriority,
sorting,
interval,
asyncResponseTimeout,
maxParallelExecutions,
@@ -174,6 +213,9 @@ class Client extends events {
if (asyncResponseTimeout) {
pollingOptions = { ...pollingOptions, asyncResponseTimeout };
}
if (sorting) {
pollingOptions = { ...pollingOptions, sorting };
}

// if there are no topic subscriptions, reschedule polling
if (!Object.keys(topicSubscriptions).length) {
3 changes: 3 additions & 0 deletions lib/Client.test.js
Original file line number Diff line number Diff line change
@@ -45,6 +45,9 @@ const customClientOptions = {
maxTasks: 3,
interval: 100,
lockDuration: 30000,
sorting: [
{ sortBy: Client.SortBy.CreateTime, sortOrder: Client.SortOrder.ASC },
],
};

describe("Client", () => {
10 changes: 10 additions & 0 deletions lib/__internal/errors.js
Original file line number Diff line number Diff line change
@@ -50,6 +50,13 @@ const MISSING_FILE_OPTIONS =
"Couldn't create a File, make sure to provide one of the following" +
" parameters: \n- path \ntypedValue";

const WRONG_SORTING =
"Couldn't instantiate Client, 'sorting' parameter should be an array.";
const WRONG_SORTING_SORT_BY =
"Couldn't instantiate Client, wrong 'sorting.sortBy' parameter. Possible values: ";
const WRONG_SORTING_SORT_ORDER =
"Couldn't instantiate Client, wrong 'sorting.sortOrder' parameter. Possible values: ";

export {
MISSING_BASE_URL,
ALREADY_REGISTERED,
@@ -64,4 +71,7 @@ export {
UNEXPECTED_KEYCLOAK_TOKEN_RESULT,
WRONG_MIDDLEWARES,
MISSING_FILE_OPTIONS,
WRONG_SORTING,
WRONG_SORTING_SORT_BY,
WRONG_SORTING_SORT_ORDER,
};
6 changes: 6 additions & 0 deletions lib/__snapshots__/Client.test.js.snap
Original file line number Diff line number Diff line change
@@ -26,6 +26,12 @@ exports[`Client subscribe should call the API with the custom configs 1`] = `
[
{
"maxTasks": 3,
"sorting": [
{
"sortBy": "createTime",
"sortOrder": "asc",
},
],
"topics": [
{
"includeExtensionProperties": true,

0 comments on commit 036b73e

Please sign in to comment.