@@ -6,6 +6,131 @@ for the register user endpoint.
6
6
In a next step we wire the form with this endpoint and take the response's field message
7
7
for form element error enrichment.
8
8
9
+ ### 9.1 Provide a mock API
10
+ Before we can wire our user registration form, we have to create an API endpoint for it.
11
+ Let's create an endpoint which validates our inputs and returns some field messages.
12
+ One or more general messages would also be good. We could connect these with the toaster in one of the next steps.
13
+
14
+ To write our mocked API endpoint let's install Express first:
15
+ ```
16
+ npm install --save-dev express cors
17
+ ```
18
+
19
+ Then let's add our mock API like so:
20
+ ``` javascript
21
+ // src/mockApi.js
22
+
23
+ const express = require (' express' );
24
+ const cors = require (' cors' );
25
+ const uuid = require (' uuid' );
26
+
27
+ const port = 9000 ;
28
+ const app = express ();
29
+ app .use (express .json ())
30
+ app .use (cors ({ origin: ' *' }));
31
+
32
+ function createErrorMessage (messageId , translationId ) {
33
+ return {
34
+ id: messageId,
35
+ severity: ' error' ,
36
+ translation: {
37
+ id: translationId,
38
+ }
39
+ };
40
+ }
41
+
42
+ app .post (' /api/v1/auth/register' , function (req , res ) {
43
+ res .header (" Access-Control-Allow-Origin" , " *" )
44
+
45
+ const gender = req .body .gender ;
46
+ const username = req .body .username ;
47
+ const email = req .body .email ;
48
+ const password = req .body .password ;
49
+
50
+ const generalMessages = [];
51
+ const fieldMessages = [];
52
+
53
+ if (! gender || ! [' f' , ' m' , ' o' ].includes (gender)) {
54
+ fieldMessages .push ({
55
+ path: [' gender' ],
56
+ message: createErrorMessage (' message-id-01' , ' api.v1.invalidValue' ),
57
+ });
58
+ }
59
+
60
+ if (! username) {
61
+ fieldMessages .push ({
62
+ path: [' username' ],
63
+ message: createErrorMessage (' message-id-02' , ' api.v1.invalidValue' ),
64
+ });
65
+ }
66
+
67
+ if (! email) {
68
+ fieldMessages .push ({
69
+ path: [' email' ],
70
+ message: createErrorMessage (' message-id-03' , ' api.v1.invalidValue' ),
71
+ });
72
+ }
73
+
74
+ if (! password) {
75
+ fieldMessages .push ({
76
+ path: [' password' ],
77
+ message: createErrorMessage (' message-id-04' , ' api.v1.invalidValue' ),
78
+ });
79
+ }
80
+
81
+ const success = fieldMessages .length === 0 ;
82
+
83
+ if (! success) {
84
+ generalMessages .push (createErrorMessage (uuid .v4 (), ' api.v1.formErrors' ));
85
+ res .status (400 );
86
+ res .header (' Content-Type' , ' application/json' );
87
+ res .write (JSON .stringify ({
88
+ success,
89
+ generalMessages,
90
+ fieldMessages,
91
+ }));
92
+ res .send ();
93
+ return ;
94
+ }
95
+
96
+ generalMessages .push (createErrorMessage (uuid .v4 (), ' api.v1.userWasCreated' ));
97
+ res .status (201 );
98
+ res .header (' Content-Type' , ' application/json' );
99
+ res .write (JSON .stringify ({
100
+ success,
101
+ generalMessages,
102
+ fieldMessages,
103
+ data: {
104
+ apiKey: ' foo' ,
105
+ user: {
106
+ id: ' fbfe874c-ea8f-4cc1-bd4e-f07bedc30487' ,
107
+ username,
108
+ },
109
+ }
110
+ }));
111
+ res .send ();
112
+ });
113
+
114
+ console .log (` Mock-API is running at: http://localhost:${ port} ` );
115
+ app .listen (port);
116
+ ```
117
+
118
+ This file should not be affected by our linting rules, so let's ignore it by adding a ` .eslintignore ` file inside
119
+ our project folder and paste following line in it: ` src/mockApi.js `
120
+
121
+ Let's also add a new script in the ` package.json ` by enhancing the ` scripts ` section like so:
122
+ ``` javascript
123
+ " scripts" : {
124
+ // other scripts...
125
+ " mockApi" : " node ./src/mockApi.js"
126
+ }
127
+ ```
128
+
129
+ Cool! You now should be able to run the script and reach the mock API endpoint e.g. by [ Postman] ( https://www.postman.com/ ) .
130
+
131
+ ### 9.2 Provide the ApiV1RequestHandler
132
+ TBD
133
+
9
134
:floppy_disk : [ branch 09-wiring-1] ( https://github.com/inkognitro/react-app-tutorial-code/compare/08-apiv1-2...09-wiring-1 )
10
135
11
136
:floppy_disk : [ branch 09-wiring-2] ( https://github.com/inkognitro/react-app-tutorial-code/compare/09-wiring-1...09-wiring-2 )
0 commit comments