@kiwiproject/kiwi-js 0.5.0 → 0.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -1
- package/dist/base/kiwi-preconditions.d.ts +8 -0
- package/dist/base/kiwi-preconditions.js +36 -0
- package/dist/express/kiwi-standard-responses-express.d.ts +7 -7
- package/dist/index.d.ts +3 -0
- package/dist/index.js +7 -1
- package/dist/model/error-message.d.ts +5 -5
- package/dist/search/kiwi-page.d.ts +60 -0
- package/dist/search/kiwi-page.js +66 -0
- package/dist/search/kiwi-sort.d.ts +11 -0
- package/dist/search/kiwi-sort.js +27 -0
- package/package.json +8 -8
package/README.md
CHANGED
|
@@ -1,2 +1,9 @@
|
|
|
1
|
-
|
|
1
|
+
### KiwiJS
|
|
2
|
+
[](https://github.com/kiwiproject/kiwi-js/actions?query=workflow%3Abuild)
|
|
3
|
+
[](https://sonarcloud.io/dashboard?id=kiwiproject_kiwi-js)
|
|
4
|
+
[](https://sonarcloud.io/dashboard?id=kiwiproject_kiwi-js)
|
|
5
|
+
[](https://opensource.org/licenses/MIT)
|
|
6
|
+
[](https://www.npmjs.com/package/@kiwiproject/kiwi-js)
|
|
7
|
+
|
|
8
|
+
|
|
2
9
|
KiwiJS is a utility library. It contains a variety of utilities that we have built over time and find useful. Most of these utilities are ports from the Java Kiwi library (https://github.com/kiwiproject/kiwi).
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export declare const KiwiPreconditions: {
|
|
2
|
+
checkArgument: (expression: boolean, errorMessage?: string) => void;
|
|
3
|
+
checkArgumentDefined: (reference: any, errorMessage?: string) => void;
|
|
4
|
+
checkArgumentNotBlank: (str: string, errorMessage?: string) => void;
|
|
5
|
+
checkState: (expression: boolean, errorMessage?: string) => void;
|
|
6
|
+
checkPositive: (value: number, errorMessage?: string) => void;
|
|
7
|
+
checkPositiveOrZero: (value: number, errorMessage?: string) => void;
|
|
8
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.KiwiPreconditions = void 0;
|
|
4
|
+
const checkPositiveOrZero = (value, errorMessage = 'value must be positive or zero') => {
|
|
5
|
+
checkState(value >= 0, errorMessage);
|
|
6
|
+
};
|
|
7
|
+
const checkState = (expression, errorMessage = undefined) => {
|
|
8
|
+
if (!expression) {
|
|
9
|
+
const fullErrorMessage = errorMessage === undefined ? 'IllegalStateException' : `IllegalStateException: ${errorMessage}`;
|
|
10
|
+
throw new Error(fullErrorMessage);
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
const checkPositive = (value, errorMessage = 'value must be a positive number') => {
|
|
14
|
+
checkState(value > 0, errorMessage);
|
|
15
|
+
};
|
|
16
|
+
const checkArgumentDefined = (reference, errorMessage = undefined) => {
|
|
17
|
+
checkArgument(reference !== undefined && reference !== null, errorMessage);
|
|
18
|
+
};
|
|
19
|
+
const checkArgument = (expression, errorMessage = undefined) => {
|
|
20
|
+
if (!expression) {
|
|
21
|
+
const fullErrorMessage = errorMessage === undefined ? 'IllegalArgumentException' : `IllegalArgumentException: ${errorMessage}`;
|
|
22
|
+
throw new Error(fullErrorMessage);
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
const checkArgumentNotBlank = (str, errorMessage = undefined) => {
|
|
26
|
+
checkArgumentDefined(str, errorMessage);
|
|
27
|
+
checkArgument(str !== '', errorMessage);
|
|
28
|
+
};
|
|
29
|
+
exports.KiwiPreconditions = {
|
|
30
|
+
checkArgument,
|
|
31
|
+
checkArgumentDefined,
|
|
32
|
+
checkArgumentNotBlank,
|
|
33
|
+
checkState,
|
|
34
|
+
checkPositive,
|
|
35
|
+
checkPositiveOrZero,
|
|
36
|
+
};
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
export declare const KiwiStandardResponsesExpress: {
|
|
2
|
-
standardGetResponseWithIdentifier: (identifierField:
|
|
3
|
-
standardGetResponseWithMessage: (entity: any, notFoundMessage:
|
|
4
|
-
standardPostResponse: (location:
|
|
2
|
+
standardGetResponseWithIdentifier: (identifierField: string, identifier: Object, entity: any, res: Express.Response) => void;
|
|
3
|
+
standardGetResponseWithMessage: (entity: any, notFoundMessage: string, res: Express.Response) => void;
|
|
4
|
+
standardPostResponse: (location: string, entity: any, res: Express.Response) => void;
|
|
5
5
|
standardPutResponse: (entity: any, res: Express.Response) => void;
|
|
6
6
|
standardDeleteResponse: (res: Express.Response) => void;
|
|
7
7
|
standardDeleteResponseWithEntity: (deletedEntity: any, res: Express.Response) => void;
|
|
8
8
|
standardAcceptedResponse: (entity: any, res: Express.Response) => void;
|
|
9
|
-
standardErrorResponse: (status:
|
|
10
|
-
standardUnauthorizedResponse: (errorDetails:
|
|
11
|
-
standardNotFoundResponse: (errorDetails:
|
|
12
|
-
standardBadRequestResponse: (errorDetails:
|
|
9
|
+
standardErrorResponse: (status: number, errorDetails: string, res: Express.Response) => void;
|
|
10
|
+
standardUnauthorizedResponse: (errorDetails: string, res: Express.Response) => void;
|
|
11
|
+
standardNotFoundResponse: (errorDetails: string, res: Express.Response) => void;
|
|
12
|
+
standardBadRequestResponse: (errorDetails: string, res: Express.Response) => void;
|
|
13
13
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -1,2 +1,5 @@
|
|
|
1
1
|
export { ErrorMessage } from './model/error-message';
|
|
2
2
|
export { KiwiStandardResponsesExpress } from './express/kiwi-standard-responses-express';
|
|
3
|
+
export { KiwiPreconditions } from './base/kiwi-preconditions';
|
|
4
|
+
export { KiwiPage } from './search/kiwi-page';
|
|
5
|
+
export { KiwiSort } from './search/kiwi-sort';
|
package/dist/index.js
CHANGED
|
@@ -1,7 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.KiwiStandardResponsesExpress = exports.ErrorMessage = void 0;
|
|
3
|
+
exports.KiwiSort = exports.KiwiPage = exports.KiwiPreconditions = exports.KiwiStandardResponsesExpress = exports.ErrorMessage = void 0;
|
|
4
4
|
var error_message_1 = require("./model/error-message");
|
|
5
5
|
Object.defineProperty(exports, "ErrorMessage", { enumerable: true, get: function () { return error_message_1.ErrorMessage; } });
|
|
6
6
|
var kiwi_standard_responses_express_1 = require("./express/kiwi-standard-responses-express");
|
|
7
7
|
Object.defineProperty(exports, "KiwiStandardResponsesExpress", { enumerable: true, get: function () { return kiwi_standard_responses_express_1.KiwiStandardResponsesExpress; } });
|
|
8
|
+
var kiwi_preconditions_1 = require("./base/kiwi-preconditions");
|
|
9
|
+
Object.defineProperty(exports, "KiwiPreconditions", { enumerable: true, get: function () { return kiwi_preconditions_1.KiwiPreconditions; } });
|
|
10
|
+
var kiwi_page_1 = require("./search/kiwi-page");
|
|
11
|
+
Object.defineProperty(exports, "KiwiPage", { enumerable: true, get: function () { return kiwi_page_1.KiwiPage; } });
|
|
12
|
+
var kiwi_sort_1 = require("./search/kiwi-sort");
|
|
13
|
+
Object.defineProperty(exports, "KiwiSort", { enumerable: true, get: function () { return kiwi_sort_1.KiwiSort; } });
|
|
@@ -10,11 +10,11 @@ export declare class ErrorMessage {
|
|
|
10
10
|
private readonly itemId;
|
|
11
11
|
private readonly code;
|
|
12
12
|
private readonly message;
|
|
13
|
-
constructor(code:
|
|
13
|
+
constructor(code: number, message: string, fieldName?: string, itemId?: string);
|
|
14
14
|
toMap(): {
|
|
15
|
-
message:
|
|
16
|
-
code:
|
|
17
|
-
fieldName:
|
|
18
|
-
itemId:
|
|
15
|
+
message: string;
|
|
16
|
+
code: number;
|
|
17
|
+
fieldName: string;
|
|
18
|
+
itemId: string;
|
|
19
19
|
};
|
|
20
20
|
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { KiwiSort } from "./kiwi-sort";
|
|
2
|
+
/**
|
|
3
|
+
* Represents one page of an overall list of results.
|
|
4
|
+
* <p>
|
|
5
|
+
* By default, pagination assumes a start page index of 0 (i.e. the page offset). You can change this
|
|
6
|
+
* by calling setPagingStartsWith(int) or usingOneAsFirstPage().
|
|
7
|
+
* <p>
|
|
8
|
+
* You can also indicate whether a sort has been applied to the data by setting the KiwiSort via
|
|
9
|
+
* the setter method or via addKiwiSort(KiwiSort).
|
|
10
|
+
*/
|
|
11
|
+
export declare class KiwiPage {
|
|
12
|
+
/**
|
|
13
|
+
* The content on this specific page.
|
|
14
|
+
*/
|
|
15
|
+
readonly content: object[];
|
|
16
|
+
/**
|
|
17
|
+
* The size limit of the pagination, for example each page can have up to 25 items. The last page will often
|
|
18
|
+
* contain fewer items than this limit unless the total number of items is such that there is no remainder
|
|
19
|
+
* when dividing the total by the page size. e.g. if the total number of items is 100 and the page size is 20,
|
|
20
|
+
* then each of the 5 pages has exactly 20 items (the page size).
|
|
21
|
+
*/
|
|
22
|
+
readonly size: number;
|
|
23
|
+
/**
|
|
24
|
+
* The number of this page, e.g. page X of Y.
|
|
25
|
+
*/
|
|
26
|
+
readonly number: number;
|
|
27
|
+
/**
|
|
28
|
+
* The number of items/elements on this page. Only on the last page can this be different.
|
|
29
|
+
*/
|
|
30
|
+
readonly numberOfElements: number;
|
|
31
|
+
/**
|
|
32
|
+
* The total number of pages, calculated from the page size and total number of elements.
|
|
33
|
+
*/
|
|
34
|
+
readonly totalPages: number;
|
|
35
|
+
/**
|
|
36
|
+
* The total number of items/elements in the overall result list.
|
|
37
|
+
*/
|
|
38
|
+
readonly totalElements: number;
|
|
39
|
+
/**
|
|
40
|
+
* Describes any sort that is active for the pagination. Default value is null.
|
|
41
|
+
*/
|
|
42
|
+
sort: KiwiSort;
|
|
43
|
+
/**
|
|
44
|
+
* Allows adjustment for instance where pagination starts with one instead of zero.
|
|
45
|
+
*/
|
|
46
|
+
pagingStartsWith: number;
|
|
47
|
+
/**
|
|
48
|
+
* Optional extra supplementary data related to the page
|
|
49
|
+
*/
|
|
50
|
+
supplementaryData: object;
|
|
51
|
+
constructor(content: object[], size: number, number: number, numberOfElements: number, totalPages: number, totalElements: number, sort: KiwiSort);
|
|
52
|
+
static of(pageNum: number, limit: number, total: number, contentList: object[]): KiwiPage;
|
|
53
|
+
addKiwiSort(sort: KiwiSort): this;
|
|
54
|
+
usingZeroAsFirstPage(): this;
|
|
55
|
+
usingOneAsFirstPage(): this;
|
|
56
|
+
isFirst(): boolean;
|
|
57
|
+
isLast(): boolean;
|
|
58
|
+
isSorted(): boolean;
|
|
59
|
+
addSupplementaryData(supplementaryData: object): this;
|
|
60
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.KiwiPage = void 0;
|
|
4
|
+
const kiwi_preconditions_1 = require("../base/kiwi-preconditions");
|
|
5
|
+
/**
|
|
6
|
+
* Represents one page of an overall list of results.
|
|
7
|
+
* <p>
|
|
8
|
+
* By default, pagination assumes a start page index of 0 (i.e. the page offset). You can change this
|
|
9
|
+
* by calling setPagingStartsWith(int) or usingOneAsFirstPage().
|
|
10
|
+
* <p>
|
|
11
|
+
* You can also indicate whether a sort has been applied to the data by setting the KiwiSort via
|
|
12
|
+
* the setter method or via addKiwiSort(KiwiSort).
|
|
13
|
+
*/
|
|
14
|
+
class KiwiPage {
|
|
15
|
+
constructor(content, size, number, numberOfElements, totalPages, totalElements, sort) {
|
|
16
|
+
/**
|
|
17
|
+
* Allows adjustment for instance where pagination starts with one instead of zero.
|
|
18
|
+
*/
|
|
19
|
+
this.pagingStartsWith = 0;
|
|
20
|
+
/**
|
|
21
|
+
* Optional extra supplementary data related to the page
|
|
22
|
+
*/
|
|
23
|
+
this.supplementaryData = {};
|
|
24
|
+
this.content = content;
|
|
25
|
+
this.size = size;
|
|
26
|
+
this.number = number;
|
|
27
|
+
this.numberOfElements = numberOfElements;
|
|
28
|
+
this.totalPages = totalPages;
|
|
29
|
+
this.totalElements = totalElements;
|
|
30
|
+
this.sort = sort;
|
|
31
|
+
}
|
|
32
|
+
static of(pageNum, limit, total, contentList) {
|
|
33
|
+
kiwi_preconditions_1.KiwiPreconditions.checkPositiveOrZero(pageNum);
|
|
34
|
+
kiwi_preconditions_1.KiwiPreconditions.checkPositive(limit);
|
|
35
|
+
kiwi_preconditions_1.KiwiPreconditions.checkPositiveOrZero(total);
|
|
36
|
+
kiwi_preconditions_1.KiwiPreconditions.checkArgumentDefined(contentList);
|
|
37
|
+
return new KiwiPage(contentList, limit, pageNum, contentList.length, Math.ceil(total / limit), total, undefined);
|
|
38
|
+
}
|
|
39
|
+
addKiwiSort(sort) {
|
|
40
|
+
this.sort = sort;
|
|
41
|
+
return this;
|
|
42
|
+
}
|
|
43
|
+
usingZeroAsFirstPage() {
|
|
44
|
+
this.pagingStartsWith = 0;
|
|
45
|
+
return this;
|
|
46
|
+
}
|
|
47
|
+
usingOneAsFirstPage() {
|
|
48
|
+
this.pagingStartsWith = 1;
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
isFirst() {
|
|
52
|
+
return this.number === this.pagingStartsWith;
|
|
53
|
+
}
|
|
54
|
+
isLast() {
|
|
55
|
+
const offset = 1 - this.pagingStartsWith;
|
|
56
|
+
return this.number === (this.totalPages - offset);
|
|
57
|
+
}
|
|
58
|
+
isSorted() {
|
|
59
|
+
return this.sort !== undefined && this.sort !== null;
|
|
60
|
+
}
|
|
61
|
+
addSupplementaryData(supplementaryData) {
|
|
62
|
+
this.supplementaryData = supplementaryData;
|
|
63
|
+
return this;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
exports.KiwiPage = KiwiPage;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export declare class KiwiSort {
|
|
2
|
+
readonly direction: string;
|
|
3
|
+
readonly property: string;
|
|
4
|
+
readonly ignoreCase: boolean;
|
|
5
|
+
readonly ascending: boolean;
|
|
6
|
+
constructor(property: string, direction: string, ignoreCase: boolean, ascending: boolean);
|
|
7
|
+
static ofAscending(property: string): KiwiSort;
|
|
8
|
+
static ofDescending(property: string): KiwiSort;
|
|
9
|
+
static of(property: string, direction: string): KiwiSort;
|
|
10
|
+
isDescending(): boolean;
|
|
11
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.KiwiSort = void 0;
|
|
4
|
+
const kiwi_preconditions_1 = require("../base/kiwi-preconditions");
|
|
5
|
+
class KiwiSort {
|
|
6
|
+
constructor(property, direction, ignoreCase, ascending) {
|
|
7
|
+
this.property = property;
|
|
8
|
+
this.direction = direction;
|
|
9
|
+
this.ignoreCase = ignoreCase;
|
|
10
|
+
this.ascending = ascending;
|
|
11
|
+
}
|
|
12
|
+
static ofAscending(property) {
|
|
13
|
+
return KiwiSort.of(property, 'ASC');
|
|
14
|
+
}
|
|
15
|
+
static ofDescending(property) {
|
|
16
|
+
return KiwiSort.of(property, 'DESC');
|
|
17
|
+
}
|
|
18
|
+
static of(property, direction) {
|
|
19
|
+
kiwi_preconditions_1.KiwiPreconditions.checkArgumentNotBlank(property);
|
|
20
|
+
kiwi_preconditions_1.KiwiPreconditions.checkArgumentNotBlank(direction);
|
|
21
|
+
return new KiwiSort(property, direction, false, 'ASC' === direction);
|
|
22
|
+
}
|
|
23
|
+
isDescending() {
|
|
24
|
+
return !this.ascending;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.KiwiSort = KiwiSort;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kiwiproject/kiwi-js",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.7.0",
|
|
4
4
|
"description": "KiwiJS is a utility library. It contains a variety of utilities that we have built over time and find useful. Most of these utilities are ports from the Java Kiwi library.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -14,14 +14,14 @@
|
|
|
14
14
|
"testPathIgnorePatterns": ["<rootDir>/__tests__/__utils__", "<rootDir>/node_modules/"]
|
|
15
15
|
},
|
|
16
16
|
"dependencies": {
|
|
17
|
-
"express": "
|
|
17
|
+
"express": "4.18.2"
|
|
18
18
|
},
|
|
19
19
|
"devDependencies": {
|
|
20
|
-
"@babel/core": "
|
|
21
|
-
"@babel/preset-env": "
|
|
22
|
-
"@babel/preset-typescript": "
|
|
23
|
-
"@jest/globals": "
|
|
24
|
-
"babel-jest": "
|
|
25
|
-
"jest": "
|
|
20
|
+
"@babel/core": "7.22.6",
|
|
21
|
+
"@babel/preset-env": "7.22.6",
|
|
22
|
+
"@babel/preset-typescript": "7.22.5",
|
|
23
|
+
"@jest/globals": "29.6.0",
|
|
24
|
+
"babel-jest": "29.6.0",
|
|
25
|
+
"jest": "29.6.0"
|
|
26
26
|
}
|
|
27
27
|
}
|