@kiwiproject/kiwi-js 0.7.0 → 0.9.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.
@@ -1,6 +1,6 @@
1
1
  export declare const KiwiPreconditions: {
2
2
  checkArgument: (expression: boolean, errorMessage?: string) => void;
3
- checkArgumentDefined: (reference: any, errorMessage?: string) => void;
3
+ checkArgumentDefined: (reference: unknown, errorMessage?: string) => void;
4
4
  checkArgumentNotBlank: (str: string, errorMessage?: string) => void;
5
5
  checkState: (expression: boolean, errorMessage?: string) => void;
6
6
  checkPositive: (value: number, errorMessage?: string) => void;
@@ -1,16 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.KiwiPreconditions = void 0;
4
- const checkPositiveOrZero = (value, errorMessage = 'value must be positive or zero') => {
4
+ const checkPositiveOrZero = (value, errorMessage = "value must be positive or zero") => {
5
5
  checkState(value >= 0, errorMessage);
6
6
  };
7
7
  const checkState = (expression, errorMessage = undefined) => {
8
8
  if (!expression) {
9
- const fullErrorMessage = errorMessage === undefined ? 'IllegalStateException' : `IllegalStateException: ${errorMessage}`;
9
+ const fullErrorMessage = errorMessage === undefined
10
+ ? "IllegalStateException"
11
+ : `IllegalStateException: ${errorMessage}`;
10
12
  throw new Error(fullErrorMessage);
11
13
  }
12
14
  };
13
- const checkPositive = (value, errorMessage = 'value must be a positive number') => {
15
+ const checkPositive = (value, errorMessage = "value must be a positive number") => {
14
16
  checkState(value > 0, errorMessage);
15
17
  };
16
18
  const checkArgumentDefined = (reference, errorMessage = undefined) => {
@@ -18,13 +20,15 @@ const checkArgumentDefined = (reference, errorMessage = undefined) => {
18
20
  };
19
21
  const checkArgument = (expression, errorMessage = undefined) => {
20
22
  if (!expression) {
21
- const fullErrorMessage = errorMessage === undefined ? 'IllegalArgumentException' : `IllegalArgumentException: ${errorMessage}`;
23
+ const fullErrorMessage = errorMessage === undefined
24
+ ? "IllegalArgumentException"
25
+ : `IllegalArgumentException: ${errorMessage}`;
22
26
  throw new Error(fullErrorMessage);
23
27
  }
24
28
  };
25
29
  const checkArgumentNotBlank = (str, errorMessage = undefined) => {
26
30
  checkArgumentDefined(str, errorMessage);
27
- checkArgument(str !== '', errorMessage);
31
+ checkArgument(str !== "", errorMessage);
28
32
  };
29
33
  exports.KiwiPreconditions = {
30
34
  checkArgument,
@@ -1,13 +1,15 @@
1
+ import { Response } from "express";
2
+ import { ErrorMessage } from "../model/error-message";
1
3
  export declare const KiwiStandardResponsesExpress: {
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
- standardPutResponse: (entity: any, res: Express.Response) => void;
6
- standardDeleteResponse: (res: Express.Response) => void;
7
- standardDeleteResponseWithEntity: (deletedEntity: any, res: Express.Response) => void;
8
- standardAcceptedResponse: (entity: any, res: Express.Response) => void;
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;
4
+ standardGetResponseWithIdentifier: (res: Response, identifierField: string, identifier: unknown, entity: unknown) => void;
5
+ standardGetResponseWithMessage: (res: Response, entity: unknown, notFoundMessage: string) => void;
6
+ standardPostResponse: (res: Response, location: string, entity: unknown) => void;
7
+ standardPutResponse: (res: Response, entity: unknown) => void;
8
+ standardDeleteResponse: (res: Response) => void;
9
+ standardDeleteResponseWithEntity: (res: Response, deletedEntity: unknown) => void;
10
+ standardAcceptedResponse: (res: Response, entity: unknown) => void;
11
+ standardErrorResponse: (res: Response, status: number, errorMessage: string, errors?: Array<ErrorMessage>, identifierField?: string, identifier?: unknown) => void;
12
+ standardUnauthorizedResponse: (res: Response, errorMessage: string) => void;
13
+ standardNotFoundResponse: (res: Response, notFoundMessage: string, identifierField?: string, identifier?: unknown) => void;
14
+ standardBadRequestResponse: (res: Response, errorMessage: string, errors?: Array<ErrorMessage>, identifierField?: string, identifier?: unknown) => void;
13
15
  };
@@ -1,65 +1,69 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.KiwiStandardResponsesExpress = void 0;
4
- const error_message_1 = require("../model/error-message");
4
+ const error_response_1 = require("../model/error-response");
5
5
  /**
6
6
  * Returns a 200 OK response if the entity contains a value. Otherwise, returns a 404 Not Found response with
7
7
  * a message stating that the entity was not found using the given identifier field and value.
8
8
  *
9
+ * @param res the Express Response
9
10
  * @param identifierField the field which identifies the entity being looked up, e.g. "id"
10
11
  * @param identifier the value of the identifier field, e.g. 42
11
12
  * @param entity the entity or undefined
12
- * @param res the Express Response
13
13
  */
14
- const standardGetResponseWithIdentifier = (identifierField, identifier, entity, res) => {
14
+ const standardGetResponseWithIdentifier = (res, identifierField, identifier, entity) => {
15
15
  if (entity !== undefined) {
16
16
  res.status(200).json(entity);
17
17
  return;
18
18
  }
19
- standardNotFoundResponse(`Object with ${identifierField} ${identifier} not found`, res);
19
+ standardNotFoundResponse(res, `Object with ${identifierField} ${identifier} not found`, identifierField, identifier);
20
20
  };
21
21
  /**
22
22
  * Returns a 200 OK response if the entity is non-null. Otherwise, returns a 404 Not Found response with
23
23
  * the given detail message.
24
24
  *
25
+ * @param res the Express Response
25
26
  * @param entity the entity or undefined
26
27
  * @param notFoundMessage the specific message to use in the 404 response (if entity is undefined)
27
- * @param res the Express Response
28
28
  */
29
- const standardGetResponseWithMessage = (entity, notFoundMessage, res) => {
29
+ const standardGetResponseWithMessage = (res, entity, notFoundMessage) => {
30
30
  if (entity !== undefined) {
31
31
  res.status(200).json(entity);
32
32
  return;
33
33
  }
34
- standardNotFoundResponse(notFoundMessage, res);
34
+ standardNotFoundResponse(res, notFoundMessage);
35
35
  };
36
36
  /**
37
- * Returns a 404 Not Found response containing an {@link ErrorMessage} entity which uses {@code errorDetails}
37
+ * Returns a 404 Not Found response containing an {@link ErrorResponse} entity which uses {@code notFoundMessage}
38
38
  * as the detailed error message.
39
39
  *
40
- * @param errorDetails the error message to use
41
40
  * @param res the Express Response
41
+ * @param notFoundMessage the error message to use
42
+ * @param identifierField the field which identifies the entity being looked up, e.g. "id"
43
+ * @param identifier the value of the identifier field, e.g. 42
42
44
  */
43
- const standardNotFoundResponse = (errorDetails, res) => {
44
- res.status(404).json(new error_message_1.ErrorMessage(404, errorDetails).toMap());
45
+ const standardNotFoundResponse = (res, notFoundMessage, identifierField = undefined, identifier = undefined) => {
46
+ res
47
+ .status(404)
48
+ .json(new error_response_1.ErrorResponse(notFoundMessage, identifierField, identifier).toMap());
45
49
  };
46
50
  /**
47
51
  * Returns a 201 Created response having the specified Location header and response entity.
48
52
  *
53
+ * @param res the Express Response
49
54
  * @param location the value for the location header
50
55
  * @param entity the new entity
51
- * @param res the Express Response
52
56
  */
53
- const standardPostResponse = (location, entity, res) => {
54
- res.status(201).set('Location', location).json(entity);
57
+ const standardPostResponse = (res, location, entity) => {
58
+ res.status(201).set("Location", location).json(entity);
55
59
  };
56
60
  /**
57
61
  * Returns a 200 OK response having the specified response entity.
58
62
  *
59
- * @param entity the updated entity
60
63
  * @param res the Express Response
64
+ * @param entity the updated entity
61
65
  */
62
- const standardPutResponse = (entity, res) => {
66
+ const standardPutResponse = (res, entity) => {
63
67
  res.status(200).json(entity);
64
68
  };
65
69
  /**
@@ -68,49 +72,59 @@ const standardPutResponse = (entity, res) => {
68
72
  * @param res the Express Response
69
73
  */
70
74
  const standardDeleteResponse = (res) => {
71
- res.status(204);
75
+ res.sendStatus(204);
72
76
  };
73
77
  /**
74
78
  * Returns a 204 No Content response for DELETE requests and return an entity.
75
79
  *
76
- * @param deletedEntity the entity that was deleted
77
80
  * @param res the Express Response
81
+ * @param deletedEntity the entity that was deleted
78
82
  */
79
- const standardDeleteResponseWithEntity = (deletedEntity, res) => {
83
+ const standardDeleteResponseWithEntity = (res, deletedEntity) => {
80
84
  res.status(204).json(deletedEntity);
81
85
  };
82
86
  /**
83
- * Returns a 400 Bad Request response containing an {@link ErrorMessage} entity which uses {@code errorDetails}
87
+ * Returns a 400 Bad Request response containing an {@link ErrorResponse} entity which uses {@code errorDetails}
84
88
  * as the detailed error message.
85
89
  *
86
- * @param errorDetails the error message to use
87
90
  * @param res the Express Response
91
+ * @param errorMessage the error message to use
92
+ * @param errors a list of errors (e.g. validation errors) to return
93
+ * @param identifierField an optional identifier field for the object
94
+ * @param identifier an optional identifier value for the object
88
95
  */
89
- const standardBadRequestResponse = (errorDetails, res) => {
90
- res.status(400).json(new error_message_1.ErrorMessage(400, errorDetails).toMap());
96
+ const standardBadRequestResponse = (res, errorMessage, errors = [], identifierField = undefined, identifier = undefined) => {
97
+ res
98
+ .status(400)
99
+ .json(new error_response_1.ErrorResponse(errorMessage, identifierField, identifier, errors).toMap());
91
100
  };
92
101
  /**
93
- * Returns a 401 Unauthorized response containing an {@link ErrorMessage} entity which uses {@code errorDetails}
102
+ * Returns a 401 Unauthorized response containing an {@link ErrorResponse} entity which uses {@code errorMessage}
94
103
  * as the detailed error message.
95
104
  *
96
- * @param errorDetails the error message to use
97
105
  * @param res the Express Response
106
+ * @param errorMessage the error message to use
98
107
  */
99
- const standardUnauthorizedResponse = (errorDetails, res) => {
100
- res.status(401).json(new error_message_1.ErrorMessage(401, errorDetails).toMap());
108
+ const standardUnauthorizedResponse = (res, errorMessage) => {
109
+ res.status(401).json(new error_response_1.ErrorResponse(errorMessage).toMap());
101
110
  };
102
111
  /**
103
- * Returns a response having the given status and an {@link ErrorMessage} entity which uses {@code errorDetails}
112
+ * Returns a response having the given status and an {@link ErrorResponse} entity which uses {@code errorMessage}
104
113
  * as the detailed error message.
105
114
  * <p>
106
115
  * Does not verify that the given status is actually an error status.
107
116
  *
108
117
  * @param status the status code
109
- * @param errorDetails the error message to use
118
+ * @param errorMessage the error message to use
110
119
  * @param res the Express Response
120
+ * @param errors an optional list of extra error details to return
121
+ * @param identifierField an
122
+ * @param identifier
111
123
  */
112
- const standardErrorResponse = (status, errorDetails, res) => {
113
- res.status(status).json(new error_message_1.ErrorMessage(status, errorDetails).toMap());
124
+ const standardErrorResponse = (res, status, errorMessage, errors = [], identifierField = undefined, identifier = undefined) => {
125
+ res
126
+ .status(status)
127
+ .json(new error_response_1.ErrorResponse(errorMessage, identifierField, identifier, errors).toMap());
114
128
  };
115
129
  /**
116
130
  * Returns a 202 Accepted response having the specified response entity.
@@ -120,7 +134,7 @@ const standardErrorResponse = (status, errorDetails, res) => {
120
134
  * @param entity the accepted entity
121
135
  * @param res the Express Response
122
136
  */
123
- const standardAcceptedResponse = (entity, res) => {
137
+ const standardAcceptedResponse = (res, entity) => {
124
138
  res.status(202).json(entity);
125
139
  };
126
140
  exports.KiwiStandardResponsesExpress = {
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
- export { ErrorMessage } from './model/error-message';
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';
1
+ export { ErrorMessage } from "./model/error-message";
2
+ export { ErrorResponse } from "./model/error-response";
3
+ export { KiwiStandardResponsesExpress } from "./express/kiwi-standard-responses-express";
4
+ export { KiwiPreconditions } from "./base/kiwi-preconditions";
5
+ export { KiwiPage } from "./search/kiwi-page";
6
+ export { KiwiSort } from "./search/kiwi-sort";
package/dist/index.js CHANGED
@@ -1,8 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.KiwiSort = exports.KiwiPage = exports.KiwiPreconditions = exports.KiwiStandardResponsesExpress = exports.ErrorMessage = void 0;
3
+ exports.KiwiSort = exports.KiwiPage = exports.KiwiPreconditions = exports.KiwiStandardResponsesExpress = exports.ErrorResponse = 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
+ var error_response_1 = require("./model/error-response");
7
+ Object.defineProperty(exports, "ErrorResponse", { enumerable: true, get: function () { return error_response_1.ErrorResponse; } });
6
8
  var kiwi_standard_responses_express_1 = require("./express/kiwi-standard-responses-express");
7
9
  Object.defineProperty(exports, "KiwiStandardResponsesExpress", { enumerable: true, get: function () { return kiwi_standard_responses_express_1.KiwiStandardResponsesExpress; } });
8
10
  var kiwi_preconditions_1 = require("./base/kiwi-preconditions");
@@ -1,20 +1,10 @@
1
1
  /**
2
- * An error message that Kiwi uses to standardize HTTP error responses.
2
+ * An error message that Kiwi uses to hold information about a single error.
3
3
  * <p>
4
- * Each instance contains the HTTP status (error) code; the error message; an optional identifier to identify the
5
- * specific item causing the error (e.g. a primary key); and an optional field/property name for cases when a specific
6
- * field causes the error.
4
+ * Each instance contains the error message; and an optional fieldName that relates to the error.
7
5
  */
8
6
  export declare class ErrorMessage {
9
- private readonly fieldName;
10
- private readonly itemId;
11
- private readonly code;
12
- private readonly message;
13
- constructor(code: number, message: string, fieldName?: string, itemId?: string);
14
- toMap(): {
15
- message: string;
16
- code: number;
17
- fieldName: string;
18
- itemId: string;
19
- };
7
+ readonly fieldName: string;
8
+ readonly message: string;
9
+ constructor(message: string, fieldName: string);
20
10
  }
@@ -2,26 +2,14 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ErrorMessage = void 0;
4
4
  /**
5
- * An error message that Kiwi uses to standardize HTTP error responses.
5
+ * An error message that Kiwi uses to hold information about a single error.
6
6
  * <p>
7
- * Each instance contains the HTTP status (error) code; the error message; an optional identifier to identify the
8
- * specific item causing the error (e.g. a primary key); and an optional field/property name for cases when a specific
9
- * field causes the error.
7
+ * Each instance contains the error message; and an optional fieldName that relates to the error.
10
8
  */
11
9
  class ErrorMessage {
12
- constructor(code, message, fieldName = '', itemId = '') {
13
- this.itemId = itemId;
14
- this.code = code;
10
+ constructor(message, fieldName) {
15
11
  this.message = message;
16
12
  this.fieldName = fieldName;
17
13
  }
18
- toMap() {
19
- return {
20
- message: this.message,
21
- code: this.code,
22
- fieldName: this.fieldName,
23
- itemId: this.itemId
24
- };
25
- }
26
14
  }
27
15
  exports.ErrorMessage = ErrorMessage;
@@ -0,0 +1,24 @@
1
+ import { ErrorMessage } from "./error-message";
2
+ /**
3
+ * An error response that Kiwi uses to standardize HTTP error responses.
4
+ * <p>
5
+ * Each instance contains the error message; an optional identifier to identify the
6
+ * specific item causing the error (e.g. a primary key); an optional field/property name for cases when a specific
7
+ * field causes the error; and an optional list of ErrorMessages for additional details (e.g. validation errors).
8
+ */
9
+ export declare class ErrorResponse {
10
+ private readonly fieldName;
11
+ private readonly itemId;
12
+ private readonly message;
13
+ private readonly errors;
14
+ constructor(message: string, fieldName?: string, itemId?: unknown, errors?: Array<ErrorMessage>);
15
+ toMap(): {
16
+ message: string;
17
+ fieldName: string;
18
+ itemId: unknown;
19
+ errors: {
20
+ message: string;
21
+ fieldName: string;
22
+ }[];
23
+ };
24
+ }
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ErrorResponse = void 0;
4
+ /**
5
+ * An error response that Kiwi uses to standardize HTTP error responses.
6
+ * <p>
7
+ * Each instance contains the error message; an optional identifier to identify the
8
+ * specific item causing the error (e.g. a primary key); an optional field/property name for cases when a specific
9
+ * field causes the error; and an optional list of ErrorMessages for additional details (e.g. validation errors).
10
+ */
11
+ class ErrorResponse {
12
+ constructor(message, fieldName = "", itemId = "", errors = []) {
13
+ this.itemId = itemId;
14
+ this.message = message;
15
+ this.fieldName = fieldName;
16
+ this.errors = errors;
17
+ }
18
+ toMap() {
19
+ return {
20
+ message: this.message,
21
+ fieldName: this.fieldName,
22
+ itemId: this.itemId,
23
+ errors: this.errors.map((error) => {
24
+ return { message: error.message, fieldName: error.fieldName };
25
+ }),
26
+ };
27
+ }
28
+ }
29
+ exports.ErrorResponse = ErrorResponse;
@@ -53,7 +53,7 @@ class KiwiPage {
53
53
  }
54
54
  isLast() {
55
55
  const offset = 1 - this.pagingStartsWith;
56
- return this.number === (this.totalPages - offset);
56
+ return this.number === this.totalPages - offset;
57
57
  }
58
58
  isSorted() {
59
59
  return this.sort !== undefined && this.sort !== null;
@@ -10,15 +10,15 @@ class KiwiSort {
10
10
  this.ascending = ascending;
11
11
  }
12
12
  static ofAscending(property) {
13
- return KiwiSort.of(property, 'ASC');
13
+ return KiwiSort.of(property, "ASC");
14
14
  }
15
15
  static ofDescending(property) {
16
- return KiwiSort.of(property, 'DESC');
16
+ return KiwiSort.of(property, "DESC");
17
17
  }
18
18
  static of(property, direction) {
19
19
  kiwi_preconditions_1.KiwiPreconditions.checkArgumentNotBlank(property);
20
20
  kiwi_preconditions_1.KiwiPreconditions.checkArgumentNotBlank(direction);
21
- return new KiwiSort(property, direction, false, 'ASC' === direction);
21
+ return new KiwiSort(property, direction, false, "ASC" === direction);
22
22
  }
23
23
  isDescending() {
24
24
  return !this.ascending;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kiwiproject/kiwi-js",
3
- "version": "0.7.0",
3
+ "version": "0.9.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",
@@ -8,20 +8,35 @@
8
8
  "/dist"
9
9
  ],
10
10
  "scripts": {
11
+ "format": "node node_modules/.bin/prettier --ignore-path .gitignore --write \"**/*.+(js|ts|json)\"",
12
+ "lint": "node node_modules/.bin/eslint --ext .js,.ts .",
11
13
  "test": "node --experimental-vm-modules node_modules/.bin/jest --coverage --collectCoverageFrom=src/**/*.ts"
12
14
  },
13
15
  "jest": {
14
- "testPathIgnorePatterns": ["<rootDir>/__tests__/__utils__", "<rootDir>/node_modules/"]
16
+ "testPathIgnorePatterns": [
17
+ "<rootDir>/__tests__/__utils__",
18
+ "<rootDir>/node_modules/"
19
+ ],
20
+ "coveragePathIgnorePatterns": [
21
+ "src/index.ts"
22
+ ]
15
23
  },
16
24
  "dependencies": {
17
25
  "express": "4.18.2"
18
26
  },
19
27
  "devDependencies": {
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"
28
+ "@babel/core": "7.23.7",
29
+ "@babel/preset-env": "7.23.7",
30
+ "@babel/preset-typescript": "7.23.3",
31
+ "@jest/globals": "29.7.0",
32
+ "@types/express": "4.17.21",
33
+ "@types/node": "20.10.6",
34
+ "@typescript-eslint/eslint-plugin": "6.16.0",
35
+ "@typescript-eslint/parser": "6.16.0",
36
+ "babel-jest": "29.7.0",
37
+ "eslint": "8.56.0",
38
+ "jest": "29.7.0",
39
+ "prettier": "3.1.1",
40
+ "typescript": "5.3.3"
26
41
  }
27
42
  }