|
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"; |
3 | 4 |
|
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 | +} |
12 | 17 |
|
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 }); |
15 | 26 | } else { |
16 | | - yield put({ type: 'REQUEST_EXAMPLE_ERROR', error: 'Network error' }); |
| 27 | + yield put({ type: "REQUEST_EXAMPLE_ERROR", error: "Network error" }); |
17 | 28 | } |
18 | | - |
19 | 29 | } |
20 | | -} |
| 30 | +}; |
21 | 31 |
|
22 | | -const watchRequestExample = function*() { |
23 | | - yield takeEvery('REQUEST_EXAMPLE', RequestExample); |
24 | | -} |
| 32 | +const watchRequestExample = function* () { |
| 33 | + yield takeEvery("REQUEST_EXAMPLE", RequestExample); |
| 34 | +}; |
25 | 35 |
|
26 | 36 | export default watchRequestExample; |
0 commit comments