@strapi/generators 4.0.0 → 4.0.4

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.
@@ -0,0 +1,40 @@
1
+ /**
2
+ * axios with a custom config.
3
+ */
4
+
5
+ import axios from 'axios';
6
+ import { auth } from '@strapi/helper-plugin';
7
+
8
+ const instance = axios.create({
9
+ baseURL: process.env.STRAPI_ADMIN_BACKEND_URL,
10
+ });
11
+
12
+ instance.interceptors.request.use(
13
+ async config => {
14
+ config.headers = {
15
+ Authorization: `Bearer ${auth.getToken()}`,
16
+ Accept: 'application/json',
17
+ 'Content-Type': 'application/json',
18
+ };
19
+
20
+ return config;
21
+ },
22
+ error => {
23
+ Promise.reject(error);
24
+ }
25
+ );
26
+
27
+ instance.interceptors.response.use(
28
+ response => response,
29
+ error => {
30
+ // whatever you want to do with the error
31
+ if (error.response?.status === 401) {
32
+ auth.clearAppStorage();
33
+ window.location.reload();
34
+ }
35
+
36
+ throw error;
37
+ }
38
+ );
39
+
40
+ export default instance;
@@ -0,0 +1,179 @@
1
+ 'use strict';
2
+
3
+ const path = require('path');
4
+ const { readFile, rm, stat } = require('fs-extra');
5
+ const strapiGenerators = require('../../index');
6
+
7
+ describe('Content Type Generator', () => {
8
+ const outputDirectory = path.join(__dirname, 'output');
9
+
10
+ afterEach(async () => {
11
+ await rm(outputDirectory, { recursive: true, force: true });
12
+ });
13
+
14
+ test('it generates the schema', async () => {
15
+ await strapiGenerators.generate(
16
+ 'content-type',
17
+ {
18
+ displayName: 'testContentType',
19
+ singularName: 'testContentType',
20
+ pluralName: 'testContentTypes',
21
+ kind: 'singleType',
22
+ id: 'testContentType',
23
+ useDraftAndPublish: false,
24
+ destination: 'new',
25
+ bootstrapApi: false,
26
+ attributes: [],
27
+ },
28
+ { dir: outputDirectory }
29
+ );
30
+
31
+ const generatedSchemaPath = path.join(
32
+ outputDirectory,
33
+ 'src/api/testContentType',
34
+ 'content-types/testContentType/schema.json'
35
+ );
36
+
37
+ expect((await stat(generatedSchemaPath)).isFile()).toBeTruthy();
38
+
39
+ const fileContent = await readFile(generatedSchemaPath, 'utf-8');
40
+
41
+ expect(fileContent).not.toBeNull();
42
+
43
+ const schema = JSON.parse(fileContent.toString());
44
+
45
+ expect(schema).toStrictEqual({
46
+ kind: 'singleType',
47
+ collectionName: 'test_content_types',
48
+ info: {
49
+ singularName: 'testContentType',
50
+ pluralName: 'testContentTypes',
51
+ displayName: 'testContentType',
52
+ },
53
+ options: {
54
+ draftAndPublish: false,
55
+ comment: '',
56
+ },
57
+ attributes: {},
58
+ });
59
+ });
60
+
61
+ test('it scaffolds a new API', async () => {
62
+ await strapiGenerators.generate(
63
+ 'content-type',
64
+ {
65
+ displayName: 'testContentType',
66
+ singularName: 'testContentType',
67
+ pluralName: 'testContentTypes',
68
+ kind: 'singleType',
69
+ id: 'testContentType',
70
+ useDraftAndPublish: false,
71
+ destination: 'new',
72
+ bootstrapApi: true,
73
+ attributes: [],
74
+ },
75
+ { dir: outputDirectory }
76
+ );
77
+ const generatedApiPath = path.join(outputDirectory, 'src/api/testContentType');
78
+
79
+ expect((await stat(generatedApiPath)).isDirectory()).toBeTruthy();
80
+ expect(
81
+ (await stat(path.join(generatedApiPath, 'controllers/testContentType.js'))).isFile()
82
+ ).toBeTruthy();
83
+ expect(
84
+ (await stat(path.join(generatedApiPath, 'services/testContentType.js'))).isFile()
85
+ ).toBeTruthy();
86
+ expect(
87
+ (await stat(path.join(generatedApiPath, 'routes/testContentType.js'))).isFile()
88
+ ).toBeTruthy();
89
+
90
+ const controller = await readFile(
91
+ path.join(generatedApiPath, 'controllers/testContentType.js')
92
+ );
93
+ const router = await readFile(path.join(generatedApiPath, 'routes/testContentType.js'));
94
+ const service = await readFile(path.join(generatedApiPath, 'services/testContentType.js'));
95
+
96
+ expect(controller.toString()).toMatchInlineSnapshot(`
97
+ "'use strict';
98
+
99
+ /**
100
+ * testContentType controller
101
+ */
102
+
103
+ const { createCoreController } = require('@strapi/strapi').factories;
104
+
105
+ module.exports = createCoreController('api::testContentType.testContentType');
106
+ "
107
+ `);
108
+ expect(router.toString()).toMatchInlineSnapshot(`
109
+ "'use strict';
110
+
111
+ /**
112
+ * testContentType router.
113
+ */
114
+
115
+ const { createCoreRouter } = require('@strapi/strapi').factories;
116
+
117
+ module.exports = createCoreRouter('api::testContentType.testContentType');
118
+ "
119
+ `);
120
+ expect(service.toString()).toMatchInlineSnapshot(`
121
+ "'use strict';
122
+
123
+ /**
124
+ * testContentType service.
125
+ */
126
+
127
+ const { createCoreService } = require('@strapi/strapi').factories;
128
+
129
+ module.exports = createCoreService('api::testContentType.testContentType');
130
+ "
131
+ `);
132
+ });
133
+
134
+ test('it generates the schema, then adds the attributes', async () => {
135
+ await strapiGenerators.generate(
136
+ 'content-type',
137
+ {
138
+ displayName: 'testContentType',
139
+ singularName: 'testContentType',
140
+ pluralName: 'testContentTypes',
141
+ kind: 'singleType',
142
+ id: 'testContentType',
143
+ useDraftAndPublish: false,
144
+ destination: 'new',
145
+ bootstrapApi: false,
146
+ attributes: [
147
+ {
148
+ attributeName: 'name',
149
+ attributeType: 'string',
150
+ },
151
+ {
152
+ attributeName: 'email',
153
+ attributeType: 'string',
154
+ },
155
+ ],
156
+ },
157
+ { dir: outputDirectory }
158
+ );
159
+
160
+ const generatedSchemaPath = path.join(
161
+ outputDirectory,
162
+ 'src/api/testContentType',
163
+ 'content-types/testContentType/schema.json'
164
+ );
165
+
166
+ expect((await stat(generatedSchemaPath)).isFile()).toBeTruthy();
167
+
168
+ const fileContent = await readFile(generatedSchemaPath, 'utf-8');
169
+
170
+ expect(fileContent).not.toBeNull();
171
+
172
+ const schema = JSON.parse(fileContent.toString());
173
+
174
+ expect(schema.attributes).toStrictEqual({
175
+ email: { type: 'string' },
176
+ name: { type: 'string' },
177
+ });
178
+ });
179
+ });
@@ -93,7 +93,7 @@ module.exports = plop => {
93
93
  },
94
94
  ];
95
95
 
96
- if (attributes.lenght > 0) {
96
+ if (Object.entries(attributes).length > 0) {
97
97
  baseActions.push({
98
98
  type: 'modify',
99
99
  path: `${filePath}/content-types/{{ singularName }}/schema.json`,
@@ -4,8 +4,7 @@
4
4
  * `{{id}}` policy.
5
5
  */
6
6
 
7
- module.exports = (config, { strapi }) => {
8
- return policyCtx => {
7
+ module.exports = (policyContext, config, { strapi }) => {
9
8
  // Add your own logic here.
10
9
  strapi.log.info('In {{id}} policy.');
11
10
 
@@ -16,5 +15,4 @@ module.exports = (config, { strapi }) => {
16
15
  }
17
16
 
18
17
  return false;
19
- };
20
18
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@strapi/generators",
3
- "version": "4.0.0",
3
+ "version": "4.0.4",
4
4
  "description": "Interactive API generator.",
5
5
  "keywords": [
6
6
  "strapi",
@@ -30,7 +30,7 @@
30
30
  "main": "lib/index.js",
31
31
  "dependencies": {
32
32
  "@sindresorhus/slugify": "1.1.0",
33
- "@strapi/utils": "4.0.0",
33
+ "@strapi/utils": "4.0.4",
34
34
  "chalk": "4.1.2",
35
35
  "fs-extra": "10.0.0",
36
36
  "node-plop": "0.26.3",
@@ -38,8 +38,8 @@
38
38
  "pluralize": "8.0.0"
39
39
  },
40
40
  "engines": {
41
- "node": ">=12.x.x <=16.x.x",
41
+ "node": ">=12.22.0 <=16.x.x",
42
42
  "npm": ">=6.0.0"
43
43
  },
44
- "gitHead": "b181702f0202b2c6d645d42b195a831f25cd0b03"
44
+ "gitHead": "d81919ac2272948b3f5d3b25a4cf8cc7b6994460"
45
45
  }
@@ -1,25 +0,0 @@
1
- /**
2
- *
3
- * This component is the skeleton around the actual pages, and should only
4
- * contain code that should be seen on all pages. (e.g. navigation bar)
5
- *
6
- */
7
-
8
- import React from 'react';
9
- import { Switch, Route } from 'react-router-dom';
10
- import { NotFound } from '@strapi/helper-plugin';
11
- import pluginId from '../../pluginId';
12
- import HomePage from '../HomePage';
13
-
14
- const App = () => {
15
- return (
16
- <div>
17
- <Switch>
18
- <Route path={`/plugins/${pluginId}`} component={HomePage} exact />
19
- <Route component={NotFound} />
20
- </Switch>
21
- </div>
22
- );
23
- };
24
-
25
- export default App;
@@ -1,20 +0,0 @@
1
- /*
2
- *
3
- * HomePage
4
- *
5
- */
6
-
7
- import React, { memo } from 'react';
8
- // import PropTypes from 'prop-types';
9
- import pluginId from '../../pluginId';
10
-
11
- const HomePage = () => {
12
- return (
13
- <div>
14
- <h1>{pluginId}&apos;s HomePage</h1>
15
- <p>Happy coding</p>
16
- </div>
17
- );
18
- };
19
-
20
- export default memo(HomePage);
@@ -1,26 +0,0 @@
1
- /**
2
- *
3
- * Initializer
4
- *
5
- */
6
-
7
- import { useEffect, useRef } from 'react';
8
- import PropTypes from 'prop-types';
9
- import pluginId from '../../pluginId';
10
-
11
- const Initializer = ({ setPlugin }) => {
12
- const ref = useRef();
13
- ref.current = setPlugin;
14
-
15
- useEffect(() => {
16
- ref.current(pluginId);
17
- }, []);
18
-
19
- return null;
20
- };
21
-
22
- Initializer.propTypes = {
23
- setPlugin: PropTypes.func.isRequired,
24
- };
25
-
26
- export default Initializer;