@beecode/msh-util 1.0.1-alpha

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.
Files changed (75) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +25 -0
  3. package/lib/class-factory-pattern.d.ts +25 -0
  4. package/lib/class-factory-pattern.d.ts.map +1 -0
  5. package/lib/class-factory-pattern.js +30 -0
  6. package/lib/class-factory-pattern.js.map +1 -0
  7. package/lib/express/error-handler.d.ts +17 -0
  8. package/lib/express/error-handler.d.ts.map +1 -0
  9. package/lib/express/error-handler.js +28 -0
  10. package/lib/express/error-handler.js.map +1 -0
  11. package/lib/index.d.ts +14 -0
  12. package/lib/index.d.ts.map +1 -0
  13. package/lib/index.js +30 -0
  14. package/lib/index.js.map +1 -0
  15. package/lib/joi-util.d.ts +47 -0
  16. package/lib/joi-util.d.ts.map +1 -0
  17. package/lib/joi-util.js +63 -0
  18. package/lib/joi-util.js.map +1 -0
  19. package/lib/memoize-factory.d.ts +16 -0
  20. package/lib/memoize-factory.d.ts.map +1 -0
  21. package/lib/memoize-factory.js +28 -0
  22. package/lib/memoize-factory.js.map +1 -0
  23. package/lib/object-util.d.ts +70 -0
  24. package/lib/object-util.d.ts.map +1 -0
  25. package/lib/object-util.js +114 -0
  26. package/lib/object-util.js.map +1 -0
  27. package/lib/regex-util.d.ts +12 -0
  28. package/lib/regex-util.d.ts.map +1 -0
  29. package/lib/regex-util.js +15 -0
  30. package/lib/regex-util.js.map +1 -0
  31. package/lib/single-threshold-promise.d.ts +31 -0
  32. package/lib/single-threshold-promise.d.ts.map +1 -0
  33. package/lib/single-threshold-promise.js +49 -0
  34. package/lib/single-threshold-promise.js.map +1 -0
  35. package/lib/singleton/async.d.ts +50 -0
  36. package/lib/singleton/async.d.ts.map +1 -0
  37. package/lib/singleton/async.js +78 -0
  38. package/lib/singleton/async.js.map +1 -0
  39. package/lib/singleton/pattern.d.ts +34 -0
  40. package/lib/singleton/pattern.d.ts.map +1 -0
  41. package/lib/singleton/pattern.js +45 -0
  42. package/lib/singleton/pattern.js.map +1 -0
  43. package/lib/string-util.d.ts +10 -0
  44. package/lib/string-util.d.ts.map +1 -0
  45. package/lib/string-util.js +19 -0
  46. package/lib/string-util.js.map +1 -0
  47. package/lib/time-util.d.ts +74 -0
  48. package/lib/time-util.d.ts.map +1 -0
  49. package/lib/time-util.js +97 -0
  50. package/lib/time-util.js.map +1 -0
  51. package/lib/timeout.d.ts +15 -0
  52. package/lib/timeout.d.ts.map +1 -0
  53. package/lib/timeout.js +21 -0
  54. package/lib/timeout.js.map +1 -0
  55. package/lib/type-util.d.ts +50 -0
  56. package/lib/type-util.d.ts.map +1 -0
  57. package/lib/type-util.js +57 -0
  58. package/lib/type-util.js.map +1 -0
  59. package/lib/types/any-function/index.d.ts +2 -0
  60. package/lib/types/any-function/index.d.ts.map +1 -0
  61. package/lib/types/any-function/index.js +3 -0
  62. package/lib/types/any-function/index.js.map +1 -0
  63. package/lib/types/any-function/no-params.d.ts +2 -0
  64. package/lib/types/any-function/no-params.d.ts.map +1 -0
  65. package/lib/types/any-function/no-params.js +3 -0
  66. package/lib/types/any-function/no-params.js.map +1 -0
  67. package/lib/types/any-function/promise-no-params.d.ts +2 -0
  68. package/lib/types/any-function/promise-no-params.d.ts.map +1 -0
  69. package/lib/types/any-function/promise-no-params.js +3 -0
  70. package/lib/types/any-function/promise-no-params.js.map +1 -0
  71. package/lib/types/any-function/promise.d.ts +2 -0
  72. package/lib/types/any-function/promise.d.ts.map +1 -0
  73. package/lib/types/any-function/promise.js +3 -0
  74. package/lib/types/any-function/promise.js.map +1 -0
  75. package/package.json +127 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2020 Beecode
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,25 @@
1
+ [![Build Status](https://beecode.semaphoreci.com/badges/msh-util/branches/main.svg?style=shields)](https://beecode.semaphoreci.com/projects/msh-util)
2
+ [![codecov](https://codecov.io/gh/beecode-rs/msh-util/branch/main/graph/badge.svg?token=fHc0YaxEiB)](https://codecov.io/gh/beecode-rs/msh-util)
3
+ [![GitHub license](https://img.shields.io/github/license/beecode-rs/msh-util)](https://github.com/beecode-rs/msh-util/blob/main/LICENSE)
4
+ [![NPM](https://nodei.co/npm/@beecode/msh-util.png)](https://nodei.co/npm/@beecode/msh-util)
5
+
6
+ # msh-util
7
+
8
+ Micro-service helper: node error
9
+
10
+ This project is intended to be used in typescript project.
11
+
12
+ <!-- toc -->
13
+
14
+ - [Install](#install)
15
+ - [Diagram](#diagram)
16
+
17
+ <!-- tocstop -->
18
+
19
+ ## Install
20
+
21
+ `npm i @beecode/msh-util`
22
+
23
+ ## Diagram
24
+
25
+ ![vision-diagram](resource/doc/vision/vision.svg)
@@ -0,0 +1,25 @@
1
+ export type ClassType<T = object> = new (...args: T extends {
2
+ new (...args: infer P): any;
3
+ } ? P : never[]) => T;
4
+ /**
5
+ * This is a wrapper that easily converts class constructor call (`new className(..constructorParams)`) into function call (`classNameFactory(..constructorParams)`)
6
+ * @param {C} classType
7
+ * @template C
8
+ * @return {(...args: ConstructorParameters<C>) => InstanceType<C>}
9
+ * @example
10
+ * export class SomeClass {
11
+ * protected _a: string
12
+ *
13
+ * constructor(params: { a: string }) {
14
+ * const { a } = params
15
+ * this._a = a
16
+ * }
17
+ * }
18
+ *
19
+ * export const someClassFactory = classFactoryPattern(SomeClass)
20
+ *
21
+ * // using
22
+ * const someClassInstance = someClassFactory({ a: 'test' })
23
+ */
24
+ export declare const classFactoryPattern: <C extends ClassType<object>>(classType: C) => (...args: ConstructorParameters<C>) => InstanceType<C>;
25
+ //# sourceMappingURL=class-factory-pattern.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"class-factory-pattern.d.ts","sourceRoot":"","sources":["../src/class-factory-pattern.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,SAAS,CAAC,CAAC,GAAG,MAAM,IAAI,KAAK,GAAG,IAAI,EAAE,CAAC,SAAS;IAAE,KAAK,GAAG,IAAI,EAAE,MAAM,CAAC,GAAG,GAAG,CAAA;CAAE,GAAG,CAAC,GAAG,KAAK,EAAE,KAAK,CAAC,CAAA;AAE/G;;;;;;;;;;;;;;;;;;;GAmBG;AACH,eAAO,MAAM,mBAAmB,uGAM/B,CAAA"}
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.classFactoryPattern = void 0;
4
+ /**
5
+ * This is a wrapper that easily converts class constructor call (`new className(..constructorParams)`) into function call (`classNameFactory(..constructorParams)`)
6
+ * @param {C} classType
7
+ * @template C
8
+ * @return {(...args: ConstructorParameters<C>) => InstanceType<C>}
9
+ * @example
10
+ * export class SomeClass {
11
+ * protected _a: string
12
+ *
13
+ * constructor(params: { a: string }) {
14
+ * const { a } = params
15
+ * this._a = a
16
+ * }
17
+ * }
18
+ *
19
+ * export const someClassFactory = classFactoryPattern(SomeClass)
20
+ *
21
+ * // using
22
+ * const someClassInstance = someClassFactory({ a: 'test' })
23
+ */
24
+ const classFactoryPattern = (classType) => {
25
+ return (...args) => {
26
+ return new classType(...args);
27
+ };
28
+ };
29
+ exports.classFactoryPattern = classFactoryPattern;
30
+ //# sourceMappingURL=class-factory-pattern.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"class-factory-pattern.js","sourceRoot":"","sources":["../src/class-factory-pattern.ts"],"names":[],"mappings":";;;AAEA;;;;;;;;;;;;;;;;;;;GAmBG;AACI,MAAM,mBAAmB,GAAG,CACjC,SAAY,EAC8C,EAAE;IAC5D,OAAO,CAAC,GAAG,IAA8B,EAAmB,EAAE;QAC5D,OAAO,IAAI,SAAS,CAAC,GAAG,IAAI,CAAoB,CAAA;IAClD,CAAC,CAAA;AACH,CAAC,CAAA;AANY,QAAA,mBAAmB,uBAM/B"}
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Wrap async express http request end return promise or call next on catch
3
+ * @param _target
4
+ * @param _key
5
+ * @param descriptor
6
+ * @example
7
+ * export class RootController {
8
+ * /@expressErrorHandler
9
+ * async login(req: Request, res: Response): Promise<void> {
10
+ * const { username, password } = validationUtil().sanitize(req.body, postLoginBodySchema)
11
+ * const result = await authorizationUseCase.login({ username, password })
12
+ * res.json(result)
13
+ * }
14
+ * }
15
+ */
16
+ export declare const expressErrorHandler: (_target: any, _key: string, descriptor: TypedPropertyDescriptor<any>) => any;
17
+ //# sourceMappingURL=error-handler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error-handler.d.ts","sourceRoot":"","sources":["../../src/express/error-handler.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,eAAO,MAAM,mBAAmB,YAAa,GAAG,QAAQ,MAAM,cAAc,wBAAwB,GAAG,CAAC,KAAG,GAS1G,CAAA"}
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.expressErrorHandler = void 0;
4
+ /**
5
+ * Wrap async express http request end return promise or call next on catch
6
+ * @param _target
7
+ * @param _key
8
+ * @param descriptor
9
+ * @example
10
+ * export class RootController {
11
+ * /@expressErrorHandler
12
+ * async login(req: Request, res: Response): Promise<void> {
13
+ * const { username, password } = validationUtil().sanitize(req.body, postLoginBodySchema)
14
+ * const result = await authorizationUseCase.login({ username, password })
15
+ * res.json(result)
16
+ * }
17
+ * }
18
+ */
19
+ const expressErrorHandler = (_target, _key, descriptor) => {
20
+ const originalMethod = descriptor.value;
21
+ descriptor.value = function () {
22
+ const next = arguments[2]; // eslint-disable-line prefer-rest-params
23
+ return Promise.resolve(originalMethod.apply(this, arguments)).catch(next); // eslint-disable-line prefer-rest-params
24
+ };
25
+ return descriptor;
26
+ };
27
+ exports.expressErrorHandler = expressErrorHandler;
28
+ //# sourceMappingURL=error-handler.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"error-handler.js","sourceRoot":"","sources":["../../src/express/error-handler.ts"],"names":[],"mappings":";;;AAAA;;;;;;;;;;;;;;GAcG;AACI,MAAM,mBAAmB,GAAG,CAAC,OAAY,EAAE,IAAY,EAAE,UAAwC,EAAO,EAAE;IAC/G,MAAM,cAAc,GAAG,UAAU,CAAC,KAAK,CAAA;IACvC,UAAU,CAAC,KAAK,GAAG;QACjB,MAAM,IAAI,GAAG,SAAS,CAAC,CAAC,CAAC,CAAA,CAAC,yCAAyC;QAEnE,OAAO,OAAO,CAAC,OAAO,CAAC,cAAc,CAAC,KAAK,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,CAAA,CAAC,yCAAyC;IACrH,CAAC,CAAA;IAED,OAAO,UAAU,CAAA;AACnB,CAAC,CAAA;AATY,QAAA,mBAAmB,uBAS/B"}
package/lib/index.d.ts ADDED
@@ -0,0 +1,14 @@
1
+ export * from './express/error-handler';
2
+ export * from './singleton/async';
3
+ export * from './singleton/pattern';
4
+ export * from './class-factory-pattern';
5
+ export * from './joi-util';
6
+ export * from './memoize-factory';
7
+ export * from './object-util';
8
+ export * from './regex-util';
9
+ export * from './single-threshold-promise';
10
+ export * from './string-util';
11
+ export * from './time-util';
12
+ export * from './timeout';
13
+ export * from './type-util';
14
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAA;AAEzC,cAAc,qBAAqB,CAAA;AAEnC,cAAc,uBAAuB,CAAA;AAErC,cAAc,2BAA2B,CAAA;AAEzC,cAAc,cAAc,CAAA;AAE5B,cAAc,qBAAqB,CAAA;AAEnC,cAAc,iBAAiB,CAAA;AAE/B,cAAc,gBAAgB,CAAA;AAE9B,cAAc,8BAA8B,CAAA;AAE5C,cAAc,iBAAiB,CAAA;AAE/B,cAAc,eAAe,CAAA;AAE7B,cAAc,aAAa,CAAA;AAE3B,cAAc,eAAe,CAAA"}
package/lib/index.js ADDED
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./express/error-handler"), exports);
18
+ __exportStar(require("./singleton/async"), exports);
19
+ __exportStar(require("./singleton/pattern"), exports);
20
+ __exportStar(require("./class-factory-pattern"), exports);
21
+ __exportStar(require("./joi-util"), exports);
22
+ __exportStar(require("./memoize-factory"), exports);
23
+ __exportStar(require("./object-util"), exports);
24
+ __exportStar(require("./regex-util"), exports);
25
+ __exportStar(require("./single-threshold-promise"), exports);
26
+ __exportStar(require("./string-util"), exports);
27
+ __exportStar(require("./time-util"), exports);
28
+ __exportStar(require("./timeout"), exports);
29
+ __exportStar(require("./type-util"), exports);
30
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,4DAAyC;AAEzC,sDAAmC;AAEnC,wDAAqC;AAErC,4DAAyC;AAEzC,+CAA4B;AAE5B,sDAAmC;AAEnC,kDAA+B;AAE/B,iDAA8B;AAE9B,+DAA4C;AAE5C,kDAA+B;AAE/B,gDAA6B;AAE7B,8CAA2B;AAE3B,gDAA6B"}
@@ -0,0 +1,47 @@
1
+ import { ObjectSchema, Schema, ValidationOptions } from 'joi';
2
+ export declare class ErrorWithPayload<T> extends Error {
3
+ payload: T;
4
+ constructor(message: string, payload: T);
5
+ }
6
+ /**
7
+ * This is a simple wrapper around Joi validation with two functions exposed validate and sanitize. If object is not valid function throws an error.
8
+ * @example
9
+ * type SomeType = {
10
+ * a: string
11
+ * b: number
12
+ * }
13
+ * const someSchema = Joi.object<SomeType>().keys({
14
+ * a: Joi.string().required(),
15
+ * b: Joi.number().required(),
16
+ * }).required()
17
+ *
18
+ * const joiUtil = new JoiUtil()
19
+ *
20
+ * // using
21
+ * const invalidObject = joiUtil.validate({}, someSchema) // validate throws an error
22
+ * const validObject = joiUtil.validate({ a: 'a', b: 1 }, someSchema)
23
+ */
24
+ export declare class JoiUtil {
25
+ /**
26
+ * Validate and clean object
27
+ * @template T
28
+ * @template Joi
29
+ * @param {any} objectToValidate
30
+ * @param {Joi.Schema | Joi.ObjectSchema<T>} schema
31
+ * @param {validationOptions} [validationOptions]
32
+ * @returns {T} expected object after validation
33
+ */
34
+ sanitize<T>(objectToValidate: any, schema: Schema | ObjectSchema<T>, validationOptions?: ValidationOptions): T;
35
+ /**
36
+ * Only validate properties specified in validation schema
37
+ * @template T
38
+ * @template Joi
39
+ * @param {any} objectToValidate
40
+ * @param {Joi.Schema | Joi.ObjectSchema<T>} schema
41
+ * @param {validationOptions} [validationOptions]
42
+ * @returns {T} expected object after validation
43
+ */
44
+ validate<T>(objectToValidate: any, schema: Schema | ObjectSchema<T>, validationOptions?: ValidationOptions): T;
45
+ protected _validate<T>(objectToValidate: any, schema: Schema | ObjectSchema<T>, validationOptions?: ValidationOptions): T;
46
+ }
47
+ //# sourceMappingURL=joi-util.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"joi-util.d.ts","sourceRoot":"","sources":["../src/joi-util.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,KAAK,CAAA;AAE7D,qBAAa,gBAAgB,CAAC,CAAC,CAAE,SAAQ,KAAK;IAC5C,OAAO,EAAE,CAAC,CAAA;gBAEE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC;CAIxC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,qBAAa,OAAO;IAClB;;;;;;;;OAQG;IACH,QAAQ,CAAC,CAAC,EAAE,gBAAgB,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,GAAG,YAAY,CAAC,CAAC,CAAC,EAAE,iBAAiB,CAAC,EAAE,iBAAiB,GAAG,CAAC;IAI9G;;;;;;;;OAQG;IACH,QAAQ,CAAC,CAAC,EAAE,gBAAgB,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,GAAG,YAAY,CAAC,CAAC,CAAC,EAAE,iBAAiB,CAAC,EAAE,iBAAiB,GAAG,CAAC;IAI9G,SAAS,CAAC,SAAS,CAAC,CAAC,EAAE,gBAAgB,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,GAAG,YAAY,CAAC,CAAC,CAAC,EAAE,iBAAiB,CAAC,EAAE,iBAAiB,GAAG,CAAC;CAQ1H"}
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.JoiUtil = exports.ErrorWithPayload = void 0;
4
+ class ErrorWithPayload extends Error {
5
+ constructor(message, payload) {
6
+ super(message);
7
+ this.payload = payload;
8
+ }
9
+ }
10
+ exports.ErrorWithPayload = ErrorWithPayload;
11
+ /**
12
+ * This is a simple wrapper around Joi validation with two functions exposed validate and sanitize. If object is not valid function throws an error.
13
+ * @example
14
+ * type SomeType = {
15
+ * a: string
16
+ * b: number
17
+ * }
18
+ * const someSchema = Joi.object<SomeType>().keys({
19
+ * a: Joi.string().required(),
20
+ * b: Joi.number().required(),
21
+ * }).required()
22
+ *
23
+ * const joiUtil = new JoiUtil()
24
+ *
25
+ * // using
26
+ * const invalidObject = joiUtil.validate({}, someSchema) // validate throws an error
27
+ * const validObject = joiUtil.validate({ a: 'a', b: 1 }, someSchema)
28
+ */
29
+ class JoiUtil {
30
+ /**
31
+ * Validate and clean object
32
+ * @template T
33
+ * @template Joi
34
+ * @param {any} objectToValidate
35
+ * @param {Joi.Schema | Joi.ObjectSchema<T>} schema
36
+ * @param {validationOptions} [validationOptions]
37
+ * @returns {T} expected object after validation
38
+ */
39
+ sanitize(objectToValidate, schema, validationOptions) {
40
+ return this._validate(objectToValidate, schema, Object.assign(Object.assign({}, validationOptions), { stripUnknown: true }));
41
+ }
42
+ /**
43
+ * Only validate properties specified in validation schema
44
+ * @template T
45
+ * @template Joi
46
+ * @param {any} objectToValidate
47
+ * @param {Joi.Schema | Joi.ObjectSchema<T>} schema
48
+ * @param {validationOptions} [validationOptions]
49
+ * @returns {T} expected object after validation
50
+ */
51
+ validate(objectToValidate, schema, validationOptions) {
52
+ return this._validate(objectToValidate, schema, validationOptions);
53
+ }
54
+ _validate(objectToValidate, schema, validationOptions) {
55
+ const { error: validationError, value } = schema.validate(objectToValidate, validationOptions);
56
+ if (validationError) {
57
+ throw new ErrorWithPayload(validationError.message.split('"').join("'"), validationError);
58
+ }
59
+ return value;
60
+ }
61
+ }
62
+ exports.JoiUtil = JoiUtil;
63
+ //# sourceMappingURL=joi-util.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"joi-util.js","sourceRoot":"","sources":["../src/joi-util.ts"],"names":[],"mappings":";;;AAEA,MAAa,gBAAoB,SAAQ,KAAK;IAG5C,YAAY,OAAe,EAAE,OAAU;QACrC,KAAK,CAAC,OAAO,CAAC,CAAA;QACd,IAAI,CAAC,OAAO,GAAG,OAAO,CAAA;IACxB,CAAC;CACF;AAPD,4CAOC;AAED;;;;;;;;;;;;;;;;;GAiBG;AACH,MAAa,OAAO;IAClB;;;;;;;;OAQG;IACH,QAAQ,CAAI,gBAAqB,EAAE,MAAgC,EAAE,iBAAqC;QACxG,OAAO,IAAI,CAAC,SAAS,CAAI,gBAAgB,EAAE,MAAM,kCAAO,iBAAiB,KAAE,YAAY,EAAE,IAAI,IAAG,CAAA;IAClG,CAAC;IAED;;;;;;;;OAQG;IACH,QAAQ,CAAI,gBAAqB,EAAE,MAAgC,EAAE,iBAAqC;QACxG,OAAO,IAAI,CAAC,SAAS,CAAI,gBAAgB,EAAE,MAAM,EAAE,iBAAiB,CAAC,CAAA;IACvE,CAAC;IAES,SAAS,CAAI,gBAAqB,EAAE,MAAgC,EAAE,iBAAqC;QACnH,MAAM,EAAE,KAAK,EAAE,eAAe,EAAE,KAAK,EAAE,GAAG,MAAM,CAAC,QAAQ,CAAC,gBAAgB,EAAE,iBAAiB,CAAC,CAAA;QAC9F,IAAI,eAAe,EAAE;YACnB,MAAM,IAAI,gBAAgB,CAAC,eAAe,CAAC,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,eAAe,CAAC,CAAA;SAC1F;QAED,OAAO,KAAU,CAAA;IACnB,CAAC;CACF;AAnCD,0BAmCC"}
@@ -0,0 +1,16 @@
1
+ import { AnyFunction } from './types/any-function';
2
+ /**
3
+ * This is a simple implementation of memoize function that caches result against the parameter passed that are passed to the
4
+ * function so it never runs the same calculation twice.
5
+ * @template F
6
+ * @template R
7
+ * @param {F} factoryFn
8
+ * @return {F: AnyFunction<R>}
9
+ * @example
10
+ * export const sumTwoNumbersMemoize = memoizeFactory((a:number, b:number): number => a + b)
11
+ *
12
+ * // using
13
+ * sumTwoNumbersMemoize(5 + 10) // 15
14
+ */
15
+ export declare const memoizeFactory: <F extends AnyFunction<R>, R>(factoryFn: F) => F;
16
+ //# sourceMappingURL=memoize-factory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"memoize-factory.d.ts","sourceRoot":"","sources":["../src/memoize-factory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,MAAM,wBAAwB,CAAA;AAEpD;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,cAAc,kDAW1B,CAAA"}
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.memoizeFactory = void 0;
4
+ /**
5
+ * This is a simple implementation of memoize function that caches result against the parameter passed that are passed to the
6
+ * function so it never runs the same calculation twice.
7
+ * @template F
8
+ * @template R
9
+ * @param {F} factoryFn
10
+ * @return {F: AnyFunction<R>}
11
+ * @example
12
+ * export const sumTwoNumbersMemoize = memoizeFactory((a:number, b:number): number => a + b)
13
+ *
14
+ * // using
15
+ * sumTwoNumbersMemoize(5 + 10) // 15
16
+ */
17
+ const memoizeFactory = (factoryFn) => {
18
+ const cache = {};
19
+ return ((...args) => {
20
+ const key = JSON.stringify(args);
21
+ if (key in cache) {
22
+ return cache[key];
23
+ }
24
+ return (cache[key] = factoryFn(...args));
25
+ });
26
+ };
27
+ exports.memoizeFactory = memoizeFactory;
28
+ //# sourceMappingURL=memoize-factory.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"memoize-factory.js","sourceRoot":"","sources":["../src/memoize-factory.ts"],"names":[],"mappings":";;;AAEA;;;;;;;;;;;;GAYG;AACI,MAAM,cAAc,GAAG,CAA8B,SAAY,EAAK,EAAE;IAC7E,MAAM,KAAK,GAAuB,EAAE,CAAA;IAEpC,OAAO,CAAC,CAAC,GAAG,IAAmB,EAAK,EAAE;QACpC,MAAM,GAAG,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAA;QAChC,IAAI,GAAG,IAAI,KAAK,EAAE;YAChB,OAAO,KAAK,CAAC,GAAG,CAAC,CAAA;SAClB;QAED,OAAO,CAAC,KAAK,CAAC,GAAG,CAAC,GAAG,SAAS,CAAC,GAAG,IAAI,CAAC,CAAC,CAAA;IAC1C,CAAC,CAAM,CAAA;AACT,CAAC,CAAA;AAXY,QAAA,cAAc,kBAW1B"}
@@ -0,0 +1,70 @@
1
+ export type ObjectType = {
2
+ [k: string]: any;
3
+ };
4
+ export declare class ObjectUtil {
5
+ /**
6
+ * Deep clone object. Returned object will have no references to the object passed through params
7
+ * @template T
8
+ * @param {T} objectToClone
9
+ * @return {T}
10
+ */
11
+ deepClone<T extends ObjectType>(objectToClone: T): T;
12
+ /**
13
+ * Pick only properties from the property list. It is only allowed to pick properties from the first level
14
+ * @template T
15
+ * @template L
16
+ * @param {T} obj
17
+ * @param {L[]} keyList
18
+ * @return {Pick<T, L>}
19
+ */
20
+ pickByList<T extends object, L extends keyof T>(obj: T, keyList: (L | string)[]): Pick<T, L>;
21
+ /**
22
+ * Pick objects properties using keys from the second object.
23
+ * @template T
24
+ * @template L
25
+ * @param {T} obj
26
+ * @param {Partial<T>} objWithPickKeys
27
+ * @return {Pick<T, L>}
28
+ */
29
+ pickByObjectKeys<T extends object, L extends keyof T>(obj: T, objWithPickKeys: Partial<T> | ObjectType): Pick<T, L>;
30
+ /**
31
+ * This function will do stringify deeper that JSON.stringify.
32
+ * @param {any} entity - entity thant needs to be stringify
33
+ * @param {object} [options] - available options
34
+ * @param {boolean} [options.isSortable=false] - if object property should be sorted
35
+ * @param {boolean} [options.isPrettyPrinted=false] - if object and array properties should be printed in a new row
36
+ * @param {number} [options.prettyPrintCompactLevel=0] - if pretty print is on define the level of deepest children that are not
37
+ * going to be pretty. It doesn't matter if the siblings doesn't have the same depth.
38
+ * @return {string} - strung result
39
+ * @example
40
+ * console.log(new ObjectUtil().deepStringify(null)) // 'null'
41
+ * console.log(new ObjectUtil().deepStringify(undefined)) // 'undefined'
42
+ * console.log(new ObjectUtil().deepStringify({ a: 1 })) // '{\n\ta: 1\n}'
43
+ * // `{
44
+ * // a:1
45
+ * // }`
46
+ * console.log(new ObjectUtil().deepStringify({ b: 1, a: 2 }, {isSorted:true, compact: true})) // { a: 2, b: 1 }
47
+ */
48
+ deepStringify(entity: any, options?: {
49
+ isSorted?: boolean;
50
+ isPrettyPrinted?: boolean;
51
+ prettyPrintCompactLevel?: number;
52
+ }): string;
53
+ /**
54
+ * We are converting objects to string (or null or undefined) and comparing if the result is equal
55
+ * @param a
56
+ * @param b
57
+ * @return {boolean}
58
+ */
59
+ deepEqual(a: any, b: any): boolean;
60
+ /**
61
+ * This function is going to convert any null to undefined in the object that is passed to it.
62
+ * @template T
63
+ * @param {T} objectWithNulls
64
+ * @return {T}
65
+ * @example
66
+ * console.log(new ObjectUtil().deepNullToUndefined({ a: null, b: { c: null } })) // { a: undefined, b: { c: undefined } }
67
+ */
68
+ deepNullToUndefined<T extends ObjectType>(objectWithNulls: T): T;
69
+ }
70
+ //# sourceMappingURL=object-util.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"object-util.d.ts","sourceRoot":"","sources":["../src/object-util.ts"],"names":[],"mappings":"AAGA,MAAM,MAAM,UAAU,GAAG;IAAE,CAAC,CAAC,EAAE,MAAM,GAAG,GAAG,CAAA;CAAE,CAAA;AAE7C,qBAAa,UAAU;IACrB;;;;;OAKG;IACH,SAAS,CAAC,CAAC,SAAS,UAAU,EAAE,aAAa,EAAE,CAAC,GAAG,CAAC;IAIpD;;;;;;;OAOG;IACH,UAAU,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC,CAAC,GAAG,MAAM,CAAC,EAAE,GAAG,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC;IAa5F;;;;;;;OAOG;IACH,gBAAgB,CAAC,CAAC,SAAS,MAAM,EAAE,CAAC,SAAS,MAAM,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,eAAe,EAAE,OAAO,CAAC,CAAC,CAAC,GAAG,UAAU,GAAG,IAAI,CAAC,CAAC,EAAE,CAAC,CAAC;IAMnH;;;;;;;;;;;;;;;;;OAiBG;IACH,aAAa,CACX,MAAM,EAAE,GAAG,EACX,OAAO,CAAC,EAAE;QAAE,QAAQ,CAAC,EAAE,OAAO,CAAC;QAAC,eAAe,CAAC,EAAE,OAAO,CAAC;QAAC,uBAAuB,CAAC,EAAE,MAAM,CAAA;KAAE,GAC5F,MAAM;IAeT;;;;;OAKG;IACH,SAAS,CAAC,CAAC,EAAE,GAAG,EAAE,CAAC,EAAE,GAAG,GAAG,OAAO;IAIlC;;;;;;;OAOG;IACH,mBAAmB,CAAC,CAAC,SAAS,UAAU,EAAE,eAAe,EAAE,CAAC,GAAG,CAAC;CAcjE"}
@@ -0,0 +1,114 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ObjectUtil = void 0;
7
+ const lodash_clonedeep_1 = __importDefault(require("lodash.clonedeep"));
8
+ const util_1 = __importDefault(require("util"));
9
+ class ObjectUtil {
10
+ /**
11
+ * Deep clone object. Returned object will have no references to the object passed through params
12
+ * @template T
13
+ * @param {T} objectToClone
14
+ * @return {T}
15
+ */
16
+ deepClone(objectToClone) {
17
+ return (0, lodash_clonedeep_1.default)(objectToClone);
18
+ }
19
+ /**
20
+ * Pick only properties from the property list. It is only allowed to pick properties from the first level
21
+ * @template T
22
+ * @template L
23
+ * @param {T} obj
24
+ * @param {L[]} keyList
25
+ * @return {Pick<T, L>}
26
+ */
27
+ pickByList(obj, keyList) {
28
+ return keyList.reduce((pickedObj, key) => {
29
+ // eslint-disable-next-line
30
+ if (obj && obj.hasOwnProperty(key)) {
31
+ // eslint-disable-next-line
32
+ // @ts-ignore
33
+ pickedObj[key] = obj[key];
34
+ }
35
+ return pickedObj;
36
+ }, {});
37
+ }
38
+ /**
39
+ * Pick objects properties using keys from the second object.
40
+ * @template T
41
+ * @template L
42
+ * @param {T} obj
43
+ * @param {Partial<T>} objWithPickKeys
44
+ * @return {Pick<T, L>}
45
+ */
46
+ pickByObjectKeys(obj, objWithPickKeys) {
47
+ const keys = Object.keys(objWithPickKeys);
48
+ return this.pickByList(obj, keys);
49
+ }
50
+ /**
51
+ * This function will do stringify deeper that JSON.stringify.
52
+ * @param {any} entity - entity thant needs to be stringify
53
+ * @param {object} [options] - available options
54
+ * @param {boolean} [options.isSortable=false] - if object property should be sorted
55
+ * @param {boolean} [options.isPrettyPrinted=false] - if object and array properties should be printed in a new row
56
+ * @param {number} [options.prettyPrintCompactLevel=0] - if pretty print is on define the level of deepest children that are not
57
+ * going to be pretty. It doesn't matter if the siblings doesn't have the same depth.
58
+ * @return {string} - strung result
59
+ * @example
60
+ * console.log(new ObjectUtil().deepStringify(null)) // 'null'
61
+ * console.log(new ObjectUtil().deepStringify(undefined)) // 'undefined'
62
+ * console.log(new ObjectUtil().deepStringify({ a: 1 })) // '{\n\ta: 1\n}'
63
+ * // `{
64
+ * // a:1
65
+ * // }`
66
+ * console.log(new ObjectUtil().deepStringify({ b: 1, a: 2 }, {isSorted:true, compact: true})) // { a: 2, b: 1 }
67
+ */
68
+ deepStringify(entity, options) {
69
+ const { isSorted = false, isPrettyPrinted = false, prettyPrintCompactLevel = 0 } = options !== null && options !== void 0 ? options : {};
70
+ const compact = isPrettyPrinted ? prettyPrintCompactLevel : true;
71
+ return util_1.default.inspect(entity, {
72
+ depth: Infinity,
73
+ sorted: isSorted,
74
+ maxArrayLength: Infinity,
75
+ maxStringLength: Infinity,
76
+ compact,
77
+ breakLength: Infinity,
78
+ });
79
+ }
80
+ /**
81
+ * We are converting objects to string (or null or undefined) and comparing if the result is equal
82
+ * @param a
83
+ * @param b
84
+ * @return {boolean}
85
+ */
86
+ deepEqual(a, b) {
87
+ return this.deepStringify(a, { isSorted: true }) === this.deepStringify(b, { isSorted: true });
88
+ }
89
+ /**
90
+ * This function is going to convert any null to undefined in the object that is passed to it.
91
+ * @template T
92
+ * @param {T} objectWithNulls
93
+ * @return {T}
94
+ * @example
95
+ * console.log(new ObjectUtil().deepNullToUndefined({ a: null, b: { c: null } })) // { a: undefined, b: { c: undefined } }
96
+ */
97
+ deepNullToUndefined(objectWithNulls) {
98
+ return Object.entries(objectWithNulls).reduce((acc, cur) => {
99
+ const [key, value] = cur;
100
+ if (value === null) {
101
+ acc[key] = undefined;
102
+ }
103
+ else if (typeof value === 'object' && !(value instanceof Date)) {
104
+ acc[key] = this.deepNullToUndefined(value);
105
+ }
106
+ else {
107
+ acc[key] = value;
108
+ }
109
+ return acc;
110
+ }, {});
111
+ }
112
+ }
113
+ exports.ObjectUtil = ObjectUtil;
114
+ //# sourceMappingURL=object-util.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"object-util.js","sourceRoot":"","sources":["../src/object-util.ts"],"names":[],"mappings":";;;;;;AAAA,wEAAwC;AACxC,gDAAuB;AAIvB,MAAa,UAAU;IACrB;;;;;OAKG;IACH,SAAS,CAAuB,aAAgB;QAC9C,OAAO,IAAA,0BAAS,EAAC,aAAa,CAAC,CAAA;IACjC,CAAC;IAED;;;;;;;OAOG;IACH,UAAU,CAAsC,GAAM,EAAE,OAAuB;QAC7E,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC,SAAS,EAAE,GAAG,EAAE,EAAE;YACvC,2BAA2B;YAC3B,IAAI,GAAG,IAAI,GAAG,CAAC,cAAc,CAAC,GAAG,CAAC,EAAE;gBAClC,2BAA2B;gBAC3B,aAAa;gBACb,SAAS,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC,GAAG,CAAC,CAAA;aAC1B;YAED,OAAO,SAAS,CAAA;QAClB,CAAC,EAAE,EAAgB,CAAC,CAAA;IACtB,CAAC;IAED;;;;;;;OAOG;IACH,gBAAgB,CAAsC,GAAM,EAAE,eAAwC;QACpG,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,eAAe,CAAQ,CAAA;QAEhD,OAAO,IAAI,CAAC,UAAU,CAAO,GAAG,EAAE,IAAI,CAAC,CAAA;IACzC,CAAC;IAED;;;;;;;;;;;;;;;;;OAiBG;IACH,aAAa,CACX,MAAW,EACX,OAA6F;QAE7F,MAAM,EAAE,QAAQ,GAAG,KAAK,EAAE,eAAe,GAAG,KAAK,EAAE,uBAAuB,GAAG,CAAC,EAAE,GAAG,OAAO,aAAP,OAAO,cAAP,OAAO,GAAI,EAAE,CAAA;QAEhG,MAAM,OAAO,GAAG,eAAe,CAAC,CAAC,CAAC,uBAAuB,CAAC,CAAC,CAAC,IAAI,CAAA;QAEhE,OAAO,cAAI,CAAC,OAAO,CAAC,MAAM,EAAE;YAC1B,KAAK,EAAE,QAAQ;YACf,MAAM,EAAE,QAAQ;YAChB,cAAc,EAAE,QAAQ;YACxB,eAAe,EAAE,QAAQ;YACzB,OAAO;YACP,WAAW,EAAE,QAAQ;SACtB,CAAC,CAAA;IACJ,CAAC;IAED;;;;;OAKG;IACH,SAAS,CAAC,CAAM,EAAE,CAAM;QACtB,OAAO,IAAI,CAAC,aAAa,CAAC,CAAC,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,KAAK,IAAI,CAAC,aAAa,CAAC,CAAC,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,CAAC,CAAA;IAChG,CAAC;IAED;;;;;;;OAOG;IACH,mBAAmB,CAAuB,eAAkB;QAC1D,OAAO,MAAM,CAAC,OAAO,CAAC,eAAe,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;YACzD,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,GAAG,GAAG,CAAA;YACxB,IAAI,KAAK,KAAK,IAAI,EAAE;gBAClB,GAAG,CAAC,GAAG,CAAC,GAAG,SAAS,CAAA;aACrB;iBAAM,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,CAAC,KAAK,YAAY,IAAI,CAAC,EAAE;gBAChE,GAAG,CAAC,GAAG,CAAC,GAAG,IAAI,CAAC,mBAAmB,CAAC,KAAK,CAAC,CAAA;aAC3C;iBAAM;gBACL,GAAG,CAAC,GAAG,CAAC,GAAG,KAAK,CAAA;aACjB;YAED,OAAO,GAAG,CAAA;QACZ,CAAC,EAAE,EAAS,CAAC,CAAA;IACf,CAAC;CACF;AAlHD,gCAkHC"}
@@ -0,0 +1,12 @@
1
+ export declare const regexUtil: {
2
+ /**
3
+ * This is a UUID regex expression. This is usually used in express router to constrict the values passed as a path parameter (if you are using UUID as your identifier).
4
+ * @return {string}
5
+ * @example
6
+ * const { uuid } = regexUtil
7
+ * router.route(`/users/:userId(${uuid})`).get(getUsersById)
8
+ * //...
9
+ */
10
+ uuid: "\\b[0-9a-f]{8}\\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\\b[0-9a-f]{12}\\b";
11
+ };
12
+ //# sourceMappingURL=regex-util.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"regex-util.d.ts","sourceRoot":"","sources":["../src/regex-util.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,SAAS;IACpB;;;;;;;OAOG;;CAEJ,CAAA"}
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.regexUtil = void 0;
4
+ exports.regexUtil = {
5
+ /**
6
+ * This is a UUID regex expression. This is usually used in express router to constrict the values passed as a path parameter (if you are using UUID as your identifier).
7
+ * @return {string}
8
+ * @example
9
+ * const { uuid } = regexUtil
10
+ * router.route(`/users/:userId(${uuid})`).get(getUsersById)
11
+ * //...
12
+ */
13
+ uuid: `\\b[0-9a-f]{8}\\b-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-\\b[0-9a-f]{12}\\b`,
14
+ };
15
+ //# sourceMappingURL=regex-util.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"regex-util.js","sourceRoot":"","sources":["../src/regex-util.ts"],"names":[],"mappings":";;;AAAa,QAAA,SAAS,GAAG;IACvB;;;;;;;OAOG;IACH,IAAI,EAAE,0EAAmF;CAC1F,CAAA"}