Skip to content

Typescript generator: Handle resources with explicit id field #132

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
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
11 changes: 11 additions & 0 deletions src/generators/TypescriptInterfaceGenerator.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,17 @@ export default class TypescriptInterfaceGenerator extends BaseGenerator {
};
}

// If id is not present, add it manually with default values
if (!("id" in fields)) {
fields["id"] = {
notrequired: true,
name: "id",
type: "string",
description: null,
readonly: false
};
}

// Parse fields to add relevant imports, required for Typescript
const fieldsArray = Object.keys(fields).map(e => fields[e]);
const imports = {};
Expand Down
60 changes: 59 additions & 1 deletion src/generators/TypescriptInterfaceGenerator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ test("Generate a typescript interface", () => {

export interface Foo {
'@id'?: string;
id?: string;
foo: any;
foobar?: FooBar;
readonly bar: string;
id?: string;
}
`;
expect(
Expand Down Expand Up @@ -106,9 +106,67 @@ test("Generate a typescript interface without references to other interfaces", (

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

tmpobj.removeCallback();
});

test("Generate a typescript interface with an explicit id field in the readableFields", () => {
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"
}),
new Field("id", {
id: "http://schema.org/url",
range: "http://www.w3.org/2001/XMLSchema#string",
reference: null,
required: false,
description: "Id"
})
],
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;
foo: any;
readonly bar: string;
readonly id?: string;
}
`;
expect(
Expand Down
1 change: 0 additions & 1 deletion templates/typescript/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { {{type}} } from "{{file}}";
{{/if}}
export interface {{{name}}} {
'@id'?: string;
id?: string;
{{#each fields}}
{{#if readonly}} readonly{{/if}} {{{name}}}{{#if notrequired}}?{{/if}}: {{{type}}};
{{/each}}
Expand Down