Skip to content

Commit d636668

Browse files
committed
[react] Refactor fetch generation
1 parent 3ee24e7 commit d636668

File tree

10 files changed

+39
-34
lines changed

10 files changed

+39
-34
lines changed

src/generators/ReactGenerator.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ export default class extends BaseGenerator {
1313
'actions/foo/update.js',
1414
'actions/foo/show.js',
1515

16-
// api
17-
'api/fooFetch.js',
16+
// utils
17+
'utils/fetch.js',
1818

1919
// reducers
2020
'reducers/foo/create.js',
@@ -74,12 +74,12 @@ combineReducers(${titleLc},{/* ... */}),
7474
fields: resource.readableFields,
7575
formFields: this.buildFields(resource.writableFields),
7676
hydraPrefix: this.hydraPrefix,
77-
titleUcFirst
77+
titleUcFirst,
7878
};
7979

8080
// Create directories
8181
// These directories may already exist
82-
for (let dir of [`${dir}/api`, `${dir}/routes`, `${dir}/utils`]) {
82+
for (let dir of [`${dir}/utils`, `${dir}/config`, `${dir}/routes`]) {
8383
this.createDir(dir, false);
8484
}
8585

@@ -95,9 +95,6 @@ combineReducers(${titleLc},{/* ... */}),
9595
'actions/%s/update.js',
9696
'actions/%s/show.js',
9797

98-
// api
99-
'api/%sFetch.js',
100-
10198
// components
10299
'components/%s/Create.js',
103100
'components/%s/Form.js',
@@ -120,7 +117,11 @@ combineReducers(${titleLc},{/* ... */}),
120117
this.createFileFromPattern(pattern, dir, lc, context)
121118
}
122119

123-
this.createFile('utils/helpers.js', `${dir}/utils/helpers.js`, {}, false);
124-
this.createEntrypoint(api.entrypoint, `${dir}/api/_entrypoint.js`)
120+
// utils
121+
for (let file of ['utils/helpers.js', 'utils/fetch.js']) {
122+
this.createFile(file, `${dir}/${file}`, {}, false);
123+
}
124+
125+
this.createEntrypoint(api.entrypoint, `${dir}/config/_entrypoint.js`)
125126
}
126127
}

src/generators/ReactGenerator.test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,16 @@ test('Generate a React app', () => {
3030
});
3131
generator.generate(api, resource, tmpobj.name);
3232

33+
expect(fs.existsSync(tmpobj.name+'/utils/fetch.js'), true);
34+
expect(fs.existsSync(tmpobj.name+'/utils/helpers.js'), true);
35+
36+
expect(fs.existsSync(tmpobj.name+'/config/_entrypoint.js'), true);
37+
3338
expect(fs.existsSync(tmpobj.name+'/actions/abc/create.js'), true);
3439
expect(fs.existsSync(tmpobj.name+'/actions/abc/delete.js'), true);
3540
expect(fs.existsSync(tmpobj.name+'/actions/abc/list.js'), true);
3641
expect(fs.existsSync(tmpobj.name+'/actions/abc/update.js'), true);
3742

38-
expect(fs.existsSync(tmpobj.name+'/api/abcFetch.js'), true);
39-
4043
expect(fs.existsSync(tmpobj.name+'/components/abc/Create.js'), true);
4144
expect(fs.existsSync(tmpobj.name+'/components/abc/Form.js'), true);
4245
expect(fs.existsSync(tmpobj.name+'/components/abc/List.js'), true);

src/generators/ReactNativeGenerator.js

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ export default class extends BaseGenerator {
1313
'actions/foo/update.js',
1414
'actions/foo/show.js',
1515

16-
// api
17-
'api/fooFetch.js',
16+
// utils
17+
'utils/fetch.js',
1818

1919
// reducers
2020
'reducers/foo/create.js',
@@ -65,10 +65,11 @@ combineReducers(${titleLc},{/* ... */}),
6565
titleUcFirst
6666
};
6767

68-
6968
// Create directories
70-
// This directories may already exist
71-
this.createDir(`${dir}/api`, false);
69+
// These directories may already exist
70+
for (let dir of [`${dir}/utils`, `${dir}/config`]) {
71+
this.createDir(dir, false);
72+
}
7273

7374
for (let dir of [`${dir}/actions/${lc}`, `${dir}/components/${lc}`, `${dir}/reducers/${lc}`]) {
7475
this.createDir(dir);
@@ -82,9 +83,6 @@ combineReducers(${titleLc},{/* ... */}),
8283
'actions/%s/update.js',
8384
'actions/%s/show.js',
8485

85-
// api
86-
'api/fooFetch.js',
87-
8886
// components
8987
'components/%s/Create.js',
9088
'components/%s/Form.js',
@@ -104,6 +102,7 @@ combineReducers(${titleLc},{/* ... */}),
104102
this.createFileFromPattern(pattern, dir, lc, context);
105103
}
106104

107-
this.createEntrypoint(api.entrypoint, `${dir}/api/_entrypoint.js`)
105+
this.createFile('utils/fetch.js', `${dir}/utils/fetch.js`, {}, false);
106+
this.createEntrypoint(api.entrypoint, `${dir}/config/_entrypoint.js`)
108107
}
109108
}

src/generators/ReactNativeGenerator.test.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,15 @@ test('Generate a React app', () => {
3030
});
3131
generator.generate(api, resource, tmpobj.name);
3232

33+
expect(fs.existsSync(tmpobj.name+'/utils/fetch.js'), true);
34+
35+
expect(fs.existsSync(tmpobj.name+'/config/_entrypoint.js'), true);
36+
3337
expect(fs.existsSync(tmpobj.name+'/actions/abc/create.js'), true);
3438
expect(fs.existsSync(tmpobj.name+'/actions/abc/delete.js'), true);
3539
expect(fs.existsSync(tmpobj.name+'/actions/abc/list.js'), true);
3640
expect(fs.existsSync(tmpobj.name+'/actions/abc/update.js'), true);
3741

38-
expect(fs.existsSync(tmpobj.name+'/api/abcFetch.js'), true);
39-
4042
expect(fs.existsSync(tmpobj.name+'/components/abc/Create.js'), true);
4143
expect(fs.existsSync(tmpobj.name+'/components/abc/Form.js'), true);
4244
expect(fs.existsSync(tmpobj.name+'/components/abc/List.js'), true);

templates/react-common/actions/foo/create.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { SubmissionError } from 'redux-form';
2-
import {{{ lc }}}Fetch from '../../api/{{{ lc }}}Fetch';
2+
import fetch from '../../utils/fetch';
33

44
export function error(error) {
55
return {type: '{{{ uc }}}_CREATE_ERROR', error};
@@ -17,7 +17,7 @@ export function create(values) {
1717
return (dispatch) => {
1818
dispatch(loading(true));
1919

20-
return {{{ lc }}}Fetch('/{{{ name }}}', {method: 'POST', body: JSON.stringify(values)})
20+
return fetch('/{{{ name }}}', {method: 'POST', body: JSON.stringify(values)})
2121
.then(response => {
2222
dispatch(loading(false));
2323

templates/react-common/actions/foo/delete.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {{{ lc }}}Fetch from '../../api/{{{ lc }}}Fetch';
1+
import fetch from '../../utils/fetch';
22

33
export function error(error) {
44
return {type: '{{{ uc }}}_DELETE_ERROR', error};
@@ -16,7 +16,7 @@ export function del(item) {
1616
return (dispatch) => {
1717
dispatch(loading(true));
1818

19-
return {{{ lc }}}Fetch(item['@id'], {method: 'DELETE'})
19+
return fetch(item['@id'], {method: 'DELETE'})
2020
.then(() => {
2121
dispatch(loading(false));
2222
dispatch(success(item))

templates/react-common/actions/foo/list.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {{{ lc }}}Fetch from '../../api/{{{ lc }}}Fetch';
1+
import fetch from '../../utils/fetch';
22

33
export function error(error) {
44
return {type: '{{{ uc }}}_LIST_ERROR', error};
@@ -21,7 +21,7 @@ export function page(page) {
2121
dispatch(loading(true));
2222
dispatch(error(''));
2323

24-
{{{ lc }}}Fetch(page)
24+
fetch(page)
2525
.then(response => response.json())
2626
.then(data => {
2727
dispatch(loading(false));

templates/react-common/actions/foo/show.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {{{ lc }}}Fetch from '../../api/{{{ lc }}}Fetch';
1+
import fetch from '../../utils/fetch';
22

33
export function error(error) {
44
return {type: '{{{ uc }}}_SHOW_ERROR', error};
@@ -16,7 +16,7 @@ export function retrieve(id) {
1616
return (dispatch) => {
1717
dispatch(loading(true));
1818

19-
return {{{ lc }}}Fetch(id)
19+
return fetch(id)
2020
.then(response => response.json())
2121
.then(data => {
2222
dispatch(loading(false));

templates/react-common/actions/foo/update.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { SubmissionError } from 'redux-form';
2-
import {{{ lc }}}Fetch from '../../api/{{{ lc }}}Fetch';
2+
import fetch from '../../utils/fetch';
33
import { success as createSuccess } from './create';
44

55
export function retrieveError(retrieveError) {
@@ -18,7 +18,7 @@ export function retrieve(id) {
1818
return (dispatch) => {
1919
dispatch(retrieveLoading(true));
2020

21-
return {{{ lc }}}Fetch(id)
21+
return fetch(id)
2222
.then(response => response.json())
2323
.then(data => {
2424
dispatch(retrieveLoading(false));
@@ -49,7 +49,7 @@ export function update(item, values) {
4949
dispatch(createSuccess(null));
5050
dispatch(updateLoading(true));
5151

52-
return {{{ lc }}}Fetch(item['@id'], {
52+
return fetch(item['@id'], {
5353
method: 'PUT',
5454
headers: new Headers({'Content-Type': 'application/ld+json'}),
5555
body: JSON.stringify(values),

templates/react-common/api/fooFetch.js renamed to templates/react-common/utils/fetch.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { API_HOST, API_PATH } from './_entrypoint';
33

44
const jsonLdMimeType = 'application/ld+json';
55

6-
export default function {{{ lc }}}Fetch(url, options = {}) {
6+
export default function fetch(url, options = {}) {
77
if ('undefined' === typeof options.headers) options.headers = new Headers();
88
if (null === options.headers.get('Accept')) options.headers.set('Accept', jsonLdMimeType);
99

0 commit comments

Comments
 (0)