Skip to content

Commit 9797f80

Browse files
authored
Merge pull request #164 from simpleweb/feature/fix-saga-example-types
Feature: Fix the types on the `RequestExample` saga
2 parents cae5f37 + 501a92e commit 9797f80

2 files changed

Lines changed: 33 additions & 18 deletions

File tree

.changeset/famous-buttons-shop.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"romulus-cli": minor
3+
---
4+
5+
Correctly types the RequestExample saga
Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
1-
import { call, put, takeEvery } from 'redux-saga/effects';
2-
import API from '<%= name %>/App/Services/API';
1+
import { AxiosResponse, AxiosError } from "axios";
2+
import { call, put, takeEvery } from "redux-saga/effects";
3+
import API from "<%= name %>/App/Services/API";
34

4-
export const RequestExample = function*() {
5-
try {
6-
7-
const url = 'https://reqres.in/api/users';
8-
const result = yield call(API.get, url);
9-
yield put({ type: 'REQUEST_EXAMPLE_SUCCESS', result });
10-
11-
} catch(error) {
5+
interface UsersResponse {
6+
page: number;
7+
per_page: number;
8+
total_pages: number;
9+
data: {
10+
id: number;
11+
email: string;
12+
first_name: string;
13+
last_name: string;
14+
avatar: string;
15+
}[];
16+
}
1217

13-
if (error.response) {
14-
yield put({ type: 'REQUEST_EXAMPLE_ERROR', error });
18+
export const RequestExample = function* () {
19+
try {
20+
const url = "https://reqres.in/api/users";
21+
const result: AxiosResponse<UsersResponse> = yield call(API.get, url);
22+
yield put({ type: "REQUEST_EXAMPLE_SUCCESS", result });
23+
} catch (error) {
24+
if (error instanceof AxiosError && error.response) {
25+
yield put({ type: "REQUEST_EXAMPLE_ERROR", error });
1526
} else {
16-
yield put({ type: 'REQUEST_EXAMPLE_ERROR', error: 'Network error' });
27+
yield put({ type: "REQUEST_EXAMPLE_ERROR", error: "Network error" });
1728
}
18-
1929
}
20-
}
30+
};
2131

22-
const watchRequestExample = function*() {
23-
yield takeEvery('REQUEST_EXAMPLE', RequestExample);
24-
}
32+
const watchRequestExample = function* () {
33+
yield takeEvery("REQUEST_EXAMPLE", RequestExample);
34+
};
2535

2636
export default watchRequestExample;

0 commit comments

Comments
 (0)