@dr.pogodin/react-utils 1.43.34 → 1.44.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/build/development/server/utils/errors.js +8 -30
- package/build/development/server/utils/errors.js.map +1 -1
- package/build/production/server/utils/errors.js +2 -14
- package/build/production/server/utils/errors.js.map +1 -1
- package/build/types-code/server/utils/errors.d.ts +2 -16
- package/package.json +7 -7
- package/src/server/utils/errors.ts +14 -23
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
|
-
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
|
|
4
3
|
Object.defineProperty(exports, "__esModule", {
|
|
5
4
|
value: true
|
|
6
5
|
});
|
|
@@ -24,15 +23,8 @@ Object.defineProperty(exports, "getErrorForCode", {
|
|
|
24
23
|
return _httpStatusCodes.getReasonPhrase;
|
|
25
24
|
}
|
|
26
25
|
});
|
|
27
|
-
Object.defineProperty(exports, "joi", {
|
|
28
|
-
enumerable: true,
|
|
29
|
-
get: function () {
|
|
30
|
-
return _joi.default;
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
26
|
exports.newError = newError;
|
|
34
27
|
var _httpStatusCodes = require("http-status-codes");
|
|
35
|
-
var _joi = _interopRequireDefault(require("joi"));
|
|
36
28
|
/**
|
|
37
29
|
* @category Utilities
|
|
38
30
|
* @module server/errors
|
|
@@ -81,20 +73,6 @@ var _joi = _interopRequireDefault(require("joi"));
|
|
|
81
73
|
* console.log(server.errors.getErrorForCode(400)); // Prints: Bad Request
|
|
82
74
|
*/
|
|
83
75
|
|
|
84
|
-
/**
|
|
85
|
-
* @static
|
|
86
|
-
* @const joi
|
|
87
|
-
* @desc An alias for [Joi library](https://joi.dev/api/?v=17.4.0),
|
|
88
|
-
* which provides tooling for HTTP request validation. You can use it in any
|
|
89
|
-
* way you would use that library import.
|
|
90
|
-
* @example
|
|
91
|
-
* import { server } from '@dr.pogodin/react-utils';
|
|
92
|
-
* const { joi } = server.errors;
|
|
93
|
-
* const requestBodySchema = joi.object({
|
|
94
|
-
* sampleKey: joi.string().max(16).required(),
|
|
95
|
-
* });
|
|
96
|
-
*/
|
|
97
|
-
|
|
98
76
|
// TODO: It could accept the status code as a constructor argument.
|
|
99
77
|
class ErrorWithStatus extends Error {
|
|
100
78
|
status = _httpStatusCodes.StatusCodes.INTERNAL_SERVER_ERROR;
|
|
@@ -138,14 +116,14 @@ function fail(message, statusCode = _httpStatusCodes.StatusCodes.INTERNAL_SERVER
|
|
|
138
116
|
* @param [statusCode=500] HTTP status code. Defaults to 400 (Bad
|
|
139
117
|
* Request).
|
|
140
118
|
*/
|
|
141
|
-
function assert(value, schema, message = '', statusCode = _httpStatusCodes.StatusCodes.BAD_REQUEST) {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
fail(message.concat(message ? '\n' : '', error.message), statusCode);
|
|
119
|
+
async function assert(value, schema, message = '', statusCode = _httpStatusCodes.StatusCodes.BAD_REQUEST) {
|
|
120
|
+
let result = schema['~standard'].validate(value);
|
|
121
|
+
if (result instanceof Promise) result = await result;
|
|
122
|
+
if (result.issues) {
|
|
123
|
+
let error = JSON.stringify(result.issues, null, 2);
|
|
124
|
+
if (message) error = `${message}\n\n${error}`;
|
|
125
|
+
throw fail(error, statusCode);
|
|
149
126
|
}
|
|
127
|
+
return result.value;
|
|
150
128
|
}
|
|
151
129
|
//# sourceMappingURL=errors.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","names":["_httpStatusCodes","require","
|
|
1
|
+
{"version":3,"file":"errors.js","names":["_httpStatusCodes","require","ErrorWithStatus","Error","status","CODES","INTERNAL_SERVER_ERROR","newError","message","statusCode","error","fail","assert","value","schema","BAD_REQUEST","result","validate","Promise","issues","JSON","stringify"],"sources":["../../../../src/server/utils/errors.ts"],"sourcesContent":["/**\n * @category Utilities\n * @module server/errors\n * @desc\n * ```js\n * import { server } from '@dr.pogodin/react-utils';\n * const { errors } = server;\n * ```\n * Server-side helpers for error handling.\n */\n\nimport {\n StatusCodes as CODES,\n ReasonPhrases as ERRORS,\n getReasonPhrase as getErrorForCode,\n} from 'http-status-codes';\n\nimport type { StandardSchemaV1 } from '@standard-schema/spec';\n\n/**\n * @static\n * @const CODES\n * @desc An alias for\n * [StatusCodes object from **http-status-codes** library](https://www.npmjs.com/package/http-status-codes).\n * It is a map between HTTP status code names and corresponding numeric codes.\n * @example\n * import { server } from '@dr.pogodin/react-utils';\n * const { CODES } = server.errors;\n * console.log(CODES.BAD_REQUEST); // Prints: 400\n */\nexport { CODES };\n\n/**\n * @static\n * @const ERRORS\n * @desc An alias for\n * [ReasonPhrases object from **http-status-codes** library](https://www.npmjs.com/package/http-status-codes).\n * It is a map between HTTP status code names and their pretty-printed forms.\n * @example\n * import { server } from '@dr.pogodin/react-utils';\n * const { ERRORS } = server.errors;\n * console.log(ERRORS.BAD_REQUEST); // Prints: Bad Request\n */\nexport { ERRORS };\n\n/**\n * @static\n * @func getErrorForCode\n * @desc An alias for\n * [getReasonPhrase() function from **http-status-codes** library](https://www.npmjs.com/package/http-status-codes).\n * Given an HTTP code it returns the corresponding error text.\n * @param {number} code HTTP code.\n * @return {string} HTTP error text.\n * @example\n * import { server } from '@dr.pogodin/react-utils';\n * console.log(server.errors.getErrorForCode(400)); // Prints: Bad Request\n */\nexport { getErrorForCode };\n\n// TODO: It could accept the status code as a constructor argument.\nclass ErrorWithStatus extends Error {\n status: number = CODES.INTERNAL_SERVER_ERROR;\n}\n\n/**\n * ```js\n * import { server } from '@dr.pogodin/react-utils';\n * const { newError } = server.errors;\n * ```\n * Creates a new `Error` object with given message, and HTTP status code\n * attached as `status` field.\n * @param {string} message Error message.\n * @param {number} [statusCode=500] HTTP status code. Defaults to 500 (Internal\n * Server Error).\n * @return {Error}\n */\nexport function newError(\n message: string,\n statusCode = CODES.INTERNAL_SERVER_ERROR,\n): ErrorWithStatus {\n const error = new ErrorWithStatus(message);\n error.status = statusCode;\n return error;\n}\n\n/**\n * Throws an error with given message and HTTP status code.\n */\nexport function fail(\n message: string,\n statusCode: CODES = CODES.INTERNAL_SERVER_ERROR,\n): Error {\n throw newError(message, statusCode);\n}\n\n/**\n * ```js\n * import { server } from '@dr.pogodin/react-utils';\n * const { assert } = server.errors;\n * ```\n * Validates a value using given Joi schema, and throws an error with given\n * message and HTTP status code in case of the validation failure.\n * @param value\n * @param schema\n * @param [message] Error message.\n * @param [statusCode=500] HTTP status code. Defaults to 400 (Bad\n * Request).\n */\nexport async function assert<T extends StandardSchemaV1>(\n value: StandardSchemaV1.InferInput<T>,\n schema: T,\n message = '',\n statusCode = CODES.BAD_REQUEST,\n): Promise<StandardSchemaV1.InferOutput<T>> {\n let result = schema['~standard'].validate(value);\n if (result instanceof Promise) result = await result;\n\n if (result.issues) {\n let error = JSON.stringify(result.issues, null, 2);\n if (message) error = `${message}\\n\\n${error}`;\n throw fail(error, statusCode);\n }\n\n return result.value;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;AAWA,IAAAA,gBAAA,GAAAC,OAAA;AAXA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAUA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAGA;AACA,MAAMC,eAAe,SAASC,KAAK,CAAC;EAClCC,MAAM,GAAWC,4BAAK,CAACC,qBAAqB;AAC9C;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,SAASC,QAAQA,CACtBC,OAAe,EACfC,UAAU,GAAGJ,4BAAK,CAACC,qBAAqB,EACvB;EACjB,MAAMI,KAAK,GAAG,IAAIR,eAAe,CAACM,OAAO,CAAC;EAC1CE,KAAK,CAACN,MAAM,GAAGK,UAAU;EACzB,OAAOC,KAAK;AACd;;AAEA;AACA;AACA;AACO,SAASC,IAAIA,CAClBH,OAAe,EACfC,UAAiB,GAAGJ,4BAAK,CAACC,qBAAqB,EACxC;EACP,MAAMC,QAAQ,CAACC,OAAO,EAAEC,UAAU,CAAC;AACrC;;AAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACO,eAAeG,MAAMA,CAC1BC,KAAqC,EACrCC,MAAS,EACTN,OAAO,GAAG,EAAE,EACZC,UAAU,GAAGJ,4BAAK,CAACU,WAAW,EACY;EAC1C,IAAIC,MAAM,GAAGF,MAAM,CAAC,WAAW,CAAC,CAACG,QAAQ,CAACJ,KAAK,CAAC;EAChD,IAAIG,MAAM,YAAYE,OAAO,EAAEF,MAAM,GAAG,MAAMA,MAAM;EAEpD,IAAIA,MAAM,CAACG,MAAM,EAAE;IACjB,IAAIT,KAAK,GAAGU,IAAI,CAACC,SAAS,CAACL,MAAM,CAACG,MAAM,EAAE,IAAI,EAAE,CAAC,CAAC;IAClD,IAAIX,OAAO,EAAEE,KAAK,GAAG,GAAGF,OAAO,OAAOE,KAAK,EAAE;IAC7C,MAAMC,IAAI,CAACD,KAAK,EAAED,UAAU,CAAC;EAC/B;EAEA,OAAOO,MAAM,CAACH,KAAK;AACrB","ignoreList":[]}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
"use strict";
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});Object.defineProperty(exports,"CODES",{enumerable:true,get:function(){return _httpStatusCodes.StatusCodes}});Object.defineProperty(exports,"ERRORS",{enumerable:true,get:function(){return _httpStatusCodes.ReasonPhrases}});exports.assert=assert;exports.fail=fail;Object.defineProperty(exports,"getErrorForCode",{enumerable:true,get:function(){return _httpStatusCodes.getReasonPhrase}});exports.newError=newError;var _httpStatusCodes=require("http-status-codes");/**
|
|
2
2
|
* @category Utilities
|
|
3
3
|
* @module server/errors
|
|
4
4
|
* @desc
|
|
@@ -38,18 +38,6 @@
|
|
|
38
38
|
* @example
|
|
39
39
|
* import { server } from '@dr.pogodin/react-utils';
|
|
40
40
|
* console.log(server.errors.getErrorForCode(400)); // Prints: Bad Request
|
|
41
|
-
*//**
|
|
42
|
-
* @static
|
|
43
|
-
* @const joi
|
|
44
|
-
* @desc An alias for [Joi library](https://joi.dev/api/?v=17.4.0),
|
|
45
|
-
* which provides tooling for HTTP request validation. You can use it in any
|
|
46
|
-
* way you would use that library import.
|
|
47
|
-
* @example
|
|
48
|
-
* import { server } from '@dr.pogodin/react-utils';
|
|
49
|
-
* const { joi } = server.errors;
|
|
50
|
-
* const requestBodySchema = joi.object({
|
|
51
|
-
* sampleKey: joi.string().max(16).required(),
|
|
52
|
-
* });
|
|
53
41
|
*/// TODO: It could accept the status code as a constructor argument.
|
|
54
42
|
class ErrorWithStatus extends Error{status=_httpStatusCodes.StatusCodes.INTERNAL_SERVER_ERROR}/**
|
|
55
43
|
* ```js
|
|
@@ -76,5 +64,5 @@ class ErrorWithStatus extends Error{status=_httpStatusCodes.StatusCodes.INTERNAL
|
|
|
76
64
|
* @param [message] Error message.
|
|
77
65
|
* @param [statusCode=500] HTTP status code. Defaults to 400 (Bad
|
|
78
66
|
* Request).
|
|
79
|
-
*/function assert(value,schema,message="",statusCode=_httpStatusCodes.StatusCodes.BAD_REQUEST){
|
|
67
|
+
*/async function assert(value,schema,message="",statusCode=_httpStatusCodes.StatusCodes.BAD_REQUEST){let result=schema["~standard"].validate(value);if(result instanceof Promise)result=await result;if(result.issues){let error=JSON.stringify(result.issues,null,2);if(message)error=`${message}\n\n${error}`;throw fail(error,statusCode)}return result.value}
|
|
80
68
|
//# sourceMappingURL=errors.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","names":["_httpStatusCodes","require","
|
|
1
|
+
{"version":3,"file":"errors.js","names":["_httpStatusCodes","require","ErrorWithStatus","Error","status","CODES","INTERNAL_SERVER_ERROR","newError","message","statusCode","error","fail","assert","value","schema","BAD_REQUEST","result","validate","Promise","issues","JSON","stringify"],"sources":["../../../../src/server/utils/errors.ts"],"sourcesContent":["/**\n * @category Utilities\n * @module server/errors\n * @desc\n * ```js\n * import { server } from '@dr.pogodin/react-utils';\n * const { errors } = server;\n * ```\n * Server-side helpers for error handling.\n */\n\nimport {\n StatusCodes as CODES,\n ReasonPhrases as ERRORS,\n getReasonPhrase as getErrorForCode,\n} from 'http-status-codes';\n\nimport type { StandardSchemaV1 } from '@standard-schema/spec';\n\n/**\n * @static\n * @const CODES\n * @desc An alias for\n * [StatusCodes object from **http-status-codes** library](https://www.npmjs.com/package/http-status-codes).\n * It is a map between HTTP status code names and corresponding numeric codes.\n * @example\n * import { server } from '@dr.pogodin/react-utils';\n * const { CODES } = server.errors;\n * console.log(CODES.BAD_REQUEST); // Prints: 400\n */\nexport { CODES };\n\n/**\n * @static\n * @const ERRORS\n * @desc An alias for\n * [ReasonPhrases object from **http-status-codes** library](https://www.npmjs.com/package/http-status-codes).\n * It is a map between HTTP status code names and their pretty-printed forms.\n * @example\n * import { server } from '@dr.pogodin/react-utils';\n * const { ERRORS } = server.errors;\n * console.log(ERRORS.BAD_REQUEST); // Prints: Bad Request\n */\nexport { ERRORS };\n\n/**\n * @static\n * @func getErrorForCode\n * @desc An alias for\n * [getReasonPhrase() function from **http-status-codes** library](https://www.npmjs.com/package/http-status-codes).\n * Given an HTTP code it returns the corresponding error text.\n * @param {number} code HTTP code.\n * @return {string} HTTP error text.\n * @example\n * import { server } from '@dr.pogodin/react-utils';\n * console.log(server.errors.getErrorForCode(400)); // Prints: Bad Request\n */\nexport { getErrorForCode };\n\n// TODO: It could accept the status code as a constructor argument.\nclass ErrorWithStatus extends Error {\n status: number = CODES.INTERNAL_SERVER_ERROR;\n}\n\n/**\n * ```js\n * import { server } from '@dr.pogodin/react-utils';\n * const { newError } = server.errors;\n * ```\n * Creates a new `Error` object with given message, and HTTP status code\n * attached as `status` field.\n * @param {string} message Error message.\n * @param {number} [statusCode=500] HTTP status code. Defaults to 500 (Internal\n * Server Error).\n * @return {Error}\n */\nexport function newError(\n message: string,\n statusCode = CODES.INTERNAL_SERVER_ERROR,\n): ErrorWithStatus {\n const error = new ErrorWithStatus(message);\n error.status = statusCode;\n return error;\n}\n\n/**\n * Throws an error with given message and HTTP status code.\n */\nexport function fail(\n message: string,\n statusCode: CODES = CODES.INTERNAL_SERVER_ERROR,\n): Error {\n throw newError(message, statusCode);\n}\n\n/**\n * ```js\n * import { server } from '@dr.pogodin/react-utils';\n * const { assert } = server.errors;\n * ```\n * Validates a value using given Joi schema, and throws an error with given\n * message and HTTP status code in case of the validation failure.\n * @param value\n * @param schema\n * @param [message] Error message.\n * @param [statusCode=500] HTTP status code. Defaults to 400 (Bad\n * Request).\n */\nexport async function assert<T extends StandardSchemaV1>(\n value: StandardSchemaV1.InferInput<T>,\n schema: T,\n message = '',\n statusCode = CODES.BAD_REQUEST,\n): Promise<StandardSchemaV1.InferOutput<T>> {\n let result = schema['~standard'].validate(value);\n if (result instanceof Promise) result = await result;\n\n if (result.issues) {\n let error = JSON.stringify(result.issues, null, 2);\n if (message) error = `${message}\\n\\n${error}`;\n throw fail(error, statusCode);\n }\n\n return result.value;\n}\n"],"mappings":"geAWA,IAAAA,gBAAA,CAAAC,OAAA,sBAXA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,GAUA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,GAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,GAGA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,GAGA;AACA,KAAM,CAAAC,eAAe,QAAS,CAAAC,KAAM,CAClCC,MAAM,CAAWC,4BAAK,CAACC,qBACzB,CAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,GACO,QAAS,CAAAC,QAAQA,CACtBC,OAAe,CACfC,UAAU,CAAGJ,4BAAK,CAACC,qBAAqB,CACvB,CACjB,KAAM,CAAAI,KAAK,CAAG,GAAI,CAAAR,eAAe,CAACM,OAAO,CAAC,CAC1CE,KAAK,CAACN,MAAM,CAAGK,UAAU,CACzB,MAAO,CAAAC,KACT,CAEA;AACA;AACA,GACO,QAAS,CAAAC,IAAIA,CAClBH,OAAe,CACfC,UAAiB,CAAGJ,4BAAK,CAACC,qBAAqB,CACxC,CACP,KAAM,CAAAC,QAAQ,CAACC,OAAO,CAAEC,UAAU,CACpC,CAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,GACO,cAAe,CAAAG,MAAMA,CAC1BC,KAAqC,CACrCC,MAAS,CACTN,OAAO,CAAG,EAAE,CACZC,UAAU,CAAGJ,4BAAK,CAACU,WAAW,CACY,CAC1C,GAAI,CAAAC,MAAM,CAAGF,MAAM,CAAC,WAAW,CAAC,CAACG,QAAQ,CAACJ,KAAK,CAAC,CAChD,GAAIG,MAAM,WAAY,CAAAE,OAAO,CAAEF,MAAM,CAAG,KAAM,CAAAA,MAAM,CAEpD,GAAIA,MAAM,CAACG,MAAM,CAAE,CACjB,GAAI,CAAAT,KAAK,CAAGU,IAAI,CAACC,SAAS,CAACL,MAAM,CAACG,MAAM,CAAE,IAAI,CAAE,CAAC,CAAC,CAClD,GAAIX,OAAO,CAAEE,KAAK,CAAG,GAAGF,OAAO,OAAOE,KAAK,EAAE,CAC7C,KAAM,CAAAC,IAAI,CAACD,KAAK,CAAED,UAAU,CAC9B,CAEA,MAAO,CAAAO,MAAM,CAACH,KAChB","ignoreList":[]}
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* Server-side helpers for error handling.
|
|
10
10
|
*/
|
|
11
11
|
import { StatusCodes as CODES, ReasonPhrases as ERRORS, getReasonPhrase as getErrorForCode } from 'http-status-codes';
|
|
12
|
-
import
|
|
12
|
+
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
13
13
|
/**
|
|
14
14
|
* @static
|
|
15
15
|
* @const CODES
|
|
@@ -47,20 +47,6 @@ export { ERRORS };
|
|
|
47
47
|
* console.log(server.errors.getErrorForCode(400)); // Prints: Bad Request
|
|
48
48
|
*/
|
|
49
49
|
export { getErrorForCode };
|
|
50
|
-
/**
|
|
51
|
-
* @static
|
|
52
|
-
* @const joi
|
|
53
|
-
* @desc An alias for [Joi library](https://joi.dev/api/?v=17.4.0),
|
|
54
|
-
* which provides tooling for HTTP request validation. You can use it in any
|
|
55
|
-
* way you would use that library import.
|
|
56
|
-
* @example
|
|
57
|
-
* import { server } from '@dr.pogodin/react-utils';
|
|
58
|
-
* const { joi } = server.errors;
|
|
59
|
-
* const requestBodySchema = joi.object({
|
|
60
|
-
* sampleKey: joi.string().max(16).required(),
|
|
61
|
-
* });
|
|
62
|
-
*/
|
|
63
|
-
export { joi };
|
|
64
50
|
declare class ErrorWithStatus extends Error {
|
|
65
51
|
status: number;
|
|
66
52
|
}
|
|
@@ -94,4 +80,4 @@ export declare function fail(message: string, statusCode?: CODES): Error;
|
|
|
94
80
|
* @param [statusCode=500] HTTP status code. Defaults to 400 (Bad
|
|
95
81
|
* Request).
|
|
96
82
|
*/
|
|
97
|
-
export declare function assert(value:
|
|
83
|
+
export declare function assert<T extends StandardSchemaV1>(value: StandardSchemaV1.InferInput<T>, schema: T, message?: string, statusCode?: CODES): Promise<StandardSchemaV1.InferOutput<T>>;
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "1.
|
|
2
|
+
"version": "1.44.0",
|
|
3
3
|
"bin": {
|
|
4
4
|
"react-utils-build": "bin/build.js",
|
|
5
5
|
"react-utils-setup": "bin/setup.js"
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"@dr.pogodin/babel-plugin-react-css-modules": "^6.13.7",
|
|
13
13
|
"@dr.pogodin/csurf": "^1.16.5",
|
|
14
14
|
"@dr.pogodin/js-utils": "^0.1.3",
|
|
15
|
-
"@dr.pogodin/react-global-state": "^0.19.
|
|
15
|
+
"@dr.pogodin/react-global-state": "^0.19.4",
|
|
16
16
|
"@dr.pogodin/react-helmet": "^3.0.2",
|
|
17
17
|
"@dr.pogodin/react-themes": "^1.9.2",
|
|
18
18
|
"@jest/environment": "^30.0.5",
|
|
@@ -27,7 +27,6 @@
|
|
|
27
27
|
"express": "^5.1.0",
|
|
28
28
|
"helmet": "^8.1.0",
|
|
29
29
|
"http-status-codes": "^2.3.0",
|
|
30
|
-
"joi": "^18.0.0",
|
|
31
30
|
"lodash": "^4.17.21",
|
|
32
31
|
"morgan": "^1.10.1",
|
|
33
32
|
"node-forge": "^1.3.1",
|
|
@@ -58,6 +57,7 @@
|
|
|
58
57
|
"@dr.pogodin/babel-preset-svgr": "^1.9.2",
|
|
59
58
|
"@dr.pogodin/eslint-configs": "^0.0.11",
|
|
60
59
|
"@pmmmwh/react-refresh-webpack-plugin": "^0.6.1",
|
|
60
|
+
"@standard-schema/spec": "^1.0.0",
|
|
61
61
|
"@testing-library/dom": "^10.4.1",
|
|
62
62
|
"@testing-library/react": "^16.3.0",
|
|
63
63
|
"@testing-library/user-event": "^14.6.1",
|
|
@@ -72,14 +72,14 @@
|
|
|
72
72
|
"@types/morgan": "^1.9.10",
|
|
73
73
|
"@types/node-forge": "^1.3.13",
|
|
74
74
|
"@types/pretty": "^2.0.3",
|
|
75
|
-
"@types/react": "^19.1.
|
|
75
|
+
"@types/react": "^19.1.10",
|
|
76
76
|
"@types/react-dom": "^19.1.7",
|
|
77
77
|
"@types/request-ip": "^0.0.41",
|
|
78
78
|
"@types/serialize-javascript": "^5.0.4",
|
|
79
79
|
"@types/serve-favicon": "^2.5.7",
|
|
80
80
|
"@types/supertest": "^6.0.3",
|
|
81
81
|
"@types/webpack": "^5.28.5",
|
|
82
|
-
"@types/webpack-hot-middleware": "^2.25.
|
|
82
|
+
"@types/webpack-hot-middleware": "^2.25.10",
|
|
83
83
|
"autoprefixer": "^10.4.21",
|
|
84
84
|
"babel-jest": "^30.0.5",
|
|
85
85
|
"babel-loader": "^10.0.0",
|
|
@@ -91,7 +91,7 @@
|
|
|
91
91
|
"jest": "^30.0.5",
|
|
92
92
|
"jest-environment-jsdom": "^30.0.5",
|
|
93
93
|
"memfs": "^4.36.0",
|
|
94
|
-
"mini-css-extract-plugin": "^2.9.
|
|
94
|
+
"mini-css-extract-plugin": "^2.9.4",
|
|
95
95
|
"mockdate": "^3.0.5",
|
|
96
96
|
"nodelist-foreach-polyfill": "^1.2.0",
|
|
97
97
|
"postcss": "^8.5.6",
|
|
@@ -112,7 +112,7 @@
|
|
|
112
112
|
"tstyche": "^4.3.0",
|
|
113
113
|
"typed-scss-modules": "^8.1.1",
|
|
114
114
|
"typescript": "^5.9.2",
|
|
115
|
-
"webpack": "^5.101.
|
|
115
|
+
"webpack": "^5.101.1",
|
|
116
116
|
"webpack-dev-middleware": "^7.4.2",
|
|
117
117
|
"webpack-hot-middleware": "^2.26.1",
|
|
118
118
|
"webpack-merge": "^6.0.1",
|
|
@@ -15,7 +15,7 @@ import {
|
|
|
15
15
|
getReasonPhrase as getErrorForCode,
|
|
16
16
|
} from 'http-status-codes';
|
|
17
17
|
|
|
18
|
-
import
|
|
18
|
+
import type { StandardSchemaV1 } from '@standard-schema/spec';
|
|
19
19
|
|
|
20
20
|
/**
|
|
21
21
|
* @static
|
|
@@ -57,21 +57,6 @@ export { ERRORS };
|
|
|
57
57
|
*/
|
|
58
58
|
export { getErrorForCode };
|
|
59
59
|
|
|
60
|
-
/**
|
|
61
|
-
* @static
|
|
62
|
-
* @const joi
|
|
63
|
-
* @desc An alias for [Joi library](https://joi.dev/api/?v=17.4.0),
|
|
64
|
-
* which provides tooling for HTTP request validation. You can use it in any
|
|
65
|
-
* way you would use that library import.
|
|
66
|
-
* @example
|
|
67
|
-
* import { server } from '@dr.pogodin/react-utils';
|
|
68
|
-
* const { joi } = server.errors;
|
|
69
|
-
* const requestBodySchema = joi.object({
|
|
70
|
-
* sampleKey: joi.string().max(16).required(),
|
|
71
|
-
* });
|
|
72
|
-
*/
|
|
73
|
-
export { joi };
|
|
74
|
-
|
|
75
60
|
// TODO: It could accept the status code as a constructor argument.
|
|
76
61
|
class ErrorWithStatus extends Error {
|
|
77
62
|
status: number = CODES.INTERNAL_SERVER_ERROR;
|
|
@@ -121,14 +106,20 @@ export function fail(
|
|
|
121
106
|
* @param [statusCode=500] HTTP status code. Defaults to 400 (Bad
|
|
122
107
|
* Request).
|
|
123
108
|
*/
|
|
124
|
-
export function assert(
|
|
125
|
-
value:
|
|
126
|
-
schema:
|
|
109
|
+
export async function assert<T extends StandardSchemaV1>(
|
|
110
|
+
value: StandardSchemaV1.InferInput<T>,
|
|
111
|
+
schema: T,
|
|
127
112
|
message = '',
|
|
128
113
|
statusCode = CODES.BAD_REQUEST,
|
|
129
|
-
):
|
|
130
|
-
|
|
131
|
-
if (
|
|
132
|
-
|
|
114
|
+
): Promise<StandardSchemaV1.InferOutput<T>> {
|
|
115
|
+
let result = schema['~standard'].validate(value);
|
|
116
|
+
if (result instanceof Promise) result = await result;
|
|
117
|
+
|
|
118
|
+
if (result.issues) {
|
|
119
|
+
let error = JSON.stringify(result.issues, null, 2);
|
|
120
|
+
if (message) error = `${message}\n\n${error}`;
|
|
121
|
+
throw fail(error, statusCode);
|
|
133
122
|
}
|
|
123
|
+
|
|
124
|
+
return result.value;
|
|
134
125
|
}
|