Skip to content

Commit

Permalink
remove old hack for component
Browse files Browse the repository at this point in the history
  • Loading branch information
aperron committed May 4, 2021
1 parent 6ac916d commit 494cc76
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 43 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,7 @@ The input folder is recursively processed and each model file is read. When done
npm install && npm run build
# output files generated in dist folder
```


console warn deprecated
template handlebrake/...
16 changes: 1 addition & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@
"homepage": "https://github.com/erikvullings/strapi-to-typescript#readme",
"dependencies": {
"command-line-args": "^5.1.1",
"command-line-usage": "^5.0.5",
"pluralize": "^8.0.0"
"command-line-usage": "^5.0.5"
},
"devDependencies": {
"@types/command-line-args": "^5.0.0",
Expand Down
18 changes: 18 additions & 0 deletions src/test/api/testobject/models/testobject.settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,24 @@
"type": "uid",
"required": true
},
"component_complex_repeatable": {
"component": "content.complex",
"repeatable": true,
"type": "component",
"required": true
},
"component_complex_optional": {
"component": "content.complex",
"repeatable": false,
"type": "component",
"required": false
},
"component_complex": {
"component": "content.complex",
"repeatable": false,
"type": "component",
"required": true
},
"single_dynamiczone": {
"type": "dynamiczone",
"repeatable": false,
Expand Down
24 changes: 22 additions & 2 deletions src/test/test1.assert.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@
import { ITestobject, ITestobjectrelation, EnumITestobjectenum_field } from './out1';
import { IFile } from './out1/file'
import { IComplex } from "./out1/content/complex";
import { EnumIComplexvariant, IComplex } from "./out1/content/complex";
import { ISimple } from "./out1/content/simple";
import { IWithDash } from "./out1/content/camel-case";
import { IJustACompleteOtherName } from "./out1/content/another";


class IComplexImpl implements IComplex {
id: string;
variant?: EnumIComplexvariant;
key?: string;
single?: IComplex | ISimple;
repeatable: (IComplex | ISimple)[];

constructor(){
this.id = "id";
this.repeatable = [];
}
}

// implementation of Itestobject test required and type fields
class ItestobjectImpl implements ITestobject {
id: string;
Expand All @@ -29,7 +43,10 @@ class ItestobjectImpl implements ITestobject {
json_field: { [key: string]: any; };
uid_field: any;
created_by: string;
single_dynamiczone?: [(IComplex | ISimple | IWithDash | IJustACompleteOtherName)];
component_complex: IComplex;
component_complex_optional?: IComplex;
component_complex_repeatable:IComplex[];
single_dynamiczone?: IComplex | ISimple | IWithDash | IJustACompleteOtherName;
repeatable_dynamiczone: (IComplex | ISimple | IWithDash | IJustACompleteOtherName)[];

testobjectrelation?: ITestobjectrelation;
Expand Down Expand Up @@ -61,6 +78,9 @@ class ItestobjectImpl implements ITestobject {
this.datetime_field = {};
this.time_field = {};
this.uid_field = {};

this.component_complex = new IComplexImpl();
this.component_complex_repeatable = [];
}
}

Expand Down
23 changes: 21 additions & 2 deletions src/test/test2.assert.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import { ITestobject, ITestobjectrelation, EnumITestobjectenum_field } from './out2';
import { IFile } from './out2/file/file';
import { IComplex } from "./out2/content/complex";
import { IComplex, EnumIComplexvariant } from "./out2/content/complex";
import { ISimple } from "./out2/content/simple";
import { IWithDash } from "./out2/content/camel-case";
import { IJustACompleteOtherName } from "./out2/content/another";

class IComplexImpl implements IComplex {
id: string;
variant?: EnumIComplexvariant;
key?: string;
single?: IComplex | ISimple;
repeatable: (IComplex | ISimple)[];

constructor(){
this.id = "id";
this.repeatable = [];
}
}

// implementation of Itestobject test required and type fields
class ItestobjectImpl implements ITestobject {
id: string;
Expand All @@ -29,7 +42,10 @@ class ItestobjectImpl implements ITestobject {
json_field: { [key: string]: any; };
uid_field: any;
created_by: string;
single_dynamiczone?: [(IComplex | ISimple | IWithDash | IJustACompleteOtherName)];
component_complex: IComplex;
component_complex_optional?: IComplex;
component_complex_repeatable:IComplex[];
single_dynamiczone?: IComplex | ISimple | IWithDash | IJustACompleteOtherName;
repeatable_dynamiczone: (IComplex | ISimple | IWithDash | IJustACompleteOtherName)[];

testobjectrelation?: ITestobjectrelation;
Expand Down Expand Up @@ -61,6 +77,9 @@ class ItestobjectImpl implements ITestobject {
this.datetime_field = {};
this.time_field = {};
this.uid_field = {};

this.component_complex = new IComplexImpl();
this.component_complex_repeatable = [];
}
}

Expand Down
23 changes: 21 additions & 2 deletions src/test/test3.assert.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import { Xtestobject, Xtestobjectrelation, EnumXtestobjectenum_field } from './out3';
import { Xfile } from './out3/Xfile';
import { Xcomplex } from "./out3/content/Xcomplex";
import { Xcomplex, EnumXcomplexvariant } from "./out3/content/Xcomplex";
import { Xsimple } from "./out3/content/Xsimple";
import { XWithDash } from "./out3/content/XWithDash";
import { XJustaCompleteOtherName } from "./out3/content/XJustaCompleteOtherName";

class XcomplexImpl implements Xcomplex {
id: string;
variant?: EnumXcomplexvariant;
key?: string;
single?: Xcomplex | Xsimple;
repeatable: (Xcomplex | Xsimple)[];

constructor(){
this.id = "id";
this.repeatable = [];
}
}

// implementation of Itestobject test required and type fields
class ItestobjectImpl implements Xtestobject {
id: string;
Expand All @@ -29,7 +42,10 @@ class ItestobjectImpl implements Xtestobject {
json_field: { [key: string]: any; };
uid_field: any;
created_by: string;
single_dynamiczone?: [(Xcomplex | Xsimple | XWithDash | XJustaCompleteOtherName)];
component_complex: Xcomplex;
component_complex_optional?: Xcomplex;
component_complex_repeatable:Xcomplex[];
single_dynamiczone?: Xcomplex | Xsimple | XWithDash | XJustaCompleteOtherName;
repeatable_dynamiczone: (Xcomplex | Xsimple | XWithDash | XJustaCompleteOtherName)[];

testobjectrelation?: Xtestobjectrelation;
Expand Down Expand Up @@ -61,6 +77,9 @@ class ItestobjectImpl implements Xtestobject {
this.datetime_field = {};
this.time_field = {};
this.uid_field = {};

this.component_complex = new XcomplexImpl();
this.component_complex_repeatable = [];
}
}

Expand Down
26 changes: 6 additions & 20 deletions src/ts-exporter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as fs from 'fs';
import * as path from 'path';
import { singular } from 'pluralize'
import { IStrapiModel, IStrapiModelAttribute } from './models/strapi-model';
import { IConfigOptions } from '..';

Expand Down Expand Up @@ -106,20 +105,6 @@ const findModel = (structure: IStrapiModelExtended[], name: string): IStrapiMode
return structure.filter((s) => s.modelName.toLowerCase() === name.toLowerCase()).shift();
};

/**
* Transform a Strapi Attribute of component.
*
* @param attr IStrapiModelAttribute
*/
const componentCompatible = (attr: IStrapiModelAttribute) => {
if (attr.type === 'component') {
let model = singular(attr.component!.split('.')[1])
return attr.repeatable ? { collection: model } : { model: model }
}
return attr;
}


class Converter {

strapiModels: IStrapiModelExtended[] = [];
Expand Down Expand Up @@ -238,10 +223,10 @@ class Converter {

if (!m.attributes.hasOwnProperty(aName)) continue;

const a = componentCompatible(m.attributes[aName]);
const a = m.attributes[aName];
if ((a.collection || a.model) === m.modelName) continue;

const proposedImport = toImportDefinition(a.collection || a.model || '')
const proposedImport = toImportDefinition(a.collection || a.model || a.component || '')
if (proposedImport) imports.push(proposedImport);

imports.push(...(a.components || [])
Expand Down Expand Up @@ -275,16 +260,17 @@ class Converter {
};

const required = !a.required && !(!this.config.collectionCanBeUndefined && (a.collection || a.repeatable)) ? '?' : '';
a = componentCompatible(a);
const collection = a.collection ? '[]' : '';
const collection = a.collection || a.repeatable ? '[]' : '';

let propType = 'unknown';
if (a.collection) {
propType = findModelName(a.collection);
} else if (a.component) {
propType = findModelName(a.component);
} else if (a.model) {
propType = findModelName(a.model);
} else if (a.type === "dynamiczone") {
propType = `(${a.components!.map(findModelName).join("|")})[]`
propType = `(${a.components!.map(findModelName).join("|")})`
} else if (a.type) {
propType = util.toPropertyType(interfaceName, name, a, this.config.enum)
}
Expand Down

0 comments on commit 494cc76

Please sign in to comment.