@oak-digital/types-4-strapi-2 0.3.5 → 0.4.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/.eslintrc.json +0 -4
- package/.prettierrc.json +1 -0
- package/README.md +41 -1
- package/lib/attributes/Attributes.d.ts +13 -0
- package/lib/attributes/Attributes.js +219 -0
- package/lib/constants.d.ts +1 -0
- package/lib/constants.js +4 -0
- package/lib/content-types/reader.d.ts +14 -0
- package/lib/content-types/reader.js +177 -0
- package/lib/extra-types/ExtraType.d.ts +11 -0
- package/lib/extra-types/ExtraType.js +47 -0
- package/lib/extra-types/createExtraTypes.d.ts +2 -0
- package/lib/extra-types/createExtraTypes.js +21 -0
- package/lib/file/File.d.ts +26 -0
- package/lib/file/File.js +72 -0
- package/lib/index.js +1 -1
- package/lib/interface/BuiltinComponentInterface.d.ts +1 -1
- package/lib/interface/BuiltinInterface.d.ts +1 -1
- package/lib/interface/ComponentInterface.d.ts +2 -1
- package/lib/interface/ComponentInterface.js +33 -0
- package/lib/interface/Interface.d.ts +7 -23
- package/lib/interface/Interface.js +60 -80
- package/lib/interface/InterfaceManager.d.ts +2 -2
- package/lib/interface/InterfaceManager.js +19 -1
- package/lib/interface/builtinInterfaces.d.ts +1 -1
- package/lib/plugins/PluginManager.d.ts +20 -0
- package/lib/plugins/PluginManager.js +41 -0
- package/lib/plugins/index.d.ts +3 -3
- package/lib/plugins/index.js +4 -4
- package/lib/plugins/url-alias/index.d.ts +2 -2
- package/lib/plugins/url-alias/index.js +11 -3
- package/lib/program/InterfaceManager.d.ts +39 -0
- package/lib/program/InterfaceManager.js +417 -0
- package/lib/utils/casing/index.d.ts +4 -0
- package/lib/utils/casing/index.js +47 -0
- package/package.json +1 -1
package/.eslintrc.json
CHANGED
package/.prettierrc.json
CHANGED
package/README.md
CHANGED
|
@@ -49,11 +49,12 @@ This can be done with the `--out` flag like in the following example.
|
|
|
49
49
|
* Generate TypeScript interfaces for builtin types such as `Media` and `MediaFormat`
|
|
50
50
|
* Select input and output directory
|
|
51
51
|
* Prettier formatting and ability to use your own `.prettierrc`.
|
|
52
|
+
* Generate types for plugins such as [url-alias](https://github.com/strapi-community/strapi-plugin-url-alias)
|
|
53
|
+
* Population by generics
|
|
52
54
|
|
|
53
55
|
### Planned features
|
|
54
56
|
|
|
55
57
|
* Support for localization
|
|
56
|
-
* Support if you are using other plugins, such as `url-alias`, which should add extra fields for some interfaces.
|
|
57
58
|
|
|
58
59
|
## Flags
|
|
59
60
|
|
|
@@ -65,6 +66,45 @@ This can be done with the `--out` flag like in the following example.
|
|
|
65
66
|
| --component-prefix <prefix> | A prefix for components | none |
|
|
66
67
|
| -D, --delete-old | CAUTION: This option is equivalent to running `rm -rf` on the output directory first | `false` |
|
|
67
68
|
| --prettier <file> | The prettier config file to use for formatting TypeScript interfaces | none |
|
|
69
|
+
| --plugins <plugins...> | The plugins to use | none |
|
|
70
|
+
|
|
71
|
+
## Using plugins
|
|
72
|
+
|
|
73
|
+
It is possible to generate types for plugins, for example url-alias gives a new field on your content types, so that plugin will automatically add that field to your types.
|
|
74
|
+
You can see a list of builtin plugins below.
|
|
75
|
+
Some plugins might not be fully featured.
|
|
76
|
+
|
|
77
|
+
It will be possible in the future to add your own plugins in later versions.
|
|
78
|
+
|
|
79
|
+
### List of supported plugins
|
|
80
|
+
|
|
81
|
+
* url-alias
|
|
82
|
+
|
|
83
|
+
### example of using plugins
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
$ t4s --plugins url-alias
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Population
|
|
90
|
+
|
|
91
|
+
If your content types contains relations, dynamic zones or media, the fields can be set to required in the same way as you would populate them in the strapi api.
|
|
92
|
+
|
|
93
|
+
Here is an example if you have a content type name page, with a relation to a related page that you want the title and date of.
|
|
94
|
+
|
|
95
|
+
```typescript
|
|
96
|
+
type PageWithRelated = Page<'related_page.title' | 'related_page.date'>
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
This will make the title and date field on the new type required, so that you do not need to make an extra if check.
|
|
100
|
+
NOTE: when doing this, you should also make sure that you are actually populating it the same way as in the api
|
|
101
|
+
|
|
102
|
+
Other example using an array
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
const populatedFields = ['related_page.title', 'related_page.date'] as const; // as const is important
|
|
106
|
+
type PageWithRelated = Page<typeof populatedFields[number]>;
|
|
107
|
+
```
|
|
68
108
|
|
|
69
109
|
## Tips and tricks
|
|
70
110
|
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { RelationNames } from '../file/File';
|
|
2
|
+
export default class Attributes {
|
|
3
|
+
Attrs: Record<string, Record<string, any>>;
|
|
4
|
+
private RelationNames;
|
|
5
|
+
constructor(attr: Record<string, Record<string, any>>, relationNames: RelationNames);
|
|
6
|
+
isAttributePopulatable(attr: any): boolean;
|
|
7
|
+
hasPopulatableAttributes(): boolean;
|
|
8
|
+
getPopulatableAttributes(): Set<string>;
|
|
9
|
+
getDependencies(): string[];
|
|
10
|
+
attributeToString(attrName: string, attr: any): string;
|
|
11
|
+
toFieldsString(): string;
|
|
12
|
+
toString(): string;
|
|
13
|
+
}
|
|
@@ -0,0 +1,219 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var constants_1 = require("../constants");
|
|
4
|
+
var Attributes = /** @class */ (function () {
|
|
5
|
+
function Attributes(attr, relationNames) {
|
|
6
|
+
this.RelationNames = {};
|
|
7
|
+
this.Attrs = attr;
|
|
8
|
+
this.RelationNames = relationNames;
|
|
9
|
+
}
|
|
10
|
+
Attributes.prototype.isAttributePopulatable = function (attr) {
|
|
11
|
+
// If it is a component / relation / dynamiczone it is always optional due to population
|
|
12
|
+
switch (attr.type) {
|
|
13
|
+
case 'nested':
|
|
14
|
+
// we need to check equality since it could be undefined
|
|
15
|
+
return attr.nullable === true;
|
|
16
|
+
case 'component':
|
|
17
|
+
case 'dynamiczone':
|
|
18
|
+
case 'relation':
|
|
19
|
+
return true;
|
|
20
|
+
default:
|
|
21
|
+
break;
|
|
22
|
+
}
|
|
23
|
+
return false;
|
|
24
|
+
};
|
|
25
|
+
Attributes.prototype.hasPopulatableAttributes = function () {
|
|
26
|
+
for (var attrName in this.Attrs) {
|
|
27
|
+
var attr = this.Attrs[attrName];
|
|
28
|
+
if (this.isAttributePopulatable(attr)) {
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return false;
|
|
33
|
+
};
|
|
34
|
+
Attributes.prototype.getPopulatableAttributes = function () {
|
|
35
|
+
var populatableAttributes = new Set();
|
|
36
|
+
for (var attrName in this.Attrs) {
|
|
37
|
+
var attr = this.Attrs[attrName];
|
|
38
|
+
if (this.isAttributePopulatable(attr)) {
|
|
39
|
+
populatableAttributes.add(attrName);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return populatableAttributes;
|
|
43
|
+
};
|
|
44
|
+
Attributes.prototype.getDependencies = function () {
|
|
45
|
+
var _a;
|
|
46
|
+
var dependencies = new Set();
|
|
47
|
+
for (var attrName in this.Attrs) {
|
|
48
|
+
var attr = this.Attrs[attrName];
|
|
49
|
+
switch (attr.type) {
|
|
50
|
+
case 'nested':
|
|
51
|
+
var attrs = new Attributes(attr.fields, this.RelationNames);
|
|
52
|
+
var deps = attrs.getDependencies();
|
|
53
|
+
deps.forEach(function (dep) { return dependencies.add(dep); });
|
|
54
|
+
break;
|
|
55
|
+
case 'relation':
|
|
56
|
+
dependencies.add(attr.target);
|
|
57
|
+
break;
|
|
58
|
+
case 'component':
|
|
59
|
+
dependencies.add(attr.component);
|
|
60
|
+
break;
|
|
61
|
+
case 'media':
|
|
62
|
+
dependencies.add('builtins::Media');
|
|
63
|
+
break;
|
|
64
|
+
case 'dynamiczone':
|
|
65
|
+
var componentDeps = (_a = attr.components) !== null && _a !== void 0 ? _a : [];
|
|
66
|
+
componentDeps.forEach(function (dep) { return dependencies.add(dep); });
|
|
67
|
+
break;
|
|
68
|
+
default:
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
// // If the current dependency is the interface itself, do not report it as a dependency
|
|
72
|
+
// if (dependencyName === strapiName) {
|
|
73
|
+
// continue;
|
|
74
|
+
// }
|
|
75
|
+
}
|
|
76
|
+
if (this.hasPopulatableAttributes()) {
|
|
77
|
+
dependencies.add('builtins::ExtractNested');
|
|
78
|
+
dependencies.add('builtins::ExtractFlat');
|
|
79
|
+
dependencies.add('builtins::RequiredBy');
|
|
80
|
+
}
|
|
81
|
+
return Array.from(dependencies);
|
|
82
|
+
};
|
|
83
|
+
Attributes.prototype.attributeToString = function (attrName, attr) {
|
|
84
|
+
var _this = this;
|
|
85
|
+
var _a, _b, _c, _d;
|
|
86
|
+
var isPopulatable = this.isAttributePopulatable(attr);
|
|
87
|
+
var isOptional = isPopulatable;
|
|
88
|
+
var optionalString = isOptional ? '?' : '';
|
|
89
|
+
var orNull = ' | null';
|
|
90
|
+
var requiredString = attr.required !== true ? orNull : '';
|
|
91
|
+
var str = " ".concat(attrName).concat(optionalString, ": ");
|
|
92
|
+
var isArray = false;
|
|
93
|
+
switch (attr.type) {
|
|
94
|
+
// types-4-strapi-2 specific, used for builtin types
|
|
95
|
+
case 'nested':
|
|
96
|
+
// Be careful with recursion
|
|
97
|
+
// console.log(attr);
|
|
98
|
+
var nullableString = ((_a = attr === null || attr === void 0 ? void 0 : attr.nullable) !== null && _a !== void 0 ? _a : false) ? ' | null' : '';
|
|
99
|
+
var newAttrs = new Attributes(attr.fields, this.RelationNames);
|
|
100
|
+
str += newAttrs.toString() + nullableString;
|
|
101
|
+
break;
|
|
102
|
+
case 'relation':
|
|
103
|
+
var apiName = attr.target;
|
|
104
|
+
// console.log(this.RelationNames, apiName)
|
|
105
|
+
var dependencyName = (_c = (_b = this.RelationNames[apiName]) === null || _b === void 0 ? void 0 : _b.name) !== null && _c !== void 0 ? _c : 'any';
|
|
106
|
+
var relationMultipleString = attr.relation.endsWith('ToMany')
|
|
107
|
+
? '[]'
|
|
108
|
+
: orNull;
|
|
109
|
+
str += "{ data: ";
|
|
110
|
+
str += dependencyName;
|
|
111
|
+
// TODO: assert correctly
|
|
112
|
+
var relationInterface = this.RelationNames[apiName].file;
|
|
113
|
+
if (relationInterface.hasPopulatableAttributes()) {
|
|
114
|
+
str += '<';
|
|
115
|
+
str += "".concat(this.RelationNames['builtins::ExtractNested'].name, "<").concat(constants_1.POPULATE_GENERIC_NAME, ", '").concat(attrName, "'>");
|
|
116
|
+
str += '>';
|
|
117
|
+
}
|
|
118
|
+
str += relationMultipleString;
|
|
119
|
+
str += "; }";
|
|
120
|
+
break;
|
|
121
|
+
case 'component':
|
|
122
|
+
var componentName = attr.component;
|
|
123
|
+
var relationNameObj = this.RelationNames[componentName];
|
|
124
|
+
/* console.log(this.RelationNames); */
|
|
125
|
+
var dependencyComponentName = relationNameObj.name;
|
|
126
|
+
isArray = (_d = attr.repeatable) !== null && _d !== void 0 ? _d : false;
|
|
127
|
+
str += dependencyComponentName;
|
|
128
|
+
break;
|
|
129
|
+
case 'media':
|
|
130
|
+
var mediaMultipleString = attr.multiple
|
|
131
|
+
? '[]'
|
|
132
|
+
: requiredString;
|
|
133
|
+
str += "{ data: ";
|
|
134
|
+
str += this.RelationNames['builtins::Media'].name;
|
|
135
|
+
str += mediaMultipleString;
|
|
136
|
+
str += "; }";
|
|
137
|
+
break;
|
|
138
|
+
case 'password':
|
|
139
|
+
return null;
|
|
140
|
+
case 'enumeration':
|
|
141
|
+
var hasDefault = 'default' in attr;
|
|
142
|
+
var enums = attr.enum.map(function (en) { return "\"".concat(en, "\""); });
|
|
143
|
+
enums.push('null');
|
|
144
|
+
var typeString = enums.join(' | ');
|
|
145
|
+
str += typeString;
|
|
146
|
+
break;
|
|
147
|
+
case 'dynamiczone':
|
|
148
|
+
// console.log(attr.components);
|
|
149
|
+
var relations = attr.components.map(function (componentName) {
|
|
150
|
+
var component = _this.RelationNames[componentName];
|
|
151
|
+
// in this context file should always be an interface
|
|
152
|
+
var file = component.file;
|
|
153
|
+
var populatable = file.hasPopulatableAttributes();
|
|
154
|
+
/* false; */
|
|
155
|
+
var populatableString = populatable
|
|
156
|
+
? "".concat(_this.RelationNames[componentName].name, "<").concat(_this.RelationNames['builtins::ExtractNested'].name, "<").concat(constants_1.POPULATE_GENERIC_NAME, ", '").concat(attrName, "'>>")
|
|
157
|
+
: _this.RelationNames[componentName].name;
|
|
158
|
+
return populatableString;
|
|
159
|
+
});
|
|
160
|
+
// console.log(relations);
|
|
161
|
+
var relationsString = relations.join(' | ');
|
|
162
|
+
// console.log(relationsString);
|
|
163
|
+
str += "Array<".concat(relationsString, ">");
|
|
164
|
+
break;
|
|
165
|
+
case 'string':
|
|
166
|
+
case 'text':
|
|
167
|
+
case 'richtext':
|
|
168
|
+
case 'email':
|
|
169
|
+
case 'uid':
|
|
170
|
+
str += 'string';
|
|
171
|
+
str += requiredString;
|
|
172
|
+
break;
|
|
173
|
+
case 'integer':
|
|
174
|
+
case 'biginteger':
|
|
175
|
+
case 'decimal':
|
|
176
|
+
case 'float':
|
|
177
|
+
str += 'number';
|
|
178
|
+
str += requiredString;
|
|
179
|
+
break;
|
|
180
|
+
case 'date':
|
|
181
|
+
case 'datetime':
|
|
182
|
+
case 'time':
|
|
183
|
+
str += 'string';
|
|
184
|
+
str += requiredString;
|
|
185
|
+
break;
|
|
186
|
+
case 'boolean':
|
|
187
|
+
str += attr.type;
|
|
188
|
+
str += requiredString;
|
|
189
|
+
break;
|
|
190
|
+
case 'json':
|
|
191
|
+
default:
|
|
192
|
+
str += 'any';
|
|
193
|
+
break;
|
|
194
|
+
}
|
|
195
|
+
var isArrayString = isArray ? '[]' : '';
|
|
196
|
+
str += "".concat(isArrayString, ";");
|
|
197
|
+
return str;
|
|
198
|
+
};
|
|
199
|
+
Attributes.prototype.toFieldsString = function () {
|
|
200
|
+
var strings = [];
|
|
201
|
+
for (var attrName in this.Attrs) {
|
|
202
|
+
var attr = this.Attrs[attrName];
|
|
203
|
+
var attrString = this.attributeToString(attrName, attr);
|
|
204
|
+
if (attrString === null) {
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
strings.push(attrString);
|
|
208
|
+
}
|
|
209
|
+
return strings.map(function (s) { return "".concat(s, "\n"); }).join('');
|
|
210
|
+
};
|
|
211
|
+
Attributes.prototype.toString = function () {
|
|
212
|
+
var strings = ['{'];
|
|
213
|
+
strings.push(this.toFieldsString());
|
|
214
|
+
strings.push('}');
|
|
215
|
+
return strings.join('\n');
|
|
216
|
+
};
|
|
217
|
+
return Attributes;
|
|
218
|
+
}());
|
|
219
|
+
exports.default = Attributes;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const POPULATE_GENERIC_NAME = "Populate";
|
package/lib/constants.js
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export declare function readSchema(schemaPath: string): Promise<any>;
|
|
2
|
+
export declare function getApiFolders(strapiSrcRoot: string): Promise<string[]>;
|
|
3
|
+
export declare function getComponentCategoryFolders(strapiSrcRoot: string): Promise<string[]>;
|
|
4
|
+
export declare function getComponentSchemas(strapiSrcRoot: string): Promise<{
|
|
5
|
+
category: string;
|
|
6
|
+
schemas: {
|
|
7
|
+
name: string;
|
|
8
|
+
attributes: any;
|
|
9
|
+
}[];
|
|
10
|
+
}[]>;
|
|
11
|
+
export declare function getApiSchemas(strapiSrcRoot: string): Promise<{
|
|
12
|
+
name: string;
|
|
13
|
+
attributes: any;
|
|
14
|
+
}[]>;
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
12
|
+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
13
|
+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
14
|
+
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
15
|
+
function step(op) {
|
|
16
|
+
if (f) throw new TypeError("Generator is already executing.");
|
|
17
|
+
while (_) try {
|
|
18
|
+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
19
|
+
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
20
|
+
switch (op[0]) {
|
|
21
|
+
case 0: case 1: t = op; break;
|
|
22
|
+
case 4: _.label++; return { value: op[1], done: false };
|
|
23
|
+
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
24
|
+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
25
|
+
default:
|
|
26
|
+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
27
|
+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
28
|
+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
29
|
+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
30
|
+
if (t[2]) _.ops.pop();
|
|
31
|
+
_.trys.pop(); continue;
|
|
32
|
+
}
|
|
33
|
+
op = body.call(thisArg, _);
|
|
34
|
+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
35
|
+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.getApiSchemas = exports.getComponentSchemas = exports.getComponentCategoryFolders = exports.getApiFolders = exports.readSchema = void 0;
|
|
40
|
+
var node_fs_1 = require("node:fs");
|
|
41
|
+
var promises_1 = require("node:fs/promises");
|
|
42
|
+
var posix_1 = require("node:path/posix");
|
|
43
|
+
var utils_1 = require("../utils");
|
|
44
|
+
function readSchema(schemaPath) {
|
|
45
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
46
|
+
var schemaData, e_1;
|
|
47
|
+
return __generator(this, function (_a) {
|
|
48
|
+
switch (_a.label) {
|
|
49
|
+
case 0:
|
|
50
|
+
_a.trys.push([0, 2, , 3]);
|
|
51
|
+
return [4 /*yield*/, (0, promises_1.readFile)(schemaPath)];
|
|
52
|
+
case 1:
|
|
53
|
+
schemaData = _a.sent();
|
|
54
|
+
return [2 /*return*/, JSON.parse(schemaData.toString()).attributes];
|
|
55
|
+
case 2:
|
|
56
|
+
e_1 = _a.sent();
|
|
57
|
+
return [2 /*return*/, null];
|
|
58
|
+
case 3: return [2 /*return*/];
|
|
59
|
+
}
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
exports.readSchema = readSchema;
|
|
64
|
+
function getApiFolders(strapiSrcRoot) {
|
|
65
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
66
|
+
var path, folders;
|
|
67
|
+
return __generator(this, function (_a) {
|
|
68
|
+
switch (_a.label) {
|
|
69
|
+
case 0:
|
|
70
|
+
path = (0, posix_1.join)(strapiSrcRoot, 'api');
|
|
71
|
+
return [4 /*yield*/, (0, utils_1.readDirFiltered)(path)];
|
|
72
|
+
case 1:
|
|
73
|
+
folders = _a.sent();
|
|
74
|
+
return [2 /*return*/, folders];
|
|
75
|
+
}
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
exports.getApiFolders = getApiFolders;
|
|
80
|
+
function getComponentCategoryFolders(strapiSrcRoot) {
|
|
81
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
82
|
+
var path, folders;
|
|
83
|
+
return __generator(this, function (_a) {
|
|
84
|
+
switch (_a.label) {
|
|
85
|
+
case 0:
|
|
86
|
+
path = (0, posix_1.join)(strapiSrcRoot, 'components');
|
|
87
|
+
// If there exists no components, just fallback to an empty array.
|
|
88
|
+
if (!(0, node_fs_1.existsSync)(path)) {
|
|
89
|
+
return [2 /*return*/, []];
|
|
90
|
+
}
|
|
91
|
+
return [4 /*yield*/, (0, utils_1.readDirFiltered)(path)];
|
|
92
|
+
case 1:
|
|
93
|
+
folders = _a.sent();
|
|
94
|
+
return [2 /*return*/, folders];
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
exports.getComponentCategoryFolders = getComponentCategoryFolders;
|
|
100
|
+
function getComponentSchemas(strapiSrcRoot) {
|
|
101
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
102
|
+
var categories, nestedSchemasPromises, nestedSchemasArr;
|
|
103
|
+
var _this = this;
|
|
104
|
+
return __generator(this, function (_a) {
|
|
105
|
+
switch (_a.label) {
|
|
106
|
+
case 0: return [4 /*yield*/, getComponentCategoryFolders(strapiSrcRoot)];
|
|
107
|
+
case 1:
|
|
108
|
+
categories = _a.sent();
|
|
109
|
+
nestedSchemasPromises = categories.map(function (category) { return __awaiter(_this, void 0, void 0, function () {
|
|
110
|
+
var schemaFilesPath, schemaFiles, schemaNamesWithAttributesPromises, schemaNamesWithAttributes;
|
|
111
|
+
var _this = this;
|
|
112
|
+
return __generator(this, function (_a) {
|
|
113
|
+
switch (_a.label) {
|
|
114
|
+
case 0:
|
|
115
|
+
schemaFilesPath = (0, posix_1.join)(strapiSrcRoot, 'components', category);
|
|
116
|
+
return [4 /*yield*/, (0, utils_1.readDirFiltered)(schemaFilesPath)];
|
|
117
|
+
case 1:
|
|
118
|
+
schemaFiles = _a.sent();
|
|
119
|
+
schemaNamesWithAttributesPromises = schemaFiles.map(function (file) { return __awaiter(_this, void 0, void 0, function () {
|
|
120
|
+
var schemaPath, attributes, name;
|
|
121
|
+
return __generator(this, function (_a) {
|
|
122
|
+
switch (_a.label) {
|
|
123
|
+
case 0:
|
|
124
|
+
schemaPath = (0, posix_1.join)(schemaFilesPath, file);
|
|
125
|
+
return [4 /*yield*/, readSchema(schemaPath)];
|
|
126
|
+
case 1:
|
|
127
|
+
attributes = _a.sent();
|
|
128
|
+
name = file.split('.')[0];
|
|
129
|
+
return [2 /*return*/, { name: name, attributes: attributes }];
|
|
130
|
+
}
|
|
131
|
+
});
|
|
132
|
+
}); });
|
|
133
|
+
return [4 /*yield*/, Promise.all(schemaNamesWithAttributesPromises)];
|
|
134
|
+
case 2:
|
|
135
|
+
schemaNamesWithAttributes = _a.sent();
|
|
136
|
+
return [2 /*return*/, { category: category, schemas: schemaNamesWithAttributes }];
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
}); });
|
|
140
|
+
return [4 /*yield*/, Promise.all(nestedSchemasPromises)];
|
|
141
|
+
case 2:
|
|
142
|
+
nestedSchemasArr = _a.sent();
|
|
143
|
+
return [2 /*return*/, nestedSchemasArr];
|
|
144
|
+
}
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
exports.getComponentSchemas = getComponentSchemas;
|
|
149
|
+
function getApiSchemas(strapiSrcRoot) {
|
|
150
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
151
|
+
var apiFolders, schemasWithAttributesPromises, schemasWithAttributes;
|
|
152
|
+
var _this = this;
|
|
153
|
+
return __generator(this, function (_a) {
|
|
154
|
+
switch (_a.label) {
|
|
155
|
+
case 0: return [4 /*yield*/, getApiFolders(strapiSrcRoot)];
|
|
156
|
+
case 1:
|
|
157
|
+
apiFolders = _a.sent();
|
|
158
|
+
schemasWithAttributesPromises = apiFolders.map(function (folder) { return __awaiter(_this, void 0, void 0, function () {
|
|
159
|
+
var schemaPath, attributes;
|
|
160
|
+
return __generator(this, function (_a) {
|
|
161
|
+
switch (_a.label) {
|
|
162
|
+
case 0:
|
|
163
|
+
schemaPath = (0, posix_1.join)(strapiSrcRoot, 'api', folder, 'content-types', folder, 'schema.json');
|
|
164
|
+
return [4 /*yield*/, readSchema(schemaPath)];
|
|
165
|
+
case 1:
|
|
166
|
+
attributes = _a.sent();
|
|
167
|
+
return [2 /*return*/, { name: folder, attributes: attributes }];
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
}); });
|
|
171
|
+
schemasWithAttributes = Promise.all(schemasWithAttributesPromises);
|
|
172
|
+
return [2 /*return*/, schemasWithAttributes];
|
|
173
|
+
}
|
|
174
|
+
});
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
exports.getApiSchemas = getApiSchemas;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { File } from "../file/File";
|
|
2
|
+
import { caseType } from "../utils/casing";
|
|
3
|
+
export declare class ExtraType extends File {
|
|
4
|
+
protected typeString: string;
|
|
5
|
+
constructor(typeString: string, baseName: string, relativeDirectoryPath: string, fileCaseType?: caseType);
|
|
6
|
+
getDependencies(): any[];
|
|
7
|
+
getStrapiName(): string;
|
|
8
|
+
getFullName(): string;
|
|
9
|
+
protected updateStrapiName(): void;
|
|
10
|
+
toString(): string;
|
|
11
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __extends = (this && this.__extends) || (function () {
|
|
3
|
+
var extendStatics = function (d, b) {
|
|
4
|
+
extendStatics = Object.setPrototypeOf ||
|
|
5
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
+
return extendStatics(d, b);
|
|
8
|
+
};
|
|
9
|
+
return function (d, b) {
|
|
10
|
+
if (typeof b !== "function" && b !== null)
|
|
11
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
+
extendStatics(d, b);
|
|
13
|
+
function __() { this.constructor = d; }
|
|
14
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
+
};
|
|
16
|
+
})();
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
exports.ExtraType = void 0;
|
|
19
|
+
var File_1 = require("../file/File");
|
|
20
|
+
var ExtraType = /** @class */ (function (_super) {
|
|
21
|
+
__extends(ExtraType, _super);
|
|
22
|
+
function ExtraType(typeString, baseName, relativeDirectoryPath, fileCaseType) {
|
|
23
|
+
if (fileCaseType === void 0) { fileCaseType = 'pascal'; }
|
|
24
|
+
var _this = _super.call(this, baseName, relativeDirectoryPath, fileCaseType) || this;
|
|
25
|
+
_this.typeString = typeString;
|
|
26
|
+
_this.updateStrapiName();
|
|
27
|
+
return _this;
|
|
28
|
+
}
|
|
29
|
+
ExtraType.prototype.getDependencies = function () {
|
|
30
|
+
return [];
|
|
31
|
+
};
|
|
32
|
+
ExtraType.prototype.getStrapiName = function () {
|
|
33
|
+
return this.StrapiName;
|
|
34
|
+
};
|
|
35
|
+
ExtraType.prototype.getFullName = function () {
|
|
36
|
+
return this.BaseName;
|
|
37
|
+
};
|
|
38
|
+
ExtraType.prototype.updateStrapiName = function () {
|
|
39
|
+
this.StrapiName = "builtins::".concat(this.BaseName);
|
|
40
|
+
};
|
|
41
|
+
ExtraType.prototype.toString = function () {
|
|
42
|
+
// TODO: make this more dynamic
|
|
43
|
+
return "export ".concat(this.typeString);
|
|
44
|
+
};
|
|
45
|
+
return ExtraType;
|
|
46
|
+
}(File_1.File));
|
|
47
|
+
exports.ExtraType = ExtraType;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createExtraTypes = void 0;
|
|
4
|
+
var ExtraType_1 = require("./ExtraType");
|
|
5
|
+
/* const requiredByString = `type RequiredBy<T, K extends string> = Required<Pick<T, K>> & Omit<T, K>;`; */
|
|
6
|
+
var requiredByString = "type RequiredBy<T, K extends string> = Required<Pick<T, Extract<K, keyof T>>> & Omit<T, Extract<K, keyof T>>;";
|
|
7
|
+
var extractNestedString = 'type ExtractNested<T, K extends string> = T extends `${K}.${infer U}` ? U : never;';
|
|
8
|
+
var extractFlatString = 'type ExtractFlat<T extends string> = T extends `${infer U}.${string}` ? U : T;';
|
|
9
|
+
var extraTypeData = {
|
|
10
|
+
'RequiredBy': requiredByString,
|
|
11
|
+
'ExtractNested': extractNestedString,
|
|
12
|
+
'ExtractFlat': extractFlatString,
|
|
13
|
+
};
|
|
14
|
+
var createExtraTypes = function () {
|
|
15
|
+
return Object.entries(extraTypeData).map(function (_a) {
|
|
16
|
+
var name = _a[0], typeString = _a[1];
|
|
17
|
+
var extraType = new ExtraType_1.ExtraType(typeString, name, 'builtins');
|
|
18
|
+
return extraType;
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
exports.createExtraTypes = createExtraTypes;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { caseType } from "../utils/casing";
|
|
2
|
+
export declare type RelationNames = Record<string, {
|
|
3
|
+
name: string;
|
|
4
|
+
file: File;
|
|
5
|
+
}>;
|
|
6
|
+
export declare abstract class File {
|
|
7
|
+
private Relations;
|
|
8
|
+
protected RelationNames: RelationNames;
|
|
9
|
+
private RelationNamesCounter;
|
|
10
|
+
protected BaseName: string;
|
|
11
|
+
protected StrapiName: string;
|
|
12
|
+
private RelativeDirectoryPath;
|
|
13
|
+
protected FileCase: caseType;
|
|
14
|
+
constructor(baseName: string, relativeDirectoryPath: string, fileCaseType?: caseType);
|
|
15
|
+
abstract getDependencies(): string[];
|
|
16
|
+
abstract getStrapiName(): string;
|
|
17
|
+
abstract getFullName(): string;
|
|
18
|
+
abstract toString(): string;
|
|
19
|
+
setRelations(relations: File[]): void;
|
|
20
|
+
getRelativeRootPath(): any;
|
|
21
|
+
getBaseName(): string;
|
|
22
|
+
getRelativeRootPathFile(): string;
|
|
23
|
+
getRelativeRootDir(): string;
|
|
24
|
+
getFileBaseName(): string;
|
|
25
|
+
protected getTsImports(): string;
|
|
26
|
+
}
|