@strapi/utils 4.5.0-alpha.0 → 4.5.0-beta.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/lib/content-types.js +4 -3
- package/lib/convert-query-params.js +9 -1
- package/lib/hooks.js +19 -12
- package/lib/provider-factory.js +1 -17
- package/lib/relations.js +15 -0
- package/package.json +2 -2
package/lib/content-types.js
CHANGED
|
@@ -106,9 +106,10 @@ const isPrivateAttribute = (model = {}, attributeName) => {
|
|
|
106
106
|
const isScalarAttribute = (attribute) => {
|
|
107
107
|
return !['media', 'component', 'relation', 'dynamiczone'].includes(attribute.type);
|
|
108
108
|
};
|
|
109
|
-
const isMediaAttribute = (attribute) => attribute.type === 'media';
|
|
110
|
-
const isRelationalAttribute = (attribute) => attribute.type === 'relation';
|
|
111
|
-
const isComponentAttribute = (attribute) =>
|
|
109
|
+
const isMediaAttribute = (attribute) => attribute && attribute.type === 'media';
|
|
110
|
+
const isRelationalAttribute = (attribute) => attribute && attribute.type === 'relation';
|
|
111
|
+
const isComponentAttribute = (attribute) =>
|
|
112
|
+
attribute && ['component', 'dynamiczone'].includes(attribute.type);
|
|
112
113
|
|
|
113
114
|
const getComponentAttributes = (schema) => {
|
|
114
115
|
return _.reduce(
|
|
@@ -49,6 +49,10 @@ const convertCountQueryParams = (countQuery) => {
|
|
|
49
49
|
return parseType({ type: 'boolean', value: countQuery });
|
|
50
50
|
};
|
|
51
51
|
|
|
52
|
+
const convertOrderingQueryParams = (ordering) => {
|
|
53
|
+
return ordering;
|
|
54
|
+
};
|
|
55
|
+
|
|
52
56
|
/**
|
|
53
57
|
* Sort query parser
|
|
54
58
|
* @param {string} sortQuery - ex: id:asc,price:desc
|
|
@@ -248,7 +252,7 @@ const convertNestedPopulate = (subPopulate, schema) => {
|
|
|
248
252
|
}
|
|
249
253
|
|
|
250
254
|
// TODO: We will need to consider a way to add limitation / pagination
|
|
251
|
-
const { sort, filters, fields, populate, count } = subPopulate;
|
|
255
|
+
const { sort, filters, fields, populate, count, ordering } = subPopulate;
|
|
252
256
|
|
|
253
257
|
const query = {};
|
|
254
258
|
|
|
@@ -272,6 +276,10 @@ const convertNestedPopulate = (subPopulate, schema) => {
|
|
|
272
276
|
query.count = convertCountQueryParams(count);
|
|
273
277
|
}
|
|
274
278
|
|
|
279
|
+
if (ordering) {
|
|
280
|
+
query.ordering = convertOrderingQueryParams(ordering);
|
|
281
|
+
}
|
|
282
|
+
|
|
275
283
|
return query;
|
|
276
284
|
};
|
|
277
285
|
|
package/lib/hooks.js
CHANGED
|
@@ -2,17 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
const { eq, remove, cloneDeep } = require('lodash/fp');
|
|
4
4
|
|
|
5
|
-
/**
|
|
6
|
-
* @typedef Hook
|
|
7
|
-
* @property {Array<Function>} _handlers - A registry of handler used by the hook
|
|
8
|
-
* @property {function(Function):Hook} register - Register a new handler into the hook's registry
|
|
9
|
-
* @property {function(Function):Hook} delete- Delete the given handler from the hook's registry
|
|
10
|
-
* @property {Function} call - Not implemented by default, can be replaced by any implementation.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
5
|
/**
|
|
14
6
|
* Create a default Strapi hook
|
|
15
|
-
* @return {Hook}
|
|
16
7
|
*/
|
|
17
8
|
const createHook = () => {
|
|
18
9
|
const state = {
|
|
@@ -45,7 +36,6 @@ const createHook = () => {
|
|
|
45
36
|
/**
|
|
46
37
|
* Create an async series hook.
|
|
47
38
|
* Upon execution, it will execute every handler in order with the same context
|
|
48
|
-
* @return {Hook}
|
|
49
39
|
*/
|
|
50
40
|
const createAsyncSeriesHook = () => ({
|
|
51
41
|
...createHook(),
|
|
@@ -60,7 +50,6 @@ const createAsyncSeriesHook = () => ({
|
|
|
60
50
|
/**
|
|
61
51
|
* Create an async series waterfall hook.
|
|
62
52
|
* Upon execution, it will execute every handler in order and pass the return value of the last handler to the next one
|
|
63
|
-
* @return {Hook}
|
|
64
53
|
*/
|
|
65
54
|
const createAsyncSeriesWaterfallHook = () => ({
|
|
66
55
|
...createHook(),
|
|
@@ -79,7 +68,6 @@ const createAsyncSeriesWaterfallHook = () => ({
|
|
|
79
68
|
/**
|
|
80
69
|
* Create an async parallel hook.
|
|
81
70
|
* Upon execution, it will execute every registered handler in band.
|
|
82
|
-
* @return {Hook}
|
|
83
71
|
*/
|
|
84
72
|
const createAsyncParallelHook = () => ({
|
|
85
73
|
...createHook(),
|
|
@@ -91,6 +79,24 @@ const createAsyncParallelHook = () => ({
|
|
|
91
79
|
},
|
|
92
80
|
});
|
|
93
81
|
|
|
82
|
+
/**
|
|
83
|
+
* Create an async parallel hook.
|
|
84
|
+
* Upon execution, it will execute every registered handler in serie and return the first result found.
|
|
85
|
+
*/
|
|
86
|
+
const createAsyncBailHook = () => ({
|
|
87
|
+
...createHook(),
|
|
88
|
+
|
|
89
|
+
async call(context) {
|
|
90
|
+
for (const handler of this.getHandlers()) {
|
|
91
|
+
const result = await handler(context);
|
|
92
|
+
|
|
93
|
+
if (result !== undefined) {
|
|
94
|
+
return result;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
|
|
94
100
|
module.exports = {
|
|
95
101
|
// Internal utils
|
|
96
102
|
internals: {
|
|
@@ -100,4 +106,5 @@ module.exports = {
|
|
|
100
106
|
createAsyncSeriesHook,
|
|
101
107
|
createAsyncSeriesWaterfallHook,
|
|
102
108
|
createAsyncParallelHook,
|
|
109
|
+
createAsyncBailHook,
|
|
103
110
|
};
|
package/lib/provider-factory.js
CHANGED
|
@@ -25,25 +25,9 @@ const createProviderHooksMap = () => ({
|
|
|
25
25
|
});
|
|
26
26
|
|
|
27
27
|
/**
|
|
28
|
-
* A
|
|
29
|
-
* @typedef {Object} Provider
|
|
30
|
-
* @property hooks
|
|
31
|
-
* @property register
|
|
32
|
-
* @property delete
|
|
33
|
-
* @property get
|
|
34
|
-
* @property getWhere
|
|
35
|
-
* @property values
|
|
36
|
-
* @property keys
|
|
37
|
-
* @property has
|
|
38
|
-
* @property size
|
|
39
|
-
* @property clear
|
|
40
|
-
*/
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* A {@link Provider} factory
|
|
28
|
+
* A Provider factory
|
|
44
29
|
* @param {Object} [options] - The factory options
|
|
45
30
|
* @param {boolean = true} options.throwOnDuplicates - Specify the wanted behaviour when encountering a duplicate key on register
|
|
46
|
-
* @return {Provider}
|
|
47
31
|
*/
|
|
48
32
|
const providerFactory = (options = {}) => {
|
|
49
33
|
const { throwOnDuplicates = true } = options;
|
package/lib/relations.js
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
const { isRelationalAttribute } = require('./content-types');
|
|
4
|
+
|
|
3
5
|
const MANY_RELATIONS = ['oneToMany', 'manyToMany'];
|
|
4
6
|
|
|
5
7
|
const getRelationalFields = (contentType) => {
|
|
@@ -8,8 +10,21 @@ const getRelationalFields = (contentType) => {
|
|
|
8
10
|
});
|
|
9
11
|
};
|
|
10
12
|
|
|
13
|
+
const isOneToAny = (attribute) =>
|
|
14
|
+
isRelationalAttribute(attribute) && ['oneToOne', 'oneToMany'].includes(attribute.relation);
|
|
15
|
+
const isManyToAny = (attribute) =>
|
|
16
|
+
isRelationalAttribute(attribute) && ['manyToMany', 'manyToOne'].includes(attribute.relation);
|
|
17
|
+
const isAnyToOne = (attribute) =>
|
|
18
|
+
isRelationalAttribute(attribute) && ['oneToOne', 'manyToOne'].includes(attribute.relation);
|
|
19
|
+
const isAnyToMany = (attribute) =>
|
|
20
|
+
isRelationalAttribute(attribute) && ['oneToMany', 'manyToMany'].includes(attribute.relation);
|
|
21
|
+
|
|
11
22
|
module.exports = {
|
|
12
23
|
getRelationalFields,
|
|
24
|
+
isOneToAny,
|
|
25
|
+
isManyToAny,
|
|
26
|
+
isAnyToOne,
|
|
27
|
+
isAnyToMany,
|
|
13
28
|
constants: {
|
|
14
29
|
MANY_RELATIONS,
|
|
15
30
|
},
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@strapi/utils",
|
|
3
|
-
"version": "4.5.0-
|
|
3
|
+
"version": "4.5.0-beta.0",
|
|
4
4
|
"description": "Shared utilities for the Strapi packages",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"strapi",
|
|
@@ -45,5 +45,5 @@
|
|
|
45
45
|
"node": ">=14.19.1 <=18.x.x",
|
|
46
46
|
"npm": ">=6.0.0"
|
|
47
47
|
},
|
|
48
|
-
"gitHead": "
|
|
48
|
+
"gitHead": "ee98b9a9cbb6e0e07e781ff9e87eb170c72e50df"
|
|
49
49
|
}
|