Skip to content

Commit ee5980c

Browse files
ValentinCrochemorealanpoulain
authored andcommitted
Replace then by async/await
1 parent 7b2ada3 commit ee5980c

File tree

6 files changed

+141
-131
lines changed

6 files changed

+141
-131
lines changed

templates/quasar/stores/foo/create.ts

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,33 @@ export const use{{titleUcFirst}}CreateStore = defineStore('{{lc}}Create', {
1919
}),
2020

2121
actions: {
22-
create(values: {{titleUcFirst}}) {
22+
async create(payload: {{titleUcFirst}}) {
2323
this.setError(undefined);
2424
this.setViolations(undefined);
2525
this.toggleLoading();
2626

27-
return fetch('{{name}}', { method: 'POST', body: JSON.stringify(values) })
28-
.then((response: Response) => {
29-
this.toggleLoading();
27+
try {
28+
const response = await fetch('{{name}}', {
29+
method: 'POST',
30+
body: JSON.stringify(payload),
31+
});
32+
const data: {{titleUcFirst}} = await response.json();
3033

31-
return response.json();
32-
})
33-
.then((data) => {
34-
this.setCreated(data);
35-
})
36-
.catch((e: Error | SubmissionError) => {
37-
this.toggleLoading();
34+
this.toggleLoading();
35+
this.setCreated(data);
36+
} catch (error) {
37+
this.toggleLoading();
3838

39-
if (e instanceof SubmissionError) {
40-
this.setViolations(e.errors);
41-
return;
42-
}
39+
if (error instanceof SubmissionError) {
40+
this.setViolations(error.errors);
41+
this.setError(error.errors._error);
42+
return;
43+
}
4344

44-
this.setError(e.message);
45-
});
45+
if (error instanceof Error) {
46+
this.setError(error.message);
47+
}
48+
}
4649
},
4750

4851
setCreated(created: {{titleUcFirst}}) {

templates/quasar/stores/foo/delete.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,27 @@ export const use{{titleUcFirst}}DeleteStore = defineStore('{{lc}}Delete', {
1818
}),
1919

2020
actions: {
21-
deleteItem(item: {{titleUcFirst}}) {
21+
async deleteItem(item: {{titleUcFirst}}) {
2222
this.toggleLoading();
2323

24-
return fetch(item['@id'] ?? '', { method: 'DELETE' })
25-
.then(() => {
26-
this.setError('');
27-
this.toggleLoading();
28-
this.setDeleted(item);
29-
this.setMercureDeleted(undefined);
30-
})
31-
.catch((e: Error) => {
32-
this.toggleLoading();
33-
this.setError(e.message);
34-
});
24+
if (!item?.['@id']) {
25+
this.setError('No {{lc}} found. Please reload');
26+
return;
27+
}
28+
29+
try {
30+
await fetch(item['@id'], { method: 'DELETE' });
31+
32+
this.toggleLoading();
33+
this.setDeleted(item);
34+
this.setMercureDeleted(undefined);
35+
} catch (error) {
36+
this.toggleLoading();
37+
38+
if (error instanceof Error) {
39+
this.setError(error.message);
40+
}
41+
}
3542
},
3643

3744
toggleLoading() {

templates/quasar/stores/foo/list.ts

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,30 @@ export const use{{titleUcFirst}}ListStore = defineStore('{{lc}}List', {
2626
}),
2727

2828
actions: {
29-
getItems(params: ListParams) {
29+
async getItems(params: ListParams) {
3030
this.toggleLoading();
3131

32-
return fetch('{{name}}', { params })
33-
.then((response: Response) =>
34-
response.json().then((data: PagedCollection<{{titleUcFirst}}>) => ({
35-
data,
36-
hubUrl: extractHubURL(response),
37-
}))
38-
)
39-
.then(
40-
({ data, hubUrl }: { data: PagedCollection<{{titleUcFirst}}>; hubUrl?: URL }) => {
41-
this.toggleLoading();
42-
this.setItems(data['{{hydraPrefix}}member']);
43-
this.setTotalItems(data['{{hydraPrefix}}totalItems'] ?? 0);
44-
this.setView(data['{{hydraPrefix}}view']);
45-
46-
if (hubUrl) {
47-
this.setHubUrl(hubUrl);
48-
}
49-
}
50-
)
51-
.catch((e: Error) => {
52-
this.toggleLoading();
53-
this.setError(e.message);
54-
});
32+
try {
33+
const response = await fetch('{{name}}', { params });
34+
const data: PagedCollection<{{titleUcFirst}}> = await response.json();
35+
const hubUrl = extractHubURL(response);
36+
37+
this.toggleLoading();
38+
39+
this.setItems(data['{{hydraPrefix}}member']);
40+
this.setTotalItems(data['{{hydraPrefix}}totalItems'] ?? 0);
41+
this.setView(data['{{hydraPrefix}}view']);
42+
43+
if (hubUrl) {
44+
this.setHubUrl(hubUrl);
45+
}
46+
} catch (error) {
47+
this.toggleLoading();
48+
49+
if (error instanceof Error) {
50+
this.setError(error.message);
51+
}
52+
}
5553
},
5654

5755
toggleLoading() {

templates/quasar/stores/foo/show.ts

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,27 @@ export const use{{titleUcFirst}}ShowStore = defineStore('{{lc}}Show', {
1919
}),
2020

2121
actions: {
22-
retrieve(id: string) {
22+
async retrieve(id: string) {
2323
this.toggleLoading();
2424

25-
return fetch(id)
26-
.then((response: Response) =>
27-
response.json().then((data: {{titleUcFirst}}) => ({
28-
data,
29-
hubUrl: extractHubURL(response),
30-
}))
31-
)
32-
.then(({ data, hubUrl }: { data: {{titleUcFirst}}; hubUrl?: URL }) => {
33-
this.toggleLoading();
34-
this.setRetrieved(data);
35-
36-
if (hubUrl) {
37-
this.setHubUrl(hubUrl);
38-
}
39-
})
40-
.catch((e: Error) => {
41-
this.toggleLoading();
42-
this.setError(e.message);
43-
});
25+
try {
26+
const response = await fetch(id);
27+
const data: {{titleUcFirst}} = await response.json();
28+
const hubUrl = extractHubURL(response);
29+
30+
this.toggleLoading();
31+
this.setRetrieved(data);
32+
33+
if (hubUrl) {
34+
this.setHubUrl(hubUrl);
35+
}
36+
} catch (error) {
37+
this.toggleLoading();
38+
39+
if (error instanceof Error) {
40+
this.setError(error.message);
41+
}
42+
}
4443
},
4544

4645
toggleLoading() {

templates/quasar/stores/foo/update.ts

Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,30 @@ export const use{{titleUcFirst}}UpdateStore = defineStore('{{lc}}Update', {
2424
}),
2525

2626
actions: {
27-
retrieve(id: string) {
27+
async retrieve(id: string) {
2828
this.toggleLoading();
2929

30-
return fetch(id)
31-
.then((response: Response) =>
32-
response.json().then((data: {{titleUcFirst}}) => ({
33-
data,
34-
hubUrl: extractHubURL(response),
35-
}))
36-
)
37-
.then(({ data, hubUrl }: { data: {{titleUcFirst}}; hubUrl?: URL }) => {
38-
this.setError('');
39-
this.toggleLoading();
40-
this.setRetrieved(data);
41-
42-
if (hubUrl) {
43-
this.setHubUrl(hubUrl);
44-
}
45-
})
46-
.catch((e: Error) => {
47-
this.toggleLoading();
48-
this.setError(e.message);
49-
});
30+
try {
31+
const response = await fetch(id);
32+
const data: {{titleUcFirst}} = await response.json();
33+
const hubUrl = extractHubURL(response);
34+
35+
this.toggleLoading();
36+
this.setRetrieved(data);
37+
38+
if (hubUrl) {
39+
this.setHubUrl(hubUrl);
40+
}
41+
} catch (error) {
42+
this.toggleLoading();
43+
44+
if (error instanceof Error) {
45+
this.setError(error.message);
46+
}
47+
}
5048
},
5149

52-
update(values: {{titleUcFirst}}) {
50+
async update(payload: {{titleUcFirst}}) {
5351
this.setError(undefined);
5452
this.toggleLoading();
5553

@@ -58,28 +56,32 @@ export const use{{titleUcFirst}}UpdateStore = defineStore('{{lc}}Update', {
5856
return;
5957
}
6058

61-
return fetch(this.retrieved['@id'] ?? values['@id'] ?? '', {
62-
method: 'PUT',
63-
headers: new Headers({ 'Content-Type': 'application/ld+json' }),
64-
body: JSON.stringify(values),
65-
})
66-
.then((response: Response) => response.json())
67-
.then((data: {{titleUcFirst}}) => {
68-
this.toggleLoading();
69-
this.resetErrors();
70-
this.setUpdated(data);
71-
})
72-
.catch((e: Error) => {
73-
this.toggleLoading();
74-
75-
if (e instanceof SubmissionError) {
76-
this.setViolations(e.errors);
77-
this.setError(e.errors._error);
78-
return;
59+
try {
60+
const response = await fetch(
61+
this.retrieved['@id'] ?? payload['@id'] ?? '',
62+
{
63+
method: 'PUT',
64+
headers: new Headers({ 'Content-Type': 'application/ld+json' }),
65+
body: JSON.stringify(payload),
7966
}
80-
81-
this.setError(e.message);
82-
});
67+
);
68+
const data: {{titleUcFirst}} = await response.json();
69+
70+
this.toggleLoading();
71+
this.setUpdated(data);
72+
} catch (error) {
73+
this.toggleLoading();
74+
75+
if (error instanceof SubmissionError) {
76+
this.setViolations(error.errors);
77+
this.setError(error.errors._error);
78+
return;
79+
}
80+
81+
if (error instanceof Error) {
82+
this.setError(error.message);
83+
}
84+
}
8385
},
8486

8587
setRetrieved(retrieved: {{titleUcFirst}}) {

templates/quasar/utils/fetch.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { ENTRYPOINT } from 'src/config/entrypoint';
44

55
const MIME_TYPE = 'application/ld+json';
66

7-
export default function (id: string, options: any = {}) {
7+
export default async function (id: string, options: any = {}) {
88
if (typeof options.headers === 'undefined') {
99
Object.assign(options, { headers: new Headers() });
1010
}
@@ -32,21 +32,22 @@ export default function (id: string, options: any = {}) {
3232
// credentials: 'include', // when credentials needed
3333
});
3434

35-
return fetch(new URL(id, ENTRYPOINT), options).then((response: Response) => {
36-
if (response.ok) return response;
35+
const response = await fetch(new URL(id, ENTRYPOINT), options);
3736

38-
return response.json().then((json) => {
39-
const error = json['{{hydraPrefix}}description'] ?? response.statusText;
40-
if (!json.violations) throw Error(error);
37+
if (!response.ok) {
38+
const data = await response.json();
39+
const error = data['{{hydraPrefix}}description'] || response.statusText;
40+
if (!data.violations) throw Error(error);
4141

42-
const errors: SubmissionErrors = { _error: error };
43-
json.violations.map(
44-
(violation: { propertyPath: string; message: string }) => {
45-
errors[violation.propertyPath] = violation.message;
46-
}
47-
);
42+
const errors: SubmissionErrors = { _error: error };
43+
data.violations.forEach(
44+
(violation: { propertyPath: string; message: string }) => {
45+
errors[violation.propertyPath] = violation.message;
46+
}
47+
);
4848

49-
throw new SubmissionError(errors);
50-
});
51-
});
49+
throw new SubmissionError(errors);
50+
}
51+
52+
return response;
5253
}

0 commit comments

Comments
 (0)