@dr.pogodin/react-utils 1.43.34 → 1.44.1

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,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;
@@ -126,26 +104,23 @@ function fail(message, statusCode = _httpStatusCodes.StatusCodes.INTERNAL_SERVER
126
104
  }
127
105
 
128
106
  /**
129
- * ```js
130
- * import { server } from '@dr.pogodin/react-utils';
131
- * const { assert } = server.errors;
132
- * ```
133
- * Validates a value using given Joi schema, and throws an error with given
134
- * message and HTTP status code in case of the validation failure.
135
- * @param value
136
- * @param schema
137
- * @param [message] Error message.
138
- * @param [statusCode=500] HTTP status code. Defaults to 400 (Bad
139
- * Request).
107
+ * Validates the `value` against the given "standard" validation `schema`.
108
+ * Resolves to the correctly typed `value`, if it has passed the validation;
109
+ * otherwise throws an error.
110
+ * @param value The value to validate.
111
+ * @param schema The "standard" validation schema to use.
112
+ * @param [message] Optional error message, to prepend the validation error
113
+ * message.
114
+ * @param [statusCode=400] HTTP status code. Defaults to 400 (Bad Request).
140
115
  */
141
- function assert(value, schema, message = '', statusCode = _httpStatusCodes.StatusCodes.BAD_REQUEST) {
142
- const {
143
- error
144
- } = schema.validate(value, {
145
- abortEarly: false
146
- });
147
- if (error) {
148
- fail(message.concat(message ? '\n' : '', error.message), statusCode);
116
+ async function assert(value, schema, message = '', statusCode = _httpStatusCodes.StatusCodes.BAD_REQUEST) {
117
+ let result = schema['~standard'].validate(value);
118
+ if (result instanceof Promise) result = await result;
119
+ if (result.issues) {
120
+ let error = JSON.stringify(result.issues, null, 2);
121
+ if (message) error = `${message}\n\n${error}`;
122
+ throw fail(error, statusCode);
149
123
  }
124
+ return result.value;
150
125
  }
151
126
  //# sourceMappingURL=errors.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"errors.js","names":["_httpStatusCodes","require","_joi","_interopRequireDefault","ErrorWithStatus","Error","status","CODES","INTERNAL_SERVER_ERROR","newError","message","statusCode","error","fail","assert","value","schema","BAD_REQUEST","validate","abortEarly","concat"],"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 joi from 'joi';\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/**\n * @static\n * @const joi\n * @desc An alias for [Joi library](https://joi.dev/api/?v=17.4.0),\n * which provides tooling for HTTP request validation. You can use it in any\n * way you would use that library import.\n * @example\n * import { server } from '@dr.pogodin/react-utils';\n * const { joi } = server.errors;\n * const requestBodySchema = joi.object({\n * sampleKey: joi.string().max(16).required(),\n * });\n */\nexport { joi };\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 function assert(\n value: unknown,\n schema: joi.AnySchema,\n message = '',\n statusCode = CODES.BAD_REQUEST,\n): void {\n const { error } = schema.validate(value, { abortEarly: false });\n if (error) {\n fail(message.concat(message ? '\\n' : '', error.message), statusCode);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAWA,IAAAA,gBAAA,GAAAC,OAAA;AAMA,IAAAC,IAAA,GAAAC,sBAAA,CAAAF,OAAA;AAjBA;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;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAGA;AACA,MAAMG,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,SAASG,MAAMA,CACpBC,KAAc,EACdC,MAAqB,EACrBN,OAAO,GAAG,EAAE,EACZC,UAAU,GAAGJ,4BAAK,CAACU,WAAW,EACxB;EACN,MAAM;IAAEL;EAAM,CAAC,GAAGI,MAAM,CAACE,QAAQ,CAACH,KAAK,EAAE;IAAEI,UAAU,EAAE;EAAM,CAAC,CAAC;EAC/D,IAAIP,KAAK,EAAE;IACTC,IAAI,CAACH,OAAO,CAACU,MAAM,CAACV,OAAO,GAAG,IAAI,GAAG,EAAE,EAAEE,KAAK,CAACF,OAAO,CAAC,EAAEC,UAAU,CAAC;EACtE;AACF","ignoreList":[]}
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 * Validates the `value` against the given \"standard\" validation `schema`.\n * Resolves to the correctly typed `value`, if it has passed the validation;\n * otherwise throws an error.\n * @param value The value to validate.\n * @param schema The \"standard\" validation schema to use.\n * @param [message] Optional error message, to prepend the validation error\n * message.\n * @param [statusCode=400] HTTP status code. Defaults to 400 (Bad Request).\n */\nexport async function assert<T extends StandardSchemaV1>(\n value: unknown,\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;AACO,eAAeG,MAAMA,CAC1BC,KAAc,EACdC,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";var _interopRequireDefault=require("@babel/runtime/helpers/interopRequireDefault");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}});Object.defineProperty(exports,"joi",{enumerable:true,get:function(){return _joi.default}});exports.newError=newError;var _httpStatusCodes=require("http-status-codes");var _joi=_interopRequireDefault(require("joi"));/**
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
@@ -65,16 +53,13 @@ class ErrorWithStatus extends Error{status=_httpStatusCodes.StatusCodes.INTERNAL
65
53
  */function newError(message,statusCode=_httpStatusCodes.StatusCodes.INTERNAL_SERVER_ERROR){const error=new ErrorWithStatus(message);error.status=statusCode;return error}/**
66
54
  * Throws an error with given message and HTTP status code.
67
55
  */function fail(message,statusCode=_httpStatusCodes.StatusCodes.INTERNAL_SERVER_ERROR){throw newError(message,statusCode)}/**
68
- * ```js
69
- * import { server } from '@dr.pogodin/react-utils';
70
- * const { assert } = server.errors;
71
- * ```
72
- * Validates a value using given Joi schema, and throws an error with given
73
- * message and HTTP status code in case of the validation failure.
74
- * @param value
75
- * @param schema
76
- * @param [message] Error message.
77
- * @param [statusCode=500] HTTP status code. Defaults to 400 (Bad
78
- * Request).
79
- */function assert(value,schema,message="",statusCode=_httpStatusCodes.StatusCodes.BAD_REQUEST){const{error}=schema.validate(value,{abortEarly:false});if(error){fail(message.concat(message?"\n":"",error.message),statusCode)}}
56
+ * Validates the `value` against the given "standard" validation `schema`.
57
+ * Resolves to the correctly typed `value`, if it has passed the validation;
58
+ * otherwise throws an error.
59
+ * @param value The value to validate.
60
+ * @param schema The "standard" validation schema to use.
61
+ * @param [message] Optional error message, to prepend the validation error
62
+ * message.
63
+ * @param [statusCode=400] HTTP status code. Defaults to 400 (Bad Request).
64
+ */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
65
  //# sourceMappingURL=errors.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"errors.js","names":["_httpStatusCodes","require","_joi","_interopRequireDefault","ErrorWithStatus","Error","status","CODES","INTERNAL_SERVER_ERROR","newError","message","statusCode","error","fail","assert","value","schema","BAD_REQUEST","validate","abortEarly","concat"],"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 joi from 'joi';\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/**\n * @static\n * @const joi\n * @desc An alias for [Joi library](https://joi.dev/api/?v=17.4.0),\n * which provides tooling for HTTP request validation. You can use it in any\n * way you would use that library import.\n * @example\n * import { server } from '@dr.pogodin/react-utils';\n * const { joi } = server.errors;\n * const requestBodySchema = joi.object({\n * sampleKey: joi.string().max(16).required(),\n * });\n */\nexport { joi };\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 function assert(\n value: unknown,\n schema: joi.AnySchema,\n message = '',\n statusCode = CODES.BAD_REQUEST,\n): void {\n const { error } = schema.validate(value, { abortEarly: false });\n if (error) {\n fail(message.concat(message ? '\\n' : '', error.message), statusCode);\n }\n}\n"],"mappings":"8oBAWA,IAAAA,gBAAA,CAAAC,OAAA,sBAMA,IAAAC,IAAA,CAAAC,sBAAA,CAAAF,OAAA,SAjBA;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;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA,GAGA;AACA,KAAM,CAAAG,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,QAAS,CAAAG,MAAMA,CACpBC,KAAc,CACdC,MAAqB,CACrBN,OAAO,CAAG,EAAE,CACZC,UAAU,CAAGJ,4BAAK,CAACU,WAAW,CACxB,CACN,KAAM,CAAEL,KAAM,CAAC,CAAGI,MAAM,CAACE,QAAQ,CAACH,KAAK,CAAE,CAAEI,UAAU,CAAE,KAAM,CAAC,CAAC,CAC/D,GAAIP,KAAK,CAAE,CACTC,IAAI,CAACH,OAAO,CAACU,MAAM,CAACV,OAAO,CAAG,IAAI,CAAG,EAAE,CAAEE,KAAK,CAACF,OAAO,CAAC,CAAEC,UAAU,CACrE,CACF","ignoreList":[]}
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 * Validates the `value` against the given \"standard\" validation `schema`.\n * Resolves to the correctly typed `value`, if it has passed the validation;\n * otherwise throws an error.\n * @param value The value to validate.\n * @param schema The \"standard\" validation schema to use.\n * @param [message] Optional error message, to prepend the validation error\n * message.\n * @param [statusCode=400] HTTP status code. Defaults to 400 (Bad Request).\n */\nexport async function assert<T extends StandardSchemaV1>(\n value: unknown,\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,GACO,cAAe,CAAAG,MAAMA,CAC1BC,KAAc,CACdC,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 joi from 'joi';
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
  }
@@ -82,16 +68,13 @@ export declare function newError(message: string, statusCode?: CODES): ErrorWith
82
68
  */
83
69
  export declare function fail(message: string, statusCode?: CODES): Error;
84
70
  /**
85
- * ```js
86
- * import { server } from '@dr.pogodin/react-utils';
87
- * const { assert } = server.errors;
88
- * ```
89
- * Validates a value using given Joi schema, and throws an error with given
90
- * message and HTTP status code in case of the validation failure.
91
- * @param value
92
- * @param schema
93
- * @param [message] Error message.
94
- * @param [statusCode=500] HTTP status code. Defaults to 400 (Bad
95
- * Request).
71
+ * Validates the `value` against the given "standard" validation `schema`.
72
+ * Resolves to the correctly typed `value`, if it has passed the validation;
73
+ * otherwise throws an error.
74
+ * @param value The value to validate.
75
+ * @param schema The "standard" validation schema to use.
76
+ * @param [message] Optional error message, to prepend the validation error
77
+ * message.
78
+ * @param [statusCode=400] HTTP status code. Defaults to 400 (Bad Request).
96
79
  */
97
- export declare function assert(value: unknown, schema: joi.AnySchema, message?: string, statusCode?: CODES): void;
80
+ export declare function assert<T extends StandardSchemaV1>(value: unknown, schema: T, message?: string, statusCode?: CODES): Promise<StandardSchemaV1.InferOutput<T>>;
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.43.34",
2
+ "version": "1.44.1",
3
3
  "bin": {
4
4
  "react-utils-build": "bin/build.js",
5
5
  "react-utils-setup": "bin/setup.js"
@@ -8,11 +8,11 @@
8
8
  "url": "https://github.com/birdofpreyru/react-utils/issues"
9
9
  },
10
10
  "dependencies": {
11
- "@babel/runtime": "^7.28.2",
11
+ "@babel/runtime": "^7.28.3",
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.3",
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",
@@ -46,18 +45,19 @@
46
45
  },
47
46
  "description": "Collection of generic ReactJS components and utils",
48
47
  "devDependencies": {
49
- "@babel/cli": "^7.28.0",
50
- "@babel/core": "^7.28.0",
48
+ "@babel/cli": "^7.28.3",
49
+ "@babel/core": "^7.28.3",
51
50
  "@babel/node": "^7.28.0",
52
- "@babel/plugin-transform-runtime": "^7.28.0",
53
- "@babel/preset-env": "^7.28.0",
51
+ "@babel/plugin-transform-runtime": "^7.28.3",
52
+ "@babel/preset-env": "^7.28.3",
54
53
  "@babel/preset-react": "^7.27.1",
55
54
  "@babel/preset-typescript": "^7.27.1",
56
- "@babel/register": "^7.27.1",
55
+ "@babel/register": "^7.27.3",
57
56
  "@dr.pogodin/babel-plugin-transform-assets": "^1.2.5",
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.9",
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.9",
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.3",
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.0",
115
+ "webpack": "^5.101.2",
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 joi from 'joi';
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;
@@ -109,26 +94,29 @@ export function fail(
109
94
  }
110
95
 
111
96
  /**
112
- * ```js
113
- * import { server } from '@dr.pogodin/react-utils';
114
- * const { assert } = server.errors;
115
- * ```
116
- * Validates a value using given Joi schema, and throws an error with given
117
- * message and HTTP status code in case of the validation failure.
118
- * @param value
119
- * @param schema
120
- * @param [message] Error message.
121
- * @param [statusCode=500] HTTP status code. Defaults to 400 (Bad
122
- * Request).
97
+ * Validates the `value` against the given "standard" validation `schema`.
98
+ * Resolves to the correctly typed `value`, if it has passed the validation;
99
+ * otherwise throws an error.
100
+ * @param value The value to validate.
101
+ * @param schema The "standard" validation schema to use.
102
+ * @param [message] Optional error message, to prepend the validation error
103
+ * message.
104
+ * @param [statusCode=400] HTTP status code. Defaults to 400 (Bad Request).
123
105
  */
124
- export function assert(
106
+ export async function assert<T extends StandardSchemaV1>(
125
107
  value: unknown,
126
- schema: joi.AnySchema,
108
+ schema: T,
127
109
  message = '',
128
110
  statusCode = CODES.BAD_REQUEST,
129
- ): void {
130
- const { error } = schema.validate(value, { abortEarly: false });
131
- if (error) {
132
- fail(message.concat(message ? '\n' : '', error.message), statusCode);
111
+ ): Promise<StandardSchemaV1.InferOutput<T>> {
112
+ let result = schema['~standard'].validate(value);
113
+ if (result instanceof Promise) result = await result;
114
+
115
+ if (result.issues) {
116
+ let error = JSON.stringify(result.issues, null, 2);
117
+ if (message) error = `${message}\n\n${error}`;
118
+ throw fail(error, statusCode);
133
119
  }
120
+
121
+ return result.value;
134
122
  }