Skip to content

Add imports to typescript generator #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions src/generators/TypescriptInterfaceGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ export default class TypescriptInterfaceGenerator extends BaseGenerator {

generate(api, resource, dir) {
const dest = `${dir}/interfaces`;
const { fields, imports } = this.parseFields(resource);

this.createDir(dest, false);
this.createFile(
"interface.ts",
`${dest}/${resource.title.toLowerCase()}.ts`,
{
fields: this.parseFields(resource),
fields,
imports,
name: resource.title
}
);
Expand Down Expand Up @@ -63,7 +65,8 @@ export default class TypescriptInterfaceGenerator extends BaseGenerator {
name: field.name,
type: this.getType(field),
description: this.getDescription(field),
readonly: false
readonly: false,
reference: field.reference
};
}

Expand All @@ -77,10 +80,26 @@ export default class TypescriptInterfaceGenerator extends BaseGenerator {
name: field.name,
type: this.getType(field),
description: this.getDescription(field),
readonly: true
readonly: true,
reference: field.reference
};
}

return Object.keys(fields).map(e => fields[e]);
// Parse fields to add relevant imports, required for Typescript
const fieldsArray = Object.keys(fields).map(e => fields[e]);
const imports = {};

for (const field of fieldsArray) {
if (field.reference) {
imports[field.type] = {
type: field.type,
file: "./" + field.type.toLowerCase()
};
}
}

const importsArray = Object.keys(imports).map(e => imports[e]);

return { fields: fieldsArray, imports: importsArray };
}
}
55 changes: 54 additions & 1 deletion src/generators/TypescriptInterfaceGenerator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ test("Generate a typescript interface", () => {

expect(fs.existsSync(tmpobj.name + "/interfaces/foo.ts")).toBe(true);

const res = `export interface Foo {
const res = `import { FooBar } from "./foobar";

export interface Foo {
'@id'?: string;
id: string;
foo: any;
Expand All @@ -64,3 +66,54 @@ test("Generate a typescript interface", () => {

tmpobj.removeCallback();
});

test("Generate a typescript interface without references to other interfaces", () => {
const generator = new TypescriptInterfaceGenerator({
templateDirectory: `${__dirname}/../../templates`
});
const tmpobj = tmp.dirSync({ unsafeCleanup: true });

const resource = new Resource("abc", "http://example.com/foos", {
id: "foo",
title: "Foo",
readableFields: [
new Field("bar", {
id: "http://schema.org/url",
range: "http://www.w3.org/2001/XMLSchema#string",
reference: null,
required: true,
description: "An URL"
})
],
writableFields: [
new Field("foo", {
id: "http://schema.org/url",
range: "http://www.w3.org/2001/XMLSchema#datetime",
reference: null,
required: true,
description: "An URL"
})
]
});
const api = new Api("http://example.com", {
entrypoint: "http://example.com:8080",
title: "My API",
resources: [resource]
});
generator.generate(api, resource, tmpobj.name);

expect(fs.existsSync(tmpobj.name + "/interfaces/foo.ts")).toBe(true);

const res = `export interface Foo {
'@id'?: string;
id: string;
foo: any;
readonly bar: string;
}
`;
expect(
fs.readFileSync(tmpobj.name + "/interfaces/foo.ts").toString()
).toEqual(res);

tmpobj.removeCallback();
});
6 changes: 6 additions & 0 deletions templates/typescript/interface.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
{{#each imports}}
import { {{type}} } from "{{file}}";
{{/each}}
{{#if imports.length}}

{{/if}}
export interface {{{name}}} {
'@id'?: string;
id: string;
Expand Down