Skip to content

Commit cf23421

Browse files
committed
fix: @dunglas review
1 parent aa0998a commit cf23421

File tree

9 files changed

+98
-117
lines changed

9 files changed

+98
-117
lines changed

src/generators/BaseGenerator.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,28 @@ export default class {
119119
}
120120
}
121121

122+
getType(field) {
123+
if (field.reference) {
124+
return field.reference.title;
125+
}
126+
127+
switch (field.range) {
128+
case "http://www.w3.org/2001/XMLSchema#integer":
129+
case "http://www.w3.org/2001/XMLSchema#decimal":
130+
return "number";
131+
case "http://www.w3.org/2001/XMLSchema#boolean":
132+
return "boolean";
133+
case "http://www.w3.org/2001/XMLSchema#date":
134+
case "http://www.w3.org/2001/XMLSchema#dateTime":
135+
case "http://www.w3.org/2001/XMLSchema#time":
136+
return "Date";
137+
case "http://www.w3.org/2001/XMLSchema#string":
138+
return "string";
139+
}
140+
141+
return "any";
142+
}
143+
122144
buildFields(fields) {
123145
return fields.map(field => ({
124146
...field,

src/generators/NextGenerator.js

Lines changed: 59 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -32,35 +32,39 @@ export default class NextGenerator extends BaseGenerator {
3232
checkDependencies(dir) {
3333
const dependencies = this.getTargetDependencies(dir);
3434

35-
if (dependencies.length) {
36-
if (!dependencies.includes("@zeit/next-typescript")) {
37-
console.log(
38-
chalk.yellow(
39-
"It seems next-typescript is not installed but generator needs typescript to work efficiently."
40-
)
41-
);
42-
}
35+
if (!dependencies.length) {
36+
return;
37+
}
4338

44-
if (!dependencies.includes("express")) {
45-
console.log(
46-
chalk.yellow(
47-
"It seems express is not installed but generator needs a custom express server to work efficiently."
48-
)
49-
);
50-
}
39+
if (!dependencies.includes("@zeit/next-typescript")) {
40+
console.log(
41+
chalk.yellow(
42+
"It seems next-typescript is not installed but generator needs typescript to work efficiently."
43+
)
44+
);
45+
}
46+
47+
if (!dependencies.includes("express")) {
48+
console.log(
49+
chalk.yellow(
50+
"It seems express is not installed but generator needs a custom express server to work efficiently."
51+
)
52+
);
5153
}
5254
}
5355

5456
checkImports(directory, imports, extension = ".ts") {
5557
imports.forEach(({ file }) => {
5658
if (!fs.existsSync(directory + file + extension)) {
57-
console.log(
58-
chalk.yellow(
59-
'An import for the file "%s" has been generated but the file doesn\'t exists.'
60-
),
61-
file
62-
);
59+
return;
6360
}
61+
62+
console.log(
63+
chalk.yellow(
64+
'An import for the file "%s" has been generated but the file doesn\'t exists.'
65+
),
66+
file
67+
);
6468
});
6569
}
6670

@@ -79,8 +83,8 @@ export default class NextGenerator extends BaseGenerator {
7983
console.log("Paste the following route to your server configuration file:");
8084
console.log(
8185
chalk.green(`
82-
server.get('/${lc}/:hash', (req, res) => {
83-
return app.render(req, res, '/${lc}', { hash: req.params.hash })
86+
server.get('/${lc}/:id', (req, res) => {
87+
return app.render(req, res, '/${lc}', { id: req.params.id })
8488
});
8589
`)
8690
);
@@ -155,73 +159,50 @@ server.get('/${lc}/:hash', (req, res) => {
155159
this.createEntrypoint(api.entrypoint, `${dir}/config/entrypoint.ts`);
156160
}
157161

158-
getType(field) {
159-
if (field.reference) {
160-
return field.reference.title;
161-
}
162-
163-
switch (field.range) {
164-
case "http://www.w3.org/2001/XMLSchema#integer":
165-
case "http://www.w3.org/2001/XMLSchema#decimal":
166-
return "number";
167-
case "http://www.w3.org/2001/XMLSchema#boolean":
168-
return "boolean";
169-
case "http://www.w3.org/2001/XMLSchema#date":
170-
case "http://www.w3.org/2001/XMLSchema#dateTime":
171-
case "http://www.w3.org/2001/XMLSchema#time":
172-
return "Date";
173-
case "http://www.w3.org/2001/XMLSchema#string":
174-
return "string";
175-
}
176-
177-
return "any";
178-
}
179-
180162
getDescription(field) {
181163
return field.description ? field.description.replace(/"/g, "'") : "";
182164
}
183165

184166
parseFields(resource) {
185-
const fields = {};
186-
187-
for (let field of resource.writableFields) {
188-
fields[field.name] = {
189-
notrequired: !field.required,
190-
name: field.name,
191-
type: this.getType(field),
192-
description: this.getDescription(field),
193-
readonly: false,
194-
reference: field.reference
195-
};
196-
}
197-
198-
for (let field of resource.readableFields) {
199-
if (fields[field.name] !== undefined) {
200-
continue;
167+
const fields = [
168+
...resource.writableFields,
169+
...resource.readableFields
170+
].reduce((list, field) => {
171+
if (list[field.name]) {
172+
return list;
201173
}
202174

203-
fields[field.name] = {
204-
notrequired: !field.required,
205-
name: field.name,
206-
type: this.getType(field),
207-
description: this.getDescription(field),
208-
readonly: true,
209-
reference: field.reference
175+
return {
176+
...list,
177+
[field.name]: {
178+
notrequired: !field.required,
179+
name: field.name,
180+
type: this.getType(field),
181+
description: this.getDescription(field),
182+
readonly: false,
183+
reference: field.reference
184+
}
210185
};
211-
}
186+
}, {});
212187

213188
// Parse fields to add relevant imports, required for Typescript
214189
const fieldsArray = Object.values(fields);
215-
const imports = {};
216-
217-
for (const field of fieldsArray) {
218-
if (field.reference) {
219-
imports[field.type] = {
220-
type: field.type,
221-
file: "./" + field.type
190+
const imports = Object.values(fields).reduce(
191+
(list, { reference, type }) => {
192+
if (!reference) {
193+
return list;
194+
}
195+
196+
return {
197+
...list,
198+
[type]: {
199+
type,
200+
file: "./" + type
201+
}
222202
};
223-
}
224-
}
203+
},
204+
{}
205+
);
225206

226207
return { fields: fieldsArray, imports: Object.values(imports) };
227208
}

src/generators/TypescriptInterfaceGenerator.js

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,28 +30,6 @@ export default class TypescriptInterfaceGenerator extends BaseGenerator {
3030
);
3131
}
3232

33-
getType(field) {
34-
if (field.reference) {
35-
return field.reference.title;
36-
}
37-
38-
switch (field.range) {
39-
case "http://www.w3.org/2001/XMLSchema#integer":
40-
case "http://www.w3.org/2001/XMLSchema#decimal":
41-
return "number";
42-
case "http://www.w3.org/2001/XMLSchema#boolean":
43-
return "boolean";
44-
case "http://www.w3.org/2001/XMLSchema#date":
45-
case "http://www.w3.org/2001/XMLSchema#dateTime":
46-
case "http://www.w3.org/2001/XMLSchema#time":
47-
return "Date";
48-
case "http://www.w3.org/2001/XMLSchema#string":
49-
return "string";
50-
}
51-
52-
return "any";
53-
}
54-
5533
getDescription(field) {
5634
return field.description ? field.description.replace(/"/g, "'") : "";
5735
}

templates/next/components/common/ReferenceLinks.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ export const ReferenceLinks: NextFunctionComponent<Props> = ({items, type, useIc
2020
}
2121

2222
// to avoid routes like "/book/books/d4s5s1-qd5sd5d-qsd5qsd4sd" we prefer enconding it
23-
const hash = btoa(items);
23+
const id = btoa(items);
2424
const resourceName = type.toLowerCase();
2525

2626
return (
27-
<Link href={`/${resourceName}?hash=${hash}`} as={`/${resourceName}/${hash}`}><a>
27+
<Link href={`/${resourceName}?id=${id}`} as={`/${resourceName}/${id}`}><a>
2828
{useIcon ? (
2929
<Fragment>
3030
<span className="fa fa-search" aria-hidden="true" />

templates/next/components/foo/List.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ interface Props {
66
{{{name}}}: {{{ucf}}}[];
77
}
88

9-
export const {{{ucf}}}List: NextFunctionComponent<Props> = ({ {{{name}}} }) => (
9+
export const List: NextFunctionComponent<Props> = ({ {{{name}}} }) => (
1010
<div>
1111
<h1>{{{ucf}}} List</h1>
1212
<table className="table table-responsive table-striped table-hover">

templates/next/components/foo/Show.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ interface Props {
77
{{{lc}}}: {{{ucf}}};
88
}
99

10-
export const {{{ucf}}}Show: NextFunctionComponent<Props> = ({ {{{lc}}} }) => (
10+
export const Show: NextFunctionComponent<Props> = ({ {{{lc}}} }) => (
1111
<div>
1212
<h1>Show { {{{lc}}}['@id'] }</h1>
1313
<table className="table table-responsive table-striped table-hover">

templates/next/pages/foo.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import { NextContext, NextFunctionComponent } from 'next';
22
import { withRouter } from 'next/router';
3-
import { {{{ucf}}}Show } from '../components/{{{lc}}}/Show';
3+
import { Show } from '../components/{{{lc}}}/Show';
44
import { {{{ucf}}} } from '../interfaces/{{{ucf}}}';
55
import { atob, fetch } from '../utils/dataAccess';
66

77
interface Props {
88
{{{lc}}}: {{{ucf}}};
99
};
1010

11-
const {{{ucf}}}Page: NextFunctionComponent<Props> = ({ {{{lc}}} }) => {
11+
const Page: NextFunctionComponent<Props> = ({ {{{lc}}} }) => {
1212
return (
13-
<{{{ucf}}}Show {{{lc}}}={ {{{lc}}} }/>
13+
<Show {{{lc}}}={ {{{lc}}} }/>
1414
);
1515
};
1616

17-
{{{ucf}}}Page.getInitialProps = async ({query}: NextContext) => {
18-
const id = atob(query.hash as string);
17+
Page.getInitialProps = async ({query}: NextContext) => {
18+
const id = atob(query.id as string);
1919
const {{{lc}}} = await fetch(id);
2020

2121
return { {{{lc}}} };
2222
};
2323

24-
export default withRouter({{{ucf}}}Page);
24+
export default withRouter(Page);

templates/next/pages/foos.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { NextFunctionComponent } from 'next';
2-
import { {{{ucf}}}List } from '../components/{{{lc}}}/List';
2+
import { List } from '../components/{{{lc}}}/List';
33
import { PagedCollection } from '../interfaces/Collection';
44
import { {{{ucf}}} } from '../interfaces/{{{ucf}}}';
55
import { fetch } from '../utils/dataAccess';
@@ -8,14 +8,14 @@ interface Props {
88
collection: PagedCollection<{{{ucf}}}>;
99
}
1010

11-
const {{{ucf}}}sPage: NextFunctionComponent<Props> = ({collection}) => (
12-
<{{{ucf}}}List {{{name}}}={collection['{{{hydraPrefix}}}member'] || []}/>
11+
const Page: NextFunctionComponent<Props> = ({collection}) => (
12+
<List {{{name}}}={collection['{{{hydraPrefix}}}member'] || []}/>
1313
);
1414

15-
{{{ucf}}}sPage.getInitialProps = async () => {
15+
Page.getInitialProps = async () => {
1616
const collection = await fetch('/{{{name}}}');
1717

1818
return {collection};
1919
};
2020

21-
export default {{{ucf}}}sPage;
21+
export default Page;

templates/next/utils/dataAccess.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ export const normalize = (data: any) => {
6969
);
7070
}
7171

72-
export const atob = (hash: string) => {
73-
return typeof window !== 'undefined' ? window.atob(hash) : Buffer.from(hash, 'base64').toString('ascii');
72+
export const atob = (id: string) => {
73+
return typeof window !== 'undefined' ? window.atob(id) : Buffer.from(id, 'base64').toString('ascii');
7474
}
7575

7676
export const btoa = (str: string) => {

0 commit comments

Comments
 (0)