@beecode/msh-util 1.2.0 → 1.2.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.
- package/lib/array-util.d.ts +2 -2
- package/lib/array-util.js +2 -2
- package/package.json +134 -127
- package/src/array-util.test.ts +50 -0
- package/src/array-util.ts +26 -0
- package/src/class-factory-pattern.test.ts +39 -0
- package/src/class-factory-pattern.ts +29 -0
- package/src/express/error-handler.test.ts +44 -0
- package/src/express/error-handler.ts +25 -0
- package/src/index.ts +25 -0
- package/src/joi-util.test.ts +192 -0
- package/src/joi-util.ts +65 -0
- package/src/memoize-factory.test.ts +40 -0
- package/src/memoize-factory.ts +27 -0
- package/src/object-util.test.ts +360 -0
- package/src/object-util.ts +127 -0
- package/src/regex-util.test.ts +25 -0
- package/src/regex-util.ts +11 -0
- package/src/single-threshold-promise.test.ts +91 -0
- package/src/single-threshold-promise.ts +56 -0
- package/src/singleton/async.test.ts +122 -0
- package/src/singleton/async.ts +90 -0
- package/src/singleton/pattern.test.ts +16 -0
- package/src/singleton/pattern.ts +44 -0
- package/src/string-util.test.ts +18 -0
- package/src/string-util.ts +18 -0
- package/src/time-util.test.ts +89 -0
- package/src/time-util.ts +98 -0
- package/src/timeout.test.ts +65 -0
- package/src/timeout.ts +16 -0
- package/src/type-util.test.ts +20 -0
- package/src/type-util.ts +54 -0
- package/src/types/any-function/index.ts +1 -0
- package/src/types/any-function/no-params.ts +1 -0
- package/src/types/any-function/promise-no-params.ts +1 -0
- package/src/types/any-function/promise.ts +1 -0
- package/src/types/types.d.ts +2 -0
package/lib/array-util.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ export declare const arrayUtil: {
|
|
|
5
5
|
* @param {T | null | undefined} value
|
|
6
6
|
* @return {value is T}
|
|
7
7
|
* @example
|
|
8
|
-
* const notEmptyArray = [0, 1, 2, null, undefined, ''].filter(arrayUtil.notEmpty)
|
|
8
|
+
* const notEmptyArray = [0, 1, 2, null, undefined, ''].filter(arrayUtil.notEmpty)
|
|
9
9
|
* console.log(notEmptyArray)// [0, 1, 2, '']
|
|
10
10
|
*/
|
|
11
11
|
notEmpty: <T>(value: T | null | undefined) => value is T;
|
|
@@ -15,7 +15,7 @@ export declare const arrayUtil: {
|
|
|
15
15
|
* @param {T | null | undefined} value
|
|
16
16
|
* @return {value is T}
|
|
17
17
|
* @example
|
|
18
|
-
* const notFalsyArray = [0, 1, 2, null, undefined, ''].filter(arrayUtil.notFalsy)
|
|
18
|
+
* const notFalsyArray = [0, 1, 2, null, undefined, ''].filter(arrayUtil.notFalsy)
|
|
19
19
|
* console.log(notFalsyArray)// [1, 2]
|
|
20
20
|
*/
|
|
21
21
|
notFalsy: <T_1>(value: T_1 | null | undefined) => value is T_1;
|
package/lib/array-util.js
CHANGED
|
@@ -8,7 +8,7 @@ exports.arrayUtil = {
|
|
|
8
8
|
* @param {T | null | undefined} value
|
|
9
9
|
* @return {value is T}
|
|
10
10
|
* @example
|
|
11
|
-
* const notEmptyArray = [0, 1, 2, null, undefined, ''].filter(arrayUtil.notEmpty)
|
|
11
|
+
* const notEmptyArray = [0, 1, 2, null, undefined, ''].filter(arrayUtil.notEmpty)
|
|
12
12
|
* console.log(notEmptyArray)// [0, 1, 2, '']
|
|
13
13
|
*/
|
|
14
14
|
notEmpty: (value) => {
|
|
@@ -20,7 +20,7 @@ exports.arrayUtil = {
|
|
|
20
20
|
* @param {T | null | undefined} value
|
|
21
21
|
* @return {value is T}
|
|
22
22
|
* @example
|
|
23
|
-
* const notFalsyArray = [0, 1, 2, null, undefined, ''].filter(arrayUtil.notFalsy)
|
|
23
|
+
* const notFalsyArray = [0, 1, 2, null, undefined, ''].filter(arrayUtil.notFalsy)
|
|
24
24
|
* console.log(notFalsyArray)// [1, 2]
|
|
25
25
|
*/
|
|
26
26
|
notFalsy: (value) => {
|
package/package.json
CHANGED
|
@@ -1,129 +1,136 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
2
|
+
"name": "@beecode/msh-util",
|
|
3
|
+
"version": "1.2.1",
|
|
4
|
+
"description": "",
|
|
5
|
+
"keywords": [],
|
|
6
|
+
"homepage": "https://github.com/beecode-rs/msh-util#readme",
|
|
7
|
+
"bugs": {
|
|
8
|
+
"url": "https://github.com/beecode-rs/msh-util/issues"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "https://github.com/beecode-rs/msh-util.git"
|
|
13
|
+
},
|
|
14
|
+
"license": "mit",
|
|
15
|
+
"author": "Milos Bugarinovic <milos.bugarinovic@gmail.com>",
|
|
16
|
+
"main": "./lib/index.js",
|
|
17
|
+
"types": "./lib/index.d.ts",
|
|
18
|
+
"files": [
|
|
19
|
+
"./lib",
|
|
20
|
+
"./src",
|
|
21
|
+
"./package-lock.json"
|
|
22
|
+
],
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "npm run clean && npm run tsc && npm run tsc:alias",
|
|
25
|
+
"build:on-success": "npm run tsc:cleaner && npm run tsc:alias",
|
|
26
|
+
"build:watch": "npm run clean && npm run tsc:watch -- --onCompilationComplete 'npm run build:on-success'",
|
|
27
|
+
"bump-version": "npm --no-git-tag-version version",
|
|
28
|
+
"clean": "rimraf ./lib/*",
|
|
29
|
+
"doc:api": "npx typedoc",
|
|
30
|
+
"doc:toc": "npx markdown-toc -i ./README.md",
|
|
31
|
+
"docker:build": "docker build -f ./.docker/Dockerfile -t bc-msh-util ./",
|
|
32
|
+
"docker:exec": "docker run --rm bc-msh-util",
|
|
33
|
+
"docker:exec:sh": "docker run --rm -it bc-msh-util sh",
|
|
34
|
+
"docker:rmi": "docker rmi bc-msh-util",
|
|
35
|
+
"eslint": "eslint ./ --quiet",
|
|
36
|
+
"eslint:fix": "npm run eslint -- --fix",
|
|
37
|
+
"initial-setup": "npm run initial-setup:husky && npm run initial-setup:git-config",
|
|
38
|
+
"initial-setup:git-config": "git config include.path ./.git-config",
|
|
39
|
+
"initial-setup:husky": "npx husky install",
|
|
40
|
+
"jsonlint": "npm run jsonlint:fix -- -c",
|
|
41
|
+
"jsonlint:fix": "jsonsort * '!./test/node_modules' '!./resource' '!**/.env*'",
|
|
42
|
+
"lint": "npm run prettier && npm run eslint && npm run jsonlint",
|
|
43
|
+
"lint:fix": "npm run prettier:fix && npm run eslint:fix && npm run jsonlint:fix",
|
|
44
|
+
"pack": "npm pack --pack-destination=./packages",
|
|
45
|
+
"prettier": "prettier --check {**/*,*}.{js,jsx,ts,tsx}",
|
|
46
|
+
"prettier:fix": "prettier --write {**/*,*}.{js,jsx,ts,tsx}",
|
|
47
|
+
"semantic-release": "semantic-release",
|
|
48
|
+
"semantic-release:check": "npm run semantic-release -- --dry-run --no-ci",
|
|
49
|
+
"test": "npm run test:unit #&& npm run test:int",
|
|
50
|
+
"test:e2e": "TZ=utc jest --config=./test/jest.config.js",
|
|
51
|
+
"test:int": "TZ=utc jest --config=./jest-int.config.js",
|
|
52
|
+
"test:unit": "TZ=utc jest --config=./jest-unit.config.js",
|
|
53
|
+
"test:unit:coverage": "npm run test:unit -- --coverage",
|
|
54
|
+
"test:unit:coverage:publish": "codecov",
|
|
55
|
+
"tsc": "tsc -p ./tsconfig-build.json",
|
|
56
|
+
"tsc:alias": "tsc-alias -p ./tsconfig-build.json",
|
|
57
|
+
"tsc:check": "npm run tsc -- --noEmit",
|
|
58
|
+
"tsc:check:list-files": "npm run tsc:check -- --listFiles",
|
|
59
|
+
"tsc:cleaner": "ts-cleaner --dist ./lib",
|
|
60
|
+
"tsc:cleaner:watch": "npm run tsc:cleaner -- --watch",
|
|
61
|
+
"tsc:time": "npm run tsc -- --diagnostics",
|
|
62
|
+
"tsc:watch": "tsc-watch -p ./tsconfig-build.json --preserveWatchOutput"
|
|
63
|
+
},
|
|
64
|
+
"commitlint": {
|
|
65
|
+
"extends": [
|
|
66
|
+
"@commitlint/config-conventional"
|
|
67
|
+
]
|
|
68
|
+
},
|
|
69
|
+
"lint-staged": {
|
|
70
|
+
"**.*.json": [
|
|
71
|
+
"npm run jsonlint"
|
|
72
|
+
],
|
|
73
|
+
"**/*.{ts,tsx,js,jsx}": [
|
|
74
|
+
"eslint",
|
|
75
|
+
"prettier --check"
|
|
76
|
+
]
|
|
77
|
+
},
|
|
78
|
+
"config": {
|
|
79
|
+
"commitizen": {
|
|
80
|
+
"path": "node_modules/cz-conventional-changelog"
|
|
81
|
+
}
|
|
82
|
+
},
|
|
83
|
+
"dependencies": {
|
|
84
|
+
"date-fns": "2.30.0",
|
|
85
|
+
"joi": "17.9.2",
|
|
86
|
+
"lodash.clonedeep": "4.5.0",
|
|
87
|
+
"rxjs": "7.8.1"
|
|
88
|
+
},
|
|
89
|
+
"devDependencies": {
|
|
90
|
+
"@commitlint/cli": "17.6.3",
|
|
91
|
+
"@commitlint/config-conventional": "17.6.3",
|
|
92
|
+
"@commitlint/prompt": "17.6.3",
|
|
93
|
+
"@semantic-release/changelog": "6.0.3",
|
|
94
|
+
"@semantic-release/commit-analyzer": "9.0.2",
|
|
95
|
+
"@semantic-release/exec": "6.0.3",
|
|
96
|
+
"@semantic-release/git": "10.0.1",
|
|
97
|
+
"@semantic-release/github": "8.0.7",
|
|
98
|
+
"@semantic-release/release-notes-generator": "11.0.1",
|
|
99
|
+
"@types/jest": "29.5.1",
|
|
100
|
+
"@types/lodash.clonedeep": "4.5.7",
|
|
101
|
+
"@types/node": "20.2.1",
|
|
102
|
+
"@typescript-eslint/eslint-plugin": "5.59.6",
|
|
103
|
+
"@typescript-eslint/parser": "5.59.6",
|
|
104
|
+
"commitizen": "4.3.0",
|
|
105
|
+
"eslint": "8.41.0",
|
|
106
|
+
"eslint-config-prettier": "8.8.0",
|
|
107
|
+
"eslint-plugin-import": "2.27.5",
|
|
108
|
+
"eslint-plugin-jest": "27.2.1",
|
|
109
|
+
"eslint-plugin-no-loops": "0.3.0",
|
|
110
|
+
"eslint-plugin-no-only-tests": "3.1.0",
|
|
111
|
+
"eslint-plugin-prettier": "4.2.1",
|
|
112
|
+
"eslint-plugin-sort-keys-fix": "1.1.2",
|
|
113
|
+
"husky": "8.0.3",
|
|
114
|
+
"jest": "29.5.0",
|
|
115
|
+
"jest-extended": "3.2.4",
|
|
116
|
+
"json-sort-cli": "3.1.3",
|
|
117
|
+
"lint-staged": "13.2.2",
|
|
118
|
+
"markdown-toc": "1.2.0",
|
|
119
|
+
"prettier": "2.8.8",
|
|
120
|
+
"rimraf": "5.0.1",
|
|
121
|
+
"semantic-release": "21.0.2",
|
|
122
|
+
"source-map-support": "0.5.21",
|
|
123
|
+
"ts-cleaner": "1.0.5",
|
|
124
|
+
"ts-jest": "29.1.0",
|
|
125
|
+
"ts-node": "10.9.1",
|
|
126
|
+
"tsc-alias": "1.8.6",
|
|
127
|
+
"tsc-watch": "6.0.4",
|
|
128
|
+
"typedoc": "0.24.7",
|
|
129
|
+
"typedoc-plugin-markdown": "3.15.3",
|
|
130
|
+
"typescript": "4.9.5"
|
|
131
|
+
},
|
|
132
|
+
"engines": {
|
|
133
|
+
"node": ">=14",
|
|
134
|
+
"npm": ">=6"
|
|
135
|
+
}
|
|
129
136
|
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { arrayUtil } from 'src/array-util'
|
|
2
|
+
|
|
3
|
+
describe('arrayUtil', () => {
|
|
4
|
+
describe('notEmpty', () => {
|
|
5
|
+
it.each([
|
|
6
|
+
[
|
|
7
|
+
[1, 2, undefined, 4, null, 5],
|
|
8
|
+
[1, 2, 4, 5],
|
|
9
|
+
],
|
|
10
|
+
[
|
|
11
|
+
['', 0],
|
|
12
|
+
['', 0],
|
|
13
|
+
],
|
|
14
|
+
[[undefined, null], []],
|
|
15
|
+
[[undefined, 4, null], [4]],
|
|
16
|
+
[
|
|
17
|
+
[[1], [2], [undefined], [4], [null], [5]],
|
|
18
|
+
[[1], [2], [undefined], [4], [null], [5]],
|
|
19
|
+
],
|
|
20
|
+
[
|
|
21
|
+
[{ a: 1 }, { a: 2 }, { a: undefined }, { a: 4 }, { a: null }, { a: 5 }],
|
|
22
|
+
[{ a: 1 }, { a: 2 }, { a: undefined }, { a: 4 }, { a: null }, { a: 5 }],
|
|
23
|
+
],
|
|
24
|
+
])('%#. should filter not empty values from array %s to be %s', (arr: any, expected) => {
|
|
25
|
+
expect(arr.filter(arrayUtil.notEmpty)).toEqual(expected)
|
|
26
|
+
})
|
|
27
|
+
})
|
|
28
|
+
|
|
29
|
+
describe('notFalsy', () => {
|
|
30
|
+
it.each([
|
|
31
|
+
[
|
|
32
|
+
[1, 2, undefined, 4, null, 5],
|
|
33
|
+
[1, 2, 4, 5],
|
|
34
|
+
],
|
|
35
|
+
[['', 0], []],
|
|
36
|
+
[[undefined, null], []],
|
|
37
|
+
[[undefined, 4, null], [4]],
|
|
38
|
+
[
|
|
39
|
+
[[1], [2], [undefined], [4], [null], [5]],
|
|
40
|
+
[[1], [2], [undefined], [4], [null], [5]],
|
|
41
|
+
],
|
|
42
|
+
[
|
|
43
|
+
[{ a: 1 }, { a: 2 }, { a: undefined }, { a: 4 }, { a: null }, { a: 5 }],
|
|
44
|
+
[{ a: 1 }, { a: 2 }, { a: undefined }, { a: 4 }, { a: null }, { a: 5 }],
|
|
45
|
+
],
|
|
46
|
+
])('%#. should filter not empty values from array %s to be %s', (arr: any, expected) => {
|
|
47
|
+
expect(arr.filter(arrayUtil.notFalsy)).toEqual(expected)
|
|
48
|
+
})
|
|
49
|
+
})
|
|
50
|
+
})
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export const arrayUtil = {
|
|
2
|
+
/**
|
|
3
|
+
* Check if array element is not empty
|
|
4
|
+
* @template T
|
|
5
|
+
* @param {T | null | undefined} value
|
|
6
|
+
* @return {value is T}
|
|
7
|
+
* @example
|
|
8
|
+
* const notEmptyArray = [0, 1, 2, null, undefined, ''].filter(arrayUtil.notEmpty)
|
|
9
|
+
* console.log(notEmptyArray)// [0, 1, 2, '']
|
|
10
|
+
*/
|
|
11
|
+
notEmpty: <T>(value: T | null | undefined): value is T => {
|
|
12
|
+
return value !== null && value !== undefined
|
|
13
|
+
},
|
|
14
|
+
/**
|
|
15
|
+
* Check if array element is not falsy
|
|
16
|
+
* @template T
|
|
17
|
+
* @param {T | null | undefined} value
|
|
18
|
+
* @return {value is T}
|
|
19
|
+
* @example
|
|
20
|
+
* const notFalsyArray = [0, 1, 2, null, undefined, ''].filter(arrayUtil.notFalsy)
|
|
21
|
+
* console.log(notFalsyArray)// [1, 2]
|
|
22
|
+
*/
|
|
23
|
+
notFalsy: <T>(value: T | null | undefined): value is T => {
|
|
24
|
+
return !!value
|
|
25
|
+
},
|
|
26
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { classFactoryPattern } from 'src/class-factory-pattern'
|
|
2
|
+
|
|
3
|
+
describe('factoryPattern', () => {
|
|
4
|
+
const fakeClassMock = jest.fn()
|
|
5
|
+
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
fakeClassMock.mockImplementation((a: string) => {
|
|
8
|
+
return { a }
|
|
9
|
+
})
|
|
10
|
+
})
|
|
11
|
+
|
|
12
|
+
afterEach(() => jest.resetAllMocks())
|
|
13
|
+
|
|
14
|
+
it('should return result from factory function', () => {
|
|
15
|
+
const expectedAValue = 'test'
|
|
16
|
+
const factoryPatternImplementation = classFactoryPattern(fakeClassMock)
|
|
17
|
+
expect(fakeClassMock).not.toHaveBeenCalled()
|
|
18
|
+
const result = factoryPatternImplementation(expectedAValue)
|
|
19
|
+
expect(fakeClassMock).toHaveBeenCalledTimes(1)
|
|
20
|
+
expect(result.a).toEqual(expectedAValue)
|
|
21
|
+
})
|
|
22
|
+
|
|
23
|
+
it('should call factory function every time the factory implementation is called', () => {
|
|
24
|
+
const expectedAValue = 'test'
|
|
25
|
+
const factoryPatternImplementation = classFactoryPattern(fakeClassMock)
|
|
26
|
+
expect(fakeClassMock).not.toHaveBeenCalled()
|
|
27
|
+
const result1 = factoryPatternImplementation(expectedAValue)
|
|
28
|
+
expect(fakeClassMock).toHaveBeenCalledTimes(1)
|
|
29
|
+
expect(result1.a).toEqual(expectedAValue)
|
|
30
|
+
|
|
31
|
+
const result2 = factoryPatternImplementation(expectedAValue)
|
|
32
|
+
expect(fakeClassMock).toHaveBeenCalledTimes(2)
|
|
33
|
+
expect(result2.a).toEqual(expectedAValue)
|
|
34
|
+
|
|
35
|
+
const result3 = factoryPatternImplementation(expectedAValue)
|
|
36
|
+
expect(fakeClassMock).toHaveBeenCalledTimes(3)
|
|
37
|
+
expect(result3.a).toEqual(expectedAValue)
|
|
38
|
+
})
|
|
39
|
+
})
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export type ClassType<T = object> = new (...args: T extends { new (...args: infer P): any } ? P : never[]) => T
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* This is a wrapper that easily converts class constructor call (`new className(..constructorParams)`) into function call (`classNameFactory(..constructorParams)`)
|
|
5
|
+
* @param {C} classType
|
|
6
|
+
* @template C
|
|
7
|
+
* @return {(...args: ConstructorParameters<C>) => InstanceType<C>}
|
|
8
|
+
* @example
|
|
9
|
+
* export class SomeClass {
|
|
10
|
+
* protected _a: string
|
|
11
|
+
*
|
|
12
|
+
* constructor(params: { a: string }) {
|
|
13
|
+
* const { a } = params
|
|
14
|
+
* this._a = a
|
|
15
|
+
* }
|
|
16
|
+
* }
|
|
17
|
+
*
|
|
18
|
+
* export const someClassFactory = classFactoryPattern(SomeClass)
|
|
19
|
+
*
|
|
20
|
+
* // using
|
|
21
|
+
* const someClassInstance = someClassFactory({ a: 'test' })
|
|
22
|
+
*/
|
|
23
|
+
export const classFactoryPattern = <C extends ClassType>(
|
|
24
|
+
classType: C
|
|
25
|
+
): ((...args: ConstructorParameters<C>) => InstanceType<C>) => {
|
|
26
|
+
return (...args: ConstructorParameters<C>): InstanceType<C> => {
|
|
27
|
+
return new classType(...args) as InstanceType<C>
|
|
28
|
+
}
|
|
29
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { expressErrorHandler } from 'src/express/error-handler'
|
|
2
|
+
|
|
3
|
+
describe('expressErrorHandler', () => {
|
|
4
|
+
const fake_fn = jest.fn()
|
|
5
|
+
const fake_req = jest.fn()
|
|
6
|
+
const fake_res = jest.fn()
|
|
7
|
+
const fake_next = jest.fn()
|
|
8
|
+
|
|
9
|
+
class FakeExpressController {
|
|
10
|
+
@expressErrorHandler
|
|
11
|
+
async login(_req: any, _res: any, _next: any): Promise<any> {
|
|
12
|
+
return await fake_fn()
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
const fakeExpressControllerInstance = new FakeExpressController()
|
|
16
|
+
|
|
17
|
+
afterEach(() => jest.resetAllMocks())
|
|
18
|
+
|
|
19
|
+
it('should call next with error if async function throws error', async () => {
|
|
20
|
+
const error = new Error()
|
|
21
|
+
fake_fn.mockRejectedValue(error)
|
|
22
|
+
|
|
23
|
+
await fakeExpressControllerInstance.login(fake_req, fake_res, fake_next)
|
|
24
|
+
|
|
25
|
+
expect(fake_fn).toHaveBeenCalledTimes(1)
|
|
26
|
+
expect(fake_req).not.toHaveBeenCalled()
|
|
27
|
+
expect(fake_res).not.toHaveBeenCalled()
|
|
28
|
+
expect(fake_next).toHaveBeenCalledTimes(1)
|
|
29
|
+
expect(fake_next).toHaveBeenCalledOnceWith(error)
|
|
30
|
+
})
|
|
31
|
+
|
|
32
|
+
it('should work as usual if async function resolves promise', async () => {
|
|
33
|
+
const expected = { successful: true }
|
|
34
|
+
fake_fn.mockResolvedValue(expected)
|
|
35
|
+
|
|
36
|
+
const result = await fakeExpressControllerInstance.login(fake_req, fake_res, fake_next)
|
|
37
|
+
|
|
38
|
+
expect(fake_fn).toHaveBeenCalledTimes(1)
|
|
39
|
+
expect(fake_req).not.toHaveBeenCalled()
|
|
40
|
+
expect(fake_res).not.toHaveBeenCalled()
|
|
41
|
+
expect(fake_next).not.toHaveBeenCalled()
|
|
42
|
+
expect(result).toBe(expected)
|
|
43
|
+
})
|
|
44
|
+
})
|
|
@@ -0,0 +1,25 @@
|
|
|
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 const expressErrorHandler = (_target: any, _key: string, descriptor: TypedPropertyDescriptor<any>): any => {
|
|
17
|
+
const originalMethod = descriptor.value
|
|
18
|
+
descriptor.value = function (): any {
|
|
19
|
+
const next = arguments[2] // eslint-disable-line prefer-rest-params
|
|
20
|
+
|
|
21
|
+
return Promise.resolve(originalMethod.apply(this, arguments)).catch(next) // eslint-disable-line prefer-rest-params
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return descriptor
|
|
25
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export * from 'src/express/error-handler'
|
|
2
|
+
|
|
3
|
+
export * from 'src/singleton/async'
|
|
4
|
+
|
|
5
|
+
export * from 'src/singleton/pattern'
|
|
6
|
+
|
|
7
|
+
export * from 'src/class-factory-pattern'
|
|
8
|
+
|
|
9
|
+
export * from 'src/joi-util'
|
|
10
|
+
|
|
11
|
+
export * from 'src/memoize-factory'
|
|
12
|
+
|
|
13
|
+
export * from 'src/object-util'
|
|
14
|
+
|
|
15
|
+
export * from 'src/regex-util'
|
|
16
|
+
|
|
17
|
+
export * from 'src/single-threshold-promise'
|
|
18
|
+
|
|
19
|
+
export * from 'src/string-util'
|
|
20
|
+
|
|
21
|
+
export * from 'src/time-util'
|
|
22
|
+
|
|
23
|
+
export * from 'src/timeout'
|
|
24
|
+
|
|
25
|
+
export * from 'src/type-util'
|