strapi-plugin-populate-all 1.4.1 → 1.5.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/README.md CHANGED
@@ -51,4 +51,4 @@ module.exports = {
51
51
  - The plugin provides a global middleware that intercepts requests with `?populate=all` and rewrites the query to trigger recursive population.
52
52
  - In the background, it builds a standard Strapi populate query as described in the [Strapi documentation](https://docs.strapi.io/cms/api/rest/populate-select).
53
53
  - You can control which relations are included using the relations config option.
54
- - Inside the document API, you can set `populate: '*'` and `populateAll: true` to make it work
54
+ - Inside the document API, you can set `populate: '*'` and `_q: "populate-all"` to make it work ("populate-all" can be anywhere inside `_q`, we detect it via .includes)
@@ -1,4 +1,39 @@
1
1
  "use strict";
2
+ const PLUGIN_QUERY_DOCUMENT_TAG = "populate-all";
3
+ const config = {
4
+ default: {
5
+ relations: true
6
+ },
7
+ validator(config2 = {}) {
8
+ for (const [key, value] of Object.entries(config2)) {
9
+ switch (key) {
10
+ case "cache": {
11
+ const isBoolean = typeof value === "boolean";
12
+ if (!isBoolean) {
13
+ throw new Error(
14
+ `[populate-all] config "${key}" of type ${typeof value} is not valid. Supported is boolean.`
15
+ );
16
+ }
17
+ break;
18
+ }
19
+ case "relations": {
20
+ const isBoolean = typeof value === "boolean";
21
+ const isArrayOfStrings = Array.isArray(value) && value?.every((relation) => typeof relation === "string");
22
+ if (!(isBoolean || isArrayOfStrings)) {
23
+ throw new Error(
24
+ `[populate-all] config "${key}" of type ${typeof value} is not valid. Supported are boolean or Array of strings.`
25
+ );
26
+ }
27
+ break;
28
+ }
29
+ default:
30
+ strapi.log.warn(
31
+ `[populate-all] unknown config "${key}" provided. This config will be ignored.`
32
+ );
33
+ }
34
+ }
35
+ }
36
+ };
2
37
  const queryCache = {};
3
38
  const getPopulateQuery = (modelUid, parentsModelUids = []) => {
4
39
  try {
@@ -86,7 +121,7 @@ const bootstrap = ({ strapi: strapi2 }) => {
86
121
  strapi2.db.lifecycles.subscribe((event) => {
87
122
  try {
88
123
  if (event.action === "beforeFindMany" || event.action === "beforeFindOne") {
89
- if (strapi2.requestContext.get()?.query?.populateAll) {
124
+ if (event.params._q?.includes(PLUGIN_QUERY_DOCUMENT_TAG)) {
90
125
  strapi2.log.debug(
91
126
  `[populate-all] recursively populate ${event.model.uid}`
92
127
  );
@@ -94,6 +129,12 @@ const bootstrap = ({ strapi: strapi2 }) => {
94
129
  if (populateQuery?.populate) {
95
130
  event.params.populate = populateQuery.populate;
96
131
  }
132
+ const query = event.params._q.replace(PLUGIN_QUERY_DOCUMENT_TAG, "");
133
+ if (query.length > 0) {
134
+ event.params._q = query;
135
+ } else {
136
+ delete event.params._q;
137
+ }
97
138
  }
98
139
  }
99
140
  } catch (error) {
@@ -103,40 +144,6 @@ const bootstrap = ({ strapi: strapi2 }) => {
103
144
  }
104
145
  });
105
146
  };
106
- const config = {
107
- default: {
108
- relations: true
109
- },
110
- validator(config2 = {}) {
111
- for (const [key, value] of Object.entries(config2)) {
112
- switch (key) {
113
- case "cache": {
114
- const isBoolean = typeof value === "boolean";
115
- if (!isBoolean) {
116
- throw new Error(
117
- `[populate-all] config "${key}" of type ${typeof value} is not valid. Supported is boolean.`
118
- );
119
- }
120
- break;
121
- }
122
- case "relations": {
123
- const isBoolean = typeof value === "boolean";
124
- const isArrayOfStrings = Array.isArray(value) && value?.every((relation) => typeof relation === "string");
125
- if (!(isBoolean || isArrayOfStrings)) {
126
- throw new Error(
127
- `[populate-all] config "${key}" of type ${typeof value} is not valid. Supported are boolean or Array of strings.`
128
- );
129
- }
130
- break;
131
- }
132
- default:
133
- strapi.log.warn(
134
- `[populate-all] unknown config "${key}" provided. This config will be ignored.`
135
- );
136
- }
137
- }
138
- }
139
- };
140
147
  const middlewares = {
141
148
  /**
142
149
  * This is a global middleware to add support for the custom query param `?populate=all`.
@@ -145,9 +152,12 @@ const middlewares = {
145
152
  * The bootstrap script later picks up `?populateAll=true` to apply the desired populate logic.
146
153
  */
147
154
  populateAll: async (ctx, next) => {
155
+ if (ctx.query.populateAll && ctx.query.populateAll !== "false" || ctx.query.populateAll === null) {
156
+ ctx.query._q = [ctx.query._q || "", PLUGIN_QUERY_DOCUMENT_TAG].join("");
157
+ }
148
158
  if (ctx.query.populate === "all") {
149
159
  ctx.query.populate = void 0;
150
- ctx.query.populateAll = true;
160
+ ctx.query._q = [ctx.query._q || "", PLUGIN_QUERY_DOCUMENT_TAG].join("");
151
161
  }
152
162
  await next();
153
163
  }
@@ -1,3 +1,38 @@
1
+ const PLUGIN_QUERY_DOCUMENT_TAG = "populate-all";
2
+ const config = {
3
+ default: {
4
+ relations: true
5
+ },
6
+ validator(config2 = {}) {
7
+ for (const [key, value] of Object.entries(config2)) {
8
+ switch (key) {
9
+ case "cache": {
10
+ const isBoolean = typeof value === "boolean";
11
+ if (!isBoolean) {
12
+ throw new Error(
13
+ `[populate-all] config "${key}" of type ${typeof value} is not valid. Supported is boolean.`
14
+ );
15
+ }
16
+ break;
17
+ }
18
+ case "relations": {
19
+ const isBoolean = typeof value === "boolean";
20
+ const isArrayOfStrings = Array.isArray(value) && value?.every((relation) => typeof relation === "string");
21
+ if (!(isBoolean || isArrayOfStrings)) {
22
+ throw new Error(
23
+ `[populate-all] config "${key}" of type ${typeof value} is not valid. Supported are boolean or Array of strings.`
24
+ );
25
+ }
26
+ break;
27
+ }
28
+ default:
29
+ strapi.log.warn(
30
+ `[populate-all] unknown config "${key}" provided. This config will be ignored.`
31
+ );
32
+ }
33
+ }
34
+ }
35
+ };
1
36
  const queryCache = {};
2
37
  const getPopulateQuery = (modelUid, parentsModelUids = []) => {
3
38
  try {
@@ -85,7 +120,7 @@ const bootstrap = ({ strapi: strapi2 }) => {
85
120
  strapi2.db.lifecycles.subscribe((event) => {
86
121
  try {
87
122
  if (event.action === "beforeFindMany" || event.action === "beforeFindOne") {
88
- if (strapi2.requestContext.get()?.query?.populateAll) {
123
+ if (event.params._q?.includes(PLUGIN_QUERY_DOCUMENT_TAG)) {
89
124
  strapi2.log.debug(
90
125
  `[populate-all] recursively populate ${event.model.uid}`
91
126
  );
@@ -93,6 +128,12 @@ const bootstrap = ({ strapi: strapi2 }) => {
93
128
  if (populateQuery?.populate) {
94
129
  event.params.populate = populateQuery.populate;
95
130
  }
131
+ const query = event.params._q.replace(PLUGIN_QUERY_DOCUMENT_TAG, "");
132
+ if (query.length > 0) {
133
+ event.params._q = query;
134
+ } else {
135
+ delete event.params._q;
136
+ }
96
137
  }
97
138
  }
98
139
  } catch (error) {
@@ -102,40 +143,6 @@ const bootstrap = ({ strapi: strapi2 }) => {
102
143
  }
103
144
  });
104
145
  };
105
- const config = {
106
- default: {
107
- relations: true
108
- },
109
- validator(config2 = {}) {
110
- for (const [key, value] of Object.entries(config2)) {
111
- switch (key) {
112
- case "cache": {
113
- const isBoolean = typeof value === "boolean";
114
- if (!isBoolean) {
115
- throw new Error(
116
- `[populate-all] config "${key}" of type ${typeof value} is not valid. Supported is boolean.`
117
- );
118
- }
119
- break;
120
- }
121
- case "relations": {
122
- const isBoolean = typeof value === "boolean";
123
- const isArrayOfStrings = Array.isArray(value) && value?.every((relation) => typeof relation === "string");
124
- if (!(isBoolean || isArrayOfStrings)) {
125
- throw new Error(
126
- `[populate-all] config "${key}" of type ${typeof value} is not valid. Supported are boolean or Array of strings.`
127
- );
128
- }
129
- break;
130
- }
131
- default:
132
- strapi.log.warn(
133
- `[populate-all] unknown config "${key}" provided. This config will be ignored.`
134
- );
135
- }
136
- }
137
- }
138
- };
139
146
  const middlewares = {
140
147
  /**
141
148
  * This is a global middleware to add support for the custom query param `?populate=all`.
@@ -144,9 +151,12 @@ const middlewares = {
144
151
  * The bootstrap script later picks up `?populateAll=true` to apply the desired populate logic.
145
152
  */
146
153
  populateAll: async (ctx, next) => {
154
+ if (ctx.query.populateAll && ctx.query.populateAll !== "false" || ctx.query.populateAll === null) {
155
+ ctx.query._q = [ctx.query._q || "", PLUGIN_QUERY_DOCUMENT_TAG].join("");
156
+ }
147
157
  if (ctx.query.populate === "all") {
148
158
  ctx.query.populate = void 0;
149
- ctx.query.populateAll = true;
159
+ ctx.query._q = [ctx.query._q || "", PLUGIN_QUERY_DOCUMENT_TAG].join("");
150
160
  }
151
161
  await next();
152
162
  }
@@ -1,3 +1,4 @@
1
+ export declare const PLUGIN_QUERY_DOCUMENT_TAG = "populate-all";
1
2
  declare const _default: {
2
3
  default: {
3
4
  relations: boolean;
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.4.1",
2
+ "version": "1.5.0",
3
3
  "name": "strapi-plugin-populate-all",
4
4
  "description": "A lightweight plugin to recursively populate nested data in RESTful API requests",
5
5
  "keywords": [],