@akemona-org/strapi-generate-api 3.7.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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015-present Strapi Solutions SAS
2
+
3
+ Portions of the Strapi software are licensed as follows:
4
+
5
+ * All software that resides under an "ee/" directory (the “EE Software”), if that directory exists, is licensed under the license defined in "ee/LICENSE".
6
+
7
+ * All software outside of the above-mentioned directories or restrictions above is available under the "MIT Expat" license as set forth below.
8
+
9
+ MIT Expat License
10
+
11
+ Permission is hereby granted, free of charge, to any person obtaining a copy
12
+ of this software and associated documentation files (the "Software"), to deal
13
+ in the Software without restriction, including without limitation the rights
14
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15
+ copies of the Software, and to permit persons to whom the Software is
16
+ furnished to do so, subject to the following conditions:
17
+
18
+ The above copyright notice and this permission notice shall be included in all
19
+ copies or substantial portions of the Software.
20
+
21
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # strapi-generate-api
2
+
3
+ [![npm version](https://img.shields.io/npm/v/strapi-generate-api.svg)](https://www.npmjs.org/package/strapi-generate-api)
4
+ [![npm downloads](https://img.shields.io/npm/dm/strapi-generate-api.svg)](https://www.npmjs.org/package/strapi-generate-api)
5
+ [![npm dependencies](https://david-dm.org/strapi/strapi-generate-api.svg)](https://david-dm.org/strapi/strapi-generate-api)
6
+ [![Build status](https://travis-ci.org/strapi/strapi-generate-api.svg?branch=master)](https://travis-ci.org/strapi/strapi-generate-api)
7
+ [![Slack status](https://slack.strapi.io/badge.svg)](https://slack.strapi.io)
8
+
9
+ This Strapi generator contains all the default files for a new API.
10
+
11
+ This generator can be called with:
12
+
13
+ ```bash
14
+ $ strapi generate:api apiName
15
+ ```
16
+
17
+ ## Resources
18
+
19
+ - [License](LICENSE)
20
+
21
+ ## Links
22
+
23
+ - [Strapi website](https://strapi.akemona.com/)
24
+ - [Strapi community on Slack](https://slack.strapi.io)
25
+ - [Strapi news on Twitter](https://twitter.com/strapijs)
@@ -0,0 +1,116 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Module dependencies
5
+ */
6
+
7
+ // Node.js core.
8
+ const fs = require('fs');
9
+
10
+ // Public node modules.
11
+ const _ = require('lodash');
12
+
13
+ function generateSingleTypeRoutes({ route, name }) {
14
+ return [
15
+ {
16
+ method: 'GET',
17
+ path: '/' + route,
18
+ handler: name + '.find',
19
+ config: {
20
+ policies: [],
21
+ },
22
+ },
23
+ {
24
+ method: 'PUT',
25
+ path: '/' + route,
26
+ handler: name + '.update',
27
+ config: {
28
+ policies: [],
29
+ },
30
+ },
31
+ {
32
+ method: 'DELETE',
33
+ path: '/' + route,
34
+ handler: name + '.delete',
35
+ config: {
36
+ policies: [],
37
+ },
38
+ },
39
+ ];
40
+ }
41
+
42
+ function generateCollectionTypeRoutes({ route, name }) {
43
+ return [
44
+ {
45
+ method: 'GET',
46
+ path: '/' + route,
47
+ handler: name + '.find',
48
+ config: {
49
+ policies: [],
50
+ },
51
+ },
52
+ {
53
+ method: 'GET',
54
+ path: '/' + route + '/count',
55
+ handler: name + '.count',
56
+ config: {
57
+ policies: [],
58
+ },
59
+ },
60
+ {
61
+ method: 'GET',
62
+ path: '/' + route + '/:id',
63
+ handler: name + '.findOne',
64
+ config: {
65
+ policies: [],
66
+ },
67
+ },
68
+ {
69
+ method: 'POST',
70
+ path: '/' + route,
71
+ handler: name + '.create',
72
+ config: {
73
+ policies: [],
74
+ },
75
+ },
76
+ {
77
+ method: 'PUT',
78
+ path: '/' + route + '/:id',
79
+ handler: name + '.update',
80
+ config: {
81
+ policies: [],
82
+ },
83
+ },
84
+ {
85
+ method: 'DELETE',
86
+ path: '/' + route + '/:id',
87
+ handler: name + '.delete',
88
+ config: {
89
+ policies: [],
90
+ },
91
+ },
92
+ ];
93
+ }
94
+
95
+ /**
96
+ * Expose main routes of the generated API
97
+ */
98
+
99
+ module.exports = scope => {
100
+ let routes = [];
101
+ if (!scope.args.plugin) {
102
+ routes =
103
+ scope.contentTypeKind === 'singleType'
104
+ ? generateSingleTypeRoutes({ route: scope.route, name: scope.name })
105
+ : generateCollectionTypeRoutes({ route: scope.route, name: scope.name });
106
+ }
107
+
108
+ // if routes.json already exists, then merge
109
+ if (fs.existsSync(scope.rootPath)) {
110
+ let current = require(scope.rootPath);
111
+ fs.unlinkSync(scope.rootPath);
112
+ routes = _.concat(routes, _.differenceWith(current.routes, routes, _.isEqual));
113
+ }
114
+
115
+ return { routes };
116
+ };
package/lib/before.js ADDED
@@ -0,0 +1,144 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Module dependencies
5
+ */
6
+
7
+ // Public node modules.
8
+ const _ = require('lodash');
9
+ const pluralize = require('pluralize');
10
+ const { nameToSlug } = require('@akemona-org/strapi-utils');
11
+
12
+ /**
13
+ * This `before` function is run before generating targets.
14
+ * Validate, configure defaults, get extra dependencies, etc.
15
+ *
16
+ * @param {Object} scope
17
+ * @param {Function} cb
18
+ */
19
+
20
+ module.exports = (scope, cb) => {
21
+ if (!scope.rootPath || !scope.id) {
22
+ return cb.invalid('Usage: `$ strapi generate:api apiName`');
23
+ }
24
+
25
+ // Format `id`.
26
+ const name = scope.name || nameToSlug(scope.id);
27
+
28
+ scope.contentTypeKind = scope.args.kind || 'collectionType';
29
+
30
+ // `scope.args` are the raw command line arguments.
31
+ _.defaults(scope, {
32
+ name,
33
+ route:
34
+ scope.contentTypeKind === 'singleType'
35
+ ? _.kebabCase(scope.id)
36
+ : _.kebabCase(pluralize(scope.id)),
37
+ });
38
+
39
+ let filePath;
40
+ if (scope.args.api) {
41
+ filePath = `./api/${scope.args.api}`;
42
+ } else if (scope.args.plugin) {
43
+ filePath = `./plugins/${scope.args.plugin}`;
44
+ } else if (scope.args.extend) {
45
+ filePath = `./extensions/${scope.args.extend}`;
46
+ } else {
47
+ filePath = `./api/${name}`;
48
+ }
49
+
50
+ // Take another pass to take advantage of the defaults absorbed in previous passes.
51
+ _.defaults(scope, {
52
+ filename: `${name}.js`,
53
+ filenameSettings: `${name}.settings.json`,
54
+ filePath,
55
+ });
56
+
57
+ // Validate optional attribute arguments.
58
+ const invalidAttributes = [];
59
+
60
+ if (_.isPlainObject(scope.args.attributes)) {
61
+ scope.attributes = scope.args.attributes;
62
+ } else {
63
+ // Map attributes and split them for CLI.
64
+ scope.attributes = scope.args.attributes.map((attribute) => {
65
+ if (_.isString(attribute)) {
66
+ const parts = attribute.split(':');
67
+
68
+ parts[1] = parts[1] || 'string';
69
+
70
+ // Handle invalid attributes.
71
+ if (!parts[1] || !parts[0]) {
72
+ invalidAttributes.push('Error: Invalid attribute notation `' + attribute + '`.');
73
+ return;
74
+ }
75
+
76
+ return {
77
+ name: _.trim(_.deburr(parts[0].toLowerCase())),
78
+ params: {
79
+ type: _.trim(_.deburr(parts[1].toLowerCase())),
80
+ },
81
+ };
82
+ } else {
83
+ return _.has(attribute, 'params.type') ? attribute : undefined;
84
+ }
85
+ });
86
+
87
+ scope.attributes = _.compact(scope.attributes);
88
+
89
+ // Handle invalid action arguments.
90
+ // Send back invalidActions.
91
+ if (invalidAttributes.length) {
92
+ return cb.invalid(invalidAttributes);
93
+ }
94
+
95
+ // Make sure there aren't duplicates.
96
+ if (
97
+ _(scope.attributes.map((attribute) => attribute.name))
98
+ .uniq()
99
+ .valueOf().length !== scope.attributes.length
100
+ ) {
101
+ return cb.invalid('Duplicate attributes not allowed!');
102
+ }
103
+
104
+ // Render some stringified code from the action template
105
+ // and make it available in our scope for use later on.
106
+ scope.attributes = scope.attributes.reduce((acc, attribute) => {
107
+ acc[attribute.name] = attribute.params;
108
+ return acc;
109
+ }, {});
110
+ }
111
+ // Set collectionName
112
+ scope.collectionName = _.has(scope.args, 'collectionName')
113
+ ? scope.args.collectionName
114
+ : _.snakeCase(pluralize(name));
115
+
116
+ // Set description
117
+ scope.description = _.has(scope.args, 'description') ? scope.args.description : '';
118
+
119
+ // Set connection
120
+ scope.connection = _.get(scope.args, 'connection', undefined);
121
+
122
+ scope.schema = JSON.stringify(
123
+ {
124
+ connection: scope.connection,
125
+ collectionName: scope.collectionName,
126
+ info: {
127
+ name: scope.args.displayName || scope.id,
128
+ description: scope.description,
129
+ },
130
+ options: {
131
+ draftAndPublish: scope.args.draftAndPublish === 'true',
132
+ increments: true,
133
+ timestamps: true,
134
+ comment: '',
135
+ },
136
+ attributes: scope.attributes,
137
+ },
138
+ null,
139
+ 2
140
+ );
141
+
142
+ // Trigger callback with no error to proceed.
143
+ return cb.success();
144
+ };
package/lib/index.js ADDED
@@ -0,0 +1,48 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Module dependencies
5
+ */
6
+
7
+ // Node.js core.
8
+ const path = require('path');
9
+
10
+ // Local dependencies.
11
+ const routesJSON = require('../json/routes.json.js');
12
+
13
+ /**
14
+ * Generate a core API
15
+ */
16
+
17
+ module.exports = {
18
+ templatesDirectory: path.resolve(__dirname, '..', 'templates'),
19
+ before: require('./before'),
20
+ targets: {
21
+ // Use the default `controller` file as a template for
22
+ // every generated controller.
23
+ ':filePath/controllers/:filename': {
24
+ template: 'controller.template',
25
+ },
26
+
27
+ // every generated controller.
28
+ ':filePath/services/:filename': {
29
+ template: 'service.template',
30
+ },
31
+
32
+ // Copy an empty JavaScript model where every functions will be.
33
+ ':filePath/models/:filename': {
34
+ template: 'model.template',
35
+ },
36
+
37
+ // Copy the generated JSON model for the connection,
38
+ // schema and attributes.
39
+ ':filePath/models/:filenameSettings': {
40
+ template: 'model.settings.template',
41
+ },
42
+
43
+ // Generate routes.
44
+ ':filePath/config/routes.json': {
45
+ jsonfile: routesJSON,
46
+ },
47
+ },
48
+ };
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@akemona-org/strapi-generate-api",
3
+ "publishConfig": {
4
+ "access": "public"
5
+ },
6
+ "version": "3.7.0",
7
+ "description": "Generate an API for a Strapi application.",
8
+ "homepage": "https://strapi.akemona.com",
9
+ "keywords": [
10
+ "generate",
11
+ "generator",
12
+ "strapi"
13
+ ],
14
+ "main": "./lib/index.js",
15
+ "directories": {
16
+ "lib": "./lib"
17
+ },
18
+ "dependencies": {
19
+ "@akemona-org/strapi-utils": "3.7.0",
20
+ "lodash": "4.17.21",
21
+ "pluralize": "^8.0.0"
22
+ },
23
+ "scripts": {
24
+ "test": "echo \"no tests yet\""
25
+ },
26
+ "author": {
27
+ "email": "strapi@akemona.com",
28
+ "name": "Akemona team",
29
+ "url": "https://strapi.akemona.com"
30
+ },
31
+ "maintainers": [
32
+ {
33
+ "name": "Akemona team",
34
+ "email": "strapi@akemona.com",
35
+ "url": "https://strapi.akemona.com"
36
+ }
37
+ ],
38
+ "repository": {
39
+ "type": "git",
40
+ "url": "git://github.com/akemona/strapi.git"
41
+ },
42
+ "bugs": {
43
+ "url": "https://github.com/akemona/strapi/issues"
44
+ },
45
+ "engines": {
46
+ "node": ">=10.16.0 <=14.x.x",
47
+ "npm": ">=6.0.0"
48
+ },
49
+ "license": "SEE LICENSE IN LICENSE",
50
+ "gitHead": "129a8d6191b55810fd66448dcc47fee829df986c"
51
+ }
@@ -0,0 +1,8 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Read the documentation (https://strapi.akemona.com/documentation/developer-docs/latest/development/backend-customization.html#core-controllers)
5
+ * to customize this controller
6
+ */
7
+
8
+ module.exports = {};
@@ -0,0 +1 @@
1
+ <%= schema %>
@@ -0,0 +1,8 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Read the documentation (https://strapi.akemona.com/documentation/developer-docs/latest/development/backend-customization.html#lifecycle-hooks)
5
+ * to customize this model
6
+ */
7
+
8
+ module.exports = {};
@@ -0,0 +1,8 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Read the documentation (https://strapi.akemona.com/documentation/developer-docs/latest/development/backend-customization.html#core-services)
5
+ * to customize this service
6
+ */
7
+
8
+ module.exports = {};