@@ -182,10 +182,14 @@ class UsersController extends ControllerBase<UsersSlice, { users: UsersSlice }>
182
182
183
183
@reducer
184
184
async addUser(action : Action <{ name: string }>) {
185
- const newUser = await Promise .resolve ().then (() => ({
186
- userId: new Date ().valueOf ().toString (),
187
- userName: action .payload .name ,
188
- }));
185
+ const newUserResponse = await fetch (' /api/user' , {
186
+ method: ' POST' ,
187
+ body: JSON .stringify ({
188
+ username: action .payload .name
189
+ })
190
+ });
191
+
192
+ const newUser = await newUserResponse .json ();
189
193
190
194
const { usersList } = this .getState ().users ;
191
195
@@ -253,12 +257,12 @@ export class Logger {
253
257
}
254
258
255
259
export class UserApi {
256
- constructor (private logger : Logger ) {
257
- }
260
+ constructor (private logger : Logger ) {}
258
261
259
262
async get() {
260
263
this .logger .log (' [my api] fetching users list' );
261
- return [' user-1' , ' user-2' ];
264
+ const response = await fetch (' /api/user' );
265
+ return response .json ();
262
266
}
263
267
}
264
268
// services.ts
@@ -276,17 +280,17 @@ export class Logger {
276
280
277
281
@inject (Logger )
278
282
export class UserApi {
279
- constructor (private logger : Logger ) {
280
- }
283
+ constructor (private logger : Logger ) {}
281
284
282
285
async get() {
283
286
this .logger .log (' [my api] fetching users list' );
284
- return [' user-1' , ' user-2' ];
287
+ const response = await fetch (' /api/user' );
288
+ return response .json ();
285
289
}
286
290
}
287
291
```
288
292
289
- > ** _ NOTE:_ ** you need to adjust tsconfig.json to use stage 2 decorators :
293
+ > ** _ NOTE:_ ** To use stage 2 decorators, you need to adjust your tsconfig.json like this :
290
294
> ``` json
291
295
> {
292
296
> "compilerOptions": {
@@ -295,6 +299,7 @@ export class UserApi {
295
299
> }
296
300
> }
297
301
> ```
302
+ > To use stage 3 decorators, you don't need extra setup.
298
303
299
304
### <a name="dependency-injection-functional"></a> Functional variant
300
305
0 commit comments