@oak-digital/types-4-strapi-2 0.2.0 → 0.2.3

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.
Files changed (34) hide show
  1. package/.editorconfig +9 -0
  2. package/.eslintrc.json +52 -0
  3. package/.prettierrc.json +7 -0
  4. package/README.md +67 -0
  5. package/lib/.prettierrc.json +7 -0
  6. package/lib/index.js +9 -4
  7. package/lib/interface/Attributes.js +4 -1
  8. package/lib/interface/Interface.d.ts +1 -1
  9. package/lib/interface/Interface.js +13 -15
  10. package/lib/interface/InterfaceManager.d.ts +3 -0
  11. package/lib/interface/InterfaceManager.js +47 -20
  12. package/lib/interface/builtinInterfaces.js +7 -0
  13. package/lib/interface/schemaReader.js +15 -11
  14. package/lib/src/index.d.ts +1 -0
  15. package/lib/src/index.js +25 -0
  16. package/lib/src/interface/Attributes.d.ts +11 -0
  17. package/lib/src/interface/Attributes.js +148 -0
  18. package/lib/src/interface/BuiltinComponentInterface.d.ts +5 -0
  19. package/lib/src/interface/BuiltinComponentInterface.js +36 -0
  20. package/lib/src/interface/BuiltinInterface.d.ts +5 -0
  21. package/lib/src/interface/BuiltinInterface.js +33 -0
  22. package/lib/src/interface/ComponentInterface.d.ts +8 -0
  23. package/lib/src/interface/ComponentInterface.js +58 -0
  24. package/lib/src/interface/Interface.d.ts +31 -0
  25. package/lib/src/interface/Interface.js +112 -0
  26. package/lib/src/interface/InterfaceManager.d.ts +25 -0
  27. package/lib/src/interface/InterfaceManager.js +288 -0
  28. package/lib/src/interface/builtinInterfaces.d.ts +4 -0
  29. package/lib/src/interface/builtinInterfaces.js +81 -0
  30. package/lib/src/interface/schemaReader.d.ts +14 -0
  31. package/lib/src/interface/schemaReader.js +172 -0
  32. package/lib/src/utils/index.d.ts +3 -0
  33. package/lib/src/utils/index.js +67 -0
  34. package/package.json +8 -2
@@ -0,0 +1,148 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ var Attributes = /** @class */ (function () {
4
+ function Attributes(attr, relationNames) {
5
+ this.RelationNames = {};
6
+ this.Attrs = attr;
7
+ this.RelationNames = relationNames;
8
+ }
9
+ Attributes.prototype.isAttributeOptional = function (attr) {
10
+ // If it is a component / relation / dynamiczone it is always optional due to population
11
+ switch (attr.type) {
12
+ case 'nested':
13
+ return attr.nullable === true;
14
+ case 'component':
15
+ case 'dynamiczone':
16
+ case 'relation':
17
+ return true;
18
+ default:
19
+ break;
20
+ }
21
+ return false;
22
+ };
23
+ Attributes.prototype.getDependencies = function () {
24
+ var dependencies = [];
25
+ for (var attrName in this.Attrs) {
26
+ var attr = this.Attrs[attrName];
27
+ var dependencyNames = [];
28
+ switch (attr.type) {
29
+ case 'nested':
30
+ var attrs = new Attributes(attr.fields, this.RelationNames);
31
+ dependencyNames.push.apply(dependencyNames, attrs.getDependencies());
32
+ break;
33
+ case 'relation':
34
+ dependencyNames.push(attr.target);
35
+ break;
36
+ case 'component':
37
+ dependencyNames.push(attr.component);
38
+ break;
39
+ case 'media':
40
+ dependencyNames.push('builtins::Media');
41
+ break;
42
+ default:
43
+ continue;
44
+ }
45
+ // // If the current dependency is the interface itself, do not report it as a dependency
46
+ // if (dependencyName === strapiName) {
47
+ // continue;
48
+ // }
49
+ dependencyNames.forEach(function (dependencyName) {
50
+ if (!dependencies.includes(dependencyName)) {
51
+ dependencies.push(dependencyName);
52
+ }
53
+ });
54
+ }
55
+ return dependencies;
56
+ };
57
+ Attributes.prototype.attributeToString = function (attrName, attr) {
58
+ var _a, _b;
59
+ var optionalString = this.isAttributeOptional(attr) ? '?' : '';
60
+ var str = " ".concat(attrName).concat(optionalString, ": ");
61
+ var isArray = false;
62
+ switch (attr.type) {
63
+ // types-4-strapi-2 specific, used for builtin types
64
+ case 'nested':
65
+ // Be careful with recursion
66
+ // console.log(attr);
67
+ var newAttrs = new Attributes(attr.fields, this.RelationNames);
68
+ str += newAttrs.toString();
69
+ break;
70
+ case 'relation':
71
+ var apiName = attr.target;
72
+ // console.log(this.RelationNames, apiName)
73
+ var dependencyName = this.RelationNames[apiName].name;
74
+ isArray = attr.relation.endsWith('ToMany');
75
+ str += dependencyName;
76
+ break;
77
+ case 'component':
78
+ var componentName = attr.component;
79
+ var relationNameObj = this.RelationNames[componentName];
80
+ var dependencyComponentName = relationNameObj.name;
81
+ isArray = (_a = attr.repeatable) !== null && _a !== void 0 ? _a : false;
82
+ str += dependencyComponentName;
83
+ break;
84
+ case 'media':
85
+ var mediaOptional = attr.required !== true ? '?' : '';
86
+ str += "{ data".concat(mediaOptional, ": ").concat(this.RelationNames['builtins::Media'].name, "; }");
87
+ isArray = (_b = attr.multiple) !== null && _b !== void 0 ? _b : false;
88
+ break;
89
+ case 'password':
90
+ return null;
91
+ case 'enumeration':
92
+ var hasDefault = 'default' in attr;
93
+ var enums = attr.enum.map(function (en) { return "\"".concat(en, "\""); });
94
+ enums.push('null');
95
+ var typeString = enums.join(' | ');
96
+ str += typeString;
97
+ break;
98
+ case 'string':
99
+ case 'text':
100
+ case 'richtext':
101
+ case 'email':
102
+ case 'uid':
103
+ str += 'string';
104
+ break;
105
+ case 'integer':
106
+ case 'biginteger':
107
+ case 'decimal':
108
+ case 'float':
109
+ str += 'number';
110
+ break;
111
+ case 'date':
112
+ case 'datetime':
113
+ case 'time':
114
+ str += 'Date';
115
+ break;
116
+ case 'boolean':
117
+ str += attr.type;
118
+ break;
119
+ case 'json':
120
+ default:
121
+ str += 'any';
122
+ break;
123
+ }
124
+ var isArrayString = isArray ? '[]' : '';
125
+ str += "".concat(isArrayString, ";");
126
+ return str;
127
+ };
128
+ Attributes.prototype.toFieldsString = function () {
129
+ var strings = [];
130
+ for (var attrName in this.Attrs) {
131
+ var attr = this.Attrs[attrName];
132
+ var attrString = this.attributeToString(attrName, attr);
133
+ if (attrString === null) {
134
+ continue;
135
+ }
136
+ strings.push(attrString);
137
+ }
138
+ return strings.map(function (s) { return "".concat(s, "\n"); }).join('');
139
+ };
140
+ Attributes.prototype.toString = function () {
141
+ var strings = ['{'];
142
+ strings.push(this.toFieldsString());
143
+ strings.push('}');
144
+ return strings.join('\n');
145
+ };
146
+ return Attributes;
147
+ }());
148
+ exports.default = Attributes;
@@ -0,0 +1,5 @@
1
+ import ComponentInterface from './ComponentInterface';
2
+ export default class BuiltinComponentInterface extends ComponentInterface {
3
+ constructor(baseName: string, attributes: any, relativeDirectoryPath: string, prefix?: string);
4
+ updateStrapiName(): void;
5
+ }
@@ -0,0 +1,36 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
18
+ return (mod && mod.__esModule) ? mod : { "default": mod };
19
+ };
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ var ComponentInterface_1 = __importDefault(require("./ComponentInterface"));
22
+ var BuiltinComponentInterface = /** @class */ (function (_super) {
23
+ __extends(BuiltinComponentInterface, _super);
24
+ function BuiltinComponentInterface(baseName, attributes, relativeDirectoryPath, prefix) {
25
+ if (prefix === void 0) { prefix = ''; }
26
+ return _super.call(this, baseName, attributes, relativeDirectoryPath, 'builtins', prefix, {
27
+ hasId: false,
28
+ hasComponent: false,
29
+ }) || this;
30
+ }
31
+ BuiltinComponentInterface.prototype.updateStrapiName = function () {
32
+ this.StrapiName = "builtins::".concat(this.BaseName);
33
+ };
34
+ return BuiltinComponentInterface;
35
+ }(ComponentInterface_1.default));
36
+ exports.default = BuiltinComponentInterface;
@@ -0,0 +1,5 @@
1
+ import Interface from './Interface';
2
+ export default class BuiltinInterface extends Interface {
3
+ constructor(baseName: string, attributes: any, relativeDirectoryPath: string, prefix?: string);
4
+ updateStrapiName(): void;
5
+ }
@@ -0,0 +1,33 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
18
+ return (mod && mod.__esModule) ? mod : { "default": mod };
19
+ };
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ var Interface_1 = __importDefault(require("./Interface"));
22
+ var BuiltinInterface = /** @class */ (function (_super) {
23
+ __extends(BuiltinInterface, _super);
24
+ function BuiltinInterface(baseName, attributes, relativeDirectoryPath, prefix) {
25
+ if (prefix === void 0) { prefix = ''; }
26
+ return _super.call(this, baseName, attributes, relativeDirectoryPath, prefix) || this;
27
+ }
28
+ BuiltinInterface.prototype.updateStrapiName = function () {
29
+ this.StrapiName = "builtins::".concat(this.BaseName);
30
+ };
31
+ return BuiltinInterface;
32
+ }(Interface_1.default));
33
+ exports.default = BuiltinInterface;
@@ -0,0 +1,8 @@
1
+ import Interface from './Interface';
2
+ export default class ComponentInterface extends Interface {
3
+ protected Category: string;
4
+ protected Options: Record<string, any>;
5
+ constructor(baseName: string, attributes: any, relativeDirectoryPath: string, category: string, prefix?: string, options?: Record<string, any>);
6
+ updateStrapiName(): void;
7
+ getInterfaceFieldsString(): string;
8
+ }
@@ -0,0 +1,58 @@
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
+ var __importDefault = (this && this.__importDefault) || function (mod) {
18
+ return (mod && mod.__esModule) ? mod : { "default": mod };
19
+ };
20
+ Object.defineProperty(exports, "__esModule", { value: true });
21
+ var Interface_1 = __importDefault(require("./Interface"));
22
+ var ComponentInterface = /** @class */ (function (_super) {
23
+ __extends(ComponentInterface, _super);
24
+ function ComponentInterface(baseName, attributes, relativeDirectoryPath, category, prefix, options) {
25
+ if (prefix === void 0) { prefix = ''; }
26
+ if (options === void 0) { options = {}; }
27
+ var _this = _super.call(this, baseName, attributes, relativeDirectoryPath, prefix) || this;
28
+ _this.Options = {
29
+ hasId: true,
30
+ hasComponent: true,
31
+ };
32
+ _this.Category = category;
33
+ // this.Attributes.id = {
34
+ // type: "number", // Components have a id field with a number
35
+ // required: true,
36
+ // };
37
+ _this.updateStrapiName();
38
+ Object.assign(_this.Options, options);
39
+ return _this;
40
+ }
41
+ ComponentInterface.prototype.updateStrapiName = function () {
42
+ this.StrapiName = "".concat(this.Category, ".").concat(this.getBaseName());
43
+ };
44
+ ComponentInterface.prototype.getInterfaceFieldsString = function () {
45
+ var attrs = this.getAttributes();
46
+ var str = '';
47
+ var _a = this.Options, hasId = _a.hasId, hasComponent = _a.hasComponent;
48
+ if (hasId) {
49
+ str += ' id: number;\n';
50
+ }
51
+ if (hasComponent) {
52
+ str += " __component: \"".concat(this.getStrapiName(), "\";\n");
53
+ }
54
+ return str + attrs.toFieldsString();
55
+ };
56
+ return ComponentInterface;
57
+ }(Interface_1.default));
58
+ exports.default = ComponentInterface;
@@ -0,0 +1,31 @@
1
+ import Attributes from './Attributes';
2
+ export declare type RelationNames = Record<string, {
3
+ name: string;
4
+ inter: Interface;
5
+ }>;
6
+ export default class Interface {
7
+ protected BaseName: string;
8
+ private Relations;
9
+ private RelationNames;
10
+ private RelationNamesCounter;
11
+ private NamePrefix;
12
+ protected Attributes: any;
13
+ private RelativeDirectoryPath;
14
+ protected StrapiName: string;
15
+ constructor(baseName: string, attributes: any, relativeDirectoryPath: string, prefix?: string);
16
+ protected updateStrapiName(): void;
17
+ getBaseName(): string;
18
+ getStrapiName(): string;
19
+ getDependencies(): any[];
20
+ getFullInterfaceName(): string;
21
+ getRelativeRootPath(): any;
22
+ getRelativeRootDir(): string;
23
+ getRelativeRootPathFile(): string;
24
+ setRelations(relations: Interface[]): void;
25
+ private getTsImports;
26
+ getAttributes(): Attributes;
27
+ attributesToString(): string;
28
+ getInerfaceString(): string;
29
+ getInterfaceFieldsString(): string;
30
+ toString(): string;
31
+ }
@@ -0,0 +1,112 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ var path_1 = require("path");
7
+ var utils_1 = require("../utils");
8
+ var Attributes_1 = __importDefault(require("./Attributes"));
9
+ var Interface = /** @class */ (function () {
10
+ function Interface(baseName, attributes, relativeDirectoryPath, prefix) {
11
+ if (prefix === void 0) { prefix = ''; }
12
+ this.Relations = []; // Components and relations
13
+ this.RelationNames = {};
14
+ this.RelationNamesCounter = {};
15
+ this.NamePrefix = '';
16
+ this.BaseName = baseName;
17
+ this.updateStrapiName();
18
+ this.NamePrefix = prefix;
19
+ this.Attributes = attributes;
20
+ this.RelativeDirectoryPath = relativeDirectoryPath;
21
+ }
22
+ Interface.prototype.updateStrapiName = function () {
23
+ this.StrapiName = "api::".concat(this.BaseName, ".").concat(this.BaseName);
24
+ };
25
+ Interface.prototype.getBaseName = function () {
26
+ return this.BaseName;
27
+ };
28
+ Interface.prototype.getStrapiName = function () {
29
+ return this.StrapiName;
30
+ };
31
+ Interface.prototype.getDependencies = function () {
32
+ var attrs = new Attributes_1.default(this.Attributes, this.RelationNames);
33
+ return attrs.getDependencies();
34
+ };
35
+ Interface.prototype.getFullInterfaceName = function () {
36
+ var pascalName = (0, utils_1.pascalCase)(this.BaseName);
37
+ return "".concat(this.NamePrefix).concat(pascalName);
38
+ };
39
+ // For typescript import from index file
40
+ Interface.prototype.getRelativeRootPath = function () {
41
+ var path = (0, path_1.join)(this.RelativeDirectoryPath, this.getBaseName());
42
+ return (0, utils_1.prefixDotSlash)(path);
43
+ };
44
+ Interface.prototype.getRelativeRootDir = function () {
45
+ var path = (0, path_1.dirname)(this.getRelativeRootPathFile());
46
+ return path;
47
+ };
48
+ Interface.prototype.getRelativeRootPathFile = function () {
49
+ return "".concat(this.getRelativeRootPath(), ".ts");
50
+ };
51
+ Interface.prototype.setRelations = function (relations) {
52
+ var _this = this;
53
+ this.Relations = relations;
54
+ this.RelationNames = {};
55
+ this.Relations.forEach(function (inter) {
56
+ var name = inter.getFullInterfaceName();
57
+ // FIXME: clean up this mess...
58
+ if (inter.getStrapiName() !== _this.getStrapiName()) {
59
+ // Avoid duplicate names
60
+ if (name in _this.RelationNamesCounter) {
61
+ name += ++_this.RelationNamesCounter[name];
62
+ }
63
+ else {
64
+ _this.RelationNamesCounter[name] = 0;
65
+ }
66
+ }
67
+ _this.RelationNames[inter.getStrapiName()] = { name: name, inter: inter };
68
+ });
69
+ };
70
+ Interface.prototype.getTsImports = function () {
71
+ var _this = this;
72
+ return Object.keys(this.RelationNames).map(function (strapiName) {
73
+ if (strapiName === _this.getStrapiName()) {
74
+ return '';
75
+ }
76
+ var relationName = _this.RelationNames[strapiName].name;
77
+ var inter = _this.RelationNames[strapiName].inter;
78
+ var importPath = (0, utils_1.prefixDotSlash)((0, path_1.relative)(_this.getRelativeRootDir(), inter.getRelativeRootPath()));
79
+ var fullName = inter.getFullInterfaceName();
80
+ var importNameString = fullName === relationName ? fullName : "".concat(fullName, " as ").concat(relationName);
81
+ return "import { ".concat(importNameString, " } from '").concat(importPath, "';");
82
+ }).filter(function (s) { return s; }).join('\n');
83
+ };
84
+ Interface.prototype.getAttributes = function () {
85
+ return new Attributes_1.default(this.Attributes, this.RelationNames);
86
+ };
87
+ Interface.prototype.attributesToString = function () {
88
+ var attrs = this.getAttributes();
89
+ return attrs.toString();
90
+ };
91
+ Interface.prototype.getInerfaceString = function () {
92
+ var str = "export interface ".concat(this.getFullInterfaceName(), " {\n");
93
+ str += this.getInterfaceFieldsString();
94
+ str += '}';
95
+ return str;
96
+ };
97
+ Interface.prototype.getInterfaceFieldsString = function () {
98
+ var str = '';
99
+ str += 'id: number;\n';
100
+ str += "attributes: ".concat(this.attributesToString(), "\n");
101
+ return str;
102
+ };
103
+ Interface.prototype.toString = function () {
104
+ var strings = [
105
+ this.getTsImports(),
106
+ this.getInerfaceString()
107
+ ];
108
+ return strings.join('\n');
109
+ };
110
+ return Interface;
111
+ }());
112
+ exports.default = Interface;
@@ -0,0 +1,25 @@
1
+ export default class InterfaceManager {
2
+ private Interfaces;
3
+ private OutRoot;
4
+ private StrapiSrcRoot;
5
+ private Options;
6
+ private PrettierOptions;
7
+ static BaseOptions: {
8
+ prefix: string;
9
+ useCategoryPrefix: boolean;
10
+ componentPrefix: string;
11
+ componentPrefixOverridesPrefix: boolean;
12
+ builtinsPrefix: string;
13
+ builtinsPrefixOverridesPrefix: boolean;
14
+ prettierFile: any;
15
+ };
16
+ constructor(outRoot: string, strapiSrcRoot: string, options?: any);
17
+ loadPrettierConfig(): Promise<void>;
18
+ createInterfaces(): Promise<void>;
19
+ createBuiltinInterfaces(): void;
20
+ injectDependencies(): void;
21
+ makeFolders(): Promise<void>;
22
+ writeInterfaces(): Promise<void>;
23
+ writeIndexFile(): Promise<void>;
24
+ run(): Promise<void>;
25
+ }