Skip to content

Commit 4f86a44

Browse files
authored
Merge pull request #132 from luca-nardelli/id-identity-through-entity
Typescript generator: Handle resources with explicit id field
2 parents c18e842 + 7aa8307 commit 4f86a44

File tree

3 files changed

+70
-2
lines changed

3 files changed

+70
-2
lines changed

src/generators/TypescriptInterfaceGenerator.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,17 @@ export default class TypescriptInterfaceGenerator extends BaseGenerator {
8585
};
8686
}
8787

88+
// If id is not present, add it manually with default values
89+
if (!("id" in fields)) {
90+
fields["id"] = {
91+
notrequired: true,
92+
name: "id",
93+
type: "string",
94+
description: null,
95+
readonly: false
96+
};
97+
}
98+
8899
// Parse fields to add relevant imports, required for Typescript
89100
const fieldsArray = Object.keys(fields).map(e => fields[e]);
90101
const imports = {};

src/generators/TypescriptInterfaceGenerator.test.js

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ test("Generate a typescript interface", () => {
5454
5555
export interface Foo {
5656
'@id'?: string;
57-
id?: string;
5857
foo: any;
5958
foobar?: FooBar;
6059
readonly bar: string;
60+
id?: string;
6161
}
6262
`;
6363
expect(
@@ -106,9 +106,67 @@ test("Generate a typescript interface without references to other interfaces", (
106106

107107
const res = `export interface Foo {
108108
'@id'?: string;
109+
foo: any;
110+
readonly bar: string;
109111
id?: string;
112+
}
113+
`;
114+
expect(
115+
fs.readFileSync(tmpobj.name + "/interfaces/foo.ts").toString()
116+
).toEqual(res);
117+
118+
tmpobj.removeCallback();
119+
});
120+
121+
test("Generate a typescript interface with an explicit id field in the readableFields", () => {
122+
const generator = new TypescriptInterfaceGenerator({
123+
templateDirectory: `${__dirname}/../../templates`
124+
});
125+
const tmpobj = tmp.dirSync({ unsafeCleanup: true });
126+
127+
const resource = new Resource("abc", "http://example.com/foos", {
128+
id: "foo",
129+
title: "Foo",
130+
readableFields: [
131+
new Field("bar", {
132+
id: "http://schema.org/url",
133+
range: "http://www.w3.org/2001/XMLSchema#string",
134+
reference: null,
135+
required: true,
136+
description: "An URL"
137+
}),
138+
new Field("id", {
139+
id: "http://schema.org/url",
140+
range: "http://www.w3.org/2001/XMLSchema#string",
141+
reference: null,
142+
required: false,
143+
description: "Id"
144+
})
145+
],
146+
writableFields: [
147+
new Field("foo", {
148+
id: "http://schema.org/url",
149+
range: "http://www.w3.org/2001/XMLSchema#datetime",
150+
reference: null,
151+
required: true,
152+
description: "An URL"
153+
})
154+
]
155+
});
156+
const api = new Api("http://example.com", {
157+
entrypoint: "http://example.com:8080",
158+
title: "My API",
159+
resources: [resource]
160+
});
161+
generator.generate(api, resource, tmpobj.name);
162+
163+
expect(fs.existsSync(tmpobj.name + "/interfaces/foo.ts")).toBe(true);
164+
165+
const res = `export interface Foo {
166+
'@id'?: string;
110167
foo: any;
111168
readonly bar: string;
169+
readonly id?: string;
112170
}
113171
`;
114172
expect(

templates/typescript/interface.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { {{type}} } from "{{file}}";
66
{{/if}}
77
export interface {{{name}}} {
88
'@id'?: string;
9-
id?: string;
109
{{#each fields}}
1110
{{#if readonly}} readonly{{/if}} {{{name}}}{{#if notrequired}}?{{/if}}: {{{type}}};
1211
{{/each}}

0 commit comments

Comments
 (0)