@kiwiproject/kiwi-js 0.5.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Kiwi Project
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,2 @@
1
+ # kiwi-js
2
+ 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,13 @@
1
+ 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;
13
+ };
@@ -0,0 +1,138 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.KiwiStandardResponsesExpress = void 0;
4
+ const error_message_1 = require("../model/error-message");
5
+ /**
6
+ * Returns a 200 OK response if the entity contains a value. Otherwise, returns a 404 Not Found response with
7
+ * a message stating that the entity was not found using the given identifier field and value.
8
+ *
9
+ * @param identifierField the field which identifies the entity being looked up, e.g. "id"
10
+ * @param identifier the value of the identifier field, e.g. 42
11
+ * @param entity the entity or undefined
12
+ * @param res the Express Response
13
+ */
14
+ const standardGetResponseWithIdentifier = (identifierField, identifier, entity, res) => {
15
+ if (entity !== undefined) {
16
+ res.status(200).json(entity);
17
+ return;
18
+ }
19
+ standardNotFoundResponse(`Object with ${identifierField} ${identifier} not found`, res);
20
+ };
21
+ /**
22
+ * Returns a 200 OK response if the entity is non-null. Otherwise, returns a 404 Not Found response with
23
+ * the given detail message.
24
+ *
25
+ * @param entity the entity or undefined
26
+ * @param notFoundMessage the specific message to use in the 404 response (if entity is undefined)
27
+ * @param res the Express Response
28
+ */
29
+ const standardGetResponseWithMessage = (entity, notFoundMessage, res) => {
30
+ if (entity !== undefined) {
31
+ res.status(200).json(entity);
32
+ return;
33
+ }
34
+ standardNotFoundResponse(notFoundMessage, res);
35
+ };
36
+ /**
37
+ * Returns a 404 Not Found response containing an {@link ErrorMessage} entity which uses {@code errorDetails}
38
+ * as the detailed error message.
39
+ *
40
+ * @param errorDetails the error message to use
41
+ * @param res the Express Response
42
+ */
43
+ const standardNotFoundResponse = (errorDetails, res) => {
44
+ res.status(404).json(new error_message_1.ErrorMessage(404, errorDetails).toMap());
45
+ };
46
+ /**
47
+ * Returns a 201 Created response having the specified Location header and response entity.
48
+ *
49
+ * @param location the value for the location header
50
+ * @param entity the new entity
51
+ * @param res the Express Response
52
+ */
53
+ const standardPostResponse = (location, entity, res) => {
54
+ res.status(201).set('Location', location).json(entity);
55
+ };
56
+ /**
57
+ * Returns a 200 OK response having the specified response entity.
58
+ *
59
+ * @param entity the updated entity
60
+ * @param res the Express Response
61
+ */
62
+ const standardPutResponse = (entity, res) => {
63
+ res.status(200).json(entity);
64
+ };
65
+ /**
66
+ * Returns a 204 No Content response for DELETE requests that do not return an entity.
67
+ *
68
+ * @param res the Express Response
69
+ */
70
+ const standardDeleteResponse = (res) => {
71
+ res.status(204);
72
+ };
73
+ /**
74
+ * Returns a 204 No Content response for DELETE requests and return an entity.
75
+ *
76
+ * @param deletedEntity the entity that was deleted
77
+ * @param res the Express Response
78
+ */
79
+ const standardDeleteResponseWithEntity = (deletedEntity, res) => {
80
+ res.status(204).json(deletedEntity);
81
+ };
82
+ /**
83
+ * Returns a 400 Bad Request response containing an {@link ErrorMessage} entity which uses {@code errorDetails}
84
+ * as the detailed error message.
85
+ *
86
+ * @param errorDetails the error message to use
87
+ * @param res the Express Response
88
+ */
89
+ const standardBadRequestResponse = (errorDetails, res) => {
90
+ res.status(400).json(new error_message_1.ErrorMessage(400, errorDetails).toMap());
91
+ };
92
+ /**
93
+ * Returns a 401 Unauthorized response containing an {@link ErrorMessage} entity which uses {@code errorDetails}
94
+ * as the detailed error message.
95
+ *
96
+ * @param errorDetails the error message to use
97
+ * @param res the Express Response
98
+ */
99
+ const standardUnauthorizedResponse = (errorDetails, res) => {
100
+ res.status(401).json(new error_message_1.ErrorMessage(401, errorDetails).toMap());
101
+ };
102
+ /**
103
+ * Returns a response having the given status and an {@link ErrorMessage} entity which uses {@code errorDetails}
104
+ * as the detailed error message.
105
+ * <p>
106
+ * Does not verify that the given status is actually an error status.
107
+ *
108
+ * @param status the status code
109
+ * @param errorDetails the error message to use
110
+ * @param res the Express Response
111
+ */
112
+ const standardErrorResponse = (status, errorDetails, res) => {
113
+ res.status(status).json(new error_message_1.ErrorMessage(status, errorDetails).toMap());
114
+ };
115
+ /**
116
+ * Returns a 202 Accepted response having the specified response entity.
117
+ * <p>
118
+ * This generally applies to POST, PUT, and PATCH requests that might take a while and are processed asynchronously.
119
+ *
120
+ * @param entity the accepted entity
121
+ * @param res the Express Response
122
+ */
123
+ const standardAcceptedResponse = (entity, res) => {
124
+ res.status(202).json(entity);
125
+ };
126
+ exports.KiwiStandardResponsesExpress = {
127
+ standardGetResponseWithIdentifier,
128
+ standardGetResponseWithMessage,
129
+ standardPostResponse,
130
+ standardPutResponse,
131
+ standardDeleteResponse,
132
+ standardDeleteResponseWithEntity,
133
+ standardAcceptedResponse,
134
+ standardErrorResponse,
135
+ standardUnauthorizedResponse,
136
+ standardNotFoundResponse,
137
+ standardBadRequestResponse,
138
+ };
@@ -0,0 +1,2 @@
1
+ export { ErrorMessage } from './model/error-message';
2
+ export { KiwiStandardResponsesExpress } from './express/kiwi-standard-responses-express';
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.KiwiStandardResponsesExpress = exports.ErrorMessage = void 0;
4
+ var error_message_1 = require("./model/error-message");
5
+ Object.defineProperty(exports, "ErrorMessage", { enumerable: true, get: function () { return error_message_1.ErrorMessage; } });
6
+ var kiwi_standard_responses_express_1 = require("./express/kiwi-standard-responses-express");
7
+ Object.defineProperty(exports, "KiwiStandardResponsesExpress", { enumerable: true, get: function () { return kiwi_standard_responses_express_1.KiwiStandardResponsesExpress; } });
@@ -0,0 +1,20 @@
1
+ /**
2
+ * An error message that Kiwi uses to standardize HTTP error responses.
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.
7
+ */
8
+ 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
+ };
20
+ }
@@ -0,0 +1,27 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ErrorMessage = void 0;
4
+ /**
5
+ * An error message that Kiwi uses to standardize HTTP error responses.
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.
10
+ */
11
+ class ErrorMessage {
12
+ constructor(code, message, fieldName = '', itemId = '') {
13
+ this.itemId = itemId;
14
+ this.code = code;
15
+ this.message = message;
16
+ this.fieldName = fieldName;
17
+ }
18
+ toMap() {
19
+ return {
20
+ message: this.message,
21
+ code: this.code,
22
+ fieldName: this.fieldName,
23
+ itemId: this.itemId
24
+ };
25
+ }
26
+ }
27
+ exports.ErrorMessage = ErrorMessage;
package/package.json ADDED
@@ -0,0 +1,27 @@
1
+ {
2
+ "name": "@kiwiproject/kiwi-js",
3
+ "version": "0.5.0",
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
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "/dist"
9
+ ],
10
+ "scripts": {
11
+ "test": "node --experimental-vm-modules node_modules/.bin/jest --coverage --collectCoverageFrom=src/**/*.ts"
12
+ },
13
+ "jest": {
14
+ "testPathIgnorePatterns": ["<rootDir>/__tests__/__utils__", "<rootDir>/node_modules/"]
15
+ },
16
+ "dependencies": {
17
+ "express": "^4.18.2"
18
+ },
19
+ "devDependencies": {
20
+ "@babel/core": "^7.21.8",
21
+ "@babel/preset-env": "^7.21.5",
22
+ "@babel/preset-typescript": "^7.21.5",
23
+ "@jest/globals": "^29.5.0",
24
+ "babel-jest": "^29.5.0",
25
+ "jest": "^29.5.0"
26
+ }
27
+ }