feathers-utils 1.6.1 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/esm/hooks/checkMulti.d.ts +2 -0
- package/dist/esm/hooks/checkMulti.js +18 -0
- package/dist/esm/hooks/runPerItem.d.ts +4 -0
- package/dist/esm/hooks/runPerItem.js +27 -0
- package/dist/esm/hooks/setData.d.ts +4 -0
- package/dist/esm/hooks/setData.js +40 -0
- package/dist/esm/index.d.ts +30 -0
- package/dist/esm/index.js +31 -0
- package/dist/esm/mixins/debounce-mixin/DebouncedStore.d.ts +15 -0
- package/dist/esm/mixins/debounce-mixin/DebouncedStore.js +46 -0
- package/dist/esm/mixins/debounce-mixin/index.d.ts +5 -0
- package/dist/esm/mixins/debounce-mixin/index.js +20 -0
- package/dist/esm/types.d.ts +61 -0
- package/dist/esm/types.js +2 -0
- package/dist/esm/utils/addHook.d.ts +3 -0
- package/dist/esm/utils/addHook.js +36 -0
- package/dist/esm/utils/filterQuery.d.ts +3 -0
- package/dist/esm/utils/filterQuery.js +28 -0
- package/dist/esm/utils/getPaginate.d.ts +2 -0
- package/dist/esm/utils/getPaginate.js +14 -0
- package/dist/esm/utils/isMulti.d.ts +2 -0
- package/dist/esm/utils/isMulti.js +17 -0
- package/dist/esm/utils/isPaginated.d.ts +2 -0
- package/dist/esm/utils/isPaginated.js +8 -0
- package/dist/esm/utils/markHookForSkip.d.ts +3 -0
- package/dist/esm/utils/markHookForSkip.js +14 -0
- package/dist/esm/utils/mergeQuery/index.d.ts +3 -0
- package/dist/esm/utils/mergeQuery/index.js +327 -0
- package/dist/esm/utils/mergeQuery/mergeArrays.d.ts +2 -0
- package/dist/esm/utils/mergeQuery/mergeArrays.js +37 -0
- package/dist/esm/utils/pushSet.d.ts +2 -0
- package/dist/esm/utils/pushSet.js +19 -0
- package/dist/esm/utils/setResultEmpty.d.ts +2 -0
- package/dist/esm/utils/setResultEmpty.js +25 -0
- package/dist/esm/utils/shouldSkip.d.ts +2 -0
- package/dist/esm/utils/shouldSkip.js +29 -0
- package/dist/hooks/checkMulti.js +2 -2
- package/dist/hooks/runPerItem.d.ts +2 -1
- package/dist/hooks/runPerItem.js +5 -3
- package/dist/hooks/setData.js +13 -6
- package/dist/index.d.ts +4 -1
- package/dist/index.js +7 -1
- package/dist/mixins/debounce-mixin/DebouncedStore.d.ts +1 -1
- package/dist/mixins/debounce-mixin/DebouncedStore.js +2 -2
- package/dist/mixins/debounce-mixin/index.js +1 -1
- package/dist/types.d.ts +4 -2
- package/dist/utils/filterQuery.js +2 -2
- package/dist/utils/getPaginate.d.ts +2 -0
- package/dist/utils/getPaginate.js +15 -0
- package/dist/utils/isMulti.js +2 -3
- package/dist/utils/isPaginated.d.ts +2 -0
- package/dist/utils/isPaginated.js +12 -0
- package/dist/utils/markHookForSkip.js +1 -1
- package/dist/utils/mergeQuery/index.js +38 -38
- package/dist/utils/pushSet.js +3 -3
- package/dist/utils/setResultEmpty.d.ts +2 -0
- package/dist/utils/setResultEmpty.js +29 -0
- package/package.json +40 -24
- package/src/hooks/runPerItem.ts +3 -1
- package/src/hooks/setData.ts +8 -1
- package/src/index.ts +3 -0
- package/src/mixins/debounce-mixin/DebouncedStore.ts +49 -49
- package/src/types.ts +7 -2
- package/src/utils/getPaginate.ts +18 -0
- package/src/utils/isMulti.ts +2 -3
- package/src/utils/isPaginated.ts +12 -0
- package/src/utils/setResultEmpty.ts +28 -0
- package/.eslintignore +0 -3
- package/.eslintrc.js +0 -44
- package/.github/workflows/node.js.yml +0 -45
- package/.gitlab-ci.yml +0 -11
- package/.mocharc.js +0 -11
- package/.nycrc.json +0 -22
- package/.vscode/launch.json +0 -28
- package/.vscode/settings.json +0 -14
- package/index.js +0 -9
- package/test/hooks/checkMulti.test.ts +0 -121
- package/test/hooks/setData.test.ts +0 -333
- package/test/mixins/debounce-mixin.test.ts +0 -174
- package/test/utils/addHook.test.ts +0 -307
- package/test/utils/isMulti.test.ts +0 -53
- package/test/utils/markHookForSkip.test.ts +0 -292
- package/test/utils/mergeQuery.test.ts +0 -407
- package/test/utils/pushSet.test.ts +0 -66
- package/test/utils/shouldSkip.test.ts +0 -67
- package/tsconfig.json +0 -16
- package/tsconfig.test.json +0 -13
package/src/utils/isMulti.ts
CHANGED
|
@@ -4,13 +4,12 @@ export const isMulti = (context: HookContext): boolean => {
|
|
|
4
4
|
const { method } = context;
|
|
5
5
|
if (method === "find") {
|
|
6
6
|
return true;
|
|
7
|
-
} else if (["patch", "remove"].includes(
|
|
7
|
+
} else if (["patch", "remove"].includes(method)) {
|
|
8
8
|
return context.id == null;
|
|
9
9
|
} else if (method === "create") {
|
|
10
10
|
const items = context.type === "before" ? context.data : context.result;
|
|
11
|
-
items && context.method === "find" ? items.data || items : items;
|
|
12
11
|
return Array.isArray(items);
|
|
13
|
-
} else if (
|
|
12
|
+
} else if (["get", "update"].includes(method)) {
|
|
14
13
|
return false;
|
|
15
14
|
}
|
|
16
15
|
return false;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { HookContext } from "@feathersjs/feathers";
|
|
2
|
+
import { getPaginate } from "./getPaginate";
|
|
3
|
+
|
|
4
|
+
export const isPaginated = (
|
|
5
|
+
context: HookContext
|
|
6
|
+
): boolean => {
|
|
7
|
+
if (context.params.paginate === false) { return false; }
|
|
8
|
+
|
|
9
|
+
const paginate = getPaginate(context);
|
|
10
|
+
|
|
11
|
+
return !!paginate;
|
|
12
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { HookContext } from "@feathersjs/feathers";
|
|
2
|
+
import { isMulti } from "..";
|
|
3
|
+
import { isPaginated } from "./isPaginated";
|
|
4
|
+
|
|
5
|
+
export const setResultEmpty = (
|
|
6
|
+
context: HookContext
|
|
7
|
+
): HookContext => {
|
|
8
|
+
if (context.result) { return context; }
|
|
9
|
+
|
|
10
|
+
const multi = isMulti(context);
|
|
11
|
+
|
|
12
|
+
if (multi) {
|
|
13
|
+
if (context.method === "find" && isPaginated(context)) {
|
|
14
|
+
context.result = {
|
|
15
|
+
total: 0,
|
|
16
|
+
skip: 0,
|
|
17
|
+
limit: 0,
|
|
18
|
+
data: []
|
|
19
|
+
};
|
|
20
|
+
} else {
|
|
21
|
+
context.result = [];
|
|
22
|
+
}
|
|
23
|
+
} else {
|
|
24
|
+
context.result = null;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
return context;
|
|
28
|
+
};
|
package/.eslintignore
DELETED
package/.eslintrc.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
module.exports = {
|
|
2
|
-
root: true,
|
|
3
|
-
env: {
|
|
4
|
-
node: true,
|
|
5
|
-
mocha: true
|
|
6
|
-
},
|
|
7
|
-
parser: "@typescript-eslint/parser",
|
|
8
|
-
plugins: [
|
|
9
|
-
"security",
|
|
10
|
-
"@typescript-eslint"
|
|
11
|
-
],
|
|
12
|
-
extends: [
|
|
13
|
-
"eslint:recommended",
|
|
14
|
-
"plugin:security/recommended",
|
|
15
|
-
"plugin:@typescript-eslint/recommended"
|
|
16
|
-
],
|
|
17
|
-
rules: {
|
|
18
|
-
"quotes": ["warn", "double", "avoid-escape"],
|
|
19
|
-
"indent": ["warn", 2, { "SwitchCase": 1 }],
|
|
20
|
-
"semi": ["warn", "always"],
|
|
21
|
-
"@typescript-eslint/no-unused-vars": "warn",
|
|
22
|
-
"no-console": "off",
|
|
23
|
-
"camelcase": "warn",
|
|
24
|
-
"require-atomic-updates": "off",
|
|
25
|
-
"prefer-destructuring": ["warn", {
|
|
26
|
-
"array": false,
|
|
27
|
-
"object": true
|
|
28
|
-
}, {
|
|
29
|
-
"enforceForRenamedProperties": false
|
|
30
|
-
}],
|
|
31
|
-
"security/detect-object-injection": "off",
|
|
32
|
-
"object-curly-spacing": ["warn", "always"],
|
|
33
|
-
"prefer-const": ["warn"]
|
|
34
|
-
},
|
|
35
|
-
overrides: [
|
|
36
|
-
{
|
|
37
|
-
"files": ["test/**/*.ts"],
|
|
38
|
-
"rules": {
|
|
39
|
-
"@typescript-eslint/ban-ts-comment": ["off"]
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
]
|
|
43
|
-
};
|
|
44
|
-
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
# This workflow will do a clean install of node dependencies, build the source code and run tests across different versions of node
|
|
2
|
-
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
|
|
3
|
-
|
|
4
|
-
name: Node.js CI
|
|
5
|
-
|
|
6
|
-
on:
|
|
7
|
-
push:
|
|
8
|
-
branches: [ main ]
|
|
9
|
-
pull_request:
|
|
10
|
-
branches: [ main ]
|
|
11
|
-
|
|
12
|
-
jobs:
|
|
13
|
-
test:
|
|
14
|
-
|
|
15
|
-
runs-on: ubuntu-latest
|
|
16
|
-
|
|
17
|
-
strategy:
|
|
18
|
-
matrix:
|
|
19
|
-
node-version: [12.x, 14.x, 16.x]
|
|
20
|
-
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
|
|
21
|
-
|
|
22
|
-
steps:
|
|
23
|
-
- uses: actions/checkout@v2
|
|
24
|
-
- name: Use Node.js ${{ matrix.node-version }}
|
|
25
|
-
uses: actions/setup-node@v1
|
|
26
|
-
with:
|
|
27
|
-
node-version: ${{ matrix.node-version }}
|
|
28
|
-
- run: npm ci
|
|
29
|
-
- run: npm run test
|
|
30
|
-
|
|
31
|
-
coverage:
|
|
32
|
-
needs: [ test ]
|
|
33
|
-
name: coverage
|
|
34
|
-
runs-on: ubuntu-latest
|
|
35
|
-
steps:
|
|
36
|
-
- uses: actions/checkout@master
|
|
37
|
-
- uses: actions/setup-node@master
|
|
38
|
-
with:
|
|
39
|
-
node-version: '14'
|
|
40
|
-
- run: npm ci
|
|
41
|
-
- uses: paambaati/codeclimate-action@v2.7.5
|
|
42
|
-
env:
|
|
43
|
-
CC_TEST_REPORTER_ID: ${{ secrets.CC_TEST_REPORTER_ID }}
|
|
44
|
-
with:
|
|
45
|
-
coverageCommand: npm run coverage
|
package/.gitlab-ci.yml
DELETED
package/.mocharc.js
DELETED
package/.nycrc.json
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"extends": "@istanbuljs/nyc-config-typescript",
|
|
3
|
-
"cache": false,
|
|
4
|
-
"check-coverage": false,
|
|
5
|
-
"include": [
|
|
6
|
-
"src/**/*.js",
|
|
7
|
-
"src/**/*.ts"
|
|
8
|
-
],
|
|
9
|
-
"exclude": [
|
|
10
|
-
"src/types.ts",
|
|
11
|
-
"src/index.ts"
|
|
12
|
-
],
|
|
13
|
-
"reporter": [
|
|
14
|
-
"html",
|
|
15
|
-
"text",
|
|
16
|
-
"text-summary",
|
|
17
|
-
"lcov"
|
|
18
|
-
],
|
|
19
|
-
"sourceMap": true,
|
|
20
|
-
"all": true,
|
|
21
|
-
"instrument": true
|
|
22
|
-
}
|
package/.vscode/launch.json
DELETED
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": "0.2.0",
|
|
3
|
-
"configurations": [
|
|
4
|
-
{
|
|
5
|
-
"type": "node",
|
|
6
|
-
"request": "launch",
|
|
7
|
-
"name": "Mocha Tests",
|
|
8
|
-
"program": "${workspaceFolder}/node_modules/mocha/bin/mocha",
|
|
9
|
-
"args": [
|
|
10
|
-
"--require",
|
|
11
|
-
"ts-node/register",
|
|
12
|
-
"--timeout",
|
|
13
|
-
"999999",
|
|
14
|
-
"--colors",
|
|
15
|
-
"--recursive",
|
|
16
|
-
],
|
|
17
|
-
"internalConsoleOptions": "openOnSessionStart",
|
|
18
|
-
"env": {
|
|
19
|
-
"NODE_ENV": "test",
|
|
20
|
-
"TS_NODE_PROJECT": "tsconfig.test.json"
|
|
21
|
-
},
|
|
22
|
-
"outputCapture": "std",
|
|
23
|
-
"skipFiles": [
|
|
24
|
-
"<node_internals>/**"
|
|
25
|
-
]
|
|
26
|
-
}
|
|
27
|
-
]
|
|
28
|
-
}
|
package/.vscode/settings.json
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
// Place your settings in this file to overwrite default and user settings.
|
|
2
|
-
{
|
|
3
|
-
"search.exclude": {
|
|
4
|
-
"**/node_modules": true,
|
|
5
|
-
"**/public": true,
|
|
6
|
-
"**/package-lock.json": true
|
|
7
|
-
},
|
|
8
|
-
"workbench.colorCustomizations": {
|
|
9
|
-
"activityBar.background": "#525252",
|
|
10
|
-
"titleBar.activeBackground": "#525252",
|
|
11
|
-
"titleBar.activeForeground": "#FAFBF4"
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
package/index.js
DELETED
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
exports.mergeQuery = require("./src/mergeQuery");
|
|
2
|
-
exports.mergeArrays = require("./src/mergeQuery/mergeArrays");
|
|
3
|
-
exports.shouldSkip = require("./src/shouldSkip");
|
|
4
|
-
exports.pushSet = require("./src/pushSet");
|
|
5
|
-
exports.isMulti = require("./src/isMulti");
|
|
6
|
-
|
|
7
|
-
exports.hooks = {
|
|
8
|
-
checkMulti: require("./src/hooks/checkMulti")
|
|
9
|
-
};
|
|
@@ -1,121 +0,0 @@
|
|
|
1
|
-
import assert from "assert";
|
|
2
|
-
import { checkMulti } from "../../src";
|
|
3
|
-
|
|
4
|
-
describe("hook - checkMulti", function() {
|
|
5
|
-
it("passes if 'allowsMulti' not defined", function() {
|
|
6
|
-
const makeContext = (type: string, method: string) => ({
|
|
7
|
-
service: {},
|
|
8
|
-
type,
|
|
9
|
-
method
|
|
10
|
-
});
|
|
11
|
-
["before", "after"].forEach(type => {
|
|
12
|
-
["find", "get", "create", "update", "patch", "remove"].forEach(method => {
|
|
13
|
-
const context = makeContext(type, method);
|
|
14
|
-
//@ts-ignore
|
|
15
|
-
assert.doesNotThrow(() => checkMulti()(context), `'${type}:${method}': does not throw`);
|
|
16
|
-
});
|
|
17
|
-
});
|
|
18
|
-
});
|
|
19
|
-
|
|
20
|
-
it("passes if 'allowsMulti' returns true", function() {
|
|
21
|
-
const makeContext = (type: string, method: string) => {
|
|
22
|
-
const context = {
|
|
23
|
-
service: {
|
|
24
|
-
allowsMulti: () => true
|
|
25
|
-
},
|
|
26
|
-
method,
|
|
27
|
-
type,
|
|
28
|
-
};
|
|
29
|
-
if (method === "create") {
|
|
30
|
-
(type === "before")
|
|
31
|
-
//@ts-ignore
|
|
32
|
-
? context.data = []
|
|
33
|
-
//@ts-ignore
|
|
34
|
-
: context.result = [];
|
|
35
|
-
}
|
|
36
|
-
return context;
|
|
37
|
-
};
|
|
38
|
-
["before", "after"].forEach(type => {
|
|
39
|
-
["find", "get", "create", "update", "patch", "remove"].forEach(method => {
|
|
40
|
-
const context = makeContext(type, method);
|
|
41
|
-
//@ts-ignore
|
|
42
|
-
assert.doesNotThrow(() => checkMulti()(context), `'${type}:${method}': does not throw`);
|
|
43
|
-
});
|
|
44
|
-
});
|
|
45
|
-
});
|
|
46
|
-
|
|
47
|
-
it("passes for 'find', 'get' and 'update'", function() {
|
|
48
|
-
const makeContext = (type: string, method: string) => ({
|
|
49
|
-
service: {
|
|
50
|
-
allowsMulti: () => false
|
|
51
|
-
},
|
|
52
|
-
type,
|
|
53
|
-
method
|
|
54
|
-
});
|
|
55
|
-
["before", "after"].forEach(type => {
|
|
56
|
-
["find", "get", "update"].forEach(method => {
|
|
57
|
-
const context = makeContext(type, method);
|
|
58
|
-
//@ts-ignore
|
|
59
|
-
assert.doesNotThrow(() => checkMulti()(context), `'${type}:${method}': does not throw`);
|
|
60
|
-
});
|
|
61
|
-
});
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it("passes if 'allowsMulti' returns false and is no multi data", function() {
|
|
65
|
-
const makeContext = (type: string, method: string) => {
|
|
66
|
-
const context = {
|
|
67
|
-
service: {
|
|
68
|
-
allowsMulti: (method: string) => method !== "find"
|
|
69
|
-
},
|
|
70
|
-
method,
|
|
71
|
-
type,
|
|
72
|
-
};
|
|
73
|
-
if (method === "create") {
|
|
74
|
-
(type === "before")
|
|
75
|
-
//@ts-ignore
|
|
76
|
-
? context.data = {}
|
|
77
|
-
//@ts-ignore
|
|
78
|
-
: context.result = {};
|
|
79
|
-
}
|
|
80
|
-
if (["patch", "remove"].includes(method)) {
|
|
81
|
-
//@ts-ignore
|
|
82
|
-
context.id = 1;
|
|
83
|
-
}
|
|
84
|
-
return context;
|
|
85
|
-
};
|
|
86
|
-
["before", "after"].forEach(type => {
|
|
87
|
-
["create", "patch", "remove"].forEach(method => {
|
|
88
|
-
const context = makeContext(type, method);
|
|
89
|
-
//@ts-ignore
|
|
90
|
-
assert.doesNotThrow(() => checkMulti()(context), `'${type}:${method}': does not throw`);
|
|
91
|
-
});
|
|
92
|
-
});
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
it("throws if 'allowsMulti' returns false and multi data", function() {
|
|
96
|
-
const makeContext = (type: string, method: string) => {
|
|
97
|
-
const context = {
|
|
98
|
-
service: {
|
|
99
|
-
allowsMulti: () => false
|
|
100
|
-
},
|
|
101
|
-
method,
|
|
102
|
-
type,
|
|
103
|
-
};
|
|
104
|
-
if (method === "create") {
|
|
105
|
-
(type === "before")
|
|
106
|
-
//@ts-ignore
|
|
107
|
-
? context.data = []
|
|
108
|
-
//@ts-ignore
|
|
109
|
-
: context.result = [];
|
|
110
|
-
}
|
|
111
|
-
return context;
|
|
112
|
-
};
|
|
113
|
-
["before", "after"].forEach(type => {
|
|
114
|
-
["create", "patch", "remove"].forEach(method => {
|
|
115
|
-
const context = makeContext(type, method);
|
|
116
|
-
//@ts-ignore
|
|
117
|
-
assert.throws(() => checkMulti()(context), `'${type}:${method}': throws`);
|
|
118
|
-
});
|
|
119
|
-
});
|
|
120
|
-
});
|
|
121
|
-
});
|