@nest-yalc-2/utils 1.0.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.
Files changed (148) hide show
  1. package/LICENSE +661 -0
  2. package/README.md +9 -0
  3. package/package.json +29 -0
  4. package/src/__mocks__/enum.mock.ts +9 -0
  5. package/src/__tests__/class.helper.spec.ts +32 -0
  6. package/src/__tests__/command.helper.spec.ts +35 -0
  7. package/src/__tests__/config-manager.helper.spec.ts +93 -0
  8. package/src/__tests__/custom-validator.spec.ts +34 -0
  9. package/src/__tests__/data-structure.helper.spec.ts +30 -0
  10. package/src/__tests__/date.helper.spec.ts +78 -0
  11. package/src/__tests__/encryption.helper.spec.ts +45 -0
  12. package/src/__tests__/enum.helper.spec.ts +53 -0
  13. package/src/__tests__/env.helper.spec.ts +68 -0
  14. package/src/__tests__/error.helper.spec.ts +20 -0
  15. package/src/__tests__/faker-helper.spec.ts +187 -0
  16. package/src/__tests__/files.helper.spec.ts +22 -0
  17. package/src/__tests__/http.helper.spec.ts +15 -0
  18. package/src/__tests__/interval.helper.spec.ts +29 -0
  19. package/src/__tests__/math.helper.spec.ts +24 -0
  20. package/src/__tests__/nest.decorator.spec.ts +29 -0
  21. package/src/__tests__/nest.helper.spec.ts +26 -0
  22. package/src/__tests__/object-mapper.helper.spec.ts +167 -0
  23. package/src/__tests__/object-mapper.interceptor.spec.ts +104 -0
  24. package/src/__tests__/object.helper.spec.ts +181 -0
  25. package/src/__tests__/plugin.helper.spec.ts +85 -0
  26. package/src/__tests__/promise.helper.spec.ts +76 -0
  27. package/src/__tests__/returnValue.spec.ts +49 -0
  28. package/src/__tests__/rxjs.helper.spec.ts +79 -0
  29. package/src/__tests__/simple-mapper.interceptor.spec.ts +82 -0
  30. package/src/__tests__/tsconfig.json +3 -0
  31. package/src/__tests__/validator-helper.spec.ts +34 -0
  32. package/src/__tests__/zlib.helper.spec.ts +20 -0
  33. package/src/class.helper.d.ts +4 -0
  34. package/src/class.helper.js +19 -0
  35. package/src/class.helper.js.map +1 -0
  36. package/src/class.helper.ts +33 -0
  37. package/src/command.helper.d.ts +1 -0
  38. package/src/command.helper.js +16 -0
  39. package/src/command.helper.js.map +1 -0
  40. package/src/command.helper.ts +19 -0
  41. package/src/config-manager.helper.d.ts +12 -0
  42. package/src/config-manager.helper.js +60 -0
  43. package/src/config-manager.helper.js.map +1 -0
  44. package/src/config-manager.helper.ts +122 -0
  45. package/src/custom-validator.d.ts +3 -0
  46. package/src/custom-validator.js +13 -0
  47. package/src/custom-validator.js.map +1 -0
  48. package/src/custom-validator.ts +13 -0
  49. package/src/data-structure.helper.d.ts +1 -0
  50. package/src/data-structure.helper.js +13 -0
  51. package/src/data-structure.helper.js.map +1 -0
  52. package/src/data-structure.helper.ts +14 -0
  53. package/src/date.helper.d.ts +12 -0
  54. package/src/date.helper.js +47 -0
  55. package/src/date.helper.js.map +1 -0
  56. package/src/date.helper.ts +65 -0
  57. package/src/encryption.helper.d.ts +2 -0
  58. package/src/encryption.helper.js +58 -0
  59. package/src/encryption.helper.js.map +1 -0
  60. package/src/encryption.helper.ts +29 -0
  61. package/src/enum.helper.d.ts +8 -0
  62. package/src/enum.helper.js +21 -0
  63. package/src/enum.helper.js.map +1 -0
  64. package/src/enum.helper.ts +34 -0
  65. package/src/env.helper.d.ts +3 -0
  66. package/src/env.helper.js +23 -0
  67. package/src/env.helper.js.map +1 -0
  68. package/src/env.helper.ts +35 -0
  69. package/src/error.helper.d.ts +1 -0
  70. package/src/error.helper.js +12 -0
  71. package/src/error.helper.js.map +1 -0
  72. package/src/error.helper.ts +12 -0
  73. package/src/faker-helper.d.ts +16 -0
  74. package/src/faker-helper.js +73 -0
  75. package/src/faker-helper.js.map +1 -0
  76. package/src/faker-helper.ts +98 -0
  77. package/src/files.helper.d.ts +2 -0
  78. package/src/files.helper.js +13 -0
  79. package/src/files.helper.js.map +1 -0
  80. package/src/files.helper.ts +20 -0
  81. package/src/http.helper.d.ts +26 -0
  82. package/src/http.helper.js +84 -0
  83. package/src/http.helper.js.map +1 -0
  84. package/src/http.helper.ts +152 -0
  85. package/src/index.d.ts +21 -0
  86. package/src/index.js +38 -0
  87. package/src/index.js.map +1 -0
  88. package/src/index.ts +23 -0
  89. package/src/interval.helper.d.ts +5 -0
  90. package/src/interval.helper.js +16 -0
  91. package/src/interval.helper.js.map +1 -0
  92. package/src/interval.helper.ts +13 -0
  93. package/src/math.helper.d.ts +1 -0
  94. package/src/math.helper.js +8 -0
  95. package/src/math.helper.js.map +1 -0
  96. package/src/math.helper.ts +4 -0
  97. package/src/nest.decorator.d.ts +3 -0
  98. package/src/nest.decorator.js +10 -0
  99. package/src/nest.decorator.js.map +1 -0
  100. package/src/nest.decorator.ts +10 -0
  101. package/src/nestjs/nest.decorator.d.ts +3 -0
  102. package/src/nestjs/nest.decorator.js +10 -0
  103. package/src/nestjs/nest.decorator.js.map +1 -0
  104. package/src/nestjs/nest.decorator.ts +10 -0
  105. package/src/nestjs/nest.helper.d.ts +2 -0
  106. package/src/nestjs/nest.helper.js +7 -0
  107. package/src/nestjs/nest.helper.js.map +1 -0
  108. package/src/nestjs/nest.helper.ts +5 -0
  109. package/src/object-mapper.helper.d.ts +16 -0
  110. package/src/object-mapper.helper.js +47 -0
  111. package/src/object-mapper.helper.js.map +1 -0
  112. package/src/object-mapper.helper.ts +148 -0
  113. package/src/object-mapper.interceptor.d.ts +16 -0
  114. package/src/object-mapper.interceptor.js +36 -0
  115. package/src/object-mapper.interceptor.js.map +1 -0
  116. package/src/object-mapper.interceptor.ts +51 -0
  117. package/src/object.helper.d.ts +18 -0
  118. package/src/object.helper.js +91 -0
  119. package/src/object.helper.js.map +1 -0
  120. package/src/object.helper.ts +173 -0
  121. package/src/plugin.helper.d.ts +22 -0
  122. package/src/plugin.helper.js +26 -0
  123. package/src/plugin.helper.js.map +1 -0
  124. package/src/plugin.helper.ts +42 -0
  125. package/src/promise.helper.d.ts +13 -0
  126. package/src/promise.helper.js +71 -0
  127. package/src/promise.helper.js.map +1 -0
  128. package/src/promise.helper.ts +65 -0
  129. package/src/returnValue.d.ts +14 -0
  130. package/src/returnValue.js +17 -0
  131. package/src/returnValue.js.map +1 -0
  132. package/src/returnValue.ts +26 -0
  133. package/src/rxjs.helper.d.ts +7 -0
  134. package/src/rxjs.helper.js +33 -0
  135. package/src/rxjs.helper.js.map +1 -0
  136. package/src/rxjs.helper.ts +71 -0
  137. package/src/simple-mapper.interceptor.d.ts +7 -0
  138. package/src/simple-mapper.interceptor.js +29 -0
  139. package/src/simple-mapper.interceptor.js.map +1 -0
  140. package/src/simple-mapper.interceptor.ts +43 -0
  141. package/src/validator-helper.d.ts +2 -0
  142. package/src/validator-helper.js +22 -0
  143. package/src/validator-helper.js.map +1 -0
  144. package/src/validator-helper.ts +26 -0
  145. package/src/zlib.helper.d.ts +3 -0
  146. package/src/zlib.helper.js +25 -0
  147. package/src/zlib.helper.js.map +1 -0
  148. package/src/zlib.helper.ts +19 -0
@@ -0,0 +1,173 @@
1
+ interface IIsObject {
2
+ (item: any): boolean;
3
+ }
4
+
5
+ interface IObject {
6
+ [key: string]: any;
7
+ }
8
+
9
+ interface IDeepMerge {
10
+ (target: IObject, ...sources: Array<IObject>): IObject;
11
+ }
12
+
13
+ /**
14
+ * @description Method to check if an item is an object. Date and Function are considered
15
+ * an object, so if you need to exclude those, please update the method accordingly.
16
+ * @param val - The item that needs to be checked
17
+ * @return {Boolean} Whether or not @item is an object
18
+ */
19
+ export const isObject: IIsObject = (val: any): val is Record<string, any> => {
20
+ return val === Object(val) && !Array.isArray(val);
21
+ };
22
+
23
+ /**
24
+ * @description Method to strictly check if an item is an object. Date and Function are not considered
25
+ * @param val - The item that needs to be checked
26
+ * @returns
27
+ */
28
+ export function isObjectStrict(val: any): val is Record<string, any> {
29
+ return (
30
+ isObject(val) &&
31
+ !Array.isArray(val) &&
32
+ !(val instanceof Date) &&
33
+ !(typeof val === 'function')
34
+ );
35
+ }
36
+
37
+ /**
38
+ * @description Method to perform a deep merge of objects
39
+ * @param isArrayConcat - Whether or not to concatenate arrays
40
+ * @param target - The targeted object that needs to be merged with the supplied @sources
41
+ * @param sources - The source(s) that will be used to update the @target object
42
+ * @return The final merged object
43
+ */
44
+ export const _deepMerge = (
45
+ isArrayConcat: boolean,
46
+ target: IObject,
47
+ ...sources: Array<IObject>
48
+ ): IObject => {
49
+ // return the target if no sources passed
50
+ if (!sources.length) {
51
+ return target;
52
+ }
53
+
54
+ const result: IObject = target;
55
+
56
+ if (isObject(result)) {
57
+ const len: number = sources.length;
58
+
59
+ for (let i = 0; i < len; i += 1) {
60
+ const elm: any = sources[i];
61
+
62
+ if (isObject(elm)) {
63
+ for (const key in elm) {
64
+ if (Object.prototype.hasOwnProperty.call(elm, key)) {
65
+ if (isObject(elm[key]) && typeof elm[key] !== 'function') {
66
+ if (!result[key] || !isObject(result[key])) {
67
+ result[key] = {};
68
+ }
69
+ _deepMerge(isArrayConcat, result[key], elm[key]);
70
+ } else {
71
+ if (
72
+ isArrayConcat &&
73
+ Array.isArray(result[key]) &&
74
+ Array.isArray(elm[key])
75
+ ) {
76
+ // concatenate the two arrays and remove any duplicate primitive values
77
+ result[key] = Array.from(new Set(result[key].concat(elm[key])));
78
+ } else {
79
+ result[key] = elm[key];
80
+ }
81
+ }
82
+ }
83
+ }
84
+ }
85
+ }
86
+ }
87
+
88
+ return result;
89
+ };
90
+
91
+ /**
92
+ * @description Method to perform a deep merge of objects
93
+ * @param target - The targeted object that needs to be merged with the supplied @sources
94
+ * @param sources - The source(s) that will be used to update the @target object
95
+ * @returns The final merged object
96
+ */
97
+ export const deepMerge: IDeepMerge = (
98
+ target: IObject,
99
+ ...sources: Array<IObject>
100
+ ): IObject => {
101
+ return _deepMerge(true, target, ...sources);
102
+ };
103
+
104
+ /**
105
+ * @description Method to perform a deep merge of objects without concatenating arrays
106
+ * @param target - The targeted object that needs to be merged with the supplied @sources
107
+ * @param sources - The source(s) that will be used to update the @target object
108
+ * @returns The final merged object
109
+ */
110
+ export const deepMergeWithoutArrayConcat: IDeepMerge = (
111
+ target: IObject,
112
+ ...sources: Array<IObject>
113
+ ): IObject => {
114
+ return _deepMerge(false, target, ...sources);
115
+ };
116
+
117
+ /**
118
+ * Utils to patch a nested property on an object
119
+ * specifying a dot-separated path.
120
+ * If the parent properties do not exist, they will be created
121
+ *
122
+ * @param obj the object to patch
123
+ * @param path the path of the property to set, e.g: "foo.bar.baz"
124
+ * @param value the value to set
125
+ * @returns the patched object
126
+ */
127
+ export function objectSetProp(
128
+ obj: Record<any, any>,
129
+ path: string,
130
+ value: any,
131
+ ): Record<any, any> {
132
+ let schema = obj; // a moving reference to internal objects within obj
133
+ const pList = path.split('.');
134
+ const len = pList.length;
135
+ for (let i = 0; i < len - 1; i++) {
136
+ const elem = pList[i];
137
+ if (!schema[elem]) schema[elem] = {};
138
+ schema = schema[elem];
139
+ }
140
+
141
+ schema[pList[len - 1]] = value;
142
+
143
+ return obj;
144
+ }
145
+
146
+ export function objectsHaveSameKeys(...objects: any[]) {
147
+ const allKeys = objects.reduce(
148
+ (keys, object) => keys.concat(Object.keys(object)),
149
+ [],
150
+ );
151
+ const union = new Set(allKeys);
152
+ return objects.every((object) => union.size === Object.keys(object).length);
153
+ }
154
+
155
+ let count: number = 0;
156
+ const idMap: WeakMap<Record<string, unknown> | Array<unknown>, number> =
157
+ new WeakMap<Record<string, unknown> | Array<unknown>, number>();
158
+ /**
159
+ * @description Method to get the object id (unique identifier) of an object
160
+ */
161
+ export function getObjectId(
162
+ object: Record<string, unknown> | Array<unknown>,
163
+ ): number {
164
+ const objectId: number | undefined = idMap.get(object);
165
+ if (objectId === undefined) {
166
+ count += 1;
167
+ idMap.set(object, count);
168
+
169
+ return count;
170
+ }
171
+
172
+ return objectId;
173
+ }
@@ -0,0 +1,22 @@
1
+ import { ClassType } from '@nest-yalc-2/types/globals.d.js';
2
+ export interface PluginMethods extends Record<string, {
3
+ (...args: any[]): void;
4
+ } | undefined> {
5
+ }
6
+ export interface Plugin<T extends PluginMethods> {
7
+ pluginMethods: T;
8
+ }
9
+ export interface PluginSystem<T extends PluginMethods> {
10
+ registerPlugin(plugin: Plugin<T>): void;
11
+ unregisterPlugin(plugin: Plugin<T>): void;
12
+ invokePlugins(methodName: keyof T, ...args: any[]): void;
13
+ }
14
+ export declare function WithPluginSystem<U extends PluginMethods>(Base?: ClassType): {
15
+ new (...args: any[]): {
16
+ [x: string]: any;
17
+ plugins: Plugin<U>[];
18
+ registerPlugin(plugin: Plugin<U>): void;
19
+ unregisterPlugin(plugin: Plugin<U>): void;
20
+ invokePlugins(methodName: keyof U, ...args: any[]): void;
21
+ };
22
+ };
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.WithPluginSystem = WithPluginSystem;
4
+ class DefaultBase {
5
+ }
6
+ function WithPluginSystem(Base = DefaultBase) {
7
+ class PluginSystemWrapper extends Base {
8
+ constructor() {
9
+ super(...arguments);
10
+ this.plugins = [];
11
+ }
12
+ registerPlugin(plugin) {
13
+ this.plugins.push(plugin);
14
+ }
15
+ unregisterPlugin(plugin) {
16
+ this.plugins = this.plugins.filter((p) => p !== plugin);
17
+ }
18
+ invokePlugins(methodName, ...args) {
19
+ for (const plugin of this.plugins) {
20
+ plugin.pluginMethods[methodName]?.(...args);
21
+ }
22
+ }
23
+ }
24
+ return PluginSystemWrapper;
25
+ }
26
+ //# sourceMappingURL=plugin.helper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plugin.helper.js","sourceRoot":"","sources":["plugin.helper.ts"],"names":[],"mappings":";;AAmBA,4CAsBC;AAvCD,MAAM,WAAW;CAAG;AAiBpB,SAAgB,gBAAgB,CAC9B,OAAkB,WAAW;IAE7B,MAAM,mBAAoB,SAAQ,IAAI;QAAtC;;YACE,YAAO,GAAgB,EAAE,CAAC;QAe5B,CAAC;QAbC,cAAc,CAAC,MAAiB;YAC9B,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAC5B,CAAC;QAED,gBAAgB,CAAC,MAAiB;YAChC,IAAI,CAAC,OAAO,GAAG,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,MAAM,CAAC,CAAC;QAC1D,CAAC;QAED,aAAa,CAAC,UAAmB,EAAE,GAAG,IAAW;YAC/C,KAAK,MAAM,MAAM,IAAI,IAAI,CAAC,OAAO,EAAE,CAAC;gBAClC,MAAM,CAAC,aAAa,CAAC,UAAU,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,CAAC;YAC9C,CAAC;QACH,CAAC;KACF;IAED,OAAO,mBAAmB,CAAC;AAC7B,CAAC"}
@@ -0,0 +1,42 @@
1
+ import { ClassType } from '@nest-yalc-2/types/globals.d.js';
2
+
3
+ class DefaultBase {}
4
+
5
+ export interface PluginMethods extends Record<
6
+ string,
7
+ { (...args: any[]): void } | undefined
8
+ > {}
9
+
10
+ export interface Plugin<T extends PluginMethods> {
11
+ pluginMethods: T;
12
+ }
13
+
14
+ export interface PluginSystem<T extends PluginMethods> {
15
+ registerPlugin(plugin: Plugin<T>): void;
16
+ unregisterPlugin(plugin: Plugin<T>): void;
17
+ invokePlugins(methodName: keyof T, ...args: any[]): void;
18
+ }
19
+
20
+ export function WithPluginSystem<U extends PluginMethods>(
21
+ Base: ClassType = DefaultBase,
22
+ ) {
23
+ class PluginSystemWrapper extends Base implements PluginSystem<U> {
24
+ plugins: Plugin<U>[] = [];
25
+
26
+ registerPlugin(plugin: Plugin<U>) {
27
+ this.plugins.push(plugin);
28
+ }
29
+
30
+ unregisterPlugin(plugin: Plugin<U>) {
31
+ this.plugins = this.plugins.filter((p) => p !== plugin);
32
+ }
33
+
34
+ invokePlugins(methodName: keyof U, ...args: any[]) {
35
+ for (const plugin of this.plugins) {
36
+ plugin.pluginMethods[methodName]?.(...args);
37
+ }
38
+ }
39
+ }
40
+
41
+ return PluginSystemWrapper;
42
+ }
@@ -0,0 +1,13 @@
1
+ import { AnyFunction } from '@nest-yalc-2/types/globals.d.js';
2
+ import * as pMap from 'p-map';
3
+ export declare const PROMISE_CONCURRENCY_LIMIT = 1000;
4
+ export declare function promiseMap<Element, NewElement>(input: Iterable<Element>, mapper: pMap.Mapper<Element, NewElement>, options?: pMap.Options): Promise<NewElement[]>;
5
+ export declare class PromiseTracker {
6
+ private promises;
7
+ private deferred;
8
+ add(promise: Promise<any>): void;
9
+ addDeferred(deferred: AnyFunction): void;
10
+ private remove;
11
+ waitForAll(): Promise<void>;
12
+ }
13
+ export declare const globalPromiseTracker: PromiseTracker;
@@ -0,0 +1,71 @@
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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
35
+ Object.defineProperty(exports, "__esModule", { value: true });
36
+ exports.globalPromiseTracker = exports.PromiseTracker = exports.PROMISE_CONCURRENCY_LIMIT = void 0;
37
+ exports.promiseMap = promiseMap;
38
+ const pMap = __importStar(require("p-map"));
39
+ exports.PROMISE_CONCURRENCY_LIMIT = 1000;
40
+ function promiseMap(input, mapper, options) {
41
+ return pMap.default(input, mapper, {
42
+ concurrency: options?.concurrency ?? exports.PROMISE_CONCURRENCY_LIMIT,
43
+ stopOnError: options?.stopOnError ?? true,
44
+ });
45
+ }
46
+ class PromiseTracker {
47
+ constructor() {
48
+ this.promises = [];
49
+ this.deferred = [];
50
+ }
51
+ add(promise) {
52
+ this.promises.push(promise);
53
+ void promise
54
+ .finally(() => this.remove(promise))
55
+ .catch(() => {
56
+ });
57
+ }
58
+ addDeferred(deferred) {
59
+ this.deferred.push(deferred);
60
+ }
61
+ remove(promise) {
62
+ this.promises = this.promises.filter((p) => p !== promise);
63
+ }
64
+ async waitForAll() {
65
+ await Promise.all(this.promises);
66
+ await Promise.all(this.deferred.map((d) => d()));
67
+ }
68
+ }
69
+ exports.PromiseTracker = PromiseTracker;
70
+ exports.globalPromiseTracker = new PromiseTracker();
71
+ //# sourceMappingURL=promise.helper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"promise.helper.js","sourceRoot":"","sources":["promise.helper.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAcA,gCASC;AAtBD,4CAA8B;AAEjB,QAAA,yBAAyB,GAAG,IAAI,CAAC;AAW9C,SAAgB,UAAU,CACxB,KAAwB,EACxB,MAAwC,EACxC,OAAsB;IAEtB,OAAO,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE;QACjC,WAAW,EAAE,OAAO,EAAE,WAAW,IAAI,iCAAyB;QAC9D,WAAW,EAAE,OAAO,EAAE,WAAW,IAAI,IAAI;KAC1C,CAAC,CAAC;AACL,CAAC;AAKD,MAAa,cAAc;IAA3B;QACU,aAAQ,GAAmB,EAAE,CAAC;QAC9B,aAAQ,GAAkB,EAAE,CAAC;IA6BvC,CAAC;IA3BC,GAAG,CAAC,OAAqB;QACvB,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QAC5B,KAAK,OAAO;aACT,OAAO,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;aACnC,KAAK,CAAC,GAAG,EAAE;QAGZ,CAAC,CAAC,CAAC;IACP,CAAC;IAKD,WAAW,CAAC,QAAqB;QAC/B,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAC/B,CAAC;IAEO,MAAM,CAAC,OAAqB;QAElC,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,OAAO,CAAC,CAAC;IAC7D,CAAC;IAED,KAAK,CAAC,UAAU;QACd,MAAM,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;QAEjC,MAAM,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;IACnD,CAAC;CACF;AA/BD,wCA+BC;AAKY,QAAA,oBAAoB,GAAG,IAAI,cAAc,EAAE,CAAC"}
@@ -0,0 +1,65 @@
1
+ import { AnyFunction } from '@nest-yalc-2/types/globals.d.js';
2
+ import * as pMap from 'p-map';
3
+
4
+ export const PROMISE_CONCURRENCY_LIMIT = 1000;
5
+
6
+ /**
7
+ * This method allows you to run multiple async functions concurrently. It's an alternative to the Promise.all native method
8
+ * with support to concurrency limit. It's an adapter of the p-map library.
9
+ * NOTE: concurrency is set to 1000 by default and stopOnError = true
10
+ *
11
+ @param input - Iterated over concurrently in the `mapper` function.
12
+ @param mapper - Function which is called for every item in `input`. Expected to return a `Promise` or value.
13
+ @returns A `Promise` that is fulfilled when all promises in `input` and ones returned from `mapper` are fulfilled, or rejects if any of the promises reject. The fulfilled value is an `Array` of the fulfilled values returned from `mapper` in `input` order.
14
+ */
15
+ export function promiseMap<Element, NewElement>(
16
+ input: Iterable<Element>,
17
+ mapper: pMap.Mapper<Element, NewElement>,
18
+ options?: pMap.Options,
19
+ ): Promise<NewElement[]> {
20
+ return pMap.default(input, mapper, {
21
+ concurrency: options?.concurrency ?? PROMISE_CONCURRENCY_LIMIT,
22
+ stopOnError: options?.stopOnError ?? true,
23
+ });
24
+ }
25
+
26
+ /**
27
+ * This class allows you to track multiple promises and wait for all of them to resolve.
28
+ */
29
+ export class PromiseTracker {
30
+ private promises: Promise<any>[] = [];
31
+ private deferred: AnyFunction[] = [];
32
+
33
+ add(promise: Promise<any>) {
34
+ this.promises.push(promise);
35
+ void promise
36
+ .finally(() => this.remove(promise))
37
+ .catch(() => {
38
+ // The original promise is still awaited by callers; this only prevents
39
+ // the cleanup chain from surfacing a duplicate unhandled rejection.
40
+ });
41
+ }
42
+
43
+ /**
44
+ * @param deferred - A function that will be executed after all promises are resolved.
45
+ */
46
+ addDeferred(deferred: AnyFunction) {
47
+ this.deferred.push(deferred);
48
+ }
49
+
50
+ private remove(promise: Promise<any>) {
51
+ // eslint-disable-next-line @typescript-eslint/no-floating-promises
52
+ this.promises = this.promises.filter((p) => p !== promise);
53
+ }
54
+
55
+ async waitForAll() {
56
+ await Promise.all(this.promises);
57
+
58
+ await Promise.all(this.deferred.map((d) => d()));
59
+ }
60
+ }
61
+
62
+ /**
63
+ * This is a global instance of the PromiseTracker class.
64
+ */
65
+ export const globalPromiseTracker = new PromiseTracker();
@@ -0,0 +1,14 @@
1
+ import { ObjectType } from 'typeorm';
2
+ export declare function returnValue<T>(value: string | boolean | number): {
3
+ (): T;
4
+ };
5
+ export declare function returnValue<T>(value: T): {
6
+ (): ObjectType<T>;
7
+ };
8
+ export declare function returnAsyncValue<T>(value: T): {
9
+ (): Promise<T>;
10
+ };
11
+ export declare const returnProperty: <T>(property: keyof T) => {
12
+ (relationEntity: T): T[keyof T];
13
+ };
14
+ export default returnValue;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.returnProperty = void 0;
4
+ exports.returnValue = returnValue;
5
+ exports.returnAsyncValue = returnAsyncValue;
6
+ function returnValue(value) {
7
+ return () => value;
8
+ }
9
+ function returnAsyncValue(value) {
10
+ return async () => value;
11
+ }
12
+ const returnProperty = (property) => {
13
+ return (relationEntity) => relationEntity[property];
14
+ };
15
+ exports.returnProperty = returnProperty;
16
+ exports.default = returnValue;
17
+ //# sourceMappingURL=returnValue.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"returnValue.js","sourceRoot":"","sources":["returnValue.ts"],"names":[],"mappings":";;;AAMA,kCAEC;AAED,4CAIC;AARD,SAAgB,WAAW,CAAI,KAAQ;IACrC,OAAO,GAAG,EAAE,CAAC,KAAK,CAAC;AACrB,CAAC;AAED,SAAgB,gBAAgB,CAAI,KAAQ;IAG1C,OAAO,KAAK,IAAI,EAAE,CAAC,KAAK,CAAC;AAC3B,CAAC;AAKM,MAAM,cAAc,GAAG,CAC5B,QAAiB,EACoB,EAAE;IACvC,OAAO,CAAC,cAAiB,EAAE,EAAE,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC;AACzD,CAAC,CAAC;AAJW,QAAA,cAAc,kBAIzB;AAEF,kBAAe,WAAW,CAAC"}
@@ -0,0 +1,26 @@
1
+ import { ObjectType } from 'typeorm';
2
+
3
+ export function returnValue<T>(value: string | boolean | number): {
4
+ (): T;
5
+ };
6
+ export function returnValue<T>(value: T): { (): ObjectType<T> };
7
+ export function returnValue<T>(value: T): { (): T | ObjectType<T> } {
8
+ return () => value;
9
+ }
10
+
11
+ export function returnAsyncValue<T>(value: T): {
12
+ (): Promise<T>;
13
+ } {
14
+ return async () => value;
15
+ }
16
+
17
+ /**
18
+ * Helper function for typeorm decorators
19
+ */
20
+ export const returnProperty = <T>(
21
+ property: keyof T,
22
+ ): { (relationEntity: T): T[keyof T] } => {
23
+ return (relationEntity: T) => relationEntity[property];
24
+ };
25
+
26
+ export default returnValue;
@@ -0,0 +1,7 @@
1
+ import { AnyFunction } from '@nest-yalc-2/types/globals.d.js';
2
+ import { MonoTypeOperatorFunction, ObservableInput, Observable, UnaryFunction } from 'rxjs';
3
+ export declare function wrapIntoObservable<T>(input: Promise<T> | {
4
+ (): Promise<T>;
5
+ } | Observable<T> | T | void): Observable<T>;
6
+ export declare function wrapIntoAnOperator(input: AnyFunction): UnaryFunction<any, any>;
7
+ export declare function switchTap<T, O extends ObservableInput<any>>(project: (value: T, index: number) => O): MonoTypeOperatorFunction<T>;
@@ -0,0 +1,33 @@
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.wrapIntoObservable = wrapIntoObservable;
7
+ exports.wrapIntoAnOperator = wrapIntoAnOperator;
8
+ exports.switchTap = switchTap;
9
+ const rxjs_1 = require("rxjs");
10
+ const operators_1 = require("rxjs/operators");
11
+ const node_util_1 = __importDefault(require("node:util"));
12
+ function wrapIntoObservable(input) {
13
+ if (input === null || input === undefined) {
14
+ return rxjs_1.EMPTY;
15
+ }
16
+ if (node_util_1.default.types.isAsyncFunction(input)) {
17
+ return (0, rxjs_1.from)(input());
18
+ }
19
+ if ((0, rxjs_1.isObservable)(input)) {
20
+ return input;
21
+ }
22
+ return (0, rxjs_1.of)(input);
23
+ }
24
+ function wrapIntoAnOperator(input) {
25
+ if (node_util_1.default.types.isAsyncFunction(input)) {
26
+ return (0, operators_1.mergeMap)(input);
27
+ }
28
+ return input;
29
+ }
30
+ function switchTap(project) {
31
+ return (input) => input.pipe((0, operators_1.switchMap)((value, index) => (0, rxjs_1.combineLatest)([(0, rxjs_1.of)(value), project(value, index)])), (0, operators_1.map)(([intialValue, projectedValue]) => projectedValue !== undefined ? projectedValue : intialValue));
32
+ }
33
+ //# sourceMappingURL=rxjs.helper.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"rxjs.helper.js","sourceRoot":"","sources":["rxjs.helper.ts"],"names":[],"mappings":";;;;;AA0BA,gDAgBC;AAED,gDAQC;AAMD,8BAYC;AArED,+BAUc;AACd,8CAA0D;AAC1D,0DAA6B;AAa7B,SAAgB,kBAAkB,CAChC,KAAiE;IAEjE,IAAI,KAAK,KAAK,IAAI,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QAC1C,OAAO,YAAK,CAAC;IACf,CAAC;IAED,IAAI,mBAAI,CAAC,KAAK,CAAC,eAAe,CAAC,KAAK,CAAC,EAAE,CAAC;QACtC,OAAO,IAAA,WAAI,EAAE,KAA4B,EAAE,CAAC,CAAC;IAC/C,CAAC;IAED,IAAI,IAAA,mBAAY,EAAC,KAAK,CAAC,EAAE,CAAC;QACxB,OAAO,KAAK,CAAC;IACf,CAAC;IAED,OAAO,IAAA,SAAE,EAAC,KAAY,CAAC,CAAC;AAC1B,CAAC;AAED,SAAgB,kBAAkB,CAChC,KAAkB;IAElB,IAAI,mBAAI,CAAC,KAAK,CAAC,eAAe,CAAC,KAAK,CAAC,EAAE,CAAC;QACtC,OAAO,IAAA,oBAAQ,EAAC,KAAK,CAAC,CAAC;IACzB,CAAC;IAED,OAAO,KAAK,CAAC;AACf,CAAC;AAMD,SAAgB,SAAS,CACvB,OAAuC;IAEvC,OAAO,CAAC,KAAK,EAAE,EAAE,CACf,KAAK,CAAC,IAAI,CACR,IAAA,qBAAS,EAAC,CAAC,KAAK,EAAE,KAAK,EAAE,EAAE,CACzB,IAAA,oBAAa,EAAC,CAAC,IAAA,SAAE,EAAC,KAAK,CAAC,EAAE,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC,CAAC,CAClD,EACD,IAAA,eAAG,EAAC,CAAC,CAAC,WAAW,EAAE,cAAc,CAAC,EAAE,EAAE,CACpC,cAAc,KAAK,SAAS,CAAC,CAAC,CAAC,cAAc,CAAC,CAAC,CAAC,WAAW,CAC5D,CACF,CAAC;AACN,CAAC"}
@@ -0,0 +1,71 @@
1
+ import { AnyFunction } from '@nest-yalc-2/types/globals.d.js';
2
+ import {
3
+ combineLatest,
4
+ MonoTypeOperatorFunction,
5
+ ObservableInput,
6
+ of,
7
+ EMPTY,
8
+ from,
9
+ isObservable,
10
+ Observable,
11
+ UnaryFunction,
12
+ } from 'rxjs';
13
+ import { map, mergeMap, switchMap } from 'rxjs/operators';
14
+ import util from 'node:util';
15
+
16
+ /**
17
+ * Wraps any value into an observable.
18
+ *
19
+ * - If the input is an Observable, the input is returned directly.
20
+ * - If the input is a Promise, it is converted to an Observable.
21
+ * - If the input is `null` or `undefined` an empty Observable is returned.
22
+ * - Otherwise the input is simply wrapped into an Observable.
23
+ *
24
+ * @param input Input which will be wrapped into an Observable.
25
+ * @returns Observable which wraps the input.
26
+ */
27
+ export function wrapIntoObservable<T>(
28
+ input: Promise<T> | { (): Promise<T> } | Observable<T> | T | void,
29
+ ): Observable<T> {
30
+ if (input === null || input === undefined) {
31
+ return EMPTY;
32
+ }
33
+
34
+ if (util.types.isAsyncFunction(input)) {
35
+ return from((input as { (): Promise<T> })());
36
+ }
37
+
38
+ if (isObservable(input)) {
39
+ return input;
40
+ }
41
+
42
+ return of(input as any);
43
+ }
44
+
45
+ export function wrapIntoAnOperator(
46
+ input: AnyFunction,
47
+ ): UnaryFunction<any, any> {
48
+ if (util.types.isAsyncFunction(input)) {
49
+ return mergeMap(input);
50
+ }
51
+
52
+ return input;
53
+ }
54
+
55
+ /**
56
+ * Behaves exactly like `switchMap`, except that it maps all values back to the initial value.
57
+ * @param project Projection function which returns the inner observable.
58
+ */
59
+ export function switchTap<T, O extends ObservableInput<any>>(
60
+ project: (value: T, index: number) => O,
61
+ ): MonoTypeOperatorFunction<T> {
62
+ return (input) =>
63
+ input.pipe(
64
+ switchMap((value, index) =>
65
+ combineLatest([of(value), project(value, index)]),
66
+ ),
67
+ map(([intialValue, projectedValue]) =>
68
+ projectedValue !== undefined ? projectedValue : intialValue,
69
+ ),
70
+ );
71
+ }
@@ -0,0 +1,7 @@
1
+ import { NestInterceptor } from '@nestjs/common';
2
+ export declare function buildSimpleMapperInterceptor<T, R>(Dto: new (data: T) => R, options?: {
3
+ transformer?: (data: T) => T;
4
+ callback?: {
5
+ (inputData: any, outputData: any): any;
6
+ };
7
+ }): new () => NestInterceptor<T, R>;
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ Object.defineProperty(exports, "__esModule", { value: true });
9
+ exports.buildSimpleMapperInterceptor = buildSimpleMapperInterceptor;
10
+ const common_1 = require("@nestjs/common");
11
+ const rxjs_1 = require("rxjs");
12
+ function buildSimpleMapperInterceptor(Dto, options) {
13
+ let SimpleMapper = class SimpleMapper {
14
+ intercept(_context, next) {
15
+ return next.handle().pipe((0, rxjs_1.map)((data) => {
16
+ const tData = options?.transformer?.(data) ?? data;
17
+ const mappedData = Array.isArray(tData)
18
+ ? tData.map((d) => new Dto(d))
19
+ : new Dto(tData);
20
+ return options?.callback?.(data, mappedData) ?? mappedData;
21
+ }));
22
+ }
23
+ };
24
+ SimpleMapper = __decorate([
25
+ (0, common_1.Injectable)()
26
+ ], SimpleMapper);
27
+ return SimpleMapper;
28
+ }
29
+ //# sourceMappingURL=simple-mapper.interceptor.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"simple-mapper.interceptor.js","sourceRoot":"","sources":["simple-mapper.interceptor.ts"],"names":[],"mappings":";;;;;;;;AAeA,oEA2BC;AA1CD,2CAKwB;AACxB,+BAAuC;AASvC,SAAgB,4BAA4B,CAC1C,GAAuB,EACvB,OAGC;IAGD,IAAM,YAAY,GAAlB,MAAM,YAAY;QAChB,SAAS,CACP,QAA0B,EAC1B,IAAoB;YAEpB,OAAO,IAAI,CAAC,MAAM,EAAE,CAAC,IAAI,CACvB,IAAA,UAAG,EAAC,CAAC,IAAI,EAAE,EAAE;gBACX,MAAM,KAAK,GAAG,OAAO,EAAE,WAAW,EAAE,CAAC,IAAI,CAAC,IAAI,IAAI,CAAC;gBACnD,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC;oBACrC,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,CAAC;oBAC9B,CAAC,CAAC,IAAI,GAAG,CAAC,KAAK,CAAC,CAAC;gBAEnB,OAAO,OAAO,EAAE,QAAQ,EAAE,CAAC,IAAI,EAAE,UAAU,CAAC,IAAI,UAAU,CAAC;YAC7D,CAAC,CAAC,CACH,CAAC;QACJ,CAAC;KACF,CAAA;IAhBK,YAAY;QADjB,IAAA,mBAAU,GAAE;OACP,YAAY,CAgBjB;IAED,OAAO,YAAY,CAAC;AACtB,CAAC"}