@oak-digital/types-4-strapi-2 0.2.2 → 0.2.5

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/README.md CHANGED
@@ -45,6 +45,7 @@ This can be done with the `--out` flag like in the following example.
45
45
 
46
46
  ### Planned features
47
47
 
48
+ * Support for localization
48
49
  * Support if you are using other plugins, such as `url-alias`, which should add extra fields for some interfaces.
49
50
 
50
51
  ## Help
@@ -1,4 +1,4 @@
1
- import { RelationNames } 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;
@@ -9,11 +9,11 @@ var Attributes = /** @class */ (function () {
9
9
  Attributes.prototype.isAttributeOptional = function (attr) {
10
10
  // If it is a component / relation / dynamiczone it is always optional due to population
11
11
  switch (attr.type) {
12
- case "nested":
12
+ case 'nested':
13
13
  return attr.nullable === true;
14
- case "component":
15
- case "dynamiczone":
16
- case "relation":
14
+ case 'component':
15
+ case 'dynamiczone':
16
+ case 'relation':
17
17
  return true;
18
18
  default:
19
19
  break;
@@ -26,18 +26,18 @@ var Attributes = /** @class */ (function () {
26
26
  var attr = this.Attrs[attrName];
27
27
  var dependencyNames = [];
28
28
  switch (attr.type) {
29
- case "nested":
29
+ case 'nested':
30
30
  var attrs = new Attributes(attr.fields, this.RelationNames);
31
31
  dependencyNames.push.apply(dependencyNames, attrs.getDependencies());
32
32
  break;
33
- case "relation":
33
+ case 'relation':
34
34
  dependencyNames.push(attr.target);
35
35
  break;
36
- case "component":
36
+ case 'component':
37
37
  dependencyNames.push(attr.component);
38
38
  break;
39
- case "media":
40
- dependencyNames.push("builtins::Media");
39
+ case 'media':
40
+ dependencyNames.push('builtins::Media');
41
41
  break;
42
42
  default:
43
43
  continue;
@@ -55,70 +55,70 @@ var Attributes = /** @class */ (function () {
55
55
  return dependencies;
56
56
  };
57
57
  Attributes.prototype.attributeToString = function (attrName, attr) {
58
- var _a, _b;
58
+ var _a;
59
59
  var optionalString = this.isAttributeOptional(attr) ? '?' : '';
60
60
  var str = " ".concat(attrName).concat(optionalString, ": ");
61
61
  var isArray = false;
62
62
  switch (attr.type) {
63
63
  // types-4-strapi-2 specific, used for builtin types
64
- case "nested":
64
+ case 'nested':
65
65
  // Be careful with recursion
66
66
  // console.log(attr);
67
67
  var newAttrs = new Attributes(attr.fields, this.RelationNames);
68
68
  str += newAttrs.toString();
69
69
  break;
70
- case "relation":
70
+ case 'relation':
71
71
  var apiName = attr.target;
72
72
  // console.log(this.RelationNames, apiName)
73
73
  var dependencyName = this.RelationNames[apiName].name;
74
- isArray = attr.relation.endsWith("ToMany");
75
- str += dependencyName;
74
+ var relationMultipleString = attr.relation.endsWith('ToMany') ? '[]' : ' | null';
75
+ str += "{ data: ".concat(dependencyName).concat(relationMultipleString, "; }");
76
76
  break;
77
- case "component":
77
+ case 'component':
78
78
  var componentName = attr.component;
79
79
  var relationNameObj = this.RelationNames[componentName];
80
80
  var dependencyComponentName = relationNameObj.name;
81
81
  isArray = (_a = attr.repeatable) !== null && _a !== void 0 ? _a : false;
82
82
  str += dependencyComponentName;
83
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;
84
+ case 'media':
85
+ var mediaOptional = attr.required !== true ? ' | null' : '';
86
+ var mediaMultipleString = attr.multiple ? '[]' : mediaOptional;
87
+ str += "{ data: ".concat(this.RelationNames['builtins::Media'].name).concat(mediaMultipleString, "; }");
88
88
  break;
89
- case "password":
89
+ case 'password':
90
90
  return null;
91
- case "enumeration":
92
- var hasDefault = "default" in attr;
91
+ case 'enumeration':
92
+ var hasDefault = 'default' in attr;
93
93
  var enums = attr.enum.map(function (en) { return "\"".concat(en, "\""); });
94
- enums.push("null");
95
- var typeString = enums.join(" | ");
94
+ enums.push('null');
95
+ var typeString = enums.join(' | ');
96
96
  str += typeString;
97
97
  break;
98
- case "string":
99
- case "text":
100
- case "richtext":
101
- case "email":
102
- case "uid":
103
- str += "string";
98
+ case 'string':
99
+ case 'text':
100
+ case 'richtext':
101
+ case 'email':
102
+ case 'uid':
103
+ str += 'string';
104
104
  break;
105
- case "integer":
106
- case "biginteger":
107
- case "decimal":
108
- case "float":
109
- str += "number";
105
+ case 'integer':
106
+ case 'biginteger':
107
+ case 'decimal':
108
+ case 'float':
109
+ str += 'number';
110
110
  break;
111
- case "date":
112
- case "datetime":
113
- case "time":
114
- str += "Date";
111
+ case 'date':
112
+ case 'datetime':
113
+ case 'time':
114
+ str += 'Date';
115
115
  break;
116
- case "boolean":
116
+ case 'boolean':
117
117
  str += attr.type;
118
118
  break;
119
- case "json":
119
+ case 'json':
120
120
  default:
121
- str += "any";
121
+ str += 'any';
122
122
  break;
123
123
  }
124
124
  var isArrayString = isArray ? '[]' : '';
@@ -135,13 +135,13 @@ var Attributes = /** @class */ (function () {
135
135
  }
136
136
  strings.push(attrString);
137
137
  }
138
- return strings.map(function (s) { return "".concat(s, "\n"); }).join("");
138
+ return strings.map(function (s) { return "".concat(s, "\n"); }).join('');
139
139
  };
140
140
  Attributes.prototype.toString = function () {
141
- var strings = ["{"];
141
+ var strings = ['{'];
142
142
  strings.push(this.toFieldsString());
143
- strings.push("}");
144
- return strings.join("\n");
143
+ strings.push('}');
144
+ return strings.join('\n');
145
145
  };
146
146
  return Attributes;
147
147
  }());
@@ -1,4 +1,4 @@
1
- import Attributes from "./Attributes";
1
+ import Attributes from './Attributes';
2
2
  export declare type RelationNames = Record<string, {
3
3
  name: string;
4
4
  inter: Interface;
@@ -3,16 +3,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- var path_1 = require("path");
6
+ var posix_1 = require("path/posix");
7
7
  var utils_1 = require("../utils");
8
8
  var Attributes_1 = __importDefault(require("./Attributes"));
9
9
  var Interface = /** @class */ (function () {
10
10
  function Interface(baseName, attributes, relativeDirectoryPath, prefix) {
11
- if (prefix === void 0) { prefix = ""; }
11
+ if (prefix === void 0) { prefix = ''; }
12
12
  this.Relations = []; // Components and relations
13
13
  this.RelationNames = {};
14
14
  this.RelationNamesCounter = {};
15
- this.NamePrefix = "";
15
+ this.NamePrefix = '';
16
16
  this.BaseName = baseName;
17
17
  this.updateStrapiName();
18
18
  this.NamePrefix = prefix;
@@ -38,11 +38,11 @@ var Interface = /** @class */ (function () {
38
38
  };
39
39
  // For typescript import from index file
40
40
  Interface.prototype.getRelativeRootPath = function () {
41
- var path = (0, path_1.join)(this.RelativeDirectoryPath, this.getBaseName());
41
+ var path = (0, posix_1.join)(this.RelativeDirectoryPath, this.getBaseName());
42
42
  return (0, utils_1.prefixDotSlash)(path);
43
43
  };
44
44
  Interface.prototype.getRelativeRootDir = function () {
45
- var path = (0, path_1.dirname)(this.getRelativeRootPathFile());
45
+ var path = (0, posix_1.dirname)(this.getRelativeRootPathFile());
46
46
  return path;
47
47
  };
48
48
  Interface.prototype.getRelativeRootPathFile = function () {
@@ -55,9 +55,7 @@ var Interface = /** @class */ (function () {
55
55
  this.Relations.forEach(function (inter) {
56
56
  var name = inter.getFullInterfaceName();
57
57
  // FIXME: clean up this mess...
58
- if (inter.getStrapiName() === _this.getStrapiName()) {
59
- }
60
- else {
58
+ if (inter.getStrapiName() !== _this.getStrapiName()) {
61
59
  // Avoid duplicate names
62
60
  if (name in _this.RelationNamesCounter) {
63
61
  name += ++_this.RelationNamesCounter[name];
@@ -73,15 +71,15 @@ var Interface = /** @class */ (function () {
73
71
  var _this = this;
74
72
  return Object.keys(this.RelationNames).map(function (strapiName) {
75
73
  if (strapiName === _this.getStrapiName()) {
76
- return "";
74
+ return '';
77
75
  }
78
76
  var relationName = _this.RelationNames[strapiName].name;
79
77
  var inter = _this.RelationNames[strapiName].inter;
80
- var importPath = (0, utils_1.prefixDotSlash)((0, path_1.relative)(_this.getRelativeRootDir(), inter.getRelativeRootPath()));
78
+ var importPath = (0, utils_1.prefixDotSlash)((0, posix_1.relative)(_this.getRelativeRootDir(), inter.getRelativeRootPath()));
81
79
  var fullName = inter.getFullInterfaceName();
82
80
  var importNameString = fullName === relationName ? fullName : "".concat(fullName, " as ").concat(relationName);
83
81
  return "import { ".concat(importNameString, " } from '").concat(importPath, "';");
84
- }).filter(function (s) { return s; }).join("\n");
82
+ }).filter(function (s) { return s; }).join('\n');
85
83
  };
86
84
  Interface.prototype.getAttributes = function () {
87
85
  return new Attributes_1.default(this.Attributes, this.RelationNames);
@@ -93,13 +91,13 @@ var Interface = /** @class */ (function () {
93
91
  Interface.prototype.getInerfaceString = function () {
94
92
  var str = "export interface ".concat(this.getFullInterfaceName(), " {\n");
95
93
  str += this.getInterfaceFieldsString();
96
- str += "}";
94
+ str += '}';
97
95
  return str;
98
96
  };
99
97
  Interface.prototype.getInterfaceFieldsString = function () {
100
98
  var str = '';
101
- str += " id: number;\n";
102
- str += " attributes: ".concat(this.attributesToString(), "\n");
99
+ str += 'id: number;\n';
100
+ str += "attributes: ".concat(this.attributesToString(), "\n");
103
101
  return str;
104
102
  };
105
103
  Interface.prototype.toString = function () {
@@ -107,7 +105,7 @@ var Interface = /** @class */ (function () {
107
105
  this.getTsImports(),
108
106
  this.getInerfaceString()
109
107
  ];
110
- return strings.join("\n");
108
+ return strings.join('\n');
111
109
  };
112
110
  return Interface;
113
111
  }());
@@ -41,7 +41,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
41
41
  Object.defineProperty(exports, "__esModule", { value: true });
42
42
  var fs_1 = require("fs");
43
43
  var promises_1 = require("fs/promises");
44
- var path_1 = require("path");
44
+ var posix_1 = require("path/posix");
45
45
  var utils_1 = require("../utils");
46
46
  var builtinInterfaces_1 = require("./builtinInterfaces");
47
47
  var ComponentInterface_1 = __importDefault(require("./ComponentInterface"));
@@ -163,7 +163,7 @@ var InterfaceManager = /** @class */ (function () {
163
163
  return __generator(this, function (_a) {
164
164
  switch (_a.label) {
165
165
  case 0:
166
- path = (0, path_1.join)(this.OutRoot, category);
166
+ path = (0, posix_1.join)(this.OutRoot, category);
167
167
  if ((0, fs_1.existsSync)(path)) {
168
168
  return [2 /*return*/];
169
169
  }
@@ -175,9 +175,9 @@ var InterfaceManager = /** @class */ (function () {
175
175
  });
176
176
  }); });
177
177
  promises.push.apply(promises, componentCategoriesPromises);
178
- builtinsPath = (0, path_1.join)(this.OutRoot, 'builtins');
178
+ builtinsPath = (0, posix_1.join)(this.OutRoot, 'builtins');
179
179
  if (!(0, fs_1.existsSync)(builtinsPath)) {
180
- promises.push((0, promises_1.mkdir)((0, path_1.join)(this.OutRoot, 'builtins')));
180
+ promises.push((0, promises_1.mkdir)((0, posix_1.join)(this.OutRoot, 'builtins')));
181
181
  }
182
182
  return [4 /*yield*/, Promise.all(promises)];
183
183
  case 4:
@@ -202,7 +202,7 @@ var InterfaceManager = /** @class */ (function () {
202
202
  inter = this.Interfaces[strapiName];
203
203
  fileData = inter.toString();
204
204
  formattedFileData = prettier_1.default.format(fileData, this.PrettierOptions);
205
- filePath = (0, path_1.join)(this.OutRoot, inter.getRelativeRootPathFile());
205
+ filePath = (0, posix_1.join)(this.OutRoot, inter.getRelativeRootPathFile());
206
206
  return [4 /*yield*/, (0, promises_1.writeFile)(filePath, formattedFileData)];
207
207
  case 1:
208
208
  _a.sent();
@@ -231,7 +231,7 @@ var InterfaceManager = /** @class */ (function () {
231
231
  });
232
232
  fileData = strings.join('\n');
233
233
  formattedFileData = prettier_1.default.format(fileData, this.PrettierOptions);
234
- filePath = (0, path_1.join)(this.OutRoot, 'index.ts');
234
+ filePath = (0, posix_1.join)(this.OutRoot, 'index.ts');
235
235
  return [4 /*yield*/, (0, promises_1.writeFile)(filePath, formattedFileData)];
236
236
  case 1:
237
237
  _a.sent();
@@ -38,7 +38,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
38
38
  Object.defineProperty(exports, "__esModule", { value: true });
39
39
  exports.getApiSchemas = exports.getComponentSchemas = exports.getComponentCategoryFolders = exports.getApiFolders = exports.readSchema = void 0;
40
40
  var promises_1 = require("node:fs/promises");
41
- var node_path_1 = require("node:path");
41
+ var posix_1 = require("node:path/posix");
42
42
  var utils_1 = require("../utils");
43
43
  function readSchema(schemaPath) {
44
44
  return __awaiter(this, void 0, void 0, function () {
@@ -62,13 +62,15 @@ function readSchema(schemaPath) {
62
62
  exports.readSchema = readSchema;
63
63
  function getApiFolders(strapiSrcRoot) {
64
64
  return __awaiter(this, void 0, void 0, function () {
65
- var path;
65
+ var path, folders;
66
66
  return __generator(this, function (_a) {
67
67
  switch (_a.label) {
68
68
  case 0:
69
- path = (0, node_path_1.join)(strapiSrcRoot, 'api');
69
+ path = (0, posix_1.join)(strapiSrcRoot, 'api');
70
70
  return [4 /*yield*/, (0, utils_1.readDirFiltered)(path)];
71
- case 1: return [2 /*return*/, _a.sent()];
71
+ case 1:
72
+ folders = _a.sent();
73
+ return [2 /*return*/, folders];
72
74
  }
73
75
  });
74
76
  });
@@ -76,13 +78,15 @@ function getApiFolders(strapiSrcRoot) {
76
78
  exports.getApiFolders = getApiFolders;
77
79
  function getComponentCategoryFolders(strapiSrcRoot) {
78
80
  return __awaiter(this, void 0, void 0, function () {
79
- var path;
81
+ var path, folders;
80
82
  return __generator(this, function (_a) {
81
83
  switch (_a.label) {
82
84
  case 0:
83
- path = (0, node_path_1.join)(strapiSrcRoot, "components");
85
+ path = (0, posix_1.join)(strapiSrcRoot, 'components');
84
86
  return [4 /*yield*/, (0, utils_1.readDirFiltered)(path)];
85
- case 1: return [2 /*return*/, _a.sent()];
87
+ case 1:
88
+ folders = _a.sent();
89
+ return [2 /*return*/, folders];
86
90
  }
87
91
  });
88
92
  });
@@ -103,7 +107,7 @@ function getComponentSchemas(strapiSrcRoot) {
103
107
  return __generator(this, function (_a) {
104
108
  switch (_a.label) {
105
109
  case 0:
106
- schemaFilesPath = (0, node_path_1.join)(strapiSrcRoot, "components", category);
110
+ schemaFilesPath = (0, posix_1.join)(strapiSrcRoot, 'components', category);
107
111
  return [4 /*yield*/, (0, utils_1.readDirFiltered)(schemaFilesPath)];
108
112
  case 1:
109
113
  schemaFiles = _a.sent();
@@ -112,11 +116,11 @@ function getComponentSchemas(strapiSrcRoot) {
112
116
  return __generator(this, function (_a) {
113
117
  switch (_a.label) {
114
118
  case 0:
115
- schemaPath = (0, node_path_1.join)(schemaFilesPath, file);
119
+ schemaPath = (0, posix_1.join)(schemaFilesPath, file);
116
120
  return [4 /*yield*/, readSchema(schemaPath)];
117
121
  case 1:
118
122
  attributes = _a.sent();
119
- name = file.split(".")[0];
123
+ name = file.split('.')[0];
120
124
  return [2 /*return*/, { name: name, attributes: attributes }];
121
125
  }
122
126
  });
@@ -151,7 +155,7 @@ function getApiSchemas(strapiSrcRoot) {
151
155
  return __generator(this, function (_a) {
152
156
  switch (_a.label) {
153
157
  case 0:
154
- schemaPath = (0, node_path_1.join)(strapiSrcRoot, "api", folder, "content-types", folder, "schema.json");
158
+ schemaPath = (0, posix_1.join)(strapiSrcRoot, 'api', folder, 'content-types', folder, 'schema.json');
155
159
  return [4 /*yield*/, readSchema(schemaPath)];
156
160
  case 1:
157
161
  attributes = _a.sent();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oak-digital/types-4-strapi-2",
3
- "version": "0.2.2",
3
+ "version": "0.2.5",
4
4
  "description": "Typescript interface generator for Strapi 4 models",
5
5
  "bin": {
6
6
  "t4s": "./bin/index.js"