@oak-digital/types-4-strapi-2 0.2.0 → 0.2.1
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 +67 -0
- package/lib/index.js +6 -3
- package/lib/interface/Attributes.js +4 -1
- package/lib/interface/builtinInterfaces.js +7 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
# types-4-strapi-2
|
|
2
|
+
|
|
3
|
+
types-4-strapi-2 is a typescript program that will generate typescript types for your strapi projects.
|
|
4
|
+
This can be useful if you have a frontend written with typescript to make sure you are using the correct types and can help report errors at compile time.
|
|
5
|
+
|
|
6
|
+
types-4-strapi-2 is a rewrite of (francescolorenzetti/types-4-strapi)[https://github.com/francescolorenzetti/types-4-strapi] written in typescript, with the goal of being much easier to extend and maintain.
|
|
7
|
+
|
|
8
|
+
## Getting started
|
|
9
|
+
|
|
10
|
+
Install the script for your project:
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install --save-dev @oak-digital/types-4-strapi-2
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
Then set up a script in your `package.json`
|
|
17
|
+
|
|
18
|
+
```jsonc
|
|
19
|
+
// package.json
|
|
20
|
+
{
|
|
21
|
+
"scripts": {
|
|
22
|
+
"types": "t4s"
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
In some cases it is desirable to change the output directory which is `./types` by default.
|
|
28
|
+
This can be done with the `--out` flag like in the following example.
|
|
29
|
+
|
|
30
|
+
```jsonc
|
|
31
|
+
// package.json
|
|
32
|
+
{
|
|
33
|
+
"scripts": {
|
|
34
|
+
"types": "t4s --out ../frontend/src/lib/types"
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Features
|
|
40
|
+
|
|
41
|
+
* Generate typescript interfaces for all your api content-types and components
|
|
42
|
+
* Generate typescript interfaces for builtin types such as `Media` and `MediaFormat`
|
|
43
|
+
* Select input and output directory
|
|
44
|
+
|
|
45
|
+
### Planned features
|
|
46
|
+
|
|
47
|
+
* Support if you are using other plugins, such as `url-alias`, which should add extra fields for some interfaces.
|
|
48
|
+
* Nicer formatted interfaces
|
|
49
|
+
|
|
50
|
+
## Help
|
|
51
|
+
|
|
52
|
+
use `t4s --help` to display which options are available for you
|
|
53
|
+
|
|
54
|
+
## Building
|
|
55
|
+
|
|
56
|
+
To build this project, use the following command
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
npm run build
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
## Publihsing
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
npm run build
|
|
66
|
+
npm publish
|
|
67
|
+
```
|
package/lib/index.js
CHANGED
|
@@ -9,11 +9,14 @@ commander_1.program
|
|
|
9
9
|
.name("t4s");
|
|
10
10
|
commander_1.program
|
|
11
11
|
.option('-i, --in <dir>', 'The src directory for strapi', './src')
|
|
12
|
-
.option('-o, --out <dir>', 'The output directory to output the types to', './types')
|
|
12
|
+
.option('-o, --out <dir>', 'The output directory to output the types to', './types')
|
|
13
|
+
.option('--component-prefix <prefix>', 'A prefix for components', "");
|
|
13
14
|
commander_1.program.parse();
|
|
14
15
|
var options = commander_1.program.opts();
|
|
15
|
-
var input = options.in, out = options.out;
|
|
16
|
-
var manager = new InterfaceManager_1.default(out, input
|
|
16
|
+
var input = options.in, out = options.out, componentPrefix = options.componentPrefix;
|
|
17
|
+
var manager = new InterfaceManager_1.default(out, input, {
|
|
18
|
+
componentPrefix: componentPrefix,
|
|
19
|
+
});
|
|
17
20
|
manager.run().catch(function (err) {
|
|
18
21
|
console.error(err);
|
|
19
22
|
});
|
|
@@ -9,6 +9,8 @@ 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":
|
|
13
|
+
return attr.nullable === true;
|
|
12
14
|
case "component":
|
|
13
15
|
case "dynamiczone":
|
|
14
16
|
case "relation":
|
|
@@ -80,7 +82,8 @@ var Attributes = /** @class */ (function () {
|
|
|
80
82
|
str += dependencyComponentName;
|
|
81
83
|
break;
|
|
82
84
|
case "media":
|
|
83
|
-
|
|
85
|
+
var mediaOptional = attr.required !== true ? "?" : "";
|
|
86
|
+
str += "{ data".concat(mediaOptional, ": ").concat(this.RelationNames["builtins::Media"].name, "; }");
|
|
84
87
|
isArray = (_b = attr.multiple) !== null && _b !== void 0 ? _b : false;
|
|
85
88
|
break;
|
|
86
89
|
case "password":
|
|
@@ -41,6 +41,13 @@ function createMediaInterface(directory, prefix) {
|
|
|
41
41
|
};
|
|
42
42
|
stringFields.forEach(function (s) { return mediaAttrs[s] = { type: "string" }; });
|
|
43
43
|
numberFields.forEach(function (s) { return mediaAttrs[s] = { type: "integer" }; });
|
|
44
|
+
// const dataAttrs = {
|
|
45
|
+
// data: {
|
|
46
|
+
// type: "nested",
|
|
47
|
+
// fields: mediaAttrs,
|
|
48
|
+
// nullable: true,
|
|
49
|
+
// },
|
|
50
|
+
// };
|
|
44
51
|
return new BuiltinInterface_1.default("Media", mediaAttrs, directory, prefix);
|
|
45
52
|
}
|
|
46
53
|
exports.createMediaInterface = createMediaInterface;
|