@oak-digital/types-4-strapi-2 0.1.2 → 0.2.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.
@@ -1,10 +1,10 @@
1
- import Interface from "./Interface";
1
+ import { RelationNames } from "./Interface";
2
2
  export default class Attributes {
3
3
  Attrs: Record<string, Record<string, any>>;
4
4
  private RelationNames;
5
- constructor(attr: Record<string, Record<string, any>>, relationNames: Record<string, [string, Interface]>);
5
+ constructor(attr: Record<string, Record<string, any>>, relationNames: RelationNames);
6
6
  isAttributeOptional(attr: any): boolean;
7
- getDependencies(strapiName: string): any[];
7
+ getDependencies(): any[];
8
8
  attributeToString(attrName: string, attr: any): string;
9
9
  toFieldsString(): string;
10
10
  toString(): string;
@@ -18,17 +18,24 @@ var Attributes = /** @class */ (function () {
18
18
  }
19
19
  return false;
20
20
  };
21
- Attributes.prototype.getDependencies = function (strapiName) {
21
+ Attributes.prototype.getDependencies = function () {
22
22
  var dependencies = [];
23
23
  for (var attrName in this.Attrs) {
24
24
  var attr = this.Attrs[attrName];
25
- var dependencyName = void 0;
25
+ var dependencyNames = [];
26
26
  switch (attr.type) {
27
+ case "nested":
28
+ var attrs = new Attributes(attr.fields, this.RelationNames);
29
+ dependencyNames.push.apply(dependencyNames, attrs.getDependencies());
30
+ break;
27
31
  case "relation":
28
- dependencyName = attr.target;
32
+ dependencyNames.push(attr.target);
29
33
  break;
30
34
  case "component":
31
- dependencyName = attr.component;
35
+ dependencyNames.push(attr.component);
36
+ break;
37
+ case "media":
38
+ dependencyNames.push("builtins::Media");
32
39
  break;
33
40
  default:
34
41
  continue;
@@ -37,30 +44,45 @@ var Attributes = /** @class */ (function () {
37
44
  // if (dependencyName === strapiName) {
38
45
  // continue;
39
46
  // }
40
- dependencies.push(dependencyName);
47
+ dependencyNames.forEach(function (dependencyName) {
48
+ if (!dependencies.includes(dependencyName)) {
49
+ dependencies.push(dependencyName);
50
+ }
51
+ });
41
52
  }
42
53
  return dependencies;
43
54
  };
44
55
  Attributes.prototype.attributeToString = function (attrName, attr) {
45
- var _a;
56
+ var _a, _b;
46
57
  var optionalString = this.isAttributeOptional(attr) ? '?' : '';
47
58
  var str = " ".concat(attrName).concat(optionalString, ": ");
48
59
  var isArray = false;
49
60
  switch (attr.type) {
61
+ // types-4-strapi-2 specific, used for builtin types
62
+ case "nested":
63
+ // Be careful with recursion
64
+ // console.log(attr);
65
+ var newAttrs = new Attributes(attr.fields, this.RelationNames);
66
+ str += newAttrs.toString();
67
+ break;
50
68
  case "relation":
51
69
  var apiName = attr.target;
52
- // console.log(attrName)
53
70
  // console.log(this.RelationNames, apiName)
54
- var dependencyName = this.RelationNames[apiName][0];
71
+ var dependencyName = this.RelationNames[apiName].name;
55
72
  isArray = attr.relation.endsWith("ToMany");
56
73
  str += dependencyName;
57
74
  break;
58
75
  case "component":
59
76
  var componentName = attr.component;
60
- var dependencyComponentName = this.RelationNames[componentName][0];
77
+ var relationNameObj = this.RelationNames[componentName];
78
+ var dependencyComponentName = relationNameObj.name;
61
79
  isArray = (_a = attr.repeatable) !== null && _a !== void 0 ? _a : false;
62
80
  str += dependencyComponentName;
63
81
  break;
82
+ case "media":
83
+ str += this.RelationNames["builtins::Media"].name;
84
+ isArray = (_b = attr.multiple) !== null && _b !== void 0 ? _b : false;
85
+ break;
64
86
  case "password":
65
87
  return null;
66
88
  case "enumeration":
@@ -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;
@@ -1,7 +1,8 @@
1
1
  import Interface from "./Interface";
2
2
  export default class ComponentInterface extends Interface {
3
3
  protected Category: string;
4
- constructor(baseName: string, attributes: any, relativeDirectoryPath: string, category: string, prefix?: string);
4
+ protected Options: Record<string, any>;
5
+ constructor(baseName: string, attributes: any, relativeDirectoryPath: string, category: string, prefix?: string, options?: Record<string, any>);
5
6
  updateStrapiName(): void;
6
7
  getInterfaceFieldsString(): string;
7
8
  }
@@ -21,15 +21,21 @@ Object.defineProperty(exports, "__esModule", { value: true });
21
21
  var Interface_1 = __importDefault(require("./Interface"));
22
22
  var ComponentInterface = /** @class */ (function (_super) {
23
23
  __extends(ComponentInterface, _super);
24
- function ComponentInterface(baseName, attributes, relativeDirectoryPath, category, prefix) {
24
+ function ComponentInterface(baseName, attributes, relativeDirectoryPath, category, prefix, options) {
25
25
  if (prefix === void 0) { prefix = ""; }
26
+ if (options === void 0) { options = {}; }
26
27
  var _this = _super.call(this, baseName, attributes, relativeDirectoryPath, prefix) || this;
28
+ _this.Options = {
29
+ hasId: true,
30
+ hasComponent: true,
31
+ };
27
32
  _this.Category = category;
28
33
  // this.Attributes.id = {
29
34
  // type: "number", // Components have a id field with a number
30
35
  // required: true,
31
36
  // };
32
37
  _this.updateStrapiName();
38
+ Object.assign(_this.Options, options);
33
39
  return _this;
34
40
  }
35
41
  ComponentInterface.prototype.updateStrapiName = function () {
@@ -38,8 +44,13 @@ var ComponentInterface = /** @class */ (function (_super) {
38
44
  ComponentInterface.prototype.getInterfaceFieldsString = function () {
39
45
  var attrs = this.getAttributes();
40
46
  var str = '';
41
- str += " id: number;\n";
42
- str += " __component: \"".concat(this.getStrapiName(), "\";\n");
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
+ }
43
54
  return str + attrs.toFieldsString();
44
55
  };
45
56
  return ComponentInterface;
@@ -1,4 +1,8 @@
1
1
  import Attributes from "./Attributes";
2
+ export declare type RelationNames = Record<string, {
3
+ name: string;
4
+ inter: Interface;
5
+ }>;
2
6
  export default class Interface {
3
7
  protected BaseName: string;
4
8
  private Relations;
@@ -30,7 +30,7 @@ var Interface = /** @class */ (function () {
30
30
  };
31
31
  Interface.prototype.getDependencies = function () {
32
32
  var attrs = new Attributes_1.default(this.Attributes, this.RelationNames);
33
- return attrs.getDependencies(this.getStrapiName());
33
+ return attrs.getDependencies();
34
34
  };
35
35
  Interface.prototype.getFullInterfaceName = function () {
36
36
  var pascalName = (0, utils_1.pascalCase)(this.BaseName);
@@ -66,7 +66,7 @@ var Interface = /** @class */ (function () {
66
66
  _this.RelationNamesCounter[name] = 0;
67
67
  }
68
68
  }
69
- _this.RelationNames[inter.getStrapiName()] = [name, inter];
69
+ _this.RelationNames[inter.getStrapiName()] = { name: name, inter: inter };
70
70
  });
71
71
  };
72
72
  Interface.prototype.getTsImports = function () {
@@ -75,8 +75,8 @@ var Interface = /** @class */ (function () {
75
75
  if (strapiName === _this.getStrapiName()) {
76
76
  return "";
77
77
  }
78
- var relationName = _this.RelationNames[strapiName][0];
79
- var inter = _this.RelationNames[strapiName][1];
78
+ var relationName = _this.RelationNames[strapiName].name;
79
+ var inter = _this.RelationNames[strapiName].inter;
80
80
  var importPath = (0, utils_1.prefixDotSlash)((0, path_1.relative)(_this.getRelativeRootDir(), inter.getRelativeRootPath()));
81
81
  var fullName = inter.getFullInterfaceName();
82
82
  var importNameString = fullName === relationName ? fullName : "".concat(fullName, " as ").concat(relationName);
@@ -8,9 +8,12 @@ export default class InterfaceManager {
8
8
  useCategoryPrefix: boolean;
9
9
  componentPrefix: string;
10
10
  componentPrefixOverridesPrefix: boolean;
11
+ builtinsPrefix: string;
12
+ builtinsPrefixOverridesPrefix: boolean;
11
13
  };
12
14
  constructor(outRoot: string, strapiSrcRoot: string, options?: any);
13
15
  createInterfaces(): Promise<void>;
16
+ createBuiltinInterfaces(): void;
14
17
  injectDependencies(): void;
15
18
  makeFolders(): Promise<void>;
16
19
  writeInterfaces(): Promise<void>;
@@ -43,6 +43,7 @@ var fs_1 = require("fs");
43
43
  var promises_1 = require("fs/promises");
44
44
  var path_1 = require("path");
45
45
  var utils_1 = require("../utils");
46
+ var builtinInterfaces_1 = require("./builtinInterfaces");
46
47
  var ComponentInterface_1 = __importDefault(require("./ComponentInterface"));
47
48
  var Interface_1 = __importDefault(require("./Interface"));
48
49
  var schemaReader_1 = require("./schemaReader");
@@ -92,6 +93,16 @@ var InterfaceManager = /** @class */ (function () {
92
93
  });
93
94
  });
94
95
  };
96
+ InterfaceManager.prototype.createBuiltinInterfaces = function () {
97
+ var _this = this;
98
+ var outDir = "./builtins";
99
+ var builtinInterfaces = [];
100
+ builtinInterfaces.push((0, builtinInterfaces_1.createMediaInterface)(outDir, this.Options.prefix));
101
+ builtinInterfaces.push((0, builtinInterfaces_1.createMediaFormatInterface)(outDir, this.Options.prefix));
102
+ builtinInterfaces.forEach(function (inter) {
103
+ _this.Interfaces[inter.getStrapiName()] = inter;
104
+ });
105
+ };
95
106
  // Inject dependencies into all interfaces
96
107
  InterfaceManager.prototype.injectDependencies = function () {
97
108
  var _this = this;
@@ -108,7 +119,7 @@ var InterfaceManager = /** @class */ (function () {
108
119
  };
109
120
  InterfaceManager.prototype.makeFolders = function () {
110
121
  return __awaiter(this, void 0, void 0, function () {
111
- var componentCategories;
122
+ var componentCategories, promises, componentCategoriesPromises, builtinsPath;
112
123
  var _this = this;
113
124
  return __generator(this, function (_a) {
114
125
  switch (_a.label) {
@@ -122,7 +133,9 @@ var InterfaceManager = /** @class */ (function () {
122
133
  case 2:
123
134
  _a.sent();
124
135
  _a.label = 3;
125
- case 3: return [4 /*yield*/, Promise.all(componentCategories.map(function (category) { return __awaiter(_this, void 0, void 0, function () {
136
+ case 3:
137
+ promises = [];
138
+ componentCategoriesPromises = componentCategories.map(function (category) { return __awaiter(_this, void 0, void 0, function () {
126
139
  var path;
127
140
  return __generator(this, function (_a) {
128
141
  switch (_a.label) {
@@ -137,7 +150,13 @@ var InterfaceManager = /** @class */ (function () {
137
150
  return [2 /*return*/];
138
151
  }
139
152
  });
140
- }); }))];
153
+ }); });
154
+ promises.push.apply(promises, componentCategoriesPromises);
155
+ builtinsPath = (0, path_1.join)(this.OutRoot, "builtins");
156
+ if (!(0, fs_1.existsSync)(builtinsPath)) {
157
+ promises.push((0, promises_1.mkdir)((0, path_1.join)(this.OutRoot, "builtins")));
158
+ }
159
+ return [4 /*yield*/, Promise.all(promises)];
141
160
  case 4:
142
161
  _a.sent();
143
162
  return [2 /*return*/];
@@ -205,9 +224,11 @@ var InterfaceManager = /** @class */ (function () {
205
224
  _a.trys.push([0, 4, , 5]);
206
225
  createInterfacesPromise = this.createInterfaces();
207
226
  makeFoldersPromise = this.makeFolders();
227
+ this.createBuiltinInterfaces();
208
228
  return [4 /*yield*/, createInterfacesPromise];
209
229
  case 1:
210
230
  _a.sent();
231
+ // Create all interfaces before injecting
211
232
  this.injectDependencies();
212
233
  return [4 /*yield*/, makeFoldersPromise];
213
234
  case 2:
@@ -230,6 +251,8 @@ var InterfaceManager = /** @class */ (function () {
230
251
  useCategoryPrefix: true,
231
252
  componentPrefix: "",
232
253
  componentPrefixOverridesPrefix: false,
254
+ builtinsPrefix: "",
255
+ builtinsPrefixOverridesPrefix: false // TODO: make this work
233
256
  };
234
257
  return InterfaceManager;
235
258
  }());
@@ -0,0 +1,4 @@
1
+ import BuiltinComponentInterface from "./BuiltinComponentInterface";
2
+ import BuiltinInterface from "./BuiltinInterface";
3
+ export declare function createMediaInterface(directory: string, prefix: string): BuiltinInterface;
4
+ export declare function createMediaFormatInterface(directory: string, prefix: string): BuiltinComponentInterface;
@@ -0,0 +1,66 @@
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
+ exports.createMediaFormatInterface = exports.createMediaInterface = void 0;
7
+ var BuiltinComponentInterface_1 = __importDefault(require("./BuiltinComponentInterface"));
8
+ var BuiltinInterface_1 = __importDefault(require("./BuiltinInterface"));
9
+ function createMediaInterface(directory, prefix) {
10
+ var stringFields = [
11
+ "name",
12
+ "alternativeText",
13
+ "caption",
14
+ "hash",
15
+ "ext",
16
+ "mime",
17
+ "url",
18
+ "previewUrl",
19
+ "provider",
20
+ ];
21
+ var numberFields = [
22
+ "width",
23
+ "height",
24
+ "size",
25
+ ];
26
+ var mediaFormat = {
27
+ type: "component",
28
+ repeatable: false,
29
+ component: "builtins::MediaFormat",
30
+ };
31
+ var mediaAttrs = {
32
+ formats: {
33
+ // types-4-strapi-2 specific
34
+ type: "nested",
35
+ fields: {
36
+ thumbnail: mediaFormat,
37
+ medium: mediaFormat,
38
+ small: mediaFormat,
39
+ },
40
+ },
41
+ };
42
+ stringFields.forEach(function (s) { return mediaAttrs[s] = { type: "string" }; });
43
+ numberFields.forEach(function (s) { return mediaAttrs[s] = { type: "integer" }; });
44
+ return new BuiltinInterface_1.default("Media", mediaAttrs, directory, prefix);
45
+ }
46
+ exports.createMediaInterface = createMediaInterface;
47
+ function createMediaFormatInterface(directory, prefix) {
48
+ var stringFields = [
49
+ "name",
50
+ "hash",
51
+ "ext",
52
+ "mime",
53
+ "path",
54
+ "url",
55
+ ];
56
+ var numberFields = [
57
+ "width",
58
+ "height",
59
+ "size",
60
+ ];
61
+ var mediaAttrs = {};
62
+ stringFields.forEach(function (s) { return mediaAttrs[s] = { type: "string" }; });
63
+ numberFields.forEach(function (s) { return mediaAttrs[s] = { type: "integer" }; });
64
+ return new BuiltinComponentInterface_1.default("MediaFormat", mediaAttrs, directory, prefix);
65
+ }
66
+ exports.createMediaFormatInterface = createMediaFormatInterface;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oak-digital/types-4-strapi-2",
3
- "version": "0.1.2",
3
+ "version": "0.2.0",
4
4
  "description": "Typescript interface generator for Strapi 4 models",
5
5
  "bin": {
6
6
  "t4s": "./bin/index.js"